From e347fd6e8b523b75e7eb28627ba3597cf9e40f6a Mon Sep 17 00:00:00 2001
From: liu <2021943741@qq.com>
Date: 星期三, 16 九月 2026 14:26:40 +0800
Subject: [PATCH] refactor(approval): 采购计划/采购订单审批由 BPM 改为轻量级状态机
---
yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/purchase/ErpPurchasePlanServiceImpl.java | 229 ++++++++++++++++++++++++++++++++++-----------------------
1 files changed, 136 insertions(+), 93 deletions(-)
diff --git a/yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/purchase/ErpPurchasePlanServiceImpl.java b/yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/purchase/ErpPurchasePlanServiceImpl.java
index c049142..c0bcb87 100644
--- a/yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/purchase/ErpPurchasePlanServiceImpl.java
+++ b/yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/purchase/ErpPurchasePlanServiceImpl.java
@@ -1,15 +1,12 @@
package cn.iocoder.yudao.module.erp.service.purchase;
import cn.hutool.core.collection.CollUtil;
+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.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.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.erp.controller.admin.purchase.vo.plan.ErpPurchasePlanPageReqVO;
+import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.plan.ErpPurchasePlanRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.plan.ErpPurchasePlanSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchasePlanDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchasePlanItemDO;
@@ -22,24 +19,26 @@
import cn.iocoder.yudao.module.erp.dal.redis.no.ErpNoRedisDAO;
import cn.iocoder.yudao.module.erp.enums.ErpPurchasePlanStatusEnum;
import cn.iocoder.yudao.module.erp.enums.ErpPurchaseRequestStatusEnum;
-import cn.iocoder.yudao.module.mes.api.item.MesMdItemApi;
-import cn.iocoder.yudao.module.mes.api.item.dto.MesMdItemSafetyStockRespDTO;
+import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi;
+import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO;
import cn.iocoder.yudao.module.mes.api.wms.MesWmStockApi;
+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.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
+import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*;
/**
@@ -53,9 +52,9 @@
public class ErpPurchasePlanServiceImpl implements ErpPurchasePlanService {
/**
- * BPM 閲囪喘璁″垝瀹℃壒鍒嗙被缂栫爜
+ * 閫氱敤瀹℃壒閰嶇疆鐨勪笟鍔$被鍨嬬紪鐮侊紙瀵瑰簲 system_approval_config.biz_type锛�
*/
- private static final String PURCHASE_PLAN_APPROVE_CATEGORY_CODE = "purchase_plan_approve";
+ private static final String PURCHASE_PLAN_APPROVE_BIZ_TYPE = "purchase_plan_approve";
/**
* 闇�姹傛潵婧愶細搴撳瓨棰勮
@@ -80,17 +79,13 @@
private ErpNoRedisDAO noRedisDAO;
@Resource
- private BpmProcessInstanceApi processInstanceApi;
- @Resource
- private BpmCategoryService bpmCategoryService;
- @Resource
- private BpmProcessDefinitionService bpmProcessDefinitionService;
+ private ApprovalConfigApi approvalConfigApi;
@Resource
private AdminUserApi adminUserApi;
@Resource
- private MesMdItemApi mesMdItemApi;
+ private MdmItemApi mdmItemApi;
@Resource
private MesWmStockApi mesWmStockApi;
@@ -193,58 +188,77 @@
@Override
@Transactional(rollbackFor = Exception.class)
- public void submitPurchasePlan(Long id, String processDefinitionKey, Long userId) {
- // 1. 鏍¢獙瀛樺湪 + 鑽夌鐘舵��
- ErpPurchasePlanDO plan = validatePurchasePlanCanModify(id);
+ public void submitPurchasePlan(Long id) {
+ // 1. 鏍¢獙瀛樺湪 + 鍙彁浜ょ姸鎬侊紙鑽夌 / 瀹℃牳涓嶉�氳繃锛�
+ ErpPurchasePlanDO plan = validatePurchasePlanExists(id);
+ if (!ErpPurchasePlanStatusEnum.DRAFT.getStatus().equals(plan.getStatus())
+ && !ErpPurchasePlanStatusEnum.REJECT.getStatus().equals(plan.getStatus())) {
+ throw exception(PURCHASE_PLAN_SUBMIT_FAIL_NOT_ALLOWED);
+ }
// 2. 鏍¢獙鏄惁鏈夋槑缁�
List<ErpPurchasePlanItemDO> items = purchasePlanItemMapper.selectListByPlanId(id);
if (CollUtil.isEmpty(items)) {
throw exception(PURCHASE_PLAN_ITEM_EMPTY);
}
- // 3. 鍒涘缓 BPM 娴佺▼瀹炰緥
- String processInstanceId = processInstanceApi.createProcessInstance(userId,
- new BpmProcessInstanceCreateReqDTO()
- .setProcessDefinitionKey(processDefinitionKey)
- .setBusinessKey(String.valueOf(id)));
- // 4. 鏇存柊閲囪喘璁″垝鐘舵�佷负瀹℃壒涓�
- purchasePlanMapper.updateById(new ErpPurchasePlanDO()
- .setId(id)
- .setStatus(ErpPurchasePlanStatusEnum.PROCESS.getStatus())
- .setProcessInstanceId(processInstanceId));
+ // 3. 鏍¢獙瀹℃壒閰嶇疆宸插惎鐢ㄤ笖閰嶇疆浜嗘湁鏁堝鎵逛汉锛堟湭閰嶇疆鏃舵姏鍑哄甫鏄庣‘鎻愮ず鐨勪笟鍔″紓甯革級
+ approvalConfigApi.validateApprovalEnabledAndGetApprovers(PURCHASE_PLAN_APPROVE_BIZ_TYPE);
+ // 4. 鐘舵�佺疆涓哄鎵逛腑锛屽苟娓呯┖涓婁竴杞鏍哥粨鏋滐紝閬垮厤娈嬬暀銆屽鏍镐笉閫氳繃銆嶇殑鍘熷洜
+ purchasePlanMapper.submitAndResetAuditInfo(id, ErpPurchasePlanStatusEnum.PROCESS.getStatus());
}
@Override
- public List<Map<String, Object>> getPurchasePlanApproveProcessDefinitionList() {
- // 1. 鏍¢獙鍒嗙被鍜屾祦绋嬪畾涔夋槸鍚﹀瓨鍦�
- validatePurchasePlanApproveCategoryAndProcessDefinition();
- // 2. 鑾峰彇鍒嗙被涓嬬殑娴佺▼瀹氫箟淇℃伅
- List<BpmProcessDefinitionInfoDO> definitionInfoList = bpmProcessDefinitionService
- .getProcessDefinitionInfoListByCategory(PURCHASE_PLAN_APPROVE_CATEGORY_CODE);
- if (CollUtil.isEmpty(definitionInfoList)) {
- return Collections.emptyList();
+ public Set<Long> getPurchasePlanApproverUserIds() {
+ return approvalConfigApi.getApproverUserIds(PURCHASE_PLAN_APPROVE_BIZ_TYPE);
+ }
+
+ @Override
+ @Transactional(rollbackFor = Exception.class)
+ public void auditPurchasePlan(Long id, Boolean pass, String reviewRemark) {
+ // 1. 鏍¢獙瀛樺湪
+ ErpPurchasePlanDO plan = validatePurchasePlanExists(id);
+ // 2. 鏍¢獙澶勪簬瀹℃壒涓姸鎬�
+ if (!ErpPurchasePlanStatusEnum.PROCESS.getStatus().equals(plan.getStatus())) {
+ throw exception(PURCHASE_PLAN_AUDIT_FAIL_NOT_PROCESS);
}
- // 3. 閬嶅巻鑾峰彇娴佺▼瀹氫箟璇︽儏锛岃繃婊ゆ縺娲荤姸鎬侊紝淇濈暀鏈�鏂扮増鏈�
- Map<String, ProcessDefinition> latestVersionMap = new HashMap<>();
- for (BpmProcessDefinitionInfoDO info : definitionInfoList) {
- ProcessDefinition pd = bpmProcessDefinitionService.getProcessDefinition(info.getProcessDefinitionId());
- if (pd == null || pd.isSuspended()) {
- continue;
+ // 3. 鏍¢獙褰撳墠鐧诲綍鐢ㄦ埛鏄涓氬姟绫诲瀷鐨勫鎵逛汉锛堟垨绛撅細浠讳竴浜哄潎鍙鏍革級
+ Long userId = getLoginUserId();
+ approvalConfigApi.validateApprover(PURCHASE_PLAN_APPROVE_BIZ_TYPE, userId);
+ // 4. 瀹℃牳涓嶉�氳繃锛氬繀椤诲~鍐欏師鍥狅紝鐘舵�佺疆涓哄鏍镐笉閫氳繃锛岄��鍥炵紪鍒朵汉淇敼鍚庡彲閲嶆柊鎻愪氦
+ if (!Boolean.TRUE.equals(pass)) {
+ if (StrUtil.isBlank(reviewRemark)) {
+ throw exception(PURCHASE_PLAN_AUDIT_REJECT_REASON_REQUIRED);
}
- ProcessDefinition existing = latestVersionMap.get(pd.getKey());
- if (existing == null || pd.getVersion() > existing.getVersion()) {
- latestVersionMap.put(pd.getKey(), pd);
+ purchasePlanMapper.auditPurchasePlan(id, ErpPurchasePlanStatusEnum.REJECT.getStatus(),
+ userId, getUserNickname(userId), reviewRemark);
+ return;
+ }
+ // 5. 瀹℃牳閫氳繃锛氳褰曞鏍镐汉涓庡鏍告剰瑙侊紝鐘舵�佺疆涓哄鏍搁�氳繃
+ purchasePlanMapper.auditPurchasePlan(id, ErpPurchasePlanStatusEnum.APPROVE.getStatus(),
+ userId, getUserNickname(userId), reviewRemark);
+ // 6. 瀹℃牳閫氳繃涓旀湭鐢熸垚鐢宠锛岃嚜鍔ㄧ敓鎴愰噰璐敵璇�
+ if (!Boolean.TRUE.equals(plan.getGeneratedFlag())) {
+ try {
+ List<Long> requestIds = doGeneratePurchaseRequests(plan);
+ log.info("[auditPurchasePlan] 閲囪喘璁″垝({}) 鑷姩鐢熸垚閲囪喘鐢宠({})", id, requestIds);
+ } catch (Exception e) {
+ // 涓嶆姏鍑哄紓甯革紝閬垮厤褰卞搷瀹℃牳缁撴灉钀藉簱锛涚敓鎴愬け璐ュ彲鐢辩敤鎴锋墜鍔ㄨЕ鍙�
+ log.error("[auditPurchasePlan] 閲囪喘璁″垝({}) 鑷姩鐢熸垚閲囪喘鐢宠澶辫触", id, e);
}
}
- // 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);
+ }
+
+ /**
+ * 鑾峰彇鐢ㄦ埛鏄电О
+ *
+ * @param userId 鐢ㄦ埛缂栧彿
+ * @return 鏄电О锛岀敤鎴蜂笉瀛樺湪鏃惰繑鍥� null
+ */
+ private String getUserNickname(Long userId) {
+ if (userId == null) {
+ return null;
}
- return result;
+ AdminUserRespDTO user = adminUserApi.getUser(userId);
+ return user == null ? null : user.getNickname();
}
@Override
@@ -275,42 +289,84 @@
}
@Override
+ public List<ErpPurchasePlanRespVO.Item> getStockAlertSuggestItems() {
+ // 1. 鑾峰彇鍚敤鐨勭墿鏂欏熀纭�鏁版嵁锛堝寘鍚粯璁や粨搴撱�佸畨鍏�/鏈�浣�/鏈�楂樺簱瀛橈級
+ List<MdmItemRespDTO> items = mdmItemApi.getItemListByStatus(0).getCheckedData();
+ if (CollUtil.isEmpty(items)) {
+ throw exception(PURCHASE_PLAN_STOCK_ALERT_NO_ITEM);
+ }
+ // 2. 鎸夐粯璁や粨搴撳簱瀛樿鍒掑缓璁噰璐噺
+ List<ErpPurchasePlanRespVO.Item> planItems = new ArrayList<>();
+ for (MdmItemRespDTO item : items) {
+ ErpPurchasePlanRespVO.Item planItem = buildStockAlertSuggestItem(item);
+ if (planItem != null) {
+ planItems.add(planItem);
+ }
+ }
+ if (CollUtil.isEmpty(planItems)) {
+ throw exception(PURCHASE_PLAN_STOCK_ALERT_NO_ITEM);
+ }
+ return planItems;
+ }
+
+ @Override
@Transactional(rollbackFor = Exception.class)
public ErpPurchasePlanDO generatePlanFromStockAlert(String name, Long supplierId) {
- // 1. 鑾峰彇鍚敤瀹夊叏搴撳瓨鐨勭墿鏂�
- List<MesMdItemSafetyStockRespDTO> safetyStockItems = mesMdItemApi.getSafetyStockItemList();
- if (CollUtil.isEmpty(safetyStockItems)) {
- throw exception(PURCHASE_PLAN_ITEM_EMPTY);
+ // 1. 鑾峰彇搴撳瓨棰勮寤鸿閲囪喘鏄庣粏
+ List<ErpPurchasePlanRespVO.Item> suggestItems = getStockAlertSuggestItems();
+ if (CollUtil.isEmpty(suggestItems)) {
+ throw exception(PURCHASE_PLAN_STOCK_ALERT_NO_ITEM);
}
- // 2. 璁$畻缂洪噺锛岀敓鎴愯鍒掓槑缁�
- List<ErpPurchasePlanSaveReqVO.Item> items = new ArrayList<>();
- for (MesMdItemSafetyStockRespDTO item : safetyStockItems) {
- if (item.getMinStock() == null) {
- continue;
- }
- BigDecimal stock = mesWmStockApi.getStockQuantity(item.getId());
- BigDecimal shortage = item.getMinStock().subtract(stock == null ? BigDecimal.ZERO : stock);
- if (shortage.compareTo(BigDecimal.ZERO) <= 0) {
- continue;
- }
- ErpPurchasePlanSaveReqVO.Item planItem = new ErpPurchasePlanSaveReqVO.Item();
- planItem.setProductId(item.getMdmItemId());
- planItem.setProductUnitId(item.getUnitMeasureId());
- planItem.setCount(shortage);
- items.add(planItem);
- }
- if (CollUtil.isEmpty(items)) {
- throw exception(PURCHASE_PLAN_ITEM_EMPTY);
- }
+ // 2. 杞崲涓轰繚瀛樻槑缁�
+ List<ErpPurchasePlanSaveReqVO.Item> planItems = convertList(suggestItems, item ->
+ BeanUtils.toBean(item, ErpPurchasePlanSaveReqVO.Item.class));
// 3. 鍒涘缓鑽夌閲囪喘璁″垝
ErpPurchasePlanSaveReqVO createReqVO = new ErpPurchasePlanSaveReqVO();
createReqVO.setName(name != null ? name : "搴撳瓨棰勮閲囪喘璁″垝");
createReqVO.setDemandSource(DEMAND_SOURCE_STOCK_ALERT);
createReqVO.setPlanDate(LocalDateTime.now());
createReqVO.setSupplierId(supplierId);
- createReqVO.setItems(items);
+ createReqVO.setItems(planItems);
Long planId = createPurchasePlan(createReqVO);
return purchasePlanMapper.selectById(planId);
+ }
+
+ /**
+ * 鏍规嵁鍗曚釜鐗╂枡鐨勯粯璁や粨搴撳簱瀛樻瀯寤哄缓璁噰璐槑缁�
+ *
+ * @param item 鐗╂枡鍩虹鏁版嵁
+ * @return 寤鸿閲囪喘鏄庣粏锛涙棤闇�琛ヨ揣鏃惰繑鍥� null
+ */
+ private ErpPurchasePlanRespVO.Item buildStockAlertSuggestItem(MdmItemRespDTO item) {
+ // 1. 鏈厤缃粯璁や粨搴撴垨鏈�浣庡簱瀛橈紙鏃犺ˉ璐у熀鍑嗭級鐨勭墿鏂欎笉鍙備笌瑙勫垝
+ if (item.getWarehouseId() == null || item.getMinStock() == null) {
+ return null;
+ }
+ // 2. 鏌ヨ榛樿浠撳簱鐨勫綋鍓嶅簱瀛�
+ BigDecimal currentStock = mesWmStockApi.getStockQuantity(item.getId(), item.getWarehouseId());
+ if (currentStock == null) {
+ currentStock = BigDecimal.ZERO;
+ }
+ // 3. 榛樿浠撳簱搴撳瓨涓嶄綆浜庢渶浣庡簱瀛樻椂涓嶈Е鍙戣ˉ璐�
+ if (currentStock.compareTo(item.getMinStock()) >= 0) {
+ return null;
+ }
+ // 4. 琛ヨ揣鐩爣锛氫紭鍏堣ˉ鍒板畨鍏ㄥ簱瀛橈紝鏈厤缃垯鍥炶惤鏈�楂樺簱瀛橈紝鏈�鍚庡彇鏈�浣庡簱瀛�
+ BigDecimal targetStock = item.getSafetyStock() != null ? item.getSafetyStock()
+ : (item.getMaxStock() != null ? item.getMaxStock() : item.getMinStock());
+ BigDecimal count = targetStock.subtract(currentStock);
+ if (count.compareTo(BigDecimal.ZERO) <= 0) {
+ return null;
+ }
+ ErpPurchasePlanRespVO.Item planItem = new ErpPurchasePlanRespVO.Item();
+ planItem.setProductId(item.getId());
+ planItem.setProductName(item.getName());
+ planItem.setProductUnitId(item.getUnitMeasureId());
+ planItem.setCount(count);
+ planItem.setDemandTime(LocalDate.now());
+ planItem.setRemark(String.format("榛樿浠撳簱搴撳瓨%s锛屽畨鍏ㄥ簱瀛�%s锛屾渶浣庡簱瀛�%s锛屾渶楂樺簱瀛�%s锛屽缓璁噰璐噺%s",
+ currentStock, item.getSafetyStock(), item.getMinStock(), item.getMaxStock(), count));
+ return planItem;
}
@Override
@@ -393,19 +449,6 @@
return ErpPurchasePlanStatusEnum.CANCEL.getStatus();
}
return null;
- }
-
- private void validatePurchasePlanApproveCategoryAndProcessDefinition() {
- List<cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmCategoryDO> categories =
- bpmCategoryService.getCategoryListByCode(Collections.singletonList(PURCHASE_PLAN_APPROVE_CATEGORY_CODE));
- if (CollUtil.isEmpty(categories)) {
- throw exception(PURCHASE_PLAN_BPM_CATEGORY_NOT_EXISTS);
- }
- List<BpmProcessDefinitionInfoDO> definitionInfoList = bpmProcessDefinitionService
- .getProcessDefinitionInfoListByCategory(PURCHASE_PLAN_APPROVE_CATEGORY_CODE);
- if (CollUtil.isEmpty(definitionInfoList)) {
- throw exception(PURCHASE_PLAN_BPM_PROCESS_DEFINITION_NOT_EXISTS);
- }
}
private ErpPurchasePlanDO validatePurchasePlanExists(Long id) {
--
Gitblit v1.9.3