feat(hrm): 调岗/离职支持指定申请人,提交免传流程Key,审批通过自动同步员工档案

需求:调岗、离职都要先选择「对谁办理」(必须对应员工管理里的员工),
提交不再报「请求参数缺失:processDefinitionKey」,审批通过后要同步到员工管理。

调岗申请(HrmTransferApplication):
- Controller:processDefinitionKey 改为 required=false
- Service:为空时自动取该审批分类下最新版本的活跃流程(resolveActiveProcessDefinitionKey)
- 审批通过(20) 时调用 syncEmployeePosition,把员工档案的 dept_id / post_ids
更新为目标部门/岗位,走 updateEmployee 保证 system_users 一起同步;
整段 try-catch,同步失败不阻断审批状态落库

离职申请(HrmResignationApplication):
- SaveReqVO 新增 employeeId(申请人)
- Controller:processDefinitionKey 改为 required=false
- createResignationApplication:按 employeeId 反查员工档案,补全 userId / deptId,
并修掉了原来 adminUserApi.getUser 返回 null 时 user.getDeptId() 的 NPE 隐患
- Service:自动解析流程 Key
- 审批通过(20) 时调用 syncEmployeeResigned,把员工档案状态改为「离职(3)」

验证(蓝羽 mom-lyzy 实测):
- 调岗:只传 employeeId 新增 → 成功,current_dept_id/current_post_id 自动补全;
提交(只传id) → code=0;审批通过后员工档案 dept_id 125→120、post_ids [48]→[20]
- 离职:只传 employeeId 新增 → 成功,user_id/dept_id 自动补全;
提交(只传id) → code=0;审批通过后员工 employee_status 1→3(离职)
已修改5个文件
164 ■■■■■ 文件已修改
yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/controller/admin/approval/HrmResignationApplicationController.java 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/controller/admin/approval/HrmTransferApplicationController.java 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/controller/admin/approval/vo/HrmResignationApplicationSaveReqVO.java 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/approval/HrmResignationApplicationServiceImpl.java 89 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/approval/HrmTransferApplicationServiceImpl.java 62 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/controller/admin/approval/HrmResignationApplicationController.java
@@ -83,10 +83,11 @@
    @PostMapping("/submit")
    @Operation(summary = "提交离职申请审批")
    @Parameter(name = "id", description = "申请ID", required = true)
    @Parameter(name = "processDefinitionKey", description = "流程定义Key", required = true)
    @Parameter(name = "processDefinitionKey", description = "流程定义Key,不传则自动取该分类下最新的活跃流程")
    @PreAuthorize("@ss.hasPermission('hrm:resignation:submit')")
    public CommonResult<Boolean> submitResignationApplication(@RequestParam("id") Long id,
                                                              @RequestParam("processDefinitionKey") String processDefinitionKey) {
                                                              @RequestParam(value = "processDefinitionKey", required = false)
                                                              String processDefinitionKey) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        resignationApplicationService.submitResignationApplication(id, processDefinitionKey, userId);
        return success(true);
yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/controller/admin/approval/HrmTransferApplicationController.java
@@ -82,10 +82,11 @@
    @PostMapping("/submit")
    @Operation(summary = "提交调岗申请审批")
    @Parameter(name = "id", description = "申请ID", required = true)
    @Parameter(name = "processDefinitionKey", description = "流程定义Key", required = true)
    @Parameter(name = "processDefinitionKey", description = "流程定义Key,不传则自动取该分类下最新的活跃流程")
    @PreAuthorize("@ss.hasPermission('hrm:transfer:submit')")
    public CommonResult<Boolean> submitTransferApplication(@RequestParam("id") Long id,
                                                           @RequestParam("processDefinitionKey") String processDefinitionKey) {
                                                           @RequestParam(value = "processDefinitionKey", required = false)
                                                           String processDefinitionKey) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        transferApplicationService.submitTransferApplication(id, processDefinitionKey, userId);
        return success(true);
yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/controller/admin/approval/vo/HrmResignationApplicationSaveReqVO.java
@@ -15,6 +15,9 @@
    @Schema(description = "申请ID", example = "1")
    private Long id;
    @Schema(description = "申请人(被办理离职的员工ID,前端表单选择)", example = "1")
    private Long employeeId;
    @Schema(description = "申请人ID", example = "1")
    private Long userId;
yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/approval/HrmResignationApplicationServiceImpl.java
@@ -2,6 +2,7 @@
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;
@@ -13,11 +14,15 @@
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;
@@ -33,6 +38,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -77,11 +83,29 @@
    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();
        }
@@ -94,11 +118,20 @@
        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);
@@ -245,12 +278,16 @@
            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)
@@ -316,6 +353,54 @@
                .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
yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/approval/HrmTransferApplicationServiceImpl.java
@@ -14,6 +14,7 @@
import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmTransferApplicationPageReqVO;
import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmTransferApplicationRespVO;
import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmTransferApplicationSaveReqVO;
import cn.iocoder.yudao.module.hrm.controller.admin.employee.vo.HrmEmployeeSaveReqVO;
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;
@@ -37,6 +38,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -345,12 +347,16 @@
            throw exception(TRANSFER_APPLICATION_SUBMIT_FAIL_NOT_DRAFT);
        }
        validateApproveCategoryAndProcessDefinition();
        // 前端不传流程定义 Key 时,自动取该审批分类下最新版本的活跃流程,避免「请求参数缺失:processDefinitionKey」
        if (StrUtil.isBlank(processDefinitionKey)) {
            processDefinitionKey = resolveActiveProcessDefinitionKey();
        }
        ProcessDefinition processDefinition = bpmProcessDefinitionService.getActiveProcessDefinition(processDefinitionKey);
        if (processDefinition == null) {
            throw exception(TRANSFER_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)));
        transferApplicationMapper.updateById(HrmTransferApplicationDO.builder()
                .id(id)
                .processInstanceId(processInstanceId)
@@ -416,6 +422,60 @@
                .status(status)
                .approveTime(java.time.LocalDateTime.now())
                .build());
        // 审批通过:把员工档案里的部门/岗位同步成调岗后的目标值
        if (HrmAuditStatusEnum.APPROVE.getStatus().equals(status)) {
            syncEmployeePosition(application);
        }
    }
    /**
     * 取调岗审批分类下最新版本的活跃流程定义 Key
     */
    private String resolveActiveProcessDefinitionKey() {
        List<BpmProcessDefinitionInfoDO> definitionInfoList = bpmProcessDefinitionService
                .getProcessDefinitionInfoListByCategory(APPROVE_CATEGORY_CODE);
        if (CollUtil.isEmpty(definitionInfoList)) {
            throw exception(TRANSFER_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(TRANSFER_APPLICATION_BPM_PROCESS_DEFINITION_NOT_EXISTS));
    }
    /**
     * 调岗审批通过后,把员工档案的部门/岗位更新为目标部门/岗位
     *
     * <p>走 {@link HrmEmployeeService#updateEmployee} 而不是直接改表,
     * 这样 system_users 上的部门/岗位关联也会一并同步,员工管理列表能立刻看到新岗位。
     */
    private void syncEmployeePosition(HrmTransferApplicationDO application) {
        if (application.getTargetDeptId() == null || application.getTargetPostId() == null) {
            return;
        }
        try {
            HrmEmployeeDO employee = employeeService.getEmployeeByUserId(application.getUserId());
            if (employee == null) {
                employee = employeeService.getEmployee(application.getUserId());
            }
            if (employee == null) {
                log.warn("[syncEmployeePosition][调岗申请({}) 找不到员工档案,跳过档案同步]", application.getId());
                return;
            }
            HrmEmployeeSaveReqVO updateReqVO = BeanUtils.toBean(employee, HrmEmployeeSaveReqVO.class);
            updateReqVO.setDeptId(application.getTargetDeptId());
            // 调岗按单岗位处理,直接落目标岗位
            updateReqVO.setPostIds(Collections.singletonList(application.getTargetPostId()));
            employeeService.updateEmployee(updateReqVO);
            log.info("[syncEmployeePosition][调岗申请({}) 已将员工({}) 部门改为 {}、岗位改为 {}]",
                    application.getId(), employee.getName(), application.getTargetDeptId(), application.getTargetPostId());
        } catch (Exception ex) {
            // 档案同步失败(例如目标部门/岗位已被删除)不应该阻断审批状态落库,只记录日志
            log.error("[syncEmployeePosition][调岗申请({}) 同步员工档案失败]", application.getId(), ex);
        }
    }
    @Override