package cn.iocoder.yudao.module.hrm.service.approval.listener; import cn.iocoder.yudao.module.bpm.api.event.BpmProcessInstanceStatusEvent; import cn.iocoder.yudao.module.hrm.dal.dataobject.approval.HrmTransferApplicationDO; import cn.iocoder.yudao.module.hrm.dal.dataobject.employee.HrmEmployeeDO; import cn.iocoder.yudao.module.hrm.dal.mysql.approval.HrmTransferApplicationMapper; import cn.iocoder.yudao.module.hrm.dal.mysql.employee.HrmEmployeeMapper; import cn.iocoder.yudao.module.hrm.enums.HrmAuditStatusEnum; import cn.iocoder.yudao.module.hrm.service.approval.HrmTransferApplicationService; import cn.iocoder.yudao.module.hrm.util.HrmAuditStatusUtils; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; /** * 调岗申请审批的结果监听器实现类 * * @author 超级管理员 */ @Component @Slf4j public class HrmTransferApplicationStatusListener implements ApplicationListener { @Resource private HrmTransferApplicationService transferApplicationService; @Resource private HrmTransferApplicationMapper transferApplicationMapper; @Resource private HrmEmployeeMapper employeeMapper; @Override public void onApplicationEvent(BpmProcessInstanceStatusEvent event) { // 通过 businessKey 查询调岗申请 Long applicationId; try { applicationId = Long.parseLong(event.getBusinessKey()); } catch (NumberFormatException e) { return; } HrmTransferApplicationDO application = transferApplicationMapper.selectById(applicationId); if (application == null) { return; } // 只有审批中状态的调岗申请才处理 if (!HrmAuditStatusEnum.PROCESS.getStatus().equals(application.getStatus())) { log.warn("[onApplicationEvent] 调岗申请({}) 不处于审批中状态,当前状态: {}", applicationId, application.getStatus()); return; } log.info("[onApplicationEvent] 调岗申请({}) 审批状态更新: {}", applicationId, event.getStatus()); // 更新审批状态 Integer auditStatus = HrmAuditStatusUtils.convertBpmResultToAuditStatus(event.getStatus()); transferApplicationService.updateTransferApplicationAuditStatus(applicationId, auditStatus); // 审批通过:回写员工主档(部门 = 目标部门,岗位 = 目标岗位) if (HrmAuditStatusEnum.APPROVE.getStatus().equals(auditStatus)) { writeBackEmployee(application); } } private void writeBackEmployee(HrmTransferApplicationDO application) { HrmEmployeeDO employee = employeeMapper.selectByUserId(application.getUserId()); if (employee == null) { log.warn("[writeBackEmployee] 调岗申请({}) 用户({}) 未找到员工档案,跳过回写", application.getId(), application.getUserId()); return; } employeeMapper.updateById(HrmEmployeeDO.builder() .id(employee.getId()) .deptId(application.getTargetDeptId()) .postId(application.getTargetPostId()) .build()); } }