3 天以前 b852254d90e7d1955db31db154193ec8ee84d8b3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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<BpmProcessInstanceStatusEvent> {
 
    @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());
    }
 
}