| | |
| | | package cn.iocoder.yudao.module.hrm.service.approval; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | 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.service.definition.BpmCategoryService; |
| | | import cn.iocoder.yudao.module.bpm.service.definition.BpmProcessDefinitionService; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmResignationApplicationPageReqVO; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmResignationApplicationRespVO; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmResignationApplicationSaveReqVO; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmResignationHandoverReqVO; |
| | | import cn.iocoder.yudao.module.hrm.dal.dataobject.approval.HrmResignationApplicationDO; |
| | | import cn.iocoder.yudao.module.hrm.dal.dataobject.employee.HrmEmployeeDO; |
| | | import cn.iocoder.yudao.module.hrm.dal.mysql.approval.HrmResignationApplicationMapper; |
| | | 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.HrmResignationTypeEnum; |
| | | import cn.iocoder.yudao.module.hrm.service.handover.HrmUserHandoverService; |
| | | import cn.iocoder.yudao.module.system.api.approval.ApprovalConfigApi; |
| | | import cn.iocoder.yudao.module.system.api.dept.DeptApi; |
| | | import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO; |
| | | import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi; |
| | |
| | | import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; |
| | | import jakarta.annotation.Resource; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.flowable.engine.repository.ProcessDefinition; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.time.LocalDate; |
| | | import java.util.Collections; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.Stream; |
| | | |
| | | import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; |
| | | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
| | | import static cn.iocoder.yudao.module.hrm.enums.ErrorCodeConstants.*; |
| | | |
| | | /** |
| | | * 离职申请 Service 实现类 |
| | | * |
| | | * @author 芋道源码 |
| | | * @author 超级管理员 |
| | | */ |
| | | @Service |
| | | @Validated |
| | |
| | | private static final Integer HANDOVER_STATUS_IN_PROGRESS = 10; |
| | | private static final Integer HANDOVER_STATUS_COMPLETED = 20; |
| | | |
| | | /** |
| | | * 离职申请审批的业务类型编码 |
| | | * |
| | | * 与 system_approval_config.biz_type 对应,审批人在「系统管理 - 审批配置」中维护。 |
| | | */ |
| | | private static final String RESIGNATION_APPROVE_BIZ_TYPE = "hrm_resignation_approve"; |
| | | |
| | | /** |
| | | * 员工状态:离职 |
| | | */ |
| | | private static final Integer EMPLOYEE_STATUS_RESIGNED = 3; |
| | | |
| | | @Resource |
| | | private HrmResignationApplicationMapper resignationApplicationMapper; |
| | | @Resource |
| | |
| | | @Resource |
| | | private AdminUserApi adminUserApi; |
| | | @Resource |
| | | private BpmProcessInstanceApi bpmProcessInstanceApi; |
| | | @Resource |
| | | private BpmCategoryService bpmCategoryService; |
| | | @Resource |
| | | private StorageAttachmentApi storageAttachmentApi; |
| | | @Resource |
| | | private BpmProcessDefinitionService bpmProcessDefinitionService; |
| | | private ApprovalConfigApi approvalConfigApi; |
| | | @Resource |
| | | private HrmEmployeeMapper employeeMapper; |
| | | @Resource |
| | | private HrmUserHandoverService handoverService; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | |
| | | AdminUserRespDTO user = adminUserApi.getUser(userId); |
| | | if (user == null) { |
| | | userId = getCurrentUserId(); |
| | | user = adminUserApi.getUser(userId); |
| | | } |
| | | HrmResignationApplicationDO application = BeanUtils.toBean(createReqVO, HrmResignationApplicationDO.class); |
| | | application.setNo(no); |
| | | application.setUserId(userId); |
| | | application.setDeptId(createReqVO.getDeptId() != null ? createReqVO.getDeptId() : user.getDeptId()); |
| | | application.setDeptId(createReqVO.getDeptId() != null ? createReqVO.getDeptId() |
| | | : (user != null ? user.getDeptId() : null)); |
| | | application.setStatus(HrmAuditStatusEnum.DRAFT.getStatus()); |
| | | application.setHandoverStatus(HANDOVER_STATUS_NOT_STARTED); |
| | | resignationApplicationMapper.insert(application); |
| | |
| | | } |
| | | |
| | | private Long getCurrentUserId() { |
| | | return 1L; |
| | | return SecurityFrameworkUtils.getLoginUserId(); |
| | | } |
| | | |
| | | private String generateUniqueNo() { |
| | |
| | | } |
| | | HrmResignationApplicationDO updateObj = BeanUtils.toBean(updateReqVO, HrmResignationApplicationDO.class); |
| | | resignationApplicationMapper.updateById(updateObj); |
| | | if (updateReqVO.getBlobIds() != null) { |
| | | storageAttachmentApi.deleteAttachmentsByRecord("hrm_resignation_application", updateReqVO.getId()); |
| | | storageAttachmentApi.bindAttachments("file", "hrm_resignation_application", |
| | | updateReqVO.getId(), updateReqVO.getBlobIds()); |
| | | } |
| | | storageAttachmentApi.updateAttachments("file", "hrm_resignation_application", updateReqVO.getId(), updateReqVO.getBlobIds()); |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | |
| | | private void fillUserNames(HrmResignationApplicationRespVO respVO, HrmResignationApplicationDO application) { |
| | | Set<Long> userIds = Set.of(application.getUserId(), application.getHandoverUserId()) |
| | | .stream().filter(Objects::nonNull).collect(Collectors.toSet()); |
| | | Set<Long> userIds = Stream.of(application.getUserId(), application.getHandoverUserId()) |
| | | .filter(Objects::nonNull).collect(Collectors.toSet()); |
| | | Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(userIds); |
| | | if (application.getUserId() != null && userMap.containsKey(application.getUserId())) { |
| | | respVO.setUserName(userMap.get(application.getUserId()).getNickname()); |
| | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void submitResignationApplication(Long id, String processDefinitionKey, Long userId) { |
| | | public void submitResignationApplication(Long id) { |
| | | // 1. 校验存在 + 可提交状态(草稿 / 审核不通过) |
| | | HrmResignationApplicationDO application = validateResignationApplicationExists(id); |
| | | if (ObjUtil.notEqual(application.getStatus(), HrmAuditStatusEnum.DRAFT.getStatus())) { |
| | | throw exception(RESIGNATION_APPLICATION_SUBMIT_FAIL_NOT_DRAFT); |
| | | if (!Objects.equals(application.getStatus(), HrmAuditStatusEnum.DRAFT.getStatus()) |
| | | && !Objects.equals(application.getStatus(), HrmAuditStatusEnum.REJECT.getStatus())) { |
| | | throw exception(RESIGNATION_APPLICATION_SUBMIT_FAIL_NOT_EDITABLE); |
| | | } |
| | | validateApproveCategoryAndProcessDefinition(); |
| | | ProcessDefinition processDefinition = bpmProcessDefinitionService.getActiveProcessDefinition(processDefinitionKey); |
| | | if (processDefinition == null) { |
| | | throw exception(RESIGNATION_APPLICATION_BPM_PROCESS_DEFINITION_NOT_EXISTS); |
| | | } |
| | | String processInstanceId = bpmProcessInstanceApi.createProcessInstance(userId, new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(processDefinitionKey).setBusinessKey(String.valueOf(id))); |
| | | resignationApplicationMapper.updateById(HrmResignationApplicationDO.builder() |
| | | .id(id) |
| | | .processInstanceId(processInstanceId) |
| | | .status(HrmAuditStatusEnum.PROCESS.getStatus()) |
| | | .build()); |
| | | } |
| | | |
| | | private void validateApproveCategoryAndProcessDefinition() { |
| | | List<String> codes = Collections.singletonList(APPROVE_CATEGORY_CODE); |
| | | Map<String, ?> categoryMap = bpmCategoryService.getCategoryMap(codes); |
| | | if (!categoryMap.containsKey(APPROVE_CATEGORY_CODE)) { |
| | | throw exception(RESIGNATION_APPLICATION_BPM_PROCESS_DEFINITION_NOT_EXISTS); |
| | | } |
| | | List<BpmProcessDefinitionInfoDO> definitionInfoList = bpmProcessDefinitionService |
| | | .getProcessDefinitionInfoListByCategory(APPROVE_CATEGORY_CODE); |
| | | if (CollUtil.isEmpty(definitionInfoList)) { |
| | | throw exception(RESIGNATION_APPLICATION_BPM_PROCESS_DEFINITION_NOT_EXISTS); |
| | | } |
| | | Set<String> processDefinitionIds = convertSet(definitionInfoList, BpmProcessDefinitionInfoDO::getProcessDefinitionId); |
| | | List<ProcessDefinition> processDefinitions = bpmProcessDefinitionService.getProcessDefinitionList(processDefinitionIds); |
| | | boolean hasActiveDefinition = processDefinitions.stream().anyMatch(pd -> !pd.isSuspended()); |
| | | if (!hasActiveDefinition) { |
| | | throw exception(RESIGNATION_APPLICATION_BPM_PROCESS_DEFINITION_NOT_EXISTS); |
| | | } |
| | | // 2. 校验审批配置已启用且配置了有效审批人(未配置时抛出带明确提示的业务异常) |
| | | approvalConfigApi.validateApprovalEnabledAndGetApprovers(RESIGNATION_APPROVE_BIZ_TYPE); |
| | | |
| | | // 3. 状态置为审批中,并清空上一轮审核结果,避免残留「审核不通过」的原因 |
| | | resignationApplicationMapper.submitAndResetAuditInfo(id, HrmAuditStatusEnum.PROCESS.getStatus()); |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> getApproveProcessDefinitionList() { |
| | | validateApproveCategoryAndProcessDefinition(); |
| | | List<BpmProcessDefinitionInfoDO> definitionInfoList = bpmProcessDefinitionService |
| | | .getProcessDefinitionInfoListByCategory(APPROVE_CATEGORY_CODE); |
| | | Set<String> processDefinitionIds = convertSet(definitionInfoList, BpmProcessDefinitionInfoDO::getProcessDefinitionId); |
| | | List<ProcessDefinition> processDefinitions = bpmProcessDefinitionService.getProcessDefinitionList(processDefinitionIds); |
| | | Map<String, ProcessDefinition> latestVersionMap = new HashMap<>(); |
| | | for (ProcessDefinition pd : processDefinitions) { |
| | | if (pd.isSuspended()) continue; |
| | | ProcessDefinition existing = latestVersionMap.get(pd.getKey()); |
| | | if (existing == null || pd.getVersion() > existing.getVersion()) { |
| | | latestVersionMap.put(pd.getKey(), pd); |
| | | } |
| | | } |
| | | List<Map<String, Object>> result = new ArrayList<>(); |
| | | for (ProcessDefinition pd : latestVersionMap.values()) { |
| | | Map<String, Object> item = new HashMap<>(); |
| | | item.put("id", pd.getId()); |
| | | item.put("key", pd.getKey()); |
| | | item.put("name", pd.getName()); |
| | | item.put("version", pd.getVersion()); |
| | | result.add(item); |
| | | } |
| | | return result; |
| | | public Set<Long> getResignationApplicationApproverUserIds() { |
| | | return approvalConfigApi.getApproverUserIds(RESIGNATION_APPROVE_BIZ_TYPE); |
| | | } |
| | | |
| | | @Override |
| | | public void updateResignationApplicationAuditStatus(Long id, Integer status) { |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void auditResignationApplication(Long id, Boolean pass, String reviewRemark) { |
| | | // 1. 校验存在 |
| | | HrmResignationApplicationDO application = validateResignationApplicationExists(id); |
| | | // 2. 校验处于审批中状态 |
| | | if (!Objects.equals(application.getStatus(), HrmAuditStatusEnum.PROCESS.getStatus())) { |
| | | log.error("[updateResignationApplicationAuditStatus][application({}) 不处于审批中,无法更新审批结果({})]", id, status); |
| | | throw exception(RESIGNATION_APPLICATION_UPDATE_AUDIT_STATUS_FAIL_NOT_PROCESS); |
| | | throw exception(RESIGNATION_APPLICATION_AUDIT_FAIL_NOT_PROCESS); |
| | | } |
| | | resignationApplicationMapper.updateById(HrmResignationApplicationDO.builder() |
| | | .id(id) |
| | | .status(status) |
| | | .approveTime(java.time.LocalDateTime.now()) |
| | | // 3. 校验当前登录用户是该业务类型的审批人(或签:任一人均可审核) |
| | | Long userId = getLoginUserId(); |
| | | approvalConfigApi.validateApprover(RESIGNATION_APPROVE_BIZ_TYPE, userId); |
| | | // 4. 审核不通过:必须填写原因,状态置为审核不通过,退回提交人修改后可重新提交 |
| | | if (!Boolean.TRUE.equals(pass)) { |
| | | if (StrUtil.isBlank(reviewRemark)) { |
| | | throw exception(RESIGNATION_APPLICATION_AUDIT_REJECT_REASON_REQUIRED); |
| | | } |
| | | resignationApplicationMapper.auditResignationApplication(id, HrmAuditStatusEnum.REJECT.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark); |
| | | return; |
| | | } |
| | | // 5. 审核通过:记录审核人与审核意见,状态置为审核通过 |
| | | resignationApplicationMapper.auditResignationApplication(id, HrmAuditStatusEnum.APPROVE.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark); |
| | | |
| | | // 6. 审核通过后置业务逻辑:回写员工主档(状态=离职,离职日期 = 实际离职日期或预计离职日期) |
| | | writeBackEmployee(application); |
| | | } |
| | | |
| | | /** |
| | | * 审核通过后回写员工主档 |
| | | * |
| | | * @param application 离职申请 |
| | | */ |
| | | private void writeBackEmployee(HrmResignationApplicationDO application) { |
| | | HrmEmployeeDO employee = employeeMapper.selectByUserId(application.getUserId()); |
| | | if (employee == null) { |
| | | log.warn("[writeBackEmployee] 离职申请({}) 用户({}) 未找到员工档案,跳过回写", application.getId(), application.getUserId()); |
| | | return; |
| | | } |
| | | LocalDate leaveDate = application.getActualDate() != null |
| | | ? application.getActualDate() : application.getExpectedDate(); |
| | | employeeMapper.updateById(HrmEmployeeDO.builder() |
| | | .id(employee.getId()) |
| | | .employeeStatus(EMPLOYEE_STATUS_RESIGNED) |
| | | .leaveDate(leaveDate) |
| | | .build()); |
| | | } |
| | | |
| | | /** |
| | | * 获取用户昵称 |
| | | * |
| | | * @param userId 用户编号 |
| | | * @return 昵称,用户不存在时返回 null |
| | | */ |
| | | private String getUserNickname(Long userId) { |
| | | if (userId == null) { |
| | | return null; |
| | | } |
| | | AdminUserRespDTO user = adminUserApi.getUser(userId); |
| | | return user == null ? null : user.getNickname(); |
| | | } |
| | | |
| | | @Override |
| | | public HrmResignationApplicationDO getResignationApplicationByProcessInstanceId(String processInstanceId) { |
| | | return resignationApplicationMapper.selectByProcessInstanceId(processInstanceId); |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateHandoverStatus(HrmResignationHandoverReqVO reqVO) { |
| | | HrmResignationApplicationDO application = validateResignationApplicationExists(reqVO.getId()); |
| | | resignationApplicationMapper.updateById(HrmResignationApplicationDO.builder() |
| | | .id(reqVO.getId()) |
| | | .handoverStatus(reqVO.getHandoverStatus()) |
| | | .build()); |
| | | // 交接完成时,创建用户数据交接映射 |
| | | if (Objects.equals(reqVO.getHandoverStatus(), HANDOVER_STATUS_COMPLETED) |
| | | && application.getHandoverUserId() != null) { |
| | | handoverService.createHandover( |
| | | application.getUserId(), |
| | | application.getHandoverUserId(), |
| | | application.getId()); |
| | | } |
| | | } |
| | | |
| | | } |