gaoluyang
4 小时以前 7a923e32e55e9c34830690341469cc170f65092e
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);
        // 报价单变化时:先解绑旧报价单,再绑定新报价单
        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);
        }
        // 解除报价单绑定后再删除
        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())
                            + "],同一报价单只能绑定一张投标。请改选其他报价单,或先在该投标中解除关联。");
        }
        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 "未知";