package me.zhengjie.modules.attendance.service.impl;
|
|
import com.alibaba.fastjson.JSONArray;
|
import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
|
import lombok.RequiredArgsConstructor;
|
import me.zhengjie.modules.api.Api;
|
import me.zhengjie.modules.attendance.service.AttendanceLeaveFlowableService;
|
import me.zhengjie.modules.attendance.service.AttendanceLeaveService;
|
import me.zhengjie.modules.attendance.service.dto.AttendanceLeaveDto;
|
import me.zhengjie.modules.constant.Constant;
|
import me.zhengjie.modules.system.domain.Role;
|
import me.zhengjie.modules.system.domain.User;
|
import me.zhengjie.modules.system.repository.RoleRepository;
|
import me.zhengjie.modules.system.repository.UserRepository;
|
import me.zhengjie.modules.system.service.RoleService;
|
import me.zhengjie.modules.system.service.dto.RoleSmallDto;
|
import me.zhengjie.utils.FileUtil;
|
import me.zhengjie.utils.HttpUtil;
|
import me.zhengjie.utils.ObjectUtil;
|
import me.zhengjie.utils.SecurityUtils;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.ResponseEntity;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.*;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
@Service
|
@RequiredArgsConstructor
|
public class AttendanceLeaveFlowableServiceImpl implements AttendanceLeaveFlowableService {
|
|
@Value("${flowable.url}")
|
private String flowableUrl;
|
|
private final Api api;
|
private final UserRepository userRepository;
|
private final RoleRepository roleRepository;
|
private final RoleService roleService;
|
private final AttendanceLeaveService attendanceLeaveService;
|
|
/**
|
* 我发起的
|
* @return
|
*/
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public Map<String, Object> report() throws Exception {
|
// 发起通信
|
Object returnObject = api.post(flowableUrl + "/api/common/report?key=" + "attendanceLeaveApply" + "&userId=" + SecurityUtils.getCurrentUserId(), new HashMap<>());
|
// 查询处理
|
return queryHandle(returnObject);
|
}
|
|
/**
|
* 待审批的
|
* @return
|
*/
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public Map<String, Object> approve() throws Exception {
|
// 发起通信
|
Object returnObject = api.post(flowableUrl + "/api/common/approve?key=" + "attendanceLeaveApply" + "&userId=" + SecurityUtils.getCurrentUserId(), new HashMap<>());
|
// 查询处理
|
return queryHandle(returnObject);
|
}
|
|
/**
|
* 查询处理
|
* @param returnObject
|
* @return
|
*/
|
private Map<String, Object> queryHandle(Object returnObject) {
|
// 返回结果
|
Map<String, Object> returnMap = new HashMap<>();
|
// 通信结果转DTO
|
List<AttendanceLeaveDto> attendanceLeaveDtoList = ((JSONArray) returnObject).toJavaList(AttendanceLeaveDto.class);
|
// 循环DTO
|
for (AttendanceLeaveDto attendanceLeaveDto : attendanceLeaveDtoList) {
|
// 查询用户信息
|
User user = userRepository.findById(attendanceLeaveDto.getUserId()).orElseGet(User::new);
|
attendanceLeaveDto.setUserName(user.getNickName());
|
// 查询审批用户信息
|
User appUser = userRepository.findById(attendanceLeaveDto.getApprovalUserId()).orElseGet(User::new);
|
attendanceLeaveDto.setAppUserName(appUser.getNickName());
|
}
|
// 返回结果
|
returnMap.put("content", attendanceLeaveDtoList);
|
returnMap.put("totalElements", attendanceLeaveDtoList.size());
|
return returnMap;
|
}
|
|
/**
|
* 创建
|
* @param resources /
|
*/
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void create(AttendanceLeaveDto resources) throws Exception {
|
// 新增校验(工作流)
|
attendanceLeaveService.flowableCreateValidation(resources, reportAppoint(resources.getUserId()));
|
// 获取用户最大角色等级
|
List<Integer> levels = roleService.findByUsersId(resources.getUserId()).stream().map(RoleSmallDto::getLevel).collect(Collectors.toList());
|
int min = Collections.min(levels);
|
// 请假时长
|
Integer leaveDuration = 0;
|
// 判断请假时长单位
|
if (Constant.LEAVE_TIME_TYPE_HOUR.equals(resources.getLeaveDurationUnit())) {
|
Double tempLeaveDuration = Math.floor(resources.getLeaveDuration() / 8);
|
// 请假时长
|
leaveDuration = tempLeaveDuration.intValue();
|
} else {
|
// 请假时长
|
leaveDuration = resources.getLeaveDuration().intValue();
|
}
|
// 一级审批人
|
String approver1 = "";
|
// 二级审批人
|
String approver2 = "";
|
// 三级审批人
|
String approver3 = "";
|
// 判断用户最大角色等级
|
if (min == Constant.ROLE_LEVEL_ORDINARY) {
|
// 一级审批人
|
approver1 = resources.getApprovalUserId().toString();
|
// 二级审批人
|
approver2 = findUserByRoleLevel(Constant.ROLE_LEVEL_MANAGER);
|
// 三级审批人
|
approver3 = findUserByRoleLevel(Constant.ROLE_LEVEL_GENERAL) + "," + findUserByRoleLevel(Constant.ROLE_LEVEL_SUPER);
|
} else if (min == Constant.ROLE_LEVEL_LEADER) {
|
// 二级审批人
|
approver2 = resources.getApprovalUserId().toString();
|
// 三级审批人
|
approver3 = findUserByRoleLevel(Constant.ROLE_LEVEL_GENERAL) + "," + findUserByRoleLevel(Constant.ROLE_LEVEL_SUPER);
|
} else if (min == Constant.ROLE_LEVEL_MANAGER) {
|
// 三级审批人
|
approver3 = resources.getApprovalUserId().toString();
|
} else {
|
|
}
|
// 参数
|
Map<String, Object> params = ObjectUtil.toObjectMap(resources);
|
params.put("role", min);
|
params.put("time", leaveDuration);
|
params.put("approver1", approver1);
|
params.put("approver2", approver2);
|
params.put("approver3", approver3);
|
// 发起通信
|
api.post(flowableUrl + "/api/common/run?key=" + "attendanceLeaveApply" + "&userId=" + resources.getUserId(), params);
|
}
|
|
/**
|
* 根据角色等级查询用户
|
* @param level
|
* @return
|
*/
|
private String findUserByRoleLevel(Long level) {
|
// 查询指定角色
|
Role role = roleRepository.findByLevel(level.intValue());
|
// 查询指定角色用户
|
List<User> userList = userRepository.findByRoleId(role.getId());
|
// 审批人字符串
|
String approver = "";
|
// 循环指定角色用户
|
for (int i = 0; i < userList.size(); i ++) {
|
if (i == 0) {
|
// 审批人字符串
|
approver = userList.get(i).getId().toString();
|
} else {
|
// 审批人字符串
|
approver += "," + userList.get(i).getId().toString();
|
}
|
}
|
// 返回
|
return approver;
|
}
|
|
/**
|
* 同意
|
* @param processInstanceId /
|
*/
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void agree(String processInstanceId) throws Exception {
|
// 参数
|
Map<String, Object> paramsMap = new HashMap<>();
|
paramsMap.put("approve", "true");
|
// 发起通信
|
api.post(flowableUrl + "/api/common/next?processInstanceId=" + processInstanceId, paramsMap);
|
}
|
|
/**
|
* 拒绝
|
* @param processInstanceId /
|
*/
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void reject(String processInstanceId) throws Exception {
|
// 参数
|
Map<String, Object> paramsMap = new HashMap<>();
|
paramsMap.put("approve", "false");
|
// 发起通信
|
api.post(flowableUrl + "/api/common/next?processInstanceId=" + processInstanceId, paramsMap);
|
}
|
|
/**
|
* 我发起的(指定用户)
|
* @return
|
*/
|
private List<AttendanceLeaveDto> reportAppoint(Long userId) {
|
// 发起通信
|
Object returnObject = api.post(flowableUrl + "/api/common/report?key=" + "attendanceLeaveApply" + "&userId=" + userId, new HashMap<>());
|
// 通信结果转DTO
|
List<AttendanceLeaveDto> attendanceLeaveDtoList = ((JSONArray) returnObject).toJavaList(AttendanceLeaveDto.class);
|
return attendanceLeaveDtoList;
|
}
|
|
/**
|
* 流程图
|
* @return
|
*/
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void processDiagram(HttpServletRequest request, HttpServletResponse response, String processInstanceId) throws Exception {
|
// 发起通信
|
byte[] obj = api.postStream(flowableUrl + "/api/common/processDiagram?processInstanceId=" + processInstanceId, new HashMap<>());
|
// 输入流
|
InputStream in = new ByteArrayInputStream(obj);
|
// inputStream 转 File
|
File file = FileUtil.inputStreamToFile(in, "流程图-" + new Date().getTime() + ".jpeg");
|
// 下载文件
|
FileUtil.downloadFile(request, response, file, true);
|
}
|
}
|