From 2a26a5fa027d17aa11bde0b7fdc4e80bde554c84 Mon Sep 17 00:00:00 2001
From: gaoluyang <2820782392@qq.com>
Date: 星期一, 12 十月 2026 14:58:29 +0800
Subject: [PATCH] 银川九鸿化工需求:商机状态组改为非必填

---
 yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/business/CrmBusinessServiceImpl.java |   78 +++++++++++++++++++++++++++++++++-----
 1 files changed, 67 insertions(+), 11 deletions(-)

diff --git a/yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/business/CrmBusinessServiceImpl.java b/yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/business/CrmBusinessServiceImpl.java
index 263b383..dad93f7 100644
--- a/yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/business/CrmBusinessServiceImpl.java
+++ b/yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/business/CrmBusinessServiceImpl.java
@@ -100,7 +100,11 @@
 
         // 2.1 鎻掑叆鍟嗘満
         CrmBusinessDO business = BeanUtils.toBean(createReqVO, CrmBusinessDO.class);
-        business.setStatusId(businessStatusService.getBusinessStatusListByTypeId(createReqVO.getStatusTypeId()).get(0).getId()); // 榛樿鐘舵��
+        // 鍟嗘満鐘舵�佺粍涓洪�夊~锛氭湭閫夋嫨鏃朵笉璁剧疆榛樿闃舵锛屼篃涓嶅弬涓庛�屽晢鏈虹姸鎬佺粍銆嶇淮搴︾殑缁熻
+        if (createReqVO.getStatusTypeId() != null) {
+            List<CrmBusinessStatusDO> statusList = businessStatusService.getBusinessStatusListByTypeId(createReqVO.getStatusTypeId());
+            business.setStatusId(CollUtil.isNotEmpty(statusList) ? statusList.get(0).getId() : null); // 榛樿鐘舵��
+        }
         calculateTotalPrice(business, businessProducts);
         businessMapper.insert(business);
         // 2.2 鎻掑叆鍟嗘満鍏宠仈鍟嗗搧
@@ -134,8 +138,9 @@
         updateReqVO.setOwnerUserId(null).setStatusTypeId(null); // 涓嶅厑璁告洿鏂扮殑瀛楁
         // 1.1 鏍¢獙瀛樺湪
         CrmBusinessDO oldBusiness = validateBusinessExists(updateReqVO.getId());
-        // 1.2 鏍¢獙浜у搧椤圭殑鏈夋晥鎬�
-        List<CrmBusinessProductDO> businessProducts = validateBusinessProducts(updateReqVO.getProducts());
+        // 1.2 鏍¢獙浜у搧椤圭殑鏈夋晥鎬э紙浼犲叆鏃т骇鍝佸垪琛ㄧ敤浜庡悎骞� null 瀛楁锛�
+        List<CrmBusinessProductDO> oldProducts = businessProductMapper.selectListByBusinessId(updateReqVO.getId());
+        List<CrmBusinessProductDO> businessProducts = validateBusinessProducts(updateReqVO.getProducts(), oldProducts);
         // 1.3 鏍¢獙鍏宠仈瀛楁
         validateRelationDataExists(updateReqVO);
 
@@ -147,7 +152,7 @@
         updateBusinessProduct(updateObj.getId(), businessProducts);
 
         // 3. 璁板綍鎿嶄綔鏃ュ織涓婁笅鏂�
-        updateReqVO.setOwnerUserId(oldBusiness.getOwnerUserId()); // 閬垮厤鎿嶄綔鏃ュ織鍑虹幇鈥滃垹闄よ礋璐d汉鈥濈殑鎯呭喌
+        updateReqVO.setOwnerUserId(oldBusiness.getOwnerUserId()); // 閬垮厤鎿嶄綔鏃ュ織鍑虹幇"鍒犻櫎璐熻矗浜�"鐨勬儏鍐�
         LogRecordContext.putVariable(DiffParseFunction.OLD_OBJECT, BeanUtils.toBean(oldBusiness, CrmBusinessSaveReqVO.class));
         LogRecordContext.putVariable("businessName", oldBusiness.getName());
     }
@@ -216,6 +221,18 @@
     }
 
     private List<CrmBusinessProductDO> validateBusinessProducts(List<CrmBusinessSaveReqVO.BusinessProduct> list) {
+        return validateBusinessProducts(list, Collections.emptyList());
+    }
+
+    /**
+     * 鏍¢獙浜у搧椤圭殑鏈夋晥鎬э紙鏇存柊鍦烘櫙鍚堝苟鏃ф暟鎹級
+     *
+     * @param list       鍓嶇浼犲叆鐨勪骇鍝佸垪琛�
+     * @param oldProducts 宸叉湁浜у搧鍒楄〃锛堢敤浜庡悎骞� null 瀛楁锛�
+     * @return 澶勭悊鍚庣殑浜у搧 DO 鍒楄〃
+     */
+    private List<CrmBusinessProductDO> validateBusinessProducts(List<CrmBusinessSaveReqVO.BusinessProduct> list,
+                                                                  List<CrmBusinessProductDO> oldProducts) {
         // 1. 鏍¢獙鐗╂枡瀛樺湪
         Set<Long> itemIds = convertSet(list, CrmBusinessSaveReqVO.BusinessProduct::getItemId);
         Map<Long, MdmItemRespDTO> itemMap = mdmItemApi.getItemMap(itemIds);
@@ -224,15 +241,50 @@
                 throw exception(PRODUCT_NOT_EXISTS);
             }
         }
-        // 2. 杞寲涓� CrmBusinessProductDO 鍒楄〃
-        return convertList(list, o -> BeanUtils.toBean(o, CrmBusinessProductDO.class,
-                item -> item.setTotalPrice(MoneyUtils.priceMultiply(item.getBusinessPrice(), item.getCount()))));
+        // 2. 鏋勫缓鏃т骇鍝� Map锛堟寜 id 绱㈠紩锛�
+        Map<Long, CrmBusinessProductDO> oldProductMap = convertMap(oldProducts, CrmBusinessProductDO::getId);
+        // 3. 杞寲涓� CrmBusinessProductDO 鍒楄〃锛堝悎骞舵棫鏁版嵁锛�
+        return convertList(list, o -> {
+            CrmBusinessProductDO product = BeanUtils.toBean(o, CrmBusinessProductDO.class);
+            // 鏇存柊鍦烘櫙锛氬鏋滀骇鍝佸凡鏈� id 涓旀湁鏃ф暟鎹紝鍒欏悎骞� null 瀛楁
+            if (o.getId() != null && oldProductMap.containsKey(o.getId())) {
+                CrmBusinessProductDO old = oldProductMap.get(o.getId());
+                if (product.getBusinessPrice() == null) {
+                    product.setBusinessPrice(old.getBusinessPrice());
+                }
+                if (product.getItemPrice() == null) {
+                    product.setItemPrice(old.getItemPrice());
+                }
+                if (product.getCount() == null) {
+                    product.setCount(old.getCount());
+                }
+                if (product.getItemUnitId() == null) {
+                    product.setItemUnitId(old.getItemUnitId());
+                }
+            } else {
+                // 鏂颁骇鍝侊細娓呴櫎鍓嶇鐢熸垚鐨勪复鏃� ID锛岃鏁版嵁搴� AUTO_INCREMENT 鎺ョ
+                product.setId(null);
+            }
+            // 浼樺厛浣跨敤鍓嶇浼犲叆鐨� totalPrice锛屽惁鍒欓�氳繃 businessPrice * count 璁$畻
+            if (o.getTotalPrice() != null) {
+                product.setTotalPrice(o.getTotalPrice());
+            } else {
+                product.setTotalPrice(MoneyUtils.priceMultiply(product.getBusinessPrice(), product.getCount()));
+            }
+            return product;
+        });
     }
 
     private void calculateTotalPrice(CrmBusinessDO business, List<CrmBusinessProductDO> businessProducts) {
-        business.setTotalProductPrice(getSumValue(businessProducts, CrmBusinessProductDO::getTotalPrice, BigDecimal::add, BigDecimal.ZERO));
-        BigDecimal discountPrice = MoneyUtils.priceMultiplyPercent(business.getTotalProductPrice(), business.getDiscountPercent());
-        business.setTotalPrice(business.getTotalProductPrice().subtract(discountPrice));
+        // totalProductPrice锛氫紭鍏堜娇鐢ㄥ墠绔紶鍏ュ�硷紝鍚﹀垯浠庝骇鍝佹眹鎬�
+        if (business.getTotalProductPrice() == null) {
+            business.setTotalProductPrice(getSumValue(businessProducts, CrmBusinessProductDO::getTotalPrice, BigDecimal::add, BigDecimal.ZERO));
+        }
+        // totalPrice锛氫紭鍏堜娇鐢ㄥ墠绔紶鍏ュ�硷紝鍚﹀垯鎸夋姌鎵h绠�
+        if (business.getTotalPrice() == null) {
+            BigDecimal discountPrice = MoneyUtils.priceMultiplyPercent(business.getTotalProductPrice(), business.getDiscountPercent());
+            business.setTotalPrice(business.getTotalProductPrice().subtract(discountPrice));
+        }
     }
 
     @Override
@@ -249,6 +301,10 @@
         // 1.3 鏍¢獙鍟嗘満鐘舵��
         CrmBusinessStatusDO status = null;
         if (reqVO.getStatusId() != null) {
+            // 鍟嗘満鐘舵�佺粍涓洪�夊~锛氭湭璁剧疆鐘舵�佺粍鐨勫晢鏈烘病鏈夊彲閫夐樁娈碉紝涓嶅厑璁稿彉鏇淬�屽晢鏈洪樁娈点��
+            if (business.getStatusTypeId() == null) {
+                throw exception(BUSINESS_UPDATE_STATUS_FAIL_NO_STATUS_TYPE, business.getName());
+            }
             status = businessStatusService.validateBusinessStatus(business.getStatusTypeId(), reqVO.getStatusId());
         }
         // 1.4 鏍¢獙鏄笉鏄姸鎬佹病鍙樻洿
@@ -264,7 +320,7 @@
         // 3. 璁板綍鎿嶄綔鏃ュ織涓婁笅鏂�
         LogRecordContext.putVariable("businessName", business.getName());
         LogRecordContext.putVariable("oldStatusName", getBusinessStatusName(business.getEndStatus(),
-                businessStatusService.getBusinessStatus(business.getStatusId())));
+                business.getStatusId() != null ? businessStatusService.getBusinessStatus(business.getStatusId()) : null));
         LogRecordContext.putVariable("newStatusName", getBusinessStatusName(reqVO.getEndStatus(), status));
     }
 

--
Gitblit v1.9.3