tuqin
2 天以前 13801ec910e891cbe1fe89e7e3285ded134aad15
yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/approval/HrmTransferApplicationServiceImpl.java
@@ -2,6 +2,8 @@
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
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;
@@ -15,10 +17,10 @@
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.service.employee.HrmEmployeeService;
import cn.iocoder.yudao.module.hrm.dal.redis.HrmNoRedisDAO;
import cn.iocoder.yudao.module.hrm.enums.HrmAuditStatusEnum;
import cn.iocoder.yudao.module.hrm.enums.HrmTransferTypeEnum;
import cn.iocoder.yudao.module.hrm.service.employee.HrmEmployeeService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.PostApi;
import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi;
@@ -81,25 +83,52 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createTransferApplication(HrmTransferApplicationSaveReqVO createReqVO) {
        // 1. 校验目标部门与原部门不能相同
        // 1. 解析申请人 & 补全原部门/原岗位
        // 前端表单只让用户选择「被调岗员工」,不直接提交 currentDeptId/currentPostId,
        // 而这两个字段在库里是 NOT NULL,缺省会直接抛 SQL 异常(Field 'current_dept_id' doesn't have a default value)
        Long userId = createReqVO.getUserId();
        Long employeeId = createReqVO.getEmployeeId();
        HrmEmployeeDO employee = null;
        if (employeeId != null) {
            employee = employeeService.getEmployee(employeeId);
        }
        if (employee == null && userId != null) {
            employee = employeeService.getEmployeeByUserId(userId);
        }
        if (userId == null && employee != null) {
            userId = employee.getUserId();
        }
        if (userId == null) {
            userId = getCurrentUserId();
            if (employee == null) {
                employee = employeeService.getEmployeeByUserId(userId);
            }
        }
        if (adminUserApi.getUser(userId) == null) {
            userId = getCurrentUserId();
        }
        if (createReqVO.getCurrentDeptId() == null && employee != null) {
            createReqVO.setCurrentDeptId(employee.getDeptId());
        }
        if (createReqVO.getCurrentPostId() == null && employee != null) {
            createReqVO.setCurrentPostId(parseFirstPostId(employee.getPostIds()));
        }
        if (createReqVO.getCurrentDeptId() == null) {
            throw exception(TRANSFER_APPLICATION_CURRENT_DEPT_REQUIRED);
        }
        if (createReqVO.getCurrentPostId() == null) {
            throw exception(TRANSFER_APPLICATION_CURRENT_POST_REQUIRED);
        }
        // 2. 校验目标部门与原部门不能相同(放在补全之后,否则补全出来的原部门绕过了校验)
        if (Objects.equals(createReqVO.getTargetDeptId(), createReqVO.getCurrentDeptId())) {
            throw exception(TRANSFER_APPLICATION_SAME_DEPT);
        }
        // 2. 校验目标岗位与原岗位不能相同
        // 3. 校验目标岗位与原岗位不能相同
        if (Objects.equals(createReqVO.getTargetPostId(), createReqVO.getCurrentPostId())) {
            throw exception(TRANSFER_APPLICATION_SAME_POST);
        }
        // 3. 生成申请单号
        // 4. 生成申请单号
        String no = generateUniqueNo();
        // 4. 设置调岗人(员工ID):为空时取当前登录用户对应的员工
        Long userId = createReqVO.getUserId();
        if (userId == null) {
            HrmEmployeeDO currentEmployee = employeeService.getEmployeeByUserId(getCurrentUserId());
            userId = currentEmployee != null ? currentEmployee.getId() : null;
        }
        if (userId != null && employeeService.getEmployee(userId) == null) {
            throw exception(EMPLOYEE_NOT_EXISTS);
        }
        // 5. 插入
        HrmTransferApplicationDO application = BeanUtils.toBean(createReqVO, HrmTransferApplicationDO.class);
        application.setNo(no);
@@ -109,6 +138,27 @@
        storageAttachmentApi.bindAttachments("file", "hrm_transfer_application",
                application.getId(), createReqVO.getBlobIds());
        return application.getId();
    }
    /**
     * 取员工岗位列表中的第一个作为「原岗位」
     *
     * <p>hrm_employee.post_ids 以 JSON 数组字符串存储,例如 {@code [20,21]}
     *
     * @param postIds 岗位编号 JSON 字符串
     * @return 第一个岗位编号;解析不出来时返回 {@code null}
     */
    private Long parseFirstPostId(String postIds) {
        if (StrUtil.isBlank(postIds)) {
            return null;
        }
        try {
            List<Long> ids = JSONUtil.toList(postIds, Long.class);
            return CollUtil.isEmpty(ids) ? null : ids.get(0);
        } catch (Exception ex) {
            log.warn("[parseFirstPostId][postIds({}) 解析失败]", postIds, ex);
            return null;
        }
    }
    private Long getCurrentUserId() {
@@ -172,9 +222,12 @@
        }
        HrmTransferApplicationRespVO respVO = BeanUtils.toBean(application, HrmTransferApplicationRespVO.class);
        if (application.getUserId() != null) {
            HrmEmployeeDO employee = employeeService.getEmployee(application.getUserId());
            // 注意:hrm_transfer_application.user_id 存的是 system_users.id,
            // 必须用 getEmployeeByUserId 查档案;用 getEmployee(userId) 会按员工主键查,属于 id 混用
            HrmEmployeeDO employee = employeeService.getEmployeeByUserId(application.getUserId());
            if (employee != null) {
                respVO.setUserName(employee.getName());
                respVO.setEmployeeId(employee.getId());
            }
        }
        fillDeptNames(respVO, application);