package cn.iocoder.yudao.module.hrm.service.approval;
|
|
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.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.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.dal.mysql.employee.HrmEmployeeMapper;
|
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.system.api.approval.ApprovalConfigApi;
|
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;
|
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
|
import cn.iocoder.yudao.module.system.api.dept.dto.PostRespDTO;
|
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
|
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
|
import jakarta.annotation.Resource;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.validation.annotation.Validated;
|
|
import java.util.Collections;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Objects;
|
import java.util.Set;
|
import java.util.stream.Collectors;
|
import java.util.stream.Stream;
|
|
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
|
import static cn.iocoder.yudao.module.hrm.enums.ErrorCodeConstants.*;
|
|
/**
|
* 调岗申请 Service 实现类
|
*
|
* @author 超级管理员
|
*/
|
@Service
|
@Validated
|
@Slf4j
|
public class HrmTransferApplicationServiceImpl implements HrmTransferApplicationService {
|
|
/**
|
* 调岗申请审批的业务类型编码
|
*
|
* 与 system_approval_config.biz_type 对应,审批人在「系统管理 - 审批配置」中维护。
|
*/
|
private static final String TRANSFER_APPROVE_BIZ_TYPE = "hrm_transfer_approve";
|
|
@Resource
|
private HrmTransferApplicationMapper transferApplicationMapper;
|
@Resource
|
private HrmNoRedisDAO noRedisDAO;
|
@Resource
|
private DeptApi deptApi;
|
@Resource
|
private PostApi postApi;
|
@Resource
|
private AdminUserApi adminUserApi;
|
@Resource
|
private StorageAttachmentApi storageAttachmentApi;
|
@Resource
|
private ApprovalConfigApi approvalConfigApi;
|
@Resource
|
private HrmEmployeeMapper employeeMapper;
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public Long createTransferApplication(HrmTransferApplicationSaveReqVO createReqVO) {
|
// 1. 校验目标部门与原部门不能相同
|
if (Objects.equals(createReqVO.getTargetDeptId(), createReqVO.getCurrentDeptId())) {
|
throw exception(TRANSFER_APPLICATION_SAME_DEPT);
|
}
|
// 2. 校验目标岗位与原岗位不能相同
|
if (Objects.equals(createReqVO.getTargetPostId(), createReqVO.getCurrentPostId())) {
|
throw exception(TRANSFER_APPLICATION_SAME_POST);
|
}
|
// 3. 生成申请单号
|
String no = generateUniqueNo();
|
// 4. 设置申请人信息
|
Long userId = createReqVO.getUserId();
|
if (userId == null) {
|
userId = getCurrentUserId();
|
}
|
AdminUserRespDTO user = adminUserApi.getUser(userId);
|
if (user == null) {
|
userId = getCurrentUserId();
|
}
|
// 5. 插入
|
HrmTransferApplicationDO application = BeanUtils.toBean(createReqVO, HrmTransferApplicationDO.class);
|
application.setNo(no);
|
application.setUserId(userId);
|
application.setStatus(HrmAuditStatusEnum.DRAFT.getStatus());
|
transferApplicationMapper.insert(application);
|
storageAttachmentApi.bindAttachments("file", "hrm_transfer_application",
|
application.getId(), createReqVO.getBlobIds());
|
return application.getId();
|
}
|
|
private Long getCurrentUserId() {
|
return SecurityFrameworkUtils.getLoginUserId();
|
}
|
|
private String generateUniqueNo() {
|
String no = noRedisDAO.generateTransferApplicationNo();
|
if (transferApplicationMapper.selectByNo(no) != null) {
|
return generateUniqueNo();
|
}
|
return no;
|
}
|
|
@Override
|
public void updateTransferApplication(HrmTransferApplicationSaveReqVO updateReqVO) {
|
HrmTransferApplicationDO application = validateTransferApplicationExists(updateReqVO.getId());
|
if (!Objects.equals(application.getStatus(), HrmAuditStatusEnum.DRAFT.getStatus())
|
&& !Objects.equals(application.getStatus(), HrmAuditStatusEnum.PROCESS.getStatus())) {
|
throw exception(TRANSFER_APPLICATION_UPDATE_FAIL_NOT_DRAFT);
|
}
|
if (Objects.equals(updateReqVO.getTargetDeptId(), updateReqVO.getCurrentDeptId())) {
|
throw exception(TRANSFER_APPLICATION_SAME_DEPT);
|
}
|
if (Objects.equals(updateReqVO.getTargetPostId(), updateReqVO.getCurrentPostId())) {
|
throw exception(TRANSFER_APPLICATION_SAME_POST);
|
}
|
HrmTransferApplicationDO updateObj = BeanUtils.toBean(updateReqVO, HrmTransferApplicationDO.class);
|
transferApplicationMapper.updateById(updateObj);
|
storageAttachmentApi.updateAttachments("file", "hrm_transfer_application", updateReqVO.getId(), updateReqVO.getBlobIds());
|
}
|
|
@Override
|
public void deleteTransferApplication(Long id) {
|
HrmTransferApplicationDO application = validateTransferApplicationExists(id);
|
if (!Objects.equals(application.getStatus(), HrmAuditStatusEnum.DRAFT.getStatus())) {
|
throw exception(TRANSFER_APPLICATION_UPDATE_FAIL_NOT_DRAFT);
|
}
|
storageAttachmentApi.deleteAttachmentsByRecord("hrm_transfer_application", id);
|
transferApplicationMapper.deleteById(id);
|
}
|
|
private HrmTransferApplicationDO validateTransferApplicationExists(Long id) {
|
HrmTransferApplicationDO application = transferApplicationMapper.selectById(id);
|
if (application == null) {
|
throw exception(TRANSFER_APPLICATION_NOT_EXISTS);
|
}
|
return application;
|
}
|
|
@Override
|
public HrmTransferApplicationDO getTransferApplication(Long id) {
|
return transferApplicationMapper.selectById(id);
|
}
|
|
@Override
|
public HrmTransferApplicationRespVO getTransferApplicationDetail(Long id) {
|
HrmTransferApplicationDO application = getTransferApplication(id);
|
if (application == null) {
|
return null;
|
}
|
HrmTransferApplicationRespVO respVO = BeanUtils.toBean(application, HrmTransferApplicationRespVO.class);
|
if (application.getUserId() != null) {
|
AdminUserRespDTO user = adminUserApi.getUser(application.getUserId());
|
if (user != null) {
|
respVO.setUserName(user.getNickname());
|
}
|
}
|
fillDeptNames(respVO, application);
|
fillPostNames(respVO, application);
|
respVO.setTransferTypeName(HrmTransferTypeEnum.getNameByStatus(application.getTransferType()));
|
respVO.setStatusName(HrmAuditStatusEnum.getNameByStatus(application.getStatus()));
|
respVO.setAttachmentList(storageAttachmentApi.listAttachments("hrm_transfer_application", id));
|
return respVO;
|
}
|
|
private void fillDeptNames(HrmTransferApplicationRespVO respVO, HrmTransferApplicationDO application) {
|
Set<Long> deptIds = Stream.of(application.getCurrentDeptId(), application.getTargetDeptId())
|
.filter(Objects::nonNull).collect(Collectors.toSet());
|
Map<Long, DeptRespDTO> deptMap = deptApi.getDeptMap(deptIds);
|
if (application.getCurrentDeptId() != null && deptMap.containsKey(application.getCurrentDeptId())) {
|
respVO.setCurrentDeptName(deptMap.get(application.getCurrentDeptId()).getName());
|
}
|
if (application.getTargetDeptId() != null && deptMap.containsKey(application.getTargetDeptId())) {
|
respVO.setTargetDeptName(deptMap.get(application.getTargetDeptId()).getName());
|
}
|
}
|
|
private void fillPostNames(HrmTransferApplicationRespVO respVO, HrmTransferApplicationDO application) {
|
Set<Long> postIds = Stream.of(application.getCurrentPostId(), application.getTargetPostId())
|
.filter(Objects::nonNull).collect(Collectors.toSet());
|
Map<Long, PostRespDTO> postMap = postApi.getPostMap(postIds);
|
if (application.getCurrentPostId() != null && postMap.containsKey(application.getCurrentPostId())) {
|
respVO.setCurrentPostName(postMap.get(application.getCurrentPostId()).getName());
|
}
|
if (application.getTargetPostId() != null && postMap.containsKey(application.getTargetPostId())) {
|
respVO.setTargetPostName(postMap.get(application.getTargetPostId()).getName());
|
}
|
}
|
|
@Override
|
public PageResult<HrmTransferApplicationRespVO> getTransferApplicationPage(HrmTransferApplicationPageReqVO pageReqVO) {
|
PageResult<HrmTransferApplicationDO> pageResult = transferApplicationMapper.selectPage(pageReqVO);
|
if (pageResult.getList().isEmpty()) {
|
return new PageResult<>(Collections.emptyList(), pageResult.getTotal());
|
}
|
Set<Long> userIds = pageResult.getList().stream()
|
.map(HrmTransferApplicationDO::getUserId)
|
.filter(Objects::nonNull)
|
.collect(Collectors.toSet());
|
Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(userIds);
|
Set<Long> deptIds = pageResult.getList().stream()
|
.flatMap(app -> Stream.of(app.getCurrentDeptId(), app.getTargetDeptId()))
|
.filter(Objects::nonNull)
|
.collect(Collectors.toSet());
|
Map<Long, DeptRespDTO> deptMap = deptApi.getDeptMap(deptIds);
|
Set<Long> postIds = pageResult.getList().stream()
|
.flatMap(app -> Stream.of(app.getCurrentPostId(), app.getTargetPostId()))
|
.filter(Objects::nonNull)
|
.collect(Collectors.toSet());
|
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 && userMap.containsKey(app.getUserId())) {
|
vo.setUserName(userMap.get(app.getUserId()).getNickname());
|
}
|
if (app.getCurrentDeptId() != null && deptMap.containsKey(app.getCurrentDeptId())) {
|
vo.setCurrentDeptName(deptMap.get(app.getCurrentDeptId()).getName());
|
}
|
if (app.getTargetDeptId() != null && deptMap.containsKey(app.getTargetDeptId())) {
|
vo.setTargetDeptName(deptMap.get(app.getTargetDeptId()).getName());
|
}
|
if (app.getCurrentPostId() != null && postMap.containsKey(app.getCurrentPostId())) {
|
vo.setCurrentPostName(postMap.get(app.getCurrentPostId()).getName());
|
}
|
if (app.getTargetPostId() != null && postMap.containsKey(app.getTargetPostId())) {
|
vo.setTargetPostName(postMap.get(app.getTargetPostId()).getName());
|
}
|
vo.setTransferTypeName(HrmTransferTypeEnum.getNameByStatus(app.getTransferType()));
|
vo.setStatusName(HrmAuditStatusEnum.getNameByStatus(app.getStatus()));
|
return vo;
|
}).collect(Collectors.toList());
|
return new PageResult<>(list, pageResult.getTotal());
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void submitTransferApplication(Long id) {
|
// 1. 校验存在 + 可提交状态(草稿 / 审核不通过)
|
HrmTransferApplicationDO application = validateTransferApplicationExists(id);
|
if (!Objects.equals(application.getStatus(), HrmAuditStatusEnum.DRAFT.getStatus())
|
&& !Objects.equals(application.getStatus(), HrmAuditStatusEnum.REJECT.getStatus())) {
|
throw exception(TRANSFER_APPLICATION_SUBMIT_FAIL_NOT_EDITABLE);
|
}
|
|
// 2. 校验审批配置已启用且配置了有效审批人(未配置时抛出带明确提示的业务异常)
|
approvalConfigApi.validateApprovalEnabledAndGetApprovers(TRANSFER_APPROVE_BIZ_TYPE);
|
|
// 3. 状态置为审批中,并清空上一轮审核结果,避免残留「审核不通过」的原因
|
transferApplicationMapper.submitAndResetAuditInfo(id, HrmAuditStatusEnum.PROCESS.getStatus());
|
}
|
|
@Override
|
public Set<Long> getTransferApplicationApproverUserIds() {
|
return approvalConfigApi.getApproverUserIds(TRANSFER_APPROVE_BIZ_TYPE);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void auditTransferApplication(Long id, Boolean pass, String reviewRemark) {
|
// 1. 校验存在
|
HrmTransferApplicationDO application = validateTransferApplicationExists(id);
|
// 2. 校验处于审批中状态
|
if (!Objects.equals(application.getStatus(), HrmAuditStatusEnum.PROCESS.getStatus())) {
|
throw exception(TRANSFER_APPLICATION_AUDIT_FAIL_NOT_PROCESS);
|
}
|
// 3. 校验当前登录用户是该业务类型的审批人(或签:任一人均可审核)
|
Long userId = getLoginUserId();
|
approvalConfigApi.validateApprover(TRANSFER_APPROVE_BIZ_TYPE, userId);
|
// 4. 审核不通过:必须填写原因,状态置为审核不通过,退回提交人修改后可重新提交
|
if (!Boolean.TRUE.equals(pass)) {
|
if (StrUtil.isBlank(reviewRemark)) {
|
throw exception(TRANSFER_APPLICATION_AUDIT_REJECT_REASON_REQUIRED);
|
}
|
transferApplicationMapper.auditTransferApplication(id, HrmAuditStatusEnum.REJECT.getStatus(),
|
userId, getUserNickname(userId), reviewRemark);
|
return;
|
}
|
// 5. 审核通过:记录审核人与审核意见,状态置为审核通过
|
transferApplicationMapper.auditTransferApplication(id, HrmAuditStatusEnum.APPROVE.getStatus(),
|
userId, getUserNickname(userId), reviewRemark);
|
|
// 6. 审核通过后置业务逻辑:回写员工主档(部门 = 目标部门,岗位 = 目标岗位)
|
writeBackEmployee(application);
|
}
|
|
/**
|
* 审核通过后回写员工主档
|
*
|
* @param application 调岗申请
|
*/
|
private void writeBackEmployee(HrmTransferApplicationDO application) {
|
HrmEmployeeDO employee = employeeMapper.selectByUserId(application.getUserId());
|
if (employee == null) {
|
log.warn("[writeBackEmployee] 调岗申请({}) 用户({}) 未找到员工档案,跳过回写", application.getId(), application.getUserId());
|
return;
|
}
|
employeeMapper.updateById(HrmEmployeeDO.builder()
|
.id(employee.getId())
|
.deptId(application.getTargetDeptId())
|
.postId(application.getTargetPostId())
|
.build());
|
}
|
|
/**
|
* 获取用户昵称
|
*
|
* @param userId 用户编号
|
* @return 昵称,用户不存在时返回 null
|
*/
|
private String getUserNickname(Long userId) {
|
if (userId == null) {
|
return null;
|
}
|
AdminUserRespDTO user = adminUserApi.getUser(userId);
|
return user == null ? null : user.getNickname();
|
}
|
|
}
|