| | |
| | | |
| | | 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.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.controller.admin.employee.vo.HrmEmployeeSaveReqVO; |
| | | 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.redis.HrmNoRedisDAO; |
| | | import cn.iocoder.yudao.module.hrm.enums.HrmAuditStatusEnum; |
| | | import cn.iocoder.yudao.module.hrm.enums.HrmEmployeeStatusEnum; |
| | | import cn.iocoder.yudao.module.hrm.enums.HrmResignationTypeEnum; |
| | | import cn.iocoder.yudao.module.hrm.service.employee.HrmEmployeeService; |
| | | import cn.iocoder.yudao.module.hrm.service.handover.HrmUserHandoverService; |
| | | 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.AdminUserApi; |
| | | import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; |
| | | import jakarta.annotation.Resource; |
| | |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.Comparator; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | /** |
| | | * 离职申请 Service 实现类 |
| | | * |
| | | * @author 芋道源码 |
| | | * @author 超级管理员 |
| | | */ |
| | | @Service |
| | | @Validated |
| | |
| | | @Resource |
| | | private BpmCategoryService bpmCategoryService; |
| | | @Resource |
| | | private StorageAttachmentApi storageAttachmentApi; |
| | | @Resource |
| | | private BpmProcessDefinitionService bpmProcessDefinitionService; |
| | | @Resource |
| | | private HrmUserHandoverService handoverService; |
| | | @Resource |
| | | private HrmEmployeeService employeeService; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long createResignationApplication(HrmResignationApplicationSaveReqVO createReqVO) { |
| | | // 1. 解析申请人:优先按前端选择的员工(employeeId)反查档案,拿到 system_users.id |
| | | Long userId = createReqVO.getUserId(); |
| | | Long employeeId = createReqVO.getEmployeeId(); |
| | | HrmEmployeeDO employee = null; |
| | | if (employeeId != null) { |
| | | employee = employeeService.getEmployee(employeeId); |
| | | } |
| | | if (employee == null && userId != null) { |
| | | // 约定 user_id 存 system_users.id;历史数据也有存「员工主键」的,两种都兼容 |
| | | employee = employeeService.getEmployeeByUserId(userId); |
| | | if (employee == null) { |
| | | employee = employeeService.getEmployee(userId); |
| | | } |
| | | } |
| | | if (employee != null && employee.getUserId() != null) { |
| | | userId = employee.getUserId(); |
| | | } |
| | | if (userId == null) { |
| | | userId = getCurrentUserId(); |
| | | } |
| | |
| | | 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()); |
| | | // 部门:优先用请求里的,其次员工档案,最后用户档案 |
| | | Long deptId = createReqVO.getDeptId(); |
| | | if (deptId == null && employee != null) { |
| | | deptId = employee.getDeptId(); |
| | | } |
| | | if (deptId == null && user != null) { |
| | | deptId = user.getDeptId(); |
| | | } |
| | | application.setDeptId(deptId); |
| | | application.setStatus(HrmAuditStatusEnum.DRAFT.getStatus()); |
| | | application.setHandoverStatus(HANDOVER_STATUS_NOT_STARTED); |
| | | resignationApplicationMapper.insert(application); |
| | | storageAttachmentApi.bindAttachments("file", "hrm_resignation_application", |
| | | application.getId(), createReqVO.getBlobIds()); |
| | | return application.getId(); |
| | | } |
| | | |
| | |
| | | } |
| | | HrmResignationApplicationDO updateObj = BeanUtils.toBean(updateReqVO, HrmResignationApplicationDO.class); |
| | | resignationApplicationMapper.updateById(updateObj); |
| | | storageAttachmentApi.updateAttachments("file", "hrm_resignation_application", updateReqVO.getId(), updateReqVO.getBlobIds()); |
| | | } |
| | | |
| | | @Override |
| | |
| | | if (!Objects.equals(application.getStatus(), HrmAuditStatusEnum.DRAFT.getStatus())) { |
| | | throw exception(RESIGNATION_APPLICATION_UPDATE_FAIL_NOT_DRAFT); |
| | | } |
| | | storageAttachmentApi.deleteAttachmentsByRecord("hrm_resignation_application", id); |
| | | resignationApplicationMapper.deleteById(id); |
| | | } |
| | | |
| | |
| | | respVO.setResignationTypeName(HrmResignationTypeEnum.getNameByStatus(application.getResignationType())); |
| | | respVO.setHandoverStatusName(getHandoverStatusName(application.getHandoverStatus())); |
| | | respVO.setStatusName(HrmAuditStatusEnum.getNameByStatus(application.getStatus())); |
| | | respVO.setAttachmentList(storageAttachmentApi.listAttachments("hrm_resignation_application", id)); |
| | | return respVO; |
| | | } |
| | | |
| | |
| | | throw exception(RESIGNATION_APPLICATION_SUBMIT_FAIL_NOT_DRAFT); |
| | | } |
| | | validateApproveCategoryAndProcessDefinition(); |
| | | // 前端不传流程定义 Key 时,自动取该审批分类下最新版本的活跃流程,避免「请求参数缺失:processDefinitionKey」 |
| | | if (StrUtil.isBlank(processDefinitionKey)) { |
| | | processDefinitionKey = resolveActiveProcessDefinitionKey(); |
| | | } |
| | | 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))); |
| | | .setProcessDefinitionKey(processDefinition.getKey()).setBusinessKey(String.valueOf(id))); |
| | | resignationApplicationMapper.updateById(HrmResignationApplicationDO.builder() |
| | | .id(id) |
| | | .processInstanceId(processInstanceId) |
| | |
| | | .status(status) |
| | | .approveTime(java.time.LocalDateTime.now()) |
| | | .build()); |
| | | // 审批通过:员工状态改成「离职」,同步到员工管理 |
| | | if (HrmAuditStatusEnum.APPROVE.getStatus().equals(status)) { |
| | | syncEmployeeResigned(application); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 取离职审批分类下最新版本的活跃流程定义 Key |
| | | */ |
| | | private String resolveActiveProcessDefinitionKey() { |
| | | 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); |
| | | return processDefinitions.stream() |
| | | .filter(pd -> !pd.isSuspended()) |
| | | .max(Comparator.comparingInt(ProcessDefinition::getVersion)) |
| | | .map(ProcessDefinition::getKey) |
| | | .orElseThrow(() -> exception(RESIGNATION_APPLICATION_BPM_PROCESS_DEFINITION_NOT_EXISTS)); |
| | | } |
| | | |
| | | /** |
| | | * 离职审批通过后,把员工档案的状态改成「离职」,同步到员工管理 |
| | | * |
| | | * <p>走 updateEmployee 而不是直接改表,这样 system_users 侧也会一起同步。 |
| | | */ |
| | | private void syncEmployeeResigned(HrmResignationApplicationDO application) { |
| | | try { |
| | | HrmEmployeeDO employee = employeeService.getEmployeeByUserId(application.getUserId()); |
| | | if (employee == null) { |
| | | employee = employeeService.getEmployee(application.getUserId()); |
| | | } |
| | | if (employee == null) { |
| | | log.warn("[syncEmployeeResigned][离职申请({}) 找不到员工档案,跳过状态同步]", application.getId()); |
| | | return; |
| | | } |
| | | HrmEmployeeSaveReqVO updateReqVO = BeanUtils.toBean(employee, HrmEmployeeSaveReqVO.class); |
| | | updateReqVO.setEmployeeStatus(HrmEmployeeStatusEnum.LEAVE.getStatus()); |
| | | employeeService.updateEmployee(updateReqVO); |
| | | log.info("[syncEmployeeResigned][离职申请({}) 已将员工({}) 状态改为离职]", |
| | | application.getId(), employee.getName()); |
| | | } catch (Exception ex) { |
| | | // 同步失败不应该阻断审批状态落库,只记录日志 |
| | | log.error("[syncEmployeeResigned][离职申请({}) 同步员工状态失败]", application.getId(), ex); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | return resignationApplicationMapper.selectByProcessInstanceId(processInstanceId); |
| | | } |
| | | |
| | | @Override |
| | | @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()); |
| | | } |
| | | } |
| | | |
| | | } |