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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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.HrmRegularizeApplicationDO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.employee.HrmEmployeeDO;
import cn.iocoder.yudao.module.hrm.dal.mysql.approval.HrmRegularizeApplicationMapper;
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.HrmRegularizeApplicationService;
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;
 
import java.time.LocalDate;
 
/**
 * 转正申请审批的结果监听器实现类
 *
 * @author 超级管理员
 */
@Component
@Slf4j
public class HrmRegularizeApplicationStatusListener implements ApplicationListener<BpmProcessInstanceStatusEvent> {
 
    /**
     * 员工状态:在职
     */
    private static final Integer EMPLOYEE_STATUS_ACTIVE = 1;
 
    /**
     * 用工性质:正式
     */
    private static final Integer EMPLOYMENT_TYPE_FORMAL = 1;
 
    @Resource
    private HrmRegularizeApplicationService regularizeApplicationService;
    @Resource
    private HrmRegularizeApplicationMapper regularizeApplicationMapper;
    @Resource
    private HrmEmployeeMapper employeeMapper;
 
    @Override
    public void onApplicationEvent(BpmProcessInstanceStatusEvent event) {
        // 通过 businessKey 查询转正申请
        Long applicationId;
        try {
            applicationId = Long.parseLong(event.getBusinessKey());
        } catch (NumberFormatException e) {
            return;
        }
 
        HrmRegularizeApplicationDO application = regularizeApplicationMapper.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());
        regularizeApplicationService.updateRegularizeApplicationAuditStatus(applicationId, auditStatus);
        // 审批通过:回写员工主档(状态=在职,用工性质=正式,转正日期 = 拟转正日期或当天)
        if (HrmAuditStatusEnum.APPROVE.getStatus().equals(auditStatus)) {
            writeBackEmployee(application);
        }
    }
 
    private void writeBackEmployee(HrmRegularizeApplicationDO application) {
        HrmEmployeeDO employee = employeeMapper.selectByUserId(application.getUserId());
        if (employee == null) {
            log.warn("[writeBackEmployee] 转正申请({}) 用户({}) 未找到员工档案,跳过回写", application.getId(), application.getUserId());
            return;
        }
        LocalDate regularDate = application.getExpectedDate() != null ? application.getExpectedDate() : LocalDate.now();
        employeeMapper.updateById(HrmEmployeeDO.builder()
                .id(employee.getId())
                .employeeStatus(EMPLOYEE_STATUS_ACTIVE)
                .employmentType(EMPLOYMENT_TYPE_FORMAL)
                .regularDate(regularDate)
                .build());
    }
 
}