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;
@@ -282,12 +284,15 @@
                .map(HrmTransferApplicationDO::getUserId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        // userId 存的是员工ID,按员工表查询姓名
        // user_id 正常存 system_users.id;历史数据也有存「员工主键」的,两种都兼容
        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(userIds);
        Map<Long, String> employeeNameMap = new HashMap<>();
        for (Long userId : userIds) {
            HrmEmployeeDO employee = employeeService.getEmployee(userId);
            if (employee != null) {
                employeeNameMap.put(userId, employee.getName());
            if (!userMap.containsKey(userId)) {
                HrmEmployeeDO employee = employeeService.getEmployee(userId);
                if (employee != null) {
                    employeeNameMap.put(userId, employee.getName());
                }
            }
        }
        Set<Long> deptIds = pageResult.getList().stream()
@@ -302,8 +307,12 @@
        Map<Long, PostRespDTO> postMap = postApi.getPostMap(postIds);
        List<HrmTransferApplicationRespVO> list = pageResult.getList().stream().map(app -> {
            HrmTransferApplicationRespVO vo = BeanUtils.toBean(app, HrmTransferApplicationRespVO.class);
            if (app.getUserId() != null && employeeNameMap.containsKey(app.getUserId())) {
                vo.setUserName(employeeNameMap.get(app.getUserId()));
            if (app.getUserId() != null) {
                if (userMap.containsKey(app.getUserId())) {
                    vo.setUserName(userMap.get(app.getUserId()).getNickname());
                } else if (employeeNameMap.containsKey(app.getUserId())) {
                    vo.setUserName(employeeNameMap.get(app.getUserId()));
                }
            }
            if (app.getCurrentDeptId() != null && deptMap.containsKey(app.getCurrentDeptId())) {
                vo.setCurrentDeptName(deptMap.get(app.getCurrentDeptId()).getName());
@@ -332,17 +341,40 @@
            throw exception(TRANSFER_APPLICATION_SUBMIT_FAIL_NOT_DRAFT);
        }
        validateApproveCategoryAndProcessDefinition();
        // 前端不传流程定义 Key 时,自动取该审批分类下最新版本的活跃流程,
        // 避免直接把 MissingServletRequestParameterException 抛给用户
        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)
                .status(HrmAuditStatusEnum.PROCESS.getStatus())
                .build());
    }
    /**
     * 取调岗审批分类下最新版本的活跃流程定义 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));
    }
    private void validateApproveCategoryAndProcessDefinition() {
@@ -403,6 +435,42 @@
                .status(status)
                .approveTime(java.time.LocalDateTime.now())
                .build());
        // 审批通过:把员工档案里的部门/岗位同步成调岗后的目标值
        if (HrmAuditStatusEnum.APPROVE.getStatus().equals(status)) {
            syncEmployeePosition(application);
        }
    }
    /**
     * 调岗审批通过后,把员工档案的部门/岗位更新为目标部门/岗位
     *
     * <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