From 7a923e32e55e9c34830690341469cc170f65092e Mon Sep 17 00:00:00 2001
From: gaoluyang <2820782392@qq.com>
Date: 星期一, 16 十一月 2026 09:32:35 +0800
Subject: [PATCH] feat: 完善销售和投标业务流程

---
 yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/bidbond/CrmBidBondServiceImpl.java |  189 +++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 183 insertions(+), 6 deletions(-)

diff --git a/yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/bidbond/CrmBidBondServiceImpl.java b/yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/bidbond/CrmBidBondServiceImpl.java
index 05f3e7c..1088a2a 100644
--- a/yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/bidbond/CrmBidBondServiceImpl.java
+++ b/yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/bidbond/CrmBidBondServiceImpl.java
@@ -10,11 +10,16 @@
 import cn.iocoder.yudao.module.crm.controller.admin.bidbond.vo.CrmBidBondSaleOrderRespVO;
 import cn.iocoder.yudao.module.crm.controller.admin.bidbond.vo.CrmBidBondSaveReqVO;
 import cn.iocoder.yudao.module.crm.dal.dataobject.bidbond.CrmBidBondDO;
+import cn.iocoder.yudao.module.crm.dal.dataobject.bidbond.CrmBidProgressDO;
 import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO;
+import cn.iocoder.yudao.module.crm.dal.dataobject.quotation.CrmSaleQuotationDO;
 import cn.iocoder.yudao.module.crm.dal.mysql.bidbond.CrmBidBondMapper;
+import cn.iocoder.yudao.module.crm.dal.mysql.bidbond.CrmBidProgressMapper;
+import cn.iocoder.yudao.module.crm.dal.mysql.quotation.CrmSaleQuotationMapper;
 import cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants;
 import cn.iocoder.yudao.module.crm.enums.bidbond.CrmBidBondRefundStatusEnum;
 import cn.iocoder.yudao.module.crm.enums.bidbond.CrmBidResultEnum;
+import cn.iocoder.yudao.module.crm.enums.quotation.CrmSaleQuotationStatusEnum;
 import cn.iocoder.yudao.module.crm.service.customer.CrmCustomerService;
 import cn.iocoder.yudao.module.erp.api.sale.ErpSaleOrderApi;
 import cn.iocoder.yudao.module.erp.api.sale.dto.ErpSaleOrderRespDTO;
@@ -48,6 +53,10 @@
     @Resource
     private CrmBidBondMapper bidBondMapper;
     @Resource
+    private CrmBidProgressMapper bidProgressMapper;
+    @Resource
+    private CrmSaleQuotationMapper saleQuotationMapper;
+    @Resource
     private CrmCustomerService customerService;
     @Resource
     private ErpSaleOrderApi erpSaleOrderApi;
@@ -55,28 +64,128 @@
     @Override
     @Transactional(rollbackFor = Exception.class)
     public Long createBidBond(CrmBidBondSaveReqVO createReqVO) {
+        if (!CrmBidResultEnum.PENDING.getResult().equals(createReqVO.getBidResult())) {
+            throw new ServiceException(400, "鏂版姇鏍囧繀椤诲厛鐧昏涓哄緟寮�鏍囷紝璇峰湪杩囩▼璺熻繘涓櫥璁扮粨鏋�");
+        }
         CrmBidBondDO bond = validateAndBuild(createReqVO);
         bond.setId(null);
         bond.setNo(generateBondNo());
         bidBondMapper.insert(bond);
+        // 鍥炲啓鎶ヤ环鍗曠粦瀹氬叧绯�
+        if (bond.getQuotationId() != null) {
+            saleQuotationMapper.bindBidBond(bond.getQuotationId(), bond.getId(), bond.getNo());
+        }
+        insertProgress(bond, "CREATED", null, CrmBidResultEnum.PENDING.getResult(), null);
         return bond.getId();
     }
 
     @Override
     @Transactional(rollbackFor = Exception.class)
     public void updateBidBond(CrmBidBondSaveReqVO updateReqVO) {
-        validateBidBondExists(updateReqVO.getId());
+        CrmBidBondDO oldBond = validateBidBondExists(updateReqVO.getId());
+        if (!Objects.equals(oldBond.getBidResult(), updateReqVO.getBidResult())) {
+            throw new ServiceException(400, "涓爣缁撴灉鍙兘閫氳繃鎶曟爣杩囩▼璺熻繘鐧昏");
+        }
+        if (CrmBidResultEnum.WON.getResult().equals(oldBond.getBidResult())
+                && !Objects.equals(oldBond.getQuotationId(), updateReqVO.getQuotationId())) {
+            throw new ServiceException(400, "宸蹭腑鏍囨姇鏍囦笉鑳芥洿鎹㈡姤浠峰崟");
+        }
         CrmBidBondDO updateObj = validateAndBuild(updateReqVO);
         updateObj.setId(updateReqVO.getId());
         // 鍗曞彿涓嶅厑璁镐慨鏀癸紝淇濇寔鍘熷��
         updateObj.setNo(null);
         bidBondMapper.updateByIdIncludingNulls(updateObj);
+        // 鎶ヤ环鍗曞彉鍖栨椂锛氬厛瑙g粦鏃ф姤浠峰崟锛屽啀缁戝畾鏂版姤浠峰崟
+        if (oldBond.getQuotationId() != null && !oldBond.getQuotationId().equals(updateObj.getQuotationId())) {
+            saleQuotationMapper.bindBidBond(oldBond.getQuotationId(), null, null);
+        }
+        if (updateObj.getQuotationId() != null && !updateObj.getQuotationId().equals(oldBond.getQuotationId())) {
+            saleQuotationMapper.bindBidBond(updateObj.getQuotationId(), updateObj.getId(), oldBond.getNo());
+        }
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public void deleteBidBond(Long id) {
-        validateBidBondExists(id);
+        CrmBidBondDO bond = validateBidBondExists(id);
+        if (!CrmBidResultEnum.PENDING.getResult().equals(bond.getBidResult())
+                || bidProgressMapper.selectByBidBondId(id).size() > 1) {
+            throw new ServiceException(400, "宸叉湁鎶曟爣杩囩▼璁板綍锛屼笉鑳藉垹闄�");
+        }
+        if (CrmBidResultEnum.WON.getResult().equals(bond.getBidResult()) && bond.getQuotationId() != null) {
+            throw ServiceExceptionUtil.exception(ErrorCodeConstants.BID_BOND_DELETE_FAIL_WON);
+        }
+        // 瑙i櫎鎶ヤ环鍗曠粦瀹氬悗鍐嶅垹闄�
+        if (bond.getQuotationId() != null) {
+            saleQuotationMapper.bindBidBond(bond.getQuotationId(), null, null);
+        }
         bidBondMapper.deleteById(id);
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void recordProgress(Long id, String action, String remark) {
+        CrmBidBondDO bond = bidBondMapper.selectByIdForUpdate(id);
+        if (bond == null) throw new ServiceException(400, "鎶曟爣璁板綍涓嶅瓨鍦�");
+        int from = bond.getBidResult();
+        int target;
+        switch (action) {
+            case "FOLLOW_UP" -> {
+                if (from == CrmBidResultEnum.WON.getResult()) throw new ServiceException(400, "宸蹭腑鏍囨姇鏍囦笉鑳界户缁窡杩涘紑鏍囪繃绋�");
+                target = from;
+            }
+            case "WON" -> {
+                if (from != CrmBidResultEnum.PENDING.getResult()) throw new ServiceException(400, "鍙湁寰呭紑鏍囨姇鏍囧彲浠ョ櫥璁颁腑鏍�");
+                target = CrmBidResultEnum.WON.getResult();
+            }
+            case "LOST" -> {
+                if (from != CrmBidResultEnum.PENDING.getResult()) throw new ServiceException(400, "鍙湁寰呭紑鏍囨姇鏍囧彲浠ョ櫥璁版湭涓爣");
+                target = CrmBidResultEnum.LOST.getResult();
+            }
+            case "REBID" -> {
+                if (from != CrmBidResultEnum.LOST.getResult()) throw new ServiceException(400, "鍙湁鏈腑鏍囨姇鏍囧彲浠ラ噸鏂版姇鏍�");
+                CrmSaleQuotationDO quotation = saleQuotationMapper.selectById(bond.getQuotationId());
+                List<CrmBidProgressDO> history = bidProgressMapper.selectByBidBondId(id);
+                int lastRound = history.stream().filter(e -> "LOST".equals(e.getAction())
+                                || ("IMPORTED".equals(e.getAction()) && Objects.equals(e.getToResult(), CrmBidResultEnum.LOST.getResult())))
+                        .map(CrmBidProgressDO::getQuotationRound).filter(Objects::nonNull)
+                        .max(Integer::compareTo).orElse(0);
+                if (quotation == null || quotation.getRevisionNo() == null || quotation.getRevisionNo() <= lastRound) {
+                    throw new ServiceException(400, "璇峰厛瀹屾垚涓嬩竴杞姤浠凤紝鍐嶉噸鏂版姇鏍�");
+                }
+                target = CrmBidResultEnum.PENDING.getResult();
+            }
+            default -> throw new ServiceException(400, "涓嶆敮鎸佺殑鎶曟爣杩囩▼鍔ㄤ綔");
+        }
+        if (remark == null || remark.isBlank()) throw new ServiceException(400, "璇峰~鍐欐湰娆¤窡杩涜鏄�");
+        if (remark.length() > 500) throw new ServiceException(400, "璺熻繘璇存槑涓嶈兘瓒呰繃500瀛�");
+        CrmSaleQuotationDO quotation = saleQuotationMapper.selectById(bond.getQuotationId());
+        bond.setBidResult(target);
+        bond.setQuotationRound(quotation != null ? quotation.getRevisionNo() : bond.getQuotationRound());
+        if (target == CrmBidResultEnum.WON.getResult()) {
+            bond.setRefundStatus(CrmBidBondRefundStatusEnum.NO_NEED.getStatus());
+            bond.setRefundAmount(null);
+            bond.setRefundDate(null);
+        }
+        bidBondMapper.updateByIdIncludingNulls(bond);
+        insertProgress(bond, action, from, target, remark);
+    }
+
+    @Override
+    public List<CrmBidProgressDO> getProgressList(Long id) {
+        validateBidBondExists(id);
+        return bidProgressMapper.selectByBidBondId(id);
+    }
+
+    private void insertProgress(CrmBidBondDO bond, String action, Integer from, Integer to, String remark) {
+        CrmBidProgressDO event = new CrmBidProgressDO();
+        event.setBidBondId(bond.getId());
+        event.setAction(action);
+        event.setFromResult(from);
+        event.setToResult(to);
+        event.setQuotationRound(bond.getQuotationRound());
+        event.setRemark(remark);
+        bidProgressMapper.insert(event);
     }
 
     @Override
@@ -170,14 +279,17 @@
                         "涓爣缁撴灉涓恒�愭湭涓爣銆戞椂涓嶈兘鍏宠仈閿�鍞鍗昜" + order.getNo()
                                 + "]銆傚宸蹭腑鏍囷紝璇峰厛灏嗕腑鏍囩粨鏋滄敼涓恒�愬凡涓爣銆戯紱濡傜‘鏈腑鏍囷紝璇锋竻绌哄叧鑱旈攢鍞鍗曘��");
             }
-            // 閫変腑閿�鍞鍗曡鏄庡凡涓爣锛屽緟寮�鏍囨椂鑷姩缃负宸蹭腑鏍�
             if (CrmBidResultEnum.PENDING.getResult().equals(bidResult)) {
-                bidResult = CrmBidResultEnum.WON.getResult();
+                throw new ServiceException(ErrorCodeConstants.BID_BOND_SALE_ORDER_NOT_APPROVE.getCode(),
+                        "寰呭紑鏍囨椂涓嶈兘鍏宠仈閿�鍞鍗曪紝璇峰厛鏄庣‘涓爣缁撴灉銆�");
             }
             saleOrderNo = order.getNo();
         }
 
-        // 5. 閫�鍥炵姸鎬佷笌閲戦
+        // 5. 鍏宠仈鎶ヤ环鍗�
+        CrmSaleQuotationDO quotation = validateQuotation(reqVO, bidResult);
+
+        // 6. 閫�鍥炵姸鎬佷笌閲戦
         Integer refundStatus = resolveRefundStatus(reqVO.getRefundStatus(), bidResult);
         validateRefund(reqVO, bondAmount, refundStatus);
 
@@ -186,7 +298,49 @@
         bond.setBidResult(bidResult);
         bond.setRefundStatus(refundStatus);
         bond.setSaleOrderNo(saleOrderNo);
+        bond.setQuotationNo(quotation != null ? quotation.getNo() : null);
+        bond.setQuotationRound(quotation != null && quotation.getRevisionNo() != null
+                ? quotation.getRevisionNo() : 0);
         return bond;
+    }
+
+    /**
+     * 鏍¢獙骞跺姞杞藉叧鑱旀姤浠峰崟
+     */
+    private CrmSaleQuotationDO validateQuotation(CrmBidBondSaveReqVO reqVO, Integer bidResult) {
+        // 鎶曟爣璁板綍蹇呴』缁戝畾鏈鎶ヤ环锛屽緟寮�鏍囦笌鏈腑鏍囦篃闇�瑕佷繚鐣欐姤浠峰叧鑱斻��
+        if (reqVO.getQuotationId() == null) {
+            throw new ServiceException(ErrorCodeConstants.BID_BOND_WON_QUOTATION_REQUIRED.getCode(),
+                    "鎶曟爣璁板綍蹇呴』鍏宠仈鏈閿�鍞姤浠峰崟锛岃閫夋嫨鎶ヤ环鍗曞悗閲嶆柊淇濆瓨銆�");
+        }
+        CrmSaleQuotationDO quotation = saleQuotationMapper.selectById(reqVO.getQuotationId());
+        if (quotation == null) {
+            throw new ServiceException(ErrorCodeConstants.BID_BOND_QUOTATION_NOT_EXISTS.getCode(),
+                    "鍏宠仈鎶ヤ环鍗�(ID=" + reqVO.getQuotationId() + ")涓嶅瓨鍦紝璇峰埛鏂版姤浠峰崟鍒楄〃鍚庨噸鏂伴�夋嫨銆�");
+        }
+        if (!Objects.equals(reqVO.getCustomerId(), quotation.getCustomerId())) {
+            throw new ServiceException(ErrorCodeConstants.BID_BOND_QUOTATION_CUSTOMER_MISMATCH.getCode(),
+                    "鍏宠仈鎶ヤ环鍗昜" + quotation.getNo() + "]鐨勫鎴�(ID=" + quotation.getCustomerId()
+                            + ")涓庝繚璇侀噾瀹㈡埛(ID=" + reqVO.getCustomerId()
+                            + ")涓嶄竴鑷淬�傝鏀归�夎瀹㈡埛鍚嶄笅鐨勬姤浠峰崟锛屾垨鍏堣皟鏁翠繚璇侀噾瀹㈡埛銆�");
+        }
+        if (!Set.of(CrmSaleQuotationStatusEnum.DRAFT.getStatus(),
+                CrmSaleQuotationStatusEnum.APPROVE.getStatus(),
+                CrmSaleQuotationStatusEnum.REJECT.getStatus()).contains(quotation.getStatus())) {
+            throw new ServiceException(ErrorCodeConstants.BID_BOND_QUOTATION_STATUS_INVALID.getCode(),
+                    "鍏宠仈鎶ヤ环鍗昜" + quotation.getNo() + "]褰撳墠鐘舵�佷负銆�"
+                            + getQuotationStatusName(quotation.getStatus())
+                            + "銆戯紝鍙湁銆岃崏绋裤�佸鎵归�氳繃銆佸鎵逛笉閫氳繃銆嶇殑鎶ヤ环鍗曞彲浠ョ粦瀹氭姇鏍囥��"
+                            + "璇锋敼閫夊叾浠栨姤浠峰崟锛屾垨鍏堥噸鏂版姤浠峰悗鍐嶇粦瀹氥��");
+        }
+        if (quotation.getBidBondId() != null && !Objects.equals(quotation.getBidBondId(), reqVO.getId())) {
+            CrmBidBondDO boundBond = bidBondMapper.selectById(quotation.getBidBondId());
+            throw new ServiceException(ErrorCodeConstants.BID_BOND_QUOTATION_ALREADY_BOUND.getCode(),
+                    "鍏宠仈鎶ヤ环鍗昜" + quotation.getNo() + "]宸茬粦瀹氭姇鏍嘯"
+                            + (boundBond != null ? boundBond.getNo() : "ID=" + quotation.getBidBondId())
+                            + "]锛屽悓涓�鎶ヤ环鍗曞彧鑳界粦瀹氫竴寮犳姇鏍囥�傝鏀归�夊叾浠栨姤浠峰崟锛屾垨鍏堝湪璇ユ姇鏍囦腑瑙i櫎鍏宠仈銆�");
+        }
+        return quotation;
     }
 
     private Integer resolveRefundStatus(Integer refundStatus, Integer bidResult) {
@@ -281,6 +435,7 @@
         Map<Long, CrmCustomerDO> customerMap = customerIds.isEmpty() ? Map.of()
                 : customerService.getCustomerMap(customerIds);
         Map<Long, ErpSaleOrderRespDTO> orderMap = loadSaleOrderMap(bonds);
+        Map<Long, CrmSaleQuotationDO> quotationMap = loadQuotationMap(bonds);
 
         return bonds.stream().map(bond -> {
             CrmBidBondRespVO respVO = BeanUtils.toBean(bond, CrmBidBondRespVO.class);
@@ -288,13 +443,30 @@
             if (customer != null) {
                 respVO.setCustomerName(customer.getName());
             }
-            ErpSaleOrderRespDTO order = orderMap.get(bond.getSaleOrderId());
+            ErpSaleOrderRespDTO order = bond.getSaleOrderId() == null ? null : orderMap.get(bond.getSaleOrderId());
             if (order != null) {
                 respVO.setSaleOrderNo(order.getNo());
                 respVO.setSaleOrderAmount(order.getTotalPrice());
             }
+            CrmSaleQuotationDO quotation = bond.getQuotationId() == null ? null
+                    : quotationMap.get(bond.getQuotationId());
+            if (quotation != null) {
+                respVO.setQuotationNo(quotation.getNo());
+                respVO.setQuotationStatus(quotation.getStatus());
+                respVO.setQuotationTotalPrice(quotation.getTotalPrice());
+            }
             return respVO;
         }).collect(Collectors.toList());
+    }
+
+    private Map<Long, CrmSaleQuotationDO> loadQuotationMap(List<CrmBidBondDO> bonds) {
+        Set<Long> quotationIds = bonds.stream().map(CrmBidBondDO::getQuotationId)
+                .filter(Objects::nonNull).collect(Collectors.toSet());
+        if (quotationIds.isEmpty()) {
+            return Map.of();
+        }
+        return saleQuotationMapper.selectByIds(quotationIds).stream()
+                .collect(Collectors.toMap(CrmSaleQuotationDO::getId, q -> q, (a, b) -> a));
     }
 
     /**
@@ -324,6 +496,11 @@
         return orderMap;
     }
 
+    private String getQuotationStatusName(Integer status) {
+        CrmSaleQuotationStatusEnum statusEnum = CrmSaleQuotationStatusEnum.getByStatus(status);
+        return statusEnum != null ? statusEnum.getName() : "鏈煡鐘舵��(" + status + ")";
+    }
+
     private String getSaleOrderStatusName(Integer status) {
         if (status == null) {
             return "鏈煡";

--
Gitblit v1.9.3