yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/quotation/CrmSaleQuotationServiceImpl.java
@@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.crm.service.quotation;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
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;
@@ -23,8 +24,10 @@
import cn.iocoder.yudao.module.crm.api.customer.CrmCustomerApi;
import cn.iocoder.yudao.module.crm.service.permission.CrmPermissionService;
import cn.iocoder.yudao.module.crm.service.permission.bo.CrmPermissionCreateReqBO;
import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi;
import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi;
import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@@ -41,7 +44,7 @@
/**
 * CRM 销售报价单 Service 实现类
 *
 * @author 芋道源码
 * @author 超级管理员
 */
@Service
@Validated
@@ -70,6 +73,8 @@
    @Resource
    private CrmPermissionService permissionService;
    @Resource
    private StorageAttachmentApi storageAttachmentApi;
    @Resource
    private CrmContractService contractService;
    @Override
@@ -77,11 +82,11 @@
    public Long createSaleQuotation(CrmSaleQuotationSaveReqVO createReqVO, Long userId) {
        // 1.1 校验物料项的有效性
        List<CrmSaleQuotationProductDO> quotationProducts = validateQuotationItems(createReqVO.getItems());
        // 1.2 生成报价单号,并校验唯一性
        String no = noRedisDAO.generate(CrmNoRedisDAO.SALE_QUOTATION_NO_PREFIX);
        if (saleQuotationMapper.selectByNo(no) != null) {
            throw exception(SALE_QUOTATION_NO_EXISTS);
        }
        // 1.2 确定报价单号(前端填写则用自定义单号,留空则自动生成),并校验唯一性
        String customNo = StrUtil.trim(createReqVO.getNo());
        String no = StrUtil.isBlank(customNo)
                ? noRedisDAO.generate(CrmNoRedisDAO.SALE_QUOTATION_NO_PREFIX) : customNo;
        validateQuotationNoUnique(no, null);
        // 1.3 校验关联字段
        validateRelationDataExists(createReqVO);
@@ -100,6 +105,8 @@
                .setBizType(CrmBizTypeEnum.CRM_SALE_QUOTATION.getType())
                .setBizId(saleQuotation.getId())
                .setLevel(CrmPermissionLevelEnum.OWNER.getLevel()));
        storageAttachmentApi.bindAttachments("file", "crm_sale_quotation",
                saleQuotation.getId(), createReqVO.getBlobIds());
        return saleQuotation.getId();
    }
@@ -117,12 +124,20 @@
        // 1.4 校验关联字段
        validateRelationDataExists(updateReqVO);
        // 2.1 更新报价单
        // 2.1 更新报价单(报价单号留空表示保持原值,填写则校验唯一性后替换)
        CrmSaleQuotationDO updateObj = BeanUtils.toBean(updateReqVO, CrmSaleQuotationDO.class);
        String no = StrUtil.trim(updateReqVO.getNo());
        if (StrUtil.isBlank(no)) {
            updateObj.setNo(saleQuotation.getNo());
        } else {
            validateQuotationNoUnique(no, saleQuotation.getId());
            updateObj.setNo(no);
        }
        calculateTotalPrice(updateObj, quotationProducts);
        saleQuotationMapper.updateById(updateObj);
        // 2.2 更新报价明细
        updateQuotationProductList(updateReqVO.getId(), quotationProducts);
        storageAttachmentApi.updateAttachments("file", "crm_sale_quotation", updateReqVO.getId(), updateReqVO.getBlobIds());
    }
    @Override
@@ -141,6 +156,8 @@
        // 2. 遍历删除
        saleQuotations.forEach(saleQuotation -> {
            // 2.0 删除附件关联
            storageAttachmentApi.deleteAttachmentsByRecord("crm_sale_quotation", saleQuotation.getId());
            // 2.1 删除报价单
            saleQuotationMapper.deleteById(saleQuotation.getId());
            // 2.2 删除报价明细
@@ -156,6 +173,19 @@
            throw exception(SALE_QUOTATION_NOT_EXISTS);
        }
        return saleQuotation;
    }
    /**
     * 校验报价单号唯一(含已逻辑删除的报价单,因 uk_no 唯一索引覆盖已删除行)
     *
     * @param no       报价单号
     * @param selfId   自身编号(修改时排除自己,新增传 null)
     */
    private void validateQuotationNoUnique(String no, Long selfId) {
        CrmSaleQuotationDO exists = saleQuotationMapper.selectAnyByNo(no);
        if (exists != null && !exists.getId().equals(selfId)) {
            throw exception(SALE_QUOTATION_NO_EXISTS, no);
        }
    }
    private List<CrmSaleQuotationProductDO> validateQuotationItems(List<CrmSaleQuotationSaveReqVO.Item> list) {
@@ -310,4 +340,14 @@
        return contractId;
    }
    @Override
    public CrmSaleQuotationDO getByContractId(Long id) {
        return saleQuotationMapper.selectOne(new LambdaQueryWrapper<CrmSaleQuotationDO>().eq(CrmSaleQuotationDO::getContractId,id));
    }
    @Override
    public void clearContractReference(Long quotationId) {
        saleQuotationMapper.clearContractReference(quotationId);
    }
}