| | |
| | | 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.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 java.util.Collections; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.*; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | |
| | | private final UserRepository userRepository; |
| | | private final RoleRepository roleRepository; |
| | | private final RoleService roleService; |
| | | private final AttendanceLeaveService attendanceLeaveService; |
| | | |
| | | /** |
| | | * 我发起的 |
| | |
| | | @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.ceil(resources.getLeaveDuration() / 8); |
| | | Double tempLeaveDuration = Math.floor(resources.getLeaveDuration() / 8); |
| | | // 请假时长 |
| | | leaveDuration = tempLeaveDuration.intValue(); |
| | | } else { |
| | | // 请假时长 |
| | | leaveDuration = resources.getLeaveDuration().intValue(); |
| | | } |
| | | // 一级审批人 |
| | | String approver1 = findUserByRoleLevel(Constant.ROLE_LEVEL_LEADER); |
| | | String approver1 = ""; |
| | | // 二级审批人 |
| | | String approver2 = findUserByRoleLevel(Constant.ROLE_LEVEL_MANAGER); |
| | | String approver2 = ""; |
| | | // 三级审批人 |
| | | String approver3 = findUserByRoleLevel(Constant.ROLE_LEVEL_GENERAL); |
| | | 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); |
| | |
| | | // 发起通信 |
| | | 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); |
| | | } |
| | | } |