| | |
| | | package cn.iocoder.yudao.module.mes.service.pd.project; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | 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.BpmCategoryDO; |
| | | import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmProcessDefinitionInfoDO; |
| | | import cn.iocoder.yudao.module.bpm.enums.task.BpmTaskStatusEnum; |
| | | import cn.iocoder.yudao.module.bpm.service.definition.BpmCategoryService; |
| | | import cn.iocoder.yudao.module.bpm.service.definition.BpmProcessDefinitionService; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pd.project.vo.MesPdProjectPageReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pd.project.vo.MesPdProjectSaveReqVO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.pd.document.MesPdDocumentDO; |
| | |
| | | import cn.iocoder.yudao.module.mes.service.pd.document.MesPdDocumentService; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import jakarta.annotation.Resource; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.flowable.engine.repository.ProcessDefinition; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.time.LocalDateTime; |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; |
| | | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
| | | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*; |
| | | |
| | | /** |
| | | * MES 设计项目 Service 实现类 |
| | | * |
| | | * @author 芋道源码 |
| | | * @author 超级管理员 |
| | | */ |
| | | @Service |
| | | @Validated |
| | | @Slf4j |
| | | public class MesPdProjectServiceImpl implements MesPdProjectService { |
| | | |
| | | /** |
| | | * BPM 产品项目审批分类编码 |
| | | */ |
| | | private static final String BPM_PROCESS_DEFINITION_CATEGORY_CODE = "mes_pd_project_approve"; |
| | | |
| | | @Resource |
| | | private MesPdProjectMapper projectMapper; |
| | |
| | | @Resource |
| | | @Lazy |
| | | private MesMdAutoCodeRecordService autoCodeRecordService; |
| | | |
| | | @Resource |
| | | private BpmProcessInstanceApi processInstanceApi; |
| | | |
| | | @Resource |
| | | private BpmCategoryService bpmCategoryService; |
| | | |
| | | @Resource |
| | | private BpmProcessDefinitionService bpmProcessDefinitionService; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | |
| | | } |
| | | |
| | | @Override |
| | | public void auditProject(Long id) { |
| | | // 校验存在 |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void submitProjectApproval(Long id, String processDefinitionKey) { |
| | | // 1. 校验存在 + 待审核状态 |
| | | MesPdProjectDO project = validateProjectExists(id); |
| | | // 校验状态为待审核 |
| | | if (!MesPdProjectStatusEnum.UPLOAD.getStatus().equals(project.getStatus())) { |
| | | throw exception(PD_PROJECT_NOT_UPLOAD_FOR_AUDIT); |
| | | throw exception(PD_PROJECT_NOT_UPLOAD_FOR_APPROVAL); |
| | | } |
| | | // 校验该项目下所有资料审核记录均已审核(不存在待审核的记录) |
| | | // 2. 校验该项目下所有资料审核记录均已审核(不存在待审核的记录) |
| | | List<MesPdDocumentDO> documents = documentService.getDocumentListByProjectId(id); |
| | | List<Long> documentIds = documents.stream().map(MesPdDocumentDO::getId).toList(); |
| | | if (documentAuditMapper.existsPendingAuditByDocumentIds(documentIds)) { |
| | | throw exception(PD_PROJECT_AUDIT_NOT_FINISHED); |
| | | } |
| | | // 更新状态 |
| | | // 3. 创建 BPM 流程实例 |
| | | String processInstanceId = processInstanceApi.createProcessInstance(getLoginUserId(), |
| | | new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(processDefinitionKey) |
| | | .setBusinessKey(String.valueOf(id))); |
| | | // 4. 更新项目状态为审批中 |
| | | projectMapper.updateById(MesPdProjectDO.builder() |
| | | .id(id) |
| | | .status(MesPdProjectStatusEnum.AUDIT.getStatus()) |
| | | .status(MesPdProjectStatusEnum.APPROVING.getStatus()) |
| | | .processInstanceId(processInstanceId) |
| | | .processDefinitionKey(processDefinitionKey) |
| | | .build()); |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> getProjectApproveProcessDefinitionList() { |
| | | // 1. 校验分类是否存在 |
| | | List<BpmCategoryDO> categories = bpmCategoryService.getCategoryListByCode( |
| | | Collections.singletonList(BPM_PROCESS_DEFINITION_CATEGORY_CODE)); |
| | | if (CollUtil.isEmpty(categories)) { |
| | | throw exception(PD_PROJECT_BPM_CATEGORY_NOT_EXISTS); |
| | | } |
| | | // 2. 获取分类下的流程定义信息 |
| | | List<BpmProcessDefinitionInfoDO> definitionInfoList = bpmProcessDefinitionService |
| | | .getProcessDefinitionInfoListByCategory(BPM_PROCESS_DEFINITION_CATEGORY_CODE); |
| | | if (CollUtil.isEmpty(definitionInfoList)) { |
| | | return Collections.emptyList(); |
| | | } |
| | | // 3. 过滤激活状态,保留最新版本 |
| | | Map<String, ProcessDefinition> latestVersionMap = new HashMap<>(); |
| | | for (BpmProcessDefinitionInfoDO info : definitionInfoList) { |
| | | ProcessDefinition pd = bpmProcessDefinitionService.getProcessDefinition(info.getProcessDefinitionId()); |
| | | if (pd == null || pd.isSuspended()) { |
| | | continue; |
| | | } |
| | | ProcessDefinition existing = latestVersionMap.get(pd.getKey()); |
| | | if (existing == null || pd.getVersion() > existing.getVersion()) { |
| | | latestVersionMap.put(pd.getKey(), pd); |
| | | } |
| | | } |
| | | // 4. 返回流程定义列表 |
| | | 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()); |
| | | result.add(item); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateProjectAuditStatus(Long id, Integer bpmResult) { |
| | | // 1. 校验存在 |
| | | MesPdProjectDO project = validateProjectExists(id); |
| | | if (!MesPdProjectStatusEnum.APPROVING.getStatus().equals(project.getStatus())) { |
| | | log.warn("[updateProjectAuditStatus] 设计项目({}) 不处于审批中状态,当前状态:{}", id, project.getStatus()); |
| | | return; |
| | | } |
| | | // 2. 根据审批结果更新状态 |
| | | Integer newStatus = convertBpmResultToProjectStatus(bpmResult); |
| | | if (newStatus != null) { |
| | | projectMapper.updateById(MesPdProjectDO.builder() |
| | | .id(id) |
| | | .status(newStatus) |
| | | .build()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转换 BPM 审批结果为设计项目状态 |
| | | */ |
| | | private Integer convertBpmResultToProjectStatus(Integer bpmResult) { |
| | | if (BpmTaskStatusEnum.APPROVE.getStatus().equals(bpmResult)) { |
| | | return MesPdProjectStatusEnum.AUDIT.getStatus(); |
| | | } else if (BpmTaskStatusEnum.REJECT.getStatus().equals(bpmResult)) { |
| | | return MesPdProjectStatusEnum.UPLOAD.getStatus(); |
| | | } else if (BpmTaskStatusEnum.CANCEL.getStatus().equals(bpmResult)) { |
| | | return MesPdProjectStatusEnum.UPLOAD.getStatus(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void archiveProject(Long id, Long userId) { |
| | | // 校验存在 |