package cn.iocoder.yudao.module.hrm.service.approval; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi; import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO; import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmProcessDefinitionInfoDO; import cn.iocoder.yudao.module.bpm.service.definition.BpmCategoryService; import cn.iocoder.yudao.module.bpm.service.definition.BpmProcessDefinitionService; import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmRegularizeApplicationPageReqVO; import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmRegularizeApplicationRespVO; import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmRegularizeApplicationSaveReqVO; import cn.iocoder.yudao.module.hrm.dal.dataobject.approval.HrmRegularizeApplicationDO; import cn.iocoder.yudao.module.hrm.dal.mysql.approval.HrmRegularizeApplicationMapper; import cn.iocoder.yudao.module.hrm.dal.redis.HrmNoRedisDAO; import cn.iocoder.yudao.module.hrm.enums.HrmAuditStatusEnum; import cn.iocoder.yudao.module.hrm.enums.HrmEmploymentTypeEnum; 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.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.flowable.engine.repository.ProcessDefinition; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; 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 static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; import static cn.iocoder.yudao.module.hrm.enums.ErrorCodeConstants.*; /** * 转正申请 Service 实现类 * * @author 超级管理员 */ @Service @Validated @Slf4j public class HrmRegularizeApplicationServiceImpl implements HrmRegularizeApplicationService { @Resource private HrmRegularizeApplicationMapper regularizeApplicationMapper; @Resource private HrmNoRedisDAO noRedisDAO; @Resource private DeptApi deptApi; @Resource private PostApi postApi; @Resource private AdminUserApi adminUserApi; @Resource private BpmProcessInstanceApi bpmProcessInstanceApi; @Resource private BpmCategoryService bpmCategoryService; @Resource private BpmProcessDefinitionService bpmProcessDefinitionService; @Override @Transactional(rollbackFor = Exception.class) public Long createRegularizeApplication(HrmRegularizeApplicationSaveReqVO createReqVO) { Long userId = createReqVO.getUserId(); if (userId == null) { userId = getCurrentUserId(); } HrmRegularizeApplicationDO existing = regularizeApplicationMapper.selectByUserIdAndStatusNot(userId, HrmAuditStatusEnum.CANCEL.getStatus()); if (existing != null) { throw exception(REGULARIZE_APPLICATION_EXISTS); } String no = generateUniqueNo(); AdminUserRespDTO user = adminUserApi.getUser(userId); if (user == null) { userId = getCurrentUserId(); user = adminUserApi.getUser(userId); } HrmRegularizeApplicationDO application = BeanUtils.toBean(createReqVO, HrmRegularizeApplicationDO.class); application.setNo(no); application.setUserId(userId); application.setDeptId(createReqVO.getDeptId() != null ? createReqVO.getDeptId() : (user != null ? user.getDeptId() : null)); application.setStatus(HrmAuditStatusEnum.DRAFT.getStatus()); regularizeApplicationMapper.insert(application); return application.getId(); } private Long getCurrentUserId() { return SecurityFrameworkUtils.getLoginUserId(); } private String generateUniqueNo() { String no = noRedisDAO.generateRegularizeApplicationNo(); if (regularizeApplicationMapper.selectByNo(no) != null) { return generateUniqueNo(); } return no; } @Override public void updateRegularizeApplication(HrmRegularizeApplicationSaveReqVO updateReqVO) { HrmRegularizeApplicationDO application = validateRegularizeApplicationExists(updateReqVO.getId()); if (!Objects.equals(application.getStatus(), HrmAuditStatusEnum.DRAFT.getStatus())) { throw exception(REGULARIZE_APPLICATION_UPDATE_FAIL_NOT_DRAFT); } HrmRegularizeApplicationDO updateObj = BeanUtils.toBean(updateReqVO, HrmRegularizeApplicationDO.class); regularizeApplicationMapper.updateById(updateObj); } @Override public void deleteRegularizeApplication(Long id) { HrmRegularizeApplicationDO application = validateRegularizeApplicationExists(id); if (!Objects.equals(application.getStatus(), HrmAuditStatusEnum.DRAFT.getStatus())) { throw exception(REGULARIZE_APPLICATION_UPDATE_FAIL_NOT_DRAFT); } regularizeApplicationMapper.deleteById(id); } private HrmRegularizeApplicationDO validateRegularizeApplicationExists(Long id) { HrmRegularizeApplicationDO application = regularizeApplicationMapper.selectById(id); if (application == null) { throw exception(REGULARIZE_APPLICATION_NOT_EXISTS); } return application; } @Override public HrmRegularizeApplicationDO getRegularizeApplication(Long id) { return regularizeApplicationMapper.selectById(id); } @Override public HrmRegularizeApplicationRespVO getRegularizeApplicationDetail(Long id) { HrmRegularizeApplicationDO application = getRegularizeApplication(id); if (application == null) { return null; } HrmRegularizeApplicationRespVO respVO = BeanUtils.toBean(application, HrmRegularizeApplicationRespVO.class); fillUserNames(respVO, application); if (application.getDeptId() != null) { DeptRespDTO dept = deptApi.getDept(application.getDeptId()); if (dept != null) { respVO.setDeptName(dept.getName()); } } if (application.getPostId() != null) { Map postMap = postApi.getPostMap(Collections.singleton(application.getPostId())); PostRespDTO post = postMap.get(application.getPostId()); if (post != null) { respVO.setPostName(post.getName()); } } respVO.setEmploymentTypeName(HrmEmploymentTypeEnum.getNameByType(application.getEmploymentType())); respVO.setStatusName(HrmAuditStatusEnum.getNameByStatus(application.getStatus())); return respVO; } private void fillUserNames(HrmRegularizeApplicationRespVO respVO, HrmRegularizeApplicationDO application) { if (application.getUserId() != null) { AdminUserRespDTO user = adminUserApi.getUser(application.getUserId()); if (user != null) { respVO.setUserName(user.getNickname()); } } } @Override public PageResult getRegularizeApplicationPage(HrmRegularizeApplicationPageReqVO pageReqVO) { PageResult pageResult = regularizeApplicationMapper.selectPage(pageReqVO); if (pageResult.getList().isEmpty()) { return new PageResult<>(Collections.emptyList(), pageResult.getTotal()); } Set userIds = pageResult.getList().stream() .map(HrmRegularizeApplicationDO::getUserId) .filter(Objects::nonNull) .collect(Collectors.toSet()); Map userMap = adminUserApi.getUserMap(userIds); Set deptIds = pageResult.getList().stream() .map(HrmRegularizeApplicationDO::getDeptId) .filter(Objects::nonNull) .collect(Collectors.toSet()); Map deptMap = deptApi.getDeptMap(deptIds); Set postIds = pageResult.getList().stream() .map(HrmRegularizeApplicationDO::getPostId) .filter(Objects::nonNull) .collect(Collectors.toSet()); Map postMap = postApi.getPostMap(postIds); List list = pageResult.getList().stream().map(app -> { HrmRegularizeApplicationRespVO vo = BeanUtils.toBean(app, HrmRegularizeApplicationRespVO.class); if (app.getUserId() != null && userMap.containsKey(app.getUserId())) { vo.setUserName(userMap.get(app.getUserId()).getNickname()); } if (app.getDeptId() != null && deptMap.containsKey(app.getDeptId())) { vo.setDeptName(deptMap.get(app.getDeptId()).getName()); } if (app.getPostId() != null && postMap.containsKey(app.getPostId())) { vo.setPostName(postMap.get(app.getPostId()).getName()); } vo.setEmploymentTypeName(HrmEmploymentTypeEnum.getNameByType(app.getEmploymentType())); vo.setStatusName(HrmAuditStatusEnum.getNameByStatus(app.getStatus())); return vo; }).collect(Collectors.toList()); return new PageResult<>(list, pageResult.getTotal()); } @Override @Transactional(rollbackFor = Exception.class) public void submitRegularizeApplication(Long id, String processDefinitionKey, Long userId) { HrmRegularizeApplicationDO application = validateRegularizeApplicationExists(id); if (ObjUtil.notEqual(application.getStatus(), HrmAuditStatusEnum.DRAFT.getStatus())) { throw exception(REGULARIZE_APPLICATION_SUBMIT_FAIL_NOT_DRAFT); } validateApproveCategoryAndProcessDefinition(); ProcessDefinition processDefinition = bpmProcessDefinitionService.getActiveProcessDefinition(processDefinitionKey); if (processDefinition == null) { throw exception(REGULARIZE_APPLICATION_BPM_PROCESS_DEFINITION_NOT_EXISTS); } String processInstanceId = bpmProcessInstanceApi.createProcessInstance(userId, new BpmProcessInstanceCreateReqDTO() .setProcessDefinitionKey(processDefinitionKey).setBusinessKey(String.valueOf(id))); regularizeApplicationMapper.updateById(HrmRegularizeApplicationDO.builder() .id(id) .processInstanceId(processInstanceId) .status(HrmAuditStatusEnum.PROCESS.getStatus()) .build()); } private void validateApproveCategoryAndProcessDefinition() { List codes = Collections.singletonList(APPROVE_CATEGORY_CODE); Map categoryMap = bpmCategoryService.getCategoryMap(codes); if (!categoryMap.containsKey(APPROVE_CATEGORY_CODE)) { throw exception(REGULARIZE_APPLICATION_BPM_PROCESS_DEFINITION_NOT_EXISTS); } List definitionInfoList = bpmProcessDefinitionService .getProcessDefinitionInfoListByCategory(APPROVE_CATEGORY_CODE); if (CollUtil.isEmpty(definitionInfoList)) { throw exception(REGULARIZE_APPLICATION_BPM_PROCESS_DEFINITION_NOT_EXISTS); } Set processDefinitionIds = convertSet(definitionInfoList, BpmProcessDefinitionInfoDO::getProcessDefinitionId); List processDefinitions = bpmProcessDefinitionService.getProcessDefinitionList(processDefinitionIds); boolean hasActiveDefinition = processDefinitions.stream().anyMatch(pd -> !pd.isSuspended()); if (!hasActiveDefinition) { throw exception(REGULARIZE_APPLICATION_BPM_PROCESS_DEFINITION_NOT_EXISTS); } } @Override public List> getApproveProcessDefinitionList() { validateApproveCategoryAndProcessDefinition(); List definitionInfoList = bpmProcessDefinitionService .getProcessDefinitionInfoListByCategory(APPROVE_CATEGORY_CODE); Set processDefinitionIds = convertSet(definitionInfoList, BpmProcessDefinitionInfoDO::getProcessDefinitionId); List processDefinitions = bpmProcessDefinitionService.getProcessDefinitionList(processDefinitionIds); Map latestVersionMap = new HashMap<>(); for (ProcessDefinition pd : processDefinitions) { if (pd.isSuspended()) continue; ProcessDefinition existing = latestVersionMap.get(pd.getKey()); if (existing == null || pd.getVersion() > existing.getVersion()) { latestVersionMap.put(pd.getKey(), pd); } } List> result = new ArrayList<>(); for (ProcessDefinition pd : latestVersionMap.values()) { Map item = new HashMap<>(); item.put("id", pd.getId()); item.put("key", pd.getKey()); item.put("name", pd.getName()); item.put("version", pd.getVersion()); result.add(item); } return result; } @Override public void updateRegularizeApplicationAuditStatus(Long id, Integer status) { HrmRegularizeApplicationDO application = validateRegularizeApplicationExists(id); if (!Objects.equals(application.getStatus(), HrmAuditStatusEnum.PROCESS.getStatus())) { log.error("[updateRegularizeApplicationAuditStatus][application({}) 不处于审批中,无法更新审批结果({})]", id, status); throw exception(REGULARIZE_APPLICATION_UPDATE_AUDIT_STATUS_FAIL_NOT_PROCESS); } regularizeApplicationMapper.updateById(HrmRegularizeApplicationDO.builder() .id(id) .status(status) .approveTime(java.time.LocalDateTime.now()) .build()); } @Override public HrmRegularizeApplicationDO getRegularizeApplicationByProcessInstanceId(String processInstanceId) { return regularizeApplicationMapper.selectByProcessInstanceId(processInstanceId); } }