From d54fbc9d4f8981fa0f7f613b33a28a8cd4f764ea Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期日, 25 十月 2026 16:29:53 +0800
Subject: [PATCH] feat(system): 优化用户搜索功能并完善ERP和MES模块验证逻辑

---
 yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/purchase/ErpPurchaseOrderServiceImpl.java |  316 ++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 242 insertions(+), 74 deletions(-)

diff --git a/yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/purchase/ErpPurchaseOrderServiceImpl.java b/yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/purchase/ErpPurchaseOrderServiceImpl.java
index cb939a8..9bb1002 100644
--- a/yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/purchase/ErpPurchaseOrderServiceImpl.java
+++ b/yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/purchase/ErpPurchaseOrderServiceImpl.java
@@ -2,35 +2,47 @@
 
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.iocoder.yudao.framework.common.exception.ServiceException;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
 import cn.iocoder.yudao.framework.common.util.number.MoneyUtils;
 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.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.erp.controller.admin.purchase.vo.order.ErpPurchaseOrderImportExcelVO;
+import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.order.ErpPurchaseOrderImportReqVO;
+import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.order.ErpPurchaseOrderImportRespVO;
 import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.order.ErpPurchaseOrderPageReqVO;
 import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.order.ErpPurchaseOrderSaveReqVO;
+import cn.iocoder.yudao.module.erp.dal.dataobject.finance.ErpAccountDO;
 import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseOrderDO;
 import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseOrderItemDO;
 import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseRequestDO;
 import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseRequestItemDO;
+import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpSupplierDO;
+import cn.iocoder.yudao.module.erp.dal.mysql.finance.ErpAccountMapper;
 import cn.iocoder.yudao.module.erp.dal.mysql.purchase.ErpPurchaseOrderItemMapper;
 import cn.iocoder.yudao.module.erp.dal.mysql.purchase.ErpPurchaseOrderMapper;
 import cn.iocoder.yudao.module.erp.dal.mysql.purchase.ErpPurchaseRequestItemMapper;
 import cn.iocoder.yudao.module.erp.dal.mysql.purchase.ErpPurchaseRequestMapper;
+import cn.iocoder.yudao.module.erp.dal.mysql.purchase.ErpSupplierMapper;
 import cn.iocoder.yudao.module.erp.dal.redis.no.ErpNoRedisDAO;
 import cn.iocoder.yudao.module.erp.enums.ErpAuditStatus;
 import cn.iocoder.yudao.module.erp.enums.ErpPurchaseOrderInStatusEnum;
+import cn.iocoder.yudao.module.erp.enums.ErpPurchaseOrderStatusEnum;
 import cn.iocoder.yudao.module.erp.service.finance.ErpAccountService;
+import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi;
+import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO;
+import cn.iocoder.yudao.module.mdm.api.unit.MdmUnitMeasureApi;
+import cn.iocoder.yudao.module.mdm.api.unit.dto.MdmUnitMeasureRespDTO;
 import cn.iocoder.yudao.module.mes.api.arrivalnotice.MesArrivalNoticeApi;
+import cn.iocoder.yudao.module.system.api.approval.ApprovalConfigApi;
 import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi;
+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.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -41,12 +53,13 @@
 
 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
+import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
 import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*;
 
 /**
  * ERP 閲囪喘璁㈠崟 Service 瀹炵幇绫�
  *
- * @author 鑺嬮亾婧愮爜
+ * @author 瓒呯骇绠$悊鍛�
  */
 @Service
 @Validated
@@ -54,9 +67,19 @@
 public class ErpPurchaseOrderServiceImpl implements ErpPurchaseOrderService {
 
     /**
-     * BPM 閲囪喘璁㈠崟瀹℃壒鍒嗙被缂栫爜
+     * 閲囪喘璁㈠崟瀹℃壒涓氬姟绫诲瀷缂栫爜锛堝搴� system_approval_config.biz_type锛�
+     *
+     * 娉ㄦ剰锛氭敼閫犱负杞婚噺绾х姸鎬佹満瀹℃壒鍓嶏紝姝ゅ鍘熶负 "purchase_request_approve"锛�
+     * 涓庨噰璐敵璇凤紙ErpPurchaseRequestServiceImpl锛夌殑 BPM 鍒嗙被缂栫爜瀹屽叏鐩稿悓锛�
+     * 瀵艰嚧閲囪喘璁㈠崟鏌ヨ瀹℃壒娴佺▼鏃朵細鍛戒腑閲囪喘鐢宠鐨勬祦绋嬪畾涔夛紝涓よ�呬簰鐩镐覆鐢ㄣ��
+     * 鐜版洿姝d负閲囪喘璁㈠崟涓撳睘缂栫爜銆�
      */
-    private static final String PURCHASE_ORDER_APPROVE_CATEGORY_CODE = "purchase_request_approve";
+    private static final String PURCHASE_ORDER_APPROVE_BIZ_TYPE = "erp_purchase_order_approve";
+
+    /**
+     * 閲囪喘璁㈠崟瀹℃壒鐨� BPM 娴佺▼瀹氫箟 Key
+     */
+    private static final String PURCHASE_ORDER_PROCESS_KEY = "purchase_approve";
 
     @Resource
     private ErpPurchaseOrderMapper purchaseOrderMapper;
@@ -70,13 +93,21 @@
     private ErpSupplierService supplierService;
     @Resource
     private ErpAccountService accountService;
+    @Resource
+    private ErpSupplierMapper supplierMapper;
+    @Resource
+    private ErpAccountMapper accountMapper;
+    @Resource
+    private MdmItemApi mdmItemApi;
+    @Resource
+    private MdmUnitMeasureApi mdmUnitMeasureApi;
 
     @Resource
-    private BpmProcessInstanceApi processInstanceApi;
+    private ApprovalConfigApi approvalConfigApi;
     @Resource
-    private BpmCategoryService bpmCategoryService;
+    private BpmProcessInstanceApi bpmProcessInstanceApi;
     @Resource
-    private BpmProcessDefinitionService bpmProcessDefinitionService;
+    private AdminUserApi adminUserApi;
 
     @Resource
     @Lazy
@@ -107,9 +138,9 @@
             throw exception(PURCHASE_ORDER_NO_EXISTS);
         }
 
-        // 2.1 鎻掑叆璁㈠崟锛堥噰璐鍗曞垱寤哄悗涓烘湭瀹℃牳鐘舵�侊紝闇�瑕佹彁浜ゅ鎵癸級
+        // 2.1 鎻掑叆璁㈠崟锛堥噰璐鍗曞垱寤哄悗涓恒�愭湭鎻愪氦銆戠姸鎬侊紝闇�鎻愪氦杩涘叆瀹℃壒娴佺▼锛�
         ErpPurchaseOrderDO purchaseOrder = BeanUtils.toBean(createReqVO, ErpPurchaseOrderDO.class, in -> in
-                .setNo(no).setStatus(ErpAuditStatus.PROCESS.getStatus())
+                .setNo(no).setStatus(ErpPurchaseOrderStatusEnum.DRAFT.getStatus())
                 .setInCount(BigDecimal.ZERO).setInStatus(ErpPurchaseOrderInStatusEnum.NOT_IN.getStatus()));
         calculateTotalPrice(purchaseOrder, purchaseOrderItems);
         purchaseOrderMapper.insert(purchaseOrder);
@@ -127,9 +158,13 @@
     public void updatePurchaseOrder(ErpPurchaseOrderSaveReqVO updateReqVO) {
         // 1.1 鏍¢獙瀛樺湪
         ErpPurchaseOrderDO purchaseOrder = validatePurchaseOrderExists(updateReqVO.getId());
-        // 閲囪喘璁㈠崟涓嶅厑璁镐慨鏀癸紙宸插鏍哥姸鎬侊級
-        if (ErpAuditStatus.APPROVE.getStatus().equals(purchaseOrder.getStatus())) {
+        // 宸插鏍哥姸鎬佷笉鍏佽淇敼
+        if (ErpPurchaseOrderStatusEnum.APPROVE.getStatus().equals(purchaseOrder.getStatus())) {
             throw exception(PURCHASE_ORDER_UPDATE_FAIL_APPROVE, purchaseOrder.getNo());
+        }
+        // 瀹℃壒涓笉鍏佽淇敼锛氶渶鍏堢敱瀹℃壒浜恒�屽鏍镐笉閫氳繃銆嶉��鍥烇紝鎵嶈兘閲嶆柊淇敼骞舵彁浜�
+        if (ErpPurchaseOrderStatusEnum.PROCESS.getStatus().equals(purchaseOrder.getStatus())) {
+            throw exception(PURCHASE_ORDER_UPDATE_FAIL_PROCESS, purchaseOrder.getNo());
         }
         // 1.2 鏍¢獙渚涘簲鍟�
         supplierService.validateSupplier(updateReqVO.getSupplierId());
@@ -147,11 +182,7 @@
         // 2.2 鏇存柊璁㈠崟椤�
         updatePurchaseOrderItemList(updateReqVO.getId(), purchaseOrderItems);
         // 2.3 鏇存柊闄勪欢
-        if (updateReqVO.getBlobIds() != null) {
-            storageAttachmentApi.deleteAttachmentsByRecord("erp_purchase_order", updateReqVO.getId());
-            storageAttachmentApi.bindAttachments("file", "erp_purchase_order",
-                    updateReqVO.getId(), updateReqVO.getBlobIds());
-        }
+        storageAttachmentApi.updateAttachments("file", "erp_purchase_order", updateReqVO.getId(), updateReqVO.getBlobIds());
     }
 
     private void calculateTotalPrice(ErpPurchaseOrderDO purchaseOrder, List<ErpPurchaseOrderItemDO> purchaseOrderItems) {
@@ -173,6 +204,10 @@
         boolean approve = ErpAuditStatus.APPROVE.getStatus().equals(status);
         // 1.1 鏍¢獙瀛樺湪
         ErpPurchaseOrderDO purchaseOrder = validatePurchaseOrderExists(id);
+        // 1.1.1 瀹℃牳閫氳繃灞炰簬瀹℃壒鍔ㄤ綔锛屽繀椤荤敱閰嶇疆鍐呯殑瀹℃壒浜烘墽琛岋紙鏈嶅姟绔己鍒讹紝閬垮厤缁曡繃瀹℃牳鎸夐挳鑷姪閫氳繃锛�
+        if (approve) {
+            approvalConfigApi.validateApprover(PURCHASE_ORDER_APPROVE_BIZ_TYPE, getLoginUserId());
+        }
         // 1.2 鏍¢獙鐘舵��
         if (purchaseOrder.getStatus().equals(status)) {
             throw exception(approve ? PURCHASE_ORDER_APPROVE_FAIL : PURCHASE_ORDER_PROCESS_FAIL);
@@ -198,61 +233,80 @@
 
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public void submitPurchaseOrder(Long id, String processDefinitionKey, Long userId) {
-        // 1. 鏍¢獙瀛樺湪 + 鏈鏍哥姸鎬�
+    public void submitPurchaseOrder(Long id) {
+        // 1.1 鏍¢獙瀛樺湪
         ErpPurchaseOrderDO purchaseOrder = validatePurchaseOrderExists(id);
-        if (!ErpAuditStatus.PROCESS.getStatus().equals(purchaseOrder.getStatus())) {
-            throw exception(PURCHASE_ORDER_SUBMIT_FAIL_STATUS);
+        // 1.2 鍙湁銆愭湭鎻愪氦銆戞垨銆愬鏍镐笉閫氳繃銆戞墠鍏佽鎻愪氦
+        if (!ErpPurchaseOrderStatusEnum.DRAFT.getStatus().equals(purchaseOrder.getStatus())
+                && !ErpPurchaseOrderStatusEnum.REJECT.getStatus().equals(purchaseOrder.getStatus())) {
+            throw exception(PURCHASE_ORDER_SUBMIT_FAIL_NOT_ALLOWED);
+        }
+        // 1.3 蹇呴』閰嶇疆璁㈠崟椤�
+        List<ErpPurchaseOrderItemDO> items = purchaseOrderItemMapper.selectListByOrderId(id);
+        if (CollUtil.isEmpty(items)) {
+            throw exception(PURCHASE_ORDER_SUBMIT_FAIL_ITEM_EMPTY);
         }
 
-        // 2. 鍒涘缓 BPM 娴佺▼瀹炰緥
-        String processInstanceId = processInstanceApi.createProcessInstance(userId,
-                new BpmProcessInstanceCreateReqDTO()
-                        .setProcessDefinitionKey(processDefinitionKey)
-                        .setBusinessKey(String.valueOf(id)));
+        // 2. 鏍¢獙瀹℃壒宸插紑鍚笖閰嶇疆浜嗘湁鏁堝鎵逛汉锛堟湭寮�鍚鎵规椂涓嶅厑璁告彁浜わ級
+        approvalConfigApi.validateApprovalEnabledAndGetApprovers(PURCHASE_ORDER_APPROVE_BIZ_TYPE);
 
-        // 3. 鏇存柊閲囪喘璁㈠崟鐘舵�佷负瀹℃壒涓�
-        purchaseOrderMapper.updateById(new ErpPurchaseOrderDO()
-                .setId(id)
-                .setStatus(ErpAuditStatus.APPROVING.getStatus())
-                .setProcessInstanceId(processInstanceId));
+        // 3. 缃负瀹℃壒涓紝骞舵竻绌轰笂涓�杞鏍镐俊鎭紝閬垮厤椹冲洖鍘熷洜娈嬬暀
+        if (purchaseOrderMapper.submitAndResetAuditInfo(id, ErpPurchaseOrderStatusEnum.PROCESS.getStatus()) != 1) {
+            throw exception(PURCHASE_ORDER_SUBMIT_FAIL_NOT_ALLOWED);
+        }
+
+        // 4. 鍒涘缓 BPM 娴佺▼瀹炰緥锛屼娇瀹℃壒鍑虹幇鍦ㄣ�屽崗鍚屽姙鍏� - 鎴戠殑娴佺▼銆�
+        bpmProcessInstanceApi.createProcessInstance(getLoginUserId(),
+                new BpmProcessInstanceCreateReqDTO()
+                        .setProcessDefinitionKey(PURCHASE_ORDER_PROCESS_KEY)
+                        .setBusinessKey(String.valueOf(id)));
     }
 
     @Override
-    public List<Map<String, Object>> getPurchaseOrderApproveProcessDefinitionList() {
-        // 1. 鏍¢獙鍒嗙被鍜屾祦绋嬪畾涔夋槸鍚﹀瓨鍦�
-        validatePurchaseOrderApproveCategoryAndProcessDefinition();
+    public Set<Long> getPurchaseOrderApproverUserIds() {
+        return approvalConfigApi.getApproverUserIds(PURCHASE_ORDER_APPROVE_BIZ_TYPE);
+    }
 
-        // 2. 鑾峰彇鍒嗙被涓嬬殑娴佺▼瀹氫箟淇℃伅
-        List<BpmProcessDefinitionInfoDO> definitionInfoList = bpmProcessDefinitionService
-                .getProcessDefinitionInfoListByCategory(PURCHASE_ORDER_APPROVE_CATEGORY_CODE);
-        if (CollUtil.isEmpty(definitionInfoList)) {
-            return Collections.emptyList();
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void auditPurchaseOrder(Long id, Boolean pass, String reviewRemark) {
+        // 1.1 鏍¢獙瀛樺湪 + 瀹℃壒涓姸鎬�
+        ErpPurchaseOrderDO purchaseOrder = validatePurchaseOrderExists(id);
+        if (!ErpPurchaseOrderStatusEnum.PROCESS.getStatus().equals(purchaseOrder.getStatus())) {
+            throw exception(PURCHASE_ORDER_AUDIT_FAIL_NOT_PROCESS);
         }
+        // 1.2 鏍¢獙褰撳墠鐧诲綍浜烘槸閰嶇疆鐨勫鎵逛汉锛堟垨绛撅細浠讳竴浜哄鏍稿嵆鍙級
+        Long userId = getLoginUserId();
+        approvalConfigApi.validateApprover(PURCHASE_ORDER_APPROVE_BIZ_TYPE, userId);
 
-        // 3. 閬嶅巻鑾峰彇娴佺▼瀹氫箟璇︽儏锛岃繃婊ゆ縺娲荤姸鎬侊紝淇濈暀鏈�鏂扮増鏈�
-        Map<String, ProcessDefinition> latestVersionMap = new HashMap<>();
-        for (BpmProcessDefinitionInfoDO info : definitionInfoList) {
-            ProcessDefinition pd = bpmProcessDefinitionService.getProcessDefinition(info.getProcessDefinitionId());
-            if (pd == null || pd.isSuspended()) {
-                continue;
+        // 2. 瀹℃牳涓嶉�氳繃锛氬繀椤诲~鍐欓┏鍥炲師鍥狅紝鍗曟嵁閫�鍥炲悗鍙紪杈戝苟閲嶆柊鎻愪氦
+        if (!Boolean.TRUE.equals(pass)) {
+            if (StrUtil.isBlank(reviewRemark)) {
+                throw exception(PURCHASE_ORDER_AUDIT_REJECT_REASON_REQUIRED);
             }
-            ProcessDefinition existing = latestVersionMap.get(pd.getKey());
-            if (existing == null || pd.getVersion() > existing.getVersion()) {
-                latestVersionMap.put(pd.getKey(), pd);
+            if (purchaseOrderMapper.auditPurchaseOrder(id, ErpPurchaseOrderStatusEnum.REJECT.getStatus(),
+                    userId, getUserNickname(userId), reviewRemark) != 1) {
+                throw exception(PURCHASE_ORDER_AUDIT_FAIL_NOT_PROCESS);
             }
+            return;
         }
 
-        // 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);
+        // 3. 瀹℃牳閫氳繃
+        if (purchaseOrderMapper.auditPurchaseOrder(id, ErpPurchaseOrderStatusEnum.APPROVE.getStatus(),
+                userId, getUserNickname(userId), reviewRemark) != 1) {
+            throw exception(PURCHASE_ORDER_AUDIT_FAIL_NOT_PROCESS);
         }
-        return result;
+    }
+
+    /**
+     * 鑾峰彇鐢ㄦ埛鏄电О锛岀敤浜庡啑浣欏瓨鍌ㄥ鏍镐汉濮撳悕
+     */
+    private String getUserNickname(Long userId) {
+        if (userId == null) {
+            return null;
+        }
+        AdminUserRespDTO user = adminUserApi.getUser(userId);
+        return user == null ? null : user.getNickname();
     }
 
     @Override
@@ -260,7 +314,7 @@
     public void updatePurchaseOrderAuditStatus(Long id, Integer bpmResult) {
         // 1. 鏍¢獙瀛樺湪
         ErpPurchaseOrderDO purchaseOrder = validatePurchaseOrderExists(id);
-        if (!ErpAuditStatus.APPROVING.getStatus().equals(purchaseOrder.getStatus())) {
+        if (!ErpPurchaseOrderStatusEnum.PROCESS.getStatus().equals(purchaseOrder.getStatus())) {
             log.warn("[updatePurchaseOrderAuditStatus] 閲囪喘璁㈠崟({}) 涓嶅浜庡鎵逛腑鐘舵��", id);
             return;
         }
@@ -273,15 +327,17 @@
         }
     }
 
-    /**
-     * 鏍¢獙閲囪喘璁㈠崟瀹℃壒鍒嗙被鍜屾祦绋嬪畾涔夋槸鍚﹀瓨鍦�
-     */
-    private void validatePurchaseOrderApproveCategoryAndProcessDefinition() {
-        // 1. 鏍¢獙鍒嗙被鏄惁瀛樺湪
-        List<BpmCategoryDO> categories = bpmCategoryService.getCategoryListByCode(
-                Collections.singletonList(PURCHASE_ORDER_APPROVE_CATEGORY_CODE));
-        if (CollUtil.isEmpty(categories)) {
-            throw exception(PURCHASE_ORDER_BPM_CATEGORY_NOT_EXISTS);
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void updatePurchaseOrderAuditStatus(Long id, Integer bpmStatus, String reason) {
+        // 1. BPM 瀹℃壒缁撴灉鏄犲皠涓洪噰璐鍗曠姸鎬侊細閫氳繃=20 / 涓嶉�氳繃=30 / 鍙栨秷=40
+        Integer newStatus = convertBpmResultToAuditStatus(bpmStatus);
+        if (newStatus == null) {
+            return;
+        }
+        // 2. 浠呭綋浠嶅浜庛�屽鎵逛腑銆嶆椂鍥炲啓锛岄伩鍏嶉噸澶嶈鐩�
+        if (purchaseOrderMapper.updateAuditStatusByBpm(id, newStatus, reason) != 1) {
+            log.warn("[updatePurchaseOrderAuditStatus] 閲囪喘璁㈠崟({}) 涓嶅浜庡鎵逛腑鐘舵�侊紝蹇界暐 BPM 缁撴灉({})", id, bpmStatus);
         }
     }
 
@@ -290,11 +346,11 @@
      */
     private Integer convertBpmResultToAuditStatus(Integer bpmResult) {
         if (BpmTaskStatusEnum.APPROVE.getStatus().equals(bpmResult)) {
-            return ErpAuditStatus.APPROVE.getStatus();
+            return ErpPurchaseOrderStatusEnum.APPROVE.getStatus();
         } else if (BpmTaskStatusEnum.REJECT.getStatus().equals(bpmResult)) {
-            return ErpAuditStatus.PROCESS.getStatus();
+            return ErpPurchaseOrderStatusEnum.REJECT.getStatus();
         } else if (BpmTaskStatusEnum.CANCEL.getStatus().equals(bpmResult)) {
-            return ErpAuditStatus.CANCEL.getStatus();
+            return ErpPurchaseOrderStatusEnum.CANCEL.getStatus();
         }
         return null;
     }
@@ -380,8 +436,12 @@
             return;
         }
         purchaseOrders.forEach(purchaseOrder -> {
-            if (ErpAuditStatus.APPROVE.getStatus().equals(purchaseOrder.getStatus())) {
+            if (ErpPurchaseOrderStatusEnum.APPROVE.getStatus().equals(purchaseOrder.getStatus())) {
                 throw exception(PURCHASE_ORDER_DELETE_FAIL_APPROVE, purchaseOrder.getNo());
+            }
+            // 瀹℃壒涓姝㈠垹闄わ紝闇�鍏堢敱瀹℃壒浜恒�屽鏍镐笉閫氳繃銆嶉��鍥�
+            if (ErpPurchaseOrderStatusEnum.PROCESS.getStatus().equals(purchaseOrder.getStatus())) {
+                throw exception(PURCHASE_ORDER_UPDATE_FAIL_PROCESS, purchaseOrder.getNo());
             }
         });
 
@@ -452,6 +512,14 @@
             return Collections.emptyList();
         }
         return purchaseOrderMapper.selectByIds(ids);
+    }
+
+    @Override
+    public List<ErpPurchaseOrderDO> getPurchaseOrderListBySource(Integer sourceType, Long sourceId) {
+        if (sourceType == null || sourceId == null) {
+            return Collections.emptyList();
+        }
+        return purchaseOrderMapper.selectListBySource(sourceType, sourceId);
     }
 
     @Override
@@ -578,4 +646,104 @@
         return arrivalNoticeId;
     }
 
+    @Override
+    public ErpPurchaseOrderImportRespVO importPurchaseOrderList(List<ErpPurchaseOrderImportExcelVO> importOrders,
+                                                                ErpPurchaseOrderImportReqVO importReqVO) {
+        // 1. 鏍¢獙闈炵┖锛堣繃婊ゅ畬鍏ㄧ┖鐧界殑琛岋級
+        importOrders = filterList(importOrders, item -> StrUtil.isNotBlank(item.getProductCode()));
+        if (CollUtil.isEmpty(importOrders)) {
+            throw exception(PURCHASE_ORDER_IMPORT_LIST_IS_EMPTY);
+        }
+        // 2. 鎸夈�屽崟鍙枫�嶅垎缁勶細鐩稿悓鍗曞彿鐨勫琛屽悎骞舵垚涓�寮犻噰璐鍗曪紱鍗曞彿涓虹┖鏃舵瘡琛屽悇鑷垚涓�寮犺鍗�
+        List<List<ErpPurchaseOrderImportExcelVO>> groups = new ArrayList<>();
+        Map<String, Integer> groupIndexMap = new LinkedHashMap<>();
+        int rowIndex = 0;
+        for (ErpPurchaseOrderImportExcelVO row : importOrders) {
+            rowIndex++;
+            String groupKey = StrUtil.isNotBlank(row.getNo()) ? row.getNo() : ("__row_" + rowIndex);
+            Integer index = groupIndexMap.get(groupKey);
+            if (index == null) {
+                index = groups.size();
+                groupIndexMap.put(groupKey, index);
+                groups.add(new ArrayList<>());
+            }
+            groups.get(index).add(row);
+        }
+        // 3. 閫愮粍鍚堝苟鍒涘缓閲囪喘璁㈠崟
+        ErpPurchaseOrderImportRespVO respVO = ErpPurchaseOrderImportRespVO.builder()
+                .createPurchaseOrderNos(new ArrayList<>())
+                .failurePurchaseOrderNos(new LinkedHashMap<>())
+                .build();
+        for (List<ErpPurchaseOrderImportExcelVO> group : groups) {
+            String groupKey = StrUtil.isNotBlank(group.get(0).getNo()) ? group.get(0).getNo()
+                    : "绗�" + (importOrders.indexOf(group.get(0)) + 1) + "琛�";
+            try {
+                ErpPurchaseOrderSaveReqVO createReqVO = buildPurchaseOrderImportSaveReqVO(group);
+                Long orderId = createPurchaseOrder(createReqVO);
+                respVO.getCreatePurchaseOrderNos().add(purchaseOrderMapper.selectById(orderId).getNo());
+            } catch (ServiceException ex) {
+                respVO.getFailurePurchaseOrderNos().put(groupKey, ex.getMessage());
+            }
+        }
+        return respVO;
+    }
+
+    /**
+     * 灏嗗悓涓�寮犻噰璐鍗曠殑澶氳鏄庣粏杞崲鎴愬垱寤� Request VO
+     */
+    private ErpPurchaseOrderSaveReqVO buildPurchaseOrderImportSaveReqVO(List<ErpPurchaseOrderImportExcelVO> group) {
+        ErpPurchaseOrderImportExcelVO first = group.get(0);
+        // 1. 鏍¢獙骞惰В鏋愪緵搴斿晢
+        if (StrUtil.isBlank(first.getSupplierName())) {
+            throw exception(PURCHASE_ORDER_IMPORT_SUPPLIER_NOT_EXISTS, "");
+        }
+        ErpSupplierDO supplier = supplierMapper.selectByName(first.getSupplierName());
+        if (supplier == null) {
+            throw exception(PURCHASE_ORDER_IMPORT_SUPPLIER_NOT_EXISTS, first.getSupplierName());
+        }
+        if (first.getOrderTime() == null) {
+            throw exception(PURCHASE_ORDER_IMPORT_ORDER_TIME_REQUIRED);
+        }
+        // 2. 瑙f瀽缁撶畻璐︽埛锛堝彲閫夛級
+        Long accountId = null;
+        if (StrUtil.isNotBlank(first.getAccountName())) {
+            ErpAccountDO account = accountMapper.selectByName(first.getAccountName());
+            if (account == null) {
+                throw exception(PURCHASE_ORDER_IMPORT_ACCOUNT_NOT_EXISTS, first.getAccountName());
+            }
+            accountId = account.getId();
+        }
+        // 3. 缁勮鏄庣粏
+        List<ErpPurchaseOrderSaveReqVO.Item> items = new ArrayList<>();
+        for (ErpPurchaseOrderImportExcelVO row : group) {
+            MdmItemRespDTO product = mdmItemApi.getItemByCode(row.getProductCode()).getCheckedData();
+            if (product == null) {
+                throw exception(PURCHASE_ORDER_IMPORT_PRODUCT_NOT_EXISTS, row.getProductCode());
+            }
+            MdmUnitMeasureRespDTO unit = mdmUnitMeasureApi.getUnitMeasureByCode(row.getProductUnitCode()).getCheckedData();
+            if (unit == null) {
+                throw exception(PURCHASE_ORDER_IMPORT_UNIT_NOT_EXISTS, row.getProductUnitCode());
+            }
+            ErpPurchaseOrderSaveReqVO.Item item = new ErpPurchaseOrderSaveReqVO.Item()
+                    .setProductId(product.getId())
+                    .setProductUnitId(unit.getId())
+                    .setProductPrice(row.getProductPrice())
+                    .setCount(row.getCount())
+                    .setTaxPercent(row.getTaxPercent())
+                    .setQcCheckFlag(row.getQcCheckFlag())
+                    .setRemark(row.getItemRemark());
+            items.add(item);
+        }
+        // 4. 缁勮涓昏〃
+        ErpPurchaseOrderSaveReqVO createReqVO = new ErpPurchaseOrderSaveReqVO()
+                .setSupplierId(supplier.getId())
+                .setAccountId(accountId)
+                .setOrderTime(first.getOrderTime())
+                .setDiscountPercent(ObjectUtil.defaultIfNull(first.getDiscountPercent(), BigDecimal.ZERO))
+                .setDepositPrice(ObjectUtil.defaultIfNull(first.getDepositPrice(), BigDecimal.ZERO))
+                .setRemark(first.getRemark())
+                .setItems(items);
+        return createReqVO;
+    }
+
 }
\ No newline at end of file

--
Gitblit v1.9.3