gaoluyang
4 小时以前 7a923e32e55e9c34830690341469cc170f65092e
yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/bidbond/CrmBidBondServiceImpl.java
@@ -10,9 +10,11 @@
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;
@@ -51,6 +53,8 @@
    @Resource
    private CrmBidBondMapper bidBondMapper;
    @Resource
    private CrmBidProgressMapper bidProgressMapper;
    @Resource
    private CrmSaleQuotationMapper saleQuotationMapper;
    @Resource
    private CrmCustomerService customerService;
@@ -60,6 +64,9 @@
    @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());
@@ -68,6 +75,7 @@
        if (bond.getQuotationId() != null) {
            saleQuotationMapper.bindBidBond(bond.getQuotationId(), bond.getId(), bond.getNo());
        }
        insertProgress(bond, "CREATED", null, CrmBidResultEnum.PENDING.getResult(), null);
        return bond.getId();
    }
@@ -75,6 +83,13 @@
    @Transactional(rollbackFor = Exception.class)
    public void updateBidBond(CrmBidBondSaveReqVO updateReqVO) {
        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());
        // 单号不允许修改,保持原值
@@ -93,6 +108,10 @@
    @Transactional(rollbackFor = Exception.class)
    public void deleteBidBond(Long 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);
        }
@@ -101,6 +120,72 @@
            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
@@ -194,9 +279,9 @@
                        "中标结果为【未中标】时不能关联销售订单[" + 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();
        }
@@ -223,14 +308,10 @@
     * 校验并加载关联报价单
     */
    private CrmSaleQuotationDO validateQuotation(CrmBidBondSaveReqVO reqVO, Integer bidResult) {
        // 中标时报价单必填,保证中标后可以据此转合同
        if (CrmBidResultEnum.WON.getResult().equals(bidResult) && reqVO.getQuotationId() == null) {
            throw new ServiceException(ErrorCodeConstants.BID_BOND_WON_QUOTATION_REQUIRED.getCode(),
                    "中标结果为【已中标】时必须关联报价单,否则无法凭该报价单转合同。"
                            + "请选择本次中标的销售报价单后重新保存。");
        }
        // 投标记录必须绑定本次报价,待开标与未中标也需要保留报价关联。
        if (reqVO.getQuotationId() == null) {
            return null;
            throw new ServiceException(ErrorCodeConstants.BID_BOND_WON_QUOTATION_REQUIRED.getCode(),
                    "投标记录必须关联本次销售报价单,请选择报价单后重新保存。");
        }
        CrmSaleQuotationDO quotation = saleQuotationMapper.selectById(reqVO.getQuotationId());
        if (quotation == null) {