| | |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; |
| | | import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi; |
| | | import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO; |
| | | import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmProcessDefinitionInfoDO; |
| | | import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmnVariableConstants; |
| | | import cn.iocoder.yudao.module.bpm.service.definition.BpmCategoryService; |
| | | import cn.iocoder.yudao.module.bpm.service.definition.BpmProcessDefinitionService; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmLeaveApplicationImportExcelVO; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmLeaveApplicationImportFailureVO; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmLeaveApplicationImportRespVO; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmLeaveApplicationPageReqVO; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmLeaveApplicationRespVO; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmLeaveApplicationSaveReqVO; |
| | | import cn.iocoder.yudao.module.hrm.dal.dataobject.approval.HrmLeaveApplicationDO; |
| | | import cn.iocoder.yudao.module.hrm.dal.dataobject.employee.HrmEmployeeDO; |
| | | import cn.iocoder.yudao.module.hrm.dal.mysql.approval.HrmLeaveApplicationMapper; |
| | | import cn.iocoder.yudao.module.hrm.dal.mysql.employee.HrmEmployeeMapper; |
| | | import cn.iocoder.yudao.module.hrm.dal.redis.HrmNoRedisDAO; |
| | | import cn.iocoder.yudao.module.hrm.enums.HrmAuditStatusEnum; |
| | | import cn.iocoder.yudao.module.hrm.enums.HrmLeaveTypeEnum; |
| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.time.LocalDateTime; |
| | | import java.time.ZoneId; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.time.format.DateTimeParseException; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.HashMap; |
| | |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.Set; |
| | | import java.util.regex.Pattern; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | |
| | | /** |
| | | * 请假申请 Service 实现类 |
| | | * |
| | | * @author 芋道源码 |
| | | * @author 超级管理员 |
| | | */ |
| | | @Service |
| | | @Validated |
| | | @Slf4j |
| | | public class HrmLeaveApplicationServiceImpl implements HrmLeaveApplicationService { |
| | | |
| | | private static final DateTimeFormatter LEAVE_TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); |
| | | |
| | | @Resource |
| | | private HrmLeaveApplicationMapper leaveApplicationMapper; |
| | | @Resource |
| | | private HrmEmployeeMapper employeeMapper; |
| | | @Resource |
| | | private HrmNoRedisDAO noRedisDAO; |
| | | @Resource |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long createLeaveApplication(HrmLeaveApplicationSaveReqVO createReqVO) { |
| | | // 1. 生成申请单号 |
| | | String no = generateUniqueNo(); |
| | | // 2. 设置申请人信息 |
| | | // 1. 确认申请人已关联员工档案 |
| | | Long userId = createReqVO.getUserId(); |
| | | if (userId == null) { |
| | | userId = getCurrentUserId(); |
| | | } |
| | | AdminUserRespDTO user = adminUserApi.getUser(userId); |
| | | if (user == null) { |
| | | userId = getCurrentUserId(); |
| | | throw exception(LEAVE_APPLICATION_APPLICANT_NOT_EXISTS); |
| | | } |
| | | HrmEmployeeDO employee = employeeMapper.selectByUserId(userId); |
| | | if (employee == null) { |
| | | throw exception(Objects.equals(userId, getCurrentUserId()) |
| | | ? LEAVE_APPLICATION_EMPLOYEE_NOT_EXISTS : LEAVE_APPLICATION_SELECTED_EMPLOYEE_NOT_EXISTS); |
| | | } |
| | | if (employee.getDeptId() == null) { |
| | | throw exception(LEAVE_APPLICATION_EMPLOYEE_DEPT_NOT_FOUND); |
| | | } |
| | | if (createReqVO.getDeptId() != null && !Objects.equals(createReqVO.getDeptId(), employee.getDeptId())) { |
| | | throw exception(LEAVE_APPLICATION_DEPT_MISMATCH); |
| | | } |
| | | // 2. 生成申请单号 |
| | | String no = generateUniqueNo(); |
| | | // 3. 插入 |
| | | HrmLeaveApplicationDO application = BeanUtils.toBean(createReqVO, HrmLeaveApplicationDO.class); |
| | | application.setNo(no); |
| | | application.setUserId(userId); |
| | | application.setDeptId(createReqVO.getDeptId() != null ? createReqVO.getDeptId() : user.getDeptId()); |
| | | application.setDeptId(employee.getDeptId()); |
| | | application.setStatus(HrmAuditStatusEnum.DRAFT.getStatus()); |
| | | leaveApplicationMapper.insert(application); |
| | | // 绑定附件 |
| | |
| | | application.getId(), createReqVO.getBlobIds()); |
| | | return application.getId(); |
| | | } |
| | | |
| | | @Override |
| | | public HrmLeaveApplicationImportRespVO importLeaveApplications(List<HrmLeaveApplicationImportExcelVO> list) { |
| | | if (CollUtil.isEmpty(list)) { |
| | | throw exception(LEAVE_APPLICATION_IMPORT_FILE_EMPTY); |
| | | } |
| | | int totalCount = 0; |
| | | int successCount = 0; |
| | | List<HrmLeaveApplicationImportFailureVO> failures = new ArrayList<>(); |
| | | for (int index = 0; index < list.size(); index++) { |
| | | HrmLeaveApplicationImportExcelVO row = list.get(index); |
| | | // Excel 第 1 行是表头,第 index 条数据对应第 index + 2 行 |
| | | int rowNum = index + 2; |
| | | if (isBlankRow(row)) { |
| | | continue; |
| | | } |
| | | totalCount++; |
| | | try { |
| | | leaveApplicationMapper.insert(buildImportedDraft(row)); |
| | | successCount++; |
| | | } catch (LeaveImportRowException e) { |
| | | failures.add(new HrmLeaveApplicationImportFailureVO(rowNum, trimToNull(row.getPhone()), |
| | | trimToNull(row.getEmployeeName()), e.getMessage())); |
| | | } catch (Exception e) { |
| | | log.error("[importLeaveApplications][第 {} 行导入失败]", rowNum, e); |
| | | failures.add(new HrmLeaveApplicationImportFailureVO(rowNum, trimToNull(row.getPhone()), |
| | | trimToNull(row.getEmployeeName()), |
| | | "写入数据库失败:" + StrUtil.blankToDefault(e.getMessage(), e.getClass().getSimpleName()) |
| | | + ";请检查请假原因/备注长度后重新导入该行,仍失败请联系管理员")); |
| | | } |
| | | } |
| | | if (totalCount == 0) { |
| | | throw exception(LEAVE_APPLICATION_IMPORT_FILE_EMPTY); |
| | | } |
| | | return HrmLeaveApplicationImportRespVO.builder() |
| | | .totalCount(totalCount) |
| | | .successCount(successCount) |
| | | .failureCount(failures.size()) |
| | | .failures(failures) |
| | | .build(); |
| | | } |
| | | |
| | | private boolean isBlankRow(HrmLeaveApplicationImportExcelVO row) { |
| | | return row == null || (StrUtil.isBlank(row.getPhone()) && StrUtil.isBlank(row.getEmployeeName()) |
| | | && StrUtil.isBlank(row.getLeaveTypeName()) && StrUtil.isBlank(row.getStartTime()) |
| | | && StrUtil.isBlank(row.getEndTime()) && StrUtil.isBlank(row.getDuration()) |
| | | && StrUtil.isBlank(row.getReason())); |
| | | } |
| | | |
| | | /** |
| | | * 将一行 Excel 数据转换为请假申请草稿;任一校验不通过时抛出带精确原因的 {@link LeaveImportRowException} |
| | | */ |
| | | private HrmLeaveApplicationDO buildImportedDraft(HrmLeaveApplicationImportExcelVO row) { |
| | | // 1. 手机号匹配员工档案 |
| | | String phone = normalizeCell(row.getPhone()); |
| | | if (StrUtil.isBlank(phone)) { |
| | | throw new LeaveImportRowException("手机号为空,请填写员工档案中登记的 11 位手机号"); |
| | | } |
| | | if (!PHONE_PATTERN.matcher(phone).matches()) { |
| | | throw new LeaveImportRowException("手机号[" + phone + "]不是有效的 11 位手机号,请检查是否多填了空格、符号或小数位"); |
| | | } |
| | | HrmEmployeeDO employee = employeeMapper.selectByPhone(phone); |
| | | if (employee == null) { |
| | | throw new LeaveImportRowException("手机号[" + phone + "]未匹配到员工档案,请先在「员工档案」中维护该手机号后重新导入"); |
| | | } |
| | | String inputName = trimToNull(row.getEmployeeName()); |
| | | if (inputName != null && !Objects.equals(inputName, StrUtil.trim(employee.getName()))) { |
| | | throw new LeaveImportRowException("手机号[" + phone + "]对应员工为[" + employee.getName() |
| | | + "],与填写的员工姓名[" + inputName + "]不一致,请核对手机号后重新导入"); |
| | | } |
| | | if (employee.getUserId() == null) { |
| | | throw new LeaveImportRowException("员工档案[" + employee.getName() + "](手机号 " + phone |
| | | + ")未关联系统登录账号,请先在员工档案中绑定账号后重新导入"); |
| | | } |
| | | if (employee.getDeptId() == null) { |
| | | throw new LeaveImportRowException("员工档案[" + employee.getName() |
| | | + "]未设置部门,请先在员工档案中补全部门后重新导入"); |
| | | } |
| | | // 2. 请假类型 |
| | | Integer leaveType = parseLeaveType(row.getLeaveTypeName()); |
| | | // 3. 起止时间与时长 |
| | | LocalDateTime startTime = parseDateTime("开始时间", row.getStartTime()); |
| | | LocalDateTime endTime = parseDateTime("结束时间", row.getEndTime()); |
| | | if (!endTime.isAfter(startTime)) { |
| | | throw new LeaveImportRowException("结束时间[" + formatDateTime(endTime) + "]必须晚于开始时间[" |
| | | + formatDateTime(startTime) + "],请调整后重新导入"); |
| | | } |
| | | BigDecimal duration = parseDuration(row.getDuration(), startTime, endTime); |
| | | // 4. 请假原因 |
| | | String reason = trimToNull(row.getReason()); |
| | | if (reason == null) { |
| | | throw new LeaveImportRowException("请假原因为空,请填写请假事由(将展示在审批单上)"); |
| | | } |
| | | if (reason.length() > 500) { |
| | | throw new LeaveImportRowException("请假原因长度为 " + reason.length() |
| | | + " 个字符,超过 500 字上限,请精简后重新导入"); |
| | | } |
| | | String remark = trimToNull(row.getRemark()); |
| | | if (remark != null && remark.length() > 500) { |
| | | throw new LeaveImportRowException("备注长度为 " + remark.length() |
| | | + " 个字符,超过 500 字上限,请精简后重新导入"); |
| | | } |
| | | // 5. 组装草稿(不提交审批) |
| | | HrmLeaveApplicationDO application = new HrmLeaveApplicationDO(); |
| | | application.setNo(generateUniqueNo()); |
| | | application.setUserId(employee.getUserId()); |
| | | application.setDeptId(employee.getDeptId()); |
| | | application.setLeaveType(leaveType); |
| | | application.setStartTime(startTime); |
| | | application.setEndTime(endTime); |
| | | application.setDuration(duration); |
| | | application.setReason(reason); |
| | | application.setRemark(remark); |
| | | application.setStatus(HrmAuditStatusEnum.DRAFT.getStatus()); |
| | | return application; |
| | | } |
| | | |
| | | private Integer parseLeaveType(String raw) { |
| | | String value = normalizeCell(raw); |
| | | if (StrUtil.isBlank(value)) { |
| | | throw new LeaveImportRowException("请假类型为空,请填写:事假、病假、年假、婚假、产假、丧假(或数字 1-6)"); |
| | | } |
| | | for (HrmLeaveTypeEnum type : HrmLeaveTypeEnum.values()) { |
| | | if (StrUtil.equals(value, type.getName()) || StrUtil.equals(value, String.valueOf(type.getType()))) { |
| | | return type.getType(); |
| | | } |
| | | } |
| | | throw new LeaveImportRowException("请假类型[" + value |
| | | + "]不在可选范围内,请填写:事假、病假、年假、婚假、产假、丧假(或数字 1-6)"); |
| | | } |
| | | |
| | | private LocalDateTime parseDateTime(String field, String raw) { |
| | | String value = normalizeCell(raw); |
| | | if (StrUtil.isBlank(value)) { |
| | | throw new LeaveImportRowException(field + "为空,请填写格式如 2026-11-10 09:00 的时间"); |
| | | } |
| | | // 兼容 Excel 把日期存成数字序列号(如 46000.375) |
| | | if (EXCEL_SERIAL_PATTERN.matcher(value).matches()) { |
| | | double serial = Double.parseDouble(value); |
| | | if (serial > 20000 && serial < 80000) { |
| | | return LocalDateTime.ofInstant( |
| | | org.apache.poi.ss.usermodel.DateUtil.getJavaDate(serial).toInstant(), ZoneId.systemDefault()); |
| | | } |
| | | } |
| | | for (DateTimeFormatter formatter : DATE_TIME_FORMATTERS) { |
| | | try { |
| | | return LocalDateTime.parse(value, formatter); |
| | | } catch (DateTimeParseException ignored) { |
| | | // 尝试下一种格式 |
| | | } |
| | | } |
| | | // 只填日期时按当天 00:00 处理 |
| | | for (DateTimeFormatter formatter : DATE_ONLY_FORMATTERS) { |
| | | try { |
| | | return java.time.LocalDate.parse(value, formatter).atStartOfDay(); |
| | | } catch (DateTimeParseException ignored) { |
| | | // 尝试下一种格式 |
| | | } |
| | | } |
| | | throw new LeaveImportRowException(field + "[" + value |
| | | + "]格式不正确,请使用 yyyy-MM-dd HH:mm 或 yyyy-MM-dd HH:mm:ss(例如 2026-11-10 09:00)"); |
| | | } |
| | | |
| | | private BigDecimal parseDuration(String raw, LocalDateTime startTime, LocalDateTime endTime) { |
| | | String value = normalizeCell(raw); |
| | | if (StrUtil.isBlank(value)) { |
| | | throw new LeaveImportRowException("请假时长为空,请填写大于 0 的天数(支持 0.5 天)"); |
| | | } |
| | | value = StrUtil.removeSuffix(value, "天").trim(); |
| | | BigDecimal duration; |
| | | try { |
| | | duration = new BigDecimal(value); |
| | | } catch (NumberFormatException e) { |
| | | throw new LeaveImportRowException("请假时长[" + value + "]不是有效数字,请填写大于 0 的天数(例如 0.5、1、2.5)"); |
| | | } |
| | | if (duration.compareTo(BigDecimal.ZERO) <= 0) { |
| | | throw new LeaveImportRowException("请假时长[" + duration.stripTrailingZeros().toPlainString() |
| | | + "]必须大于 0 天,请按实际请假天数填写"); |
| | | } |
| | | // 仅做提示,不阻断:时长与起止时间明显不符时给出可核对信息 |
| | | long minutes = java.time.Duration.between(startTime, endTime).toMinutes(); |
| | | BigDecimal expectedMax = BigDecimal.valueOf(minutes) |
| | | .divide(BigDecimal.valueOf(60 * 24), 2, RoundingMode.HALF_UP); |
| | | if (duration.compareTo(expectedMax) > 0) { |
| | | throw new LeaveImportRowException("请假时长[" + duration.stripTrailingZeros().toPlainString() |
| | | + " 天]超过起止时间跨度[" + expectedMax.stripTrailingZeros().toPlainString() |
| | | + " 天],请核对开始/结束时间与时长后重新导入"); |
| | | } |
| | | return duration; |
| | | } |
| | | |
| | | private static String normalizeCell(String raw) { |
| | | if (StrUtil.isBlank(raw)) { |
| | | return null; |
| | | } |
| | | String value = raw.trim(); |
| | | // Excel 数值单元格可能被读成科学计数法或带 .0 的小数,统一还原为普通数字 |
| | | if (SCIENTIFIC_PATTERN.matcher(value).matches() || TRAILING_ZERO_PATTERN.matcher(value).matches()) { |
| | | try { |
| | | value = new BigDecimal(value).stripTrailingZeros().toPlainString(); |
| | | } catch (NumberFormatException ignored) { |
| | | // 保持原值 |
| | | } |
| | | } |
| | | return value; |
| | | } |
| | | |
| | | private static String trimToNull(String value) { |
| | | return StrUtil.isBlank(value) ? null : value.trim(); |
| | | } |
| | | |
| | | private static String formatDateTime(LocalDateTime value) { |
| | | return value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")); |
| | | } |
| | | |
| | | /** 单行导入校验异常:message 直接作为该行的失败原因返回给用户 */ |
| | | private static class LeaveImportRowException extends RuntimeException { |
| | | LeaveImportRowException(String message) { |
| | | super(message); |
| | | } |
| | | } |
| | | |
| | | private static final Pattern PHONE_PATTERN = Pattern.compile("^1\\d{10}$"); |
| | | private static final Pattern EXCEL_SERIAL_PATTERN = Pattern.compile("^\\d{5}(\\.\\d+)?$"); |
| | | private static final Pattern SCIENTIFIC_PATTERN = Pattern.compile("^-?\\d+(\\.\\d+)?[eE][+-]?\\d+$"); |
| | | private static final Pattern TRAILING_ZERO_PATTERN = Pattern.compile("^-?\\d+\\.0+$"); |
| | | private static final DateTimeFormatter[] DATE_TIME_FORMATTERS = new DateTimeFormatter[]{ |
| | | DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"), |
| | | DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"), |
| | | DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"), |
| | | DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm") |
| | | }; |
| | | private static final DateTimeFormatter[] DATE_ONLY_FORMATTERS = new DateTimeFormatter[]{ |
| | | DateTimeFormatter.ofPattern("yyyy-MM-dd"), |
| | | DateTimeFormatter.ofPattern("yyyy/MM/dd") |
| | | }; |
| | | |
| | | private Long getCurrentUserId() { |
| | | return SecurityFrameworkUtils.getLoginUserId(); |
| | |
| | | public void updateLeaveApplication(HrmLeaveApplicationSaveReqVO updateReqVO) { |
| | | // 校验存在 |
| | | HrmLeaveApplicationDO application = validateLeaveApplicationExists(updateReqVO.getId()); |
| | | // 校验状态为草稿或审批中 |
| | | validateOperator(application, getCurrentUserId()); |
| | | // 驳回后允许修改并重新提交审批。 |
| | | if (!Objects.equals(application.getStatus(), HrmAuditStatusEnum.DRAFT.getStatus()) |
| | | && !Objects.equals(application.getStatus(), HrmAuditStatusEnum.PROCESS.getStatus())) { |
| | | throw exception(LEAVE_APPLICATION_UPDATE_FAIL_NOT_DRAFT); |
| | | && !Objects.equals(application.getStatus(), HrmAuditStatusEnum.REJECT.getStatus())) { |
| | | throw exception(LEAVE_APPLICATION_UPDATE_FAIL_NOT_EDITABLE); |
| | | } |
| | | if (updateReqVO.getUserId() != null && !Objects.equals(updateReqVO.getUserId(), application.getUserId())) { |
| | | throw exception(LEAVE_APPLICATION_APPLICANT_IMMUTABLE); |
| | | } |
| | | validateApplicantDept(application.getUserId(), |
| | | updateReqVO.getDeptId() != null ? updateReqVO.getDeptId() : application.getDeptId()); |
| | | // 更新 |
| | | HrmLeaveApplicationDO updateObj = BeanUtils.toBean(updateReqVO, HrmLeaveApplicationDO.class); |
| | | updateObj.setUserId(null); |
| | | leaveApplicationMapper.updateById(updateObj); |
| | | storageAttachmentApi.updateAttachments("file", "hrm_leave_application", updateReqVO.getId(), updateReqVO.getBlobIds()); |
| | | } |
| | |
| | | public void deleteLeaveApplication(Long id) { |
| | | // 校验存在 |
| | | HrmLeaveApplicationDO application = validateLeaveApplicationExists(id); |
| | | validateOperator(application, getCurrentUserId()); |
| | | // 校验状态为草稿 |
| | | if (!Objects.equals(application.getStatus(), HrmAuditStatusEnum.DRAFT.getStatus())) { |
| | | throw exception(LEAVE_APPLICATION_UPDATE_FAIL_NOT_DRAFT); |
| | |
| | | public void submitLeaveApplication(Long id, String processDefinitionKey, Long userId) { |
| | | // 1. 校验请假申请是否存在 |
| | | HrmLeaveApplicationDO application = validateLeaveApplicationExists(id); |
| | | if (ObjUtil.notEqual(application.getStatus(), HrmAuditStatusEnum.DRAFT.getStatus())) { |
| | | validateOperator(application, userId); |
| | | if (ObjUtil.notEqual(application.getStatus(), HrmAuditStatusEnum.DRAFT.getStatus()) |
| | | && ObjUtil.notEqual(application.getStatus(), HrmAuditStatusEnum.REJECT.getStatus())) { |
| | | throw exception(LEAVE_APPLICATION_SUBMIT_FAIL_NOT_DRAFT); |
| | | } |
| | | validateApplicantDept(application.getUserId(), application.getDeptId()); |
| | | |
| | | // 2. 校验分类和流程定义是否存在 |
| | | // 全局免审批:业务校验通过后直接执行审批通过逻辑,无需流程模型。 |
| | | if (bpmProcessInstanceApi.isGlobalAutoApprove()) { |
| | | leaveApplicationMapper.updateById(HrmLeaveApplicationDO.builder() |
| | | .id(id) |
| | | .processInstanceId("") |
| | | .status(HrmAuditStatusEnum.PROCESS.getStatus()) |
| | | .build()); |
| | | updateLeaveApplicationAuditStatus(id, HrmAuditStatusEnum.APPROVE.getStatus()); |
| | | return; |
| | | } |
| | | |
| | | validateApproveCategoryAndProcessDefinition(); |
| | | |
| | | // 3. 校验传入的流程定义 Key 是否属于该分类且处于激活状态 |
| | | if (processDefinitionKey == null || processDefinitionKey.isBlank()) { |
| | | throw exception(LEAVE_APPLICATION_BPM_PROCESS_DEFINITION_NOT_EXISTS); |
| | | } |
| | | ProcessDefinition processDefinition = bpmProcessDefinitionService.getActiveProcessDefinition(processDefinitionKey); |
| | | if (processDefinition == null) { |
| | | if (processDefinition == null || bpmProcessDefinitionService |
| | | .getProcessDefinitionInfoListByCategory(APPROVE_CATEGORY_CODE).stream() |
| | | .noneMatch(info -> Objects.equals(info.getProcessDefinitionId(), processDefinition.getId()))) { |
| | | throw exception(LEAVE_APPLICATION_BPM_PROCESS_DEFINITION_NOT_EXISTS); |
| | | } |
| | | |
| | | // 4. 创建审批流程实例 |
| | | String processInstanceId = bpmProcessInstanceApi.createProcessInstance(userId, new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(processDefinitionKey).setBusinessKey(String.valueOf(id))); |
| | | Map<String, Object> variables = new HashMap<>(); |
| | | variables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_BUSINESS_NO, application.getNo()); |
| | | AdminUserRespDTO applicant = adminUserApi.getUser(application.getUserId()); |
| | | variables.put("Fl55mt725q5yabc", applicant != null ? applicant.getNickname() : String.valueOf(application.getUserId())); |
| | | variables.put("F5eqmt725u3naec", LEAVE_TIME_FORMAT.format(application.getStartTime()) |
| | | + " 至 " + LEAVE_TIME_FORMAT.format(application.getEndTime())); |
| | | variables.put("F8wtmt726c3zahc", application.getDuration().toPlainString()); |
| | | String processInstanceId = bpmProcessInstanceApi.createProcessInstance(application.getUserId(), new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(processDefinitionKey).setBusinessKey(String.valueOf(id)) |
| | | .setVariables(variables)); |
| | | |
| | | // 5. 更新请假申请工作流编号 |
| | | leaveApplicationMapper.updateById(HrmLeaveApplicationDO.builder() |
| | | .id(id) |
| | | .processInstanceId(processInstanceId) |
| | | .status(HrmAuditStatusEnum.PROCESS.getStatus()) |
| | | .build()); |
| | | leaveApplicationMapper.update(null, new LambdaUpdateWrapper<HrmLeaveApplicationDO>() |
| | | .eq(HrmLeaveApplicationDO::getId, id) |
| | | .set(HrmLeaveApplicationDO::getProcessInstanceId, processInstanceId) |
| | | .set(HrmLeaveApplicationDO::getStatus, HrmAuditStatusEnum.PROCESS.getStatus()) |
| | | .set(HrmLeaveApplicationDO::getApproveTime, null)); |
| | | } |
| | | |
| | | private void validateOperator(HrmLeaveApplicationDO application, Long operatorId) { |
| | | if (!Objects.equals(application.getUserId(), operatorId) |
| | | && !Objects.equals(application.getCreator(), String.valueOf(operatorId))) { |
| | | throw exception(LEAVE_APPLICATION_OPERATOR_NOT_ALLOWED); |
| | | } |
| | | } |
| | | |
| | | private void validateApplicantDept(Long userId, Long deptId) { |
| | | HrmEmployeeDO employee = employeeMapper.selectByUserId(userId); |
| | | if (employee == null || employee.getDeptId() == null) { |
| | | throw exception(LEAVE_APPLICATION_EMPLOYEE_DEPT_NOT_FOUND); |
| | | } |
| | | if (!Objects.equals(employee.getDeptId(), deptId)) { |
| | | throw exception(LEAVE_APPLICATION_DEPT_MISMATCH); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | throw exception(LEAVE_APPLICATION_UPDATE_AUDIT_STATUS_FAIL_NOT_PROCESS); |
| | | } |
| | | // 更新状态 |
| | | leaveApplicationMapper.updateById(HrmLeaveApplicationDO.builder() |
| | | .id(id) |
| | | .status(status) |
| | | .approveTime(java.time.LocalDateTime.now()) |
| | | .build()); |
| | | leaveApplicationMapper.update(null, new LambdaUpdateWrapper<HrmLeaveApplicationDO>() |
| | | .eq(HrmLeaveApplicationDO::getId, id) |
| | | .set(HrmLeaveApplicationDO::getStatus, status) |
| | | .set(HrmLeaveApplicationDO::getApproveTime, |
| | | Objects.equals(status, HrmAuditStatusEnum.APPROVE.getStatus()) |
| | | ? java.time.LocalDateTime.now() : null)); |
| | | } |
| | | |
| | | @Override |
| | |
| | | return leaveApplicationMapper.selectByProcessInstanceId(processInstanceId); |
| | | } |
| | | |
| | | } |
| | | } |