| | |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; |
| | | 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.performanceappraise.vo.*; |
| | | import cn.iocoder.yudao.module.hrm.dal.dataobject.employee.HrmEmployeeDO; |
| | | import cn.iocoder.yudao.module.hrm.dal.dataobject.performanceappraise.*; |
| | |
| | | import cn.iocoder.yudao.module.hrm.dal.mysql.performanceappraise.*; |
| | | import cn.iocoder.yudao.module.hrm.enums.HrmAuditStatusEnum; |
| | | import cn.iocoder.yudao.module.hrm.enums.ErrorCodeConstants; |
| | | import cn.iocoder.yudao.module.system.api.approval.ApprovalConfigApi; |
| | | 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.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.time.LocalDateTime; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; |
| | | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
| | | import static cn.iocoder.yudao.module.hrm.util.HrmAuditStatusUtils.convertBpmResultToAuditStatus; |
| | | |
| | | /** |
| | | * HRM 绩效考核方案 Service 实现类 |
| | |
| | | private static final Integer EMPLOYEE_STATUS_ACTIVE = 1; |
| | | |
| | | /** |
| | | * BPM 绩效考核审批分类编码 |
| | | * 绩效考核方案审批的业务类型编码 |
| | | * |
| | | * 与 system_approval_config.biz_type 对应,审批人在「系统管理 - 审批配置」中维护。 |
| | | */ |
| | | private static final String APPRAISE_APPROVE_CATEGORY_CODE = "performance_appraise_approve"; |
| | | private static final String APPRAISE_APPROVE_BIZ_TYPE = "performance_appraise_approve"; |
| | | |
| | | @Resource |
| | | private HrmPerformanceAppraiseMapper appraiseMapper; |
| | |
| | | @Resource |
| | | private HrmEmployeeMapper employeeMapper; |
| | | @Resource |
| | | private BpmProcessInstanceApi bpmProcessInstanceApi; |
| | | private ApprovalConfigApi approvalConfigApi; |
| | | @Resource |
| | | private BpmCategoryService bpmCategoryService; |
| | | @Resource |
| | | private BpmProcessDefinitionService bpmProcessDefinitionService; |
| | | private AdminUserApi adminUserApi; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | |
| | | HrmPerformanceAppraiseDO oldAppraise = validateAppraiseCanModify(updateReqVO.getId()); |
| | | HrmPerformanceAppraiseDO updateObj = BeanUtils.toBean(updateReqVO, HrmPerformanceAppraiseDO.class, req -> req |
| | | .setStatus(oldAppraise.getStatus()) |
| | | .setAuditStatus(oldAppraise.getAuditStatus()) |
| | | .setProcessInstanceId(oldAppraise.getProcessInstanceId())); |
| | | .setAuditStatus(oldAppraise.getAuditStatus())); |
| | | updateObj.setScopeTeamIds(CollUtil.isEmpty(updateReqVO.getScopeTeamIds()) |
| | | ? null : JSONUtil.toJsonStr(updateReqVO.getScopeTeamIds())); |
| | | appraiseMapper.updateById(updateObj); |
| | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void submitAppraise(Long id, String processDefinitionKey, Long userId) { |
| | | // 1. 校验方案是否在草稿状态 |
| | | public void submitAppraise(Long id) { |
| | | // 1. 校验方案存在 + 可提交状态(草稿 / 审核不通过) |
| | | HrmPerformanceAppraiseDO appraise = appraiseMapper.selectById(id); |
| | | if (appraise == null) { |
| | | throw exception(ErrorCodeConstants.PERFORMANCE_APPRAISE_NOT_EXISTS); |
| | | } |
| | | if (ObjUtil.notEqual(appraise.getAuditStatus(), HrmAuditStatusEnum.DRAFT.getStatus())) { |
| | | throw exception(ErrorCodeConstants.PERFORMANCE_APPRAISE_SUBMIT_FAIL_NOT_DRAFT); |
| | | if (ObjUtil.notEqual(appraise.getAuditStatus(), HrmAuditStatusEnum.DRAFT.getStatus()) |
| | | && ObjUtil.notEqual(appraise.getAuditStatus(), HrmAuditStatusEnum.REJECT.getStatus())) { |
| | | throw exception(ErrorCodeConstants.PERFORMANCE_APPRAISE_SUBMIT_FAIL_NOT_EDITABLE); |
| | | } |
| | | if (CollUtil.isEmpty(itemMapper.selectListByAppraiseId(id))) { |
| | | throw exception(ErrorCodeConstants.PERFORMANCE_APPRAISE_ITEM_EMPTY); |
| | | } |
| | | |
| | | // 2. 校验审批分类和流程定义是否存在,且传入的流程定义处于激活状态 |
| | | validateAppraiseApproveCategoryAndProcessDefinition(); |
| | | ProcessDefinition processDefinition = bpmProcessDefinitionService.getActiveProcessDefinition(processDefinitionKey); |
| | | if (processDefinition == null) { |
| | | throw exception(ErrorCodeConstants.PERFORMANCE_APPRAISE_BPM_PROCESS_DEFINITION_NOT_EXISTS); |
| | | } |
| | | // 2. 校验审批配置已启用且配置了有效审批人(未配置时抛出带明确提示的业务异常) |
| | | approvalConfigApi.validateApprovalEnabledAndGetApprovers(APPRAISE_APPROVE_BIZ_TYPE); |
| | | |
| | | // 3. 创建审批流程实例 |
| | | String processInstanceId = bpmProcessInstanceApi.createProcessInstance(userId, |
| | | new BpmProcessInstanceCreateReqDTO().setProcessDefinitionKey(processDefinitionKey) |
| | | .setBusinessKey(String.valueOf(id))); |
| | | // 3. 审批状态置为审批中,并清空上一轮审核结果,避免残留「审核不通过」的原因 |
| | | appraiseMapper.submitAndResetAuditInfo(id, HrmAuditStatusEnum.PROCESS.getStatus()); |
| | | } |
| | | |
| | | // 4. 更新方案流程编号与审批状态 |
| | | appraiseMapper.updateById(new HrmPerformanceAppraiseDO().setId(id).setProcessInstanceId(processInstanceId) |
| | | .setAuditStatus(HrmAuditStatusEnum.PROCESS.getStatus())); |
| | | @Override |
| | | public Set<Long> getAppraiseApproverUserIds() { |
| | | return approvalConfigApi.getApproverUserIds(APPRAISE_APPROVE_BIZ_TYPE); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void auditAppraise(HrmPerformanceAppraiseAuditReqVO auditReqVO) { |
| | | HrmPerformanceAppraiseDO appraise = validateAppraiseExists(auditReqVO.getId()); |
| | | public void auditAppraise(Long id, Boolean pass, String reviewRemark) { |
| | | // 1. 校验存在 |
| | | HrmPerformanceAppraiseDO appraise = validateAppraiseExists(id); |
| | | // 2. 校验处于审批中状态 |
| | | if (ObjUtil.notEqual(appraise.getAuditStatus(), HrmAuditStatusEnum.PROCESS.getStatus())) { |
| | | throw exception(ErrorCodeConstants.PERFORMANCE_APPRAISE_UPDATE_AUDIT_STATUS_FAIL_NOT_PROCESS); |
| | | throw exception(ErrorCodeConstants.PERFORMANCE_APPRAISE_AUDIT_FAIL_NOT_PROCESS); |
| | | } |
| | | Integer auditStatus = auditReqVO.getAuditStatus(); |
| | | if (!Objects.equals(auditStatus, HrmAuditStatusEnum.APPROVE.getStatus()) |
| | | && !Objects.equals(auditStatus, HrmAuditStatusEnum.REJECT.getStatus())) { |
| | | throw exception(ErrorCodeConstants.PERFORMANCE_APPRAISE_UPDATE_AUDIT_STATUS_FAIL_NOT_PROCESS); |
| | | // 3. 校验当前登录用户是该业务类型的审批人(或签:任一人均可审核) |
| | | Long userId = getLoginUserId(); |
| | | approvalConfigApi.validateApprover(APPRAISE_APPROVE_BIZ_TYPE, userId); |
| | | // 4. 审核不通过:必须填写原因,审批状态置为审核不通过,退回提交人修改后可重新提交 |
| | | if (!Boolean.TRUE.equals(pass)) { |
| | | if (StrUtil.isBlank(reviewRemark)) { |
| | | throw exception(ErrorCodeConstants.PERFORMANCE_APPRAISE_AUDIT_REJECT_REASON_REQUIRED); |
| | | } |
| | | // 方案状态保持不变 |
| | | appraiseMapper.auditAppraise(id, HrmAuditStatusEnum.REJECT.getStatus(), appraise.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark); |
| | | return; |
| | | } |
| | | HrmPerformanceAppraiseDO update = new HrmPerformanceAppraiseDO() |
| | | .setId(appraise.getId()) |
| | | .setAuditStatus(auditStatus) |
| | | .setAuditUserId(getLoginUserId()) |
| | | .setAuditTime(LocalDateTime.now()); |
| | | if (auditReqVO.getRemark() != null) { |
| | | update.setAuditRemark(auditReqVO.getRemark()); |
| | | } |
| | | // 审批通过 -> 方案发布 |
| | | if (Objects.equals(auditStatus, HrmAuditStatusEnum.APPROVE.getStatus())) { |
| | | update.setStatus(STATUS_PUBLISHED); |
| | | } |
| | | appraiseMapper.updateById(update); |
| | | // 5. 审核通过:记录审核人与审核意见,审批状态置为审核通过 |
| | | // 6. 审核通过后置业务逻辑:方案状态流转为「已发布」 |
| | | appraiseMapper.auditAppraise(id, HrmAuditStatusEnum.APPROVE.getStatus(), STATUS_PUBLISHED, |
| | | userId, getUserNickname(userId), reviewRemark); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateAppraiseAuditStatus(Long id, Integer bpmResult) { |
| | | // 1.1 校验方案是否存在 |
| | | HrmPerformanceAppraiseDO appraise = appraiseMapper.selectById(id); |
| | | if (appraise == null) { |
| | | throw exception(ErrorCodeConstants.PERFORMANCE_APPRAISE_NOT_EXISTS); |
| | | /** |
| | | * 获取用户昵称 |
| | | * |
| | | * @param userId 用户编号 |
| | | * @return 昵称,用户不存在时返回 null |
| | | */ |
| | | private String getUserNickname(Long userId) { |
| | | if (userId == null) { |
| | | return null; |
| | | } |
| | | // 1.2 只有审批中,可以更新审批结果 |
| | | if (ObjUtil.notEqual(appraise.getAuditStatus(), HrmAuditStatusEnum.PROCESS.getStatus())) { |
| | | log.error("[updateAppraiseAuditStatus][appraise({}) 不处于审批中,无法更新审批结果({})]", appraise.getId(), bpmResult); |
| | | throw exception(ErrorCodeConstants.PERFORMANCE_APPRAISE_UPDATE_AUDIT_STATUS_FAIL_NOT_PROCESS); |
| | | } |
| | | |
| | | // 2. 更新方案审批结果 |
| | | Integer auditStatus = convertBpmResultToAuditStatus(bpmResult); |
| | | HrmPerformanceAppraiseDO update = new HrmPerformanceAppraiseDO().setId(id).setAuditStatus(auditStatus); |
| | | if (Objects.equals(auditStatus, HrmAuditStatusEnum.APPROVE.getStatus())) { |
| | | update.setStatus(STATUS_PUBLISHED); |
| | | } |
| | | appraiseMapper.updateById(update); |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> getAppraiseApproveProcessDefinitionList() { |
| | | // 1. 校验审批分类和流程定义是否存在 |
| | | validateAppraiseApproveCategoryAndProcessDefinition(); |
| | | |
| | | // 2. 获取分类下的流程定义信息 |
| | | List<BpmProcessDefinitionInfoDO> definitionInfoList = bpmProcessDefinitionService |
| | | .getProcessDefinitionInfoListByCategory(APPRAISE_APPROVE_CATEGORY_CODE); |
| | | |
| | | // 3. 获取流程定义详情 |
| | | Set<String> processDefinitionIds = convertSet(definitionInfoList, BpmProcessDefinitionInfoDO::getProcessDefinitionId); |
| | | List<ProcessDefinition> processDefinitions = bpmProcessDefinitionService.getProcessDefinitionList(processDefinitionIds); |
| | | |
| | | // 4. 过滤:只保留激活状态,且相同 key 只保留最新版本 |
| | | Map<String, ProcessDefinition> 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); |
| | | } |
| | | } |
| | | |
| | | // 5. 返回流程定义列表 |
| | | List<Map<String, Object>> result = new ArrayList<>(); |
| | | for (ProcessDefinition pd : latestVersionMap.values()) { |
| | | Map<String, Object> 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; |
| | | AdminUserRespDTO user = adminUserApi.getUser(userId); |
| | | return user == null ? null : user.getNickname(); |
| | | } |
| | | |
| | | @Override |
| | |
| | | throw exception(ErrorCodeConstants.PERFORMANCE_APPRAISE_UPDATE_FAIL_NOT_DRAFT); |
| | | } |
| | | return appraise; |
| | | } |
| | | |
| | | /** |
| | | * 校验 BPM 绩效考核审批分类和流程定义是否存在 |
| | | */ |
| | | private void validateAppraiseApproveCategoryAndProcessDefinition() { |
| | | // 1. 校验分类是否存在 |
| | | Map<String, ?> categoryMap = bpmCategoryService.getCategoryMap(Collections.singletonList(APPRAISE_APPROVE_CATEGORY_CODE)); |
| | | if (!categoryMap.containsKey(APPRAISE_APPROVE_CATEGORY_CODE)) { |
| | | throw exception(ErrorCodeConstants.PERFORMANCE_APPRAISE_BPM_PROCESS_DEFINITION_NOT_EXISTS); |
| | | } |
| | | |
| | | // 2. 校验分类下是否有可用的流程定义 |
| | | List<BpmProcessDefinitionInfoDO> definitionInfoList = bpmProcessDefinitionService |
| | | .getProcessDefinitionInfoListByCategory(APPRAISE_APPROVE_CATEGORY_CODE); |
| | | if (CollUtil.isEmpty(definitionInfoList)) { |
| | | throw exception(ErrorCodeConstants.PERFORMANCE_APPRAISE_BPM_PROCESS_DEFINITION_NOT_EXISTS); |
| | | } |
| | | |
| | | // 3. 校验是否有激活状态的流程定义 |
| | | Set<String> processDefinitionIds = convertSet(definitionInfoList, BpmProcessDefinitionInfoDO::getProcessDefinitionId); |
| | | List<ProcessDefinition> processDefinitions = bpmProcessDefinitionService.getProcessDefinitionList(processDefinitionIds); |
| | | boolean hasActiveDefinition = processDefinitions.stream().anyMatch(pd -> !pd.isSuspended()); |
| | | if (!hasActiveDefinition) { |
| | | throw exception(ErrorCodeConstants.PERFORMANCE_APPRAISE_BPM_PROCESS_DEFINITION_NOT_EXISTS); |
| | | } |
| | | } |
| | | |
| | | } |