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());
|
}
|
|
}
|