liu
2026-09-04 afd32af33acb4ab6f8e890c817d012cad75639c7
yudao-module-srm/src/main/java/cn/iocoder/yudao/module/srm/service/tender/SrmTenderProjectServiceImpl.java
@@ -7,6 +7,7 @@
import cn.iocoder.yudao.module.erp.api.purchase.dto.ErpPurchaseOrderCreateReqDTO;
import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi;
import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO;
import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi;
import cn.iocoder.yudao.module.srm.controller.admin.tender.vo.SrmTenderMaterialSaveReqVO;
import cn.iocoder.yudao.module.srm.controller.admin.tender.vo.SrmTenderProjectPageReqVO;
import cn.iocoder.yudao.module.srm.controller.admin.tender.vo.SrmTenderProjectPartialUpdateReqVO;
@@ -18,6 +19,7 @@
import cn.iocoder.yudao.module.srm.enums.BidStatusEnum;
import cn.iocoder.yudao.module.srm.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.srm.enums.TenderStatusEnum;
import cn.iocoder.yudao.module.srm.service.apply.SrmSupplierApplyService;
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import com.mzt.logapi.context.LogRecordContext;
import com.mzt.logapi.starter.annotation.LogRecord;
@@ -41,6 +43,11 @@
@Slf4j
public class SrmTenderProjectServiceImpl implements SrmTenderProjectService {
    /**
     * 招标项目资质要求附件的业务记录类型,对应 {@link cn.iocoder.yudao.module.system.enums.storage.StorageRecordTypeEnum#SRM_TENDER_PROJECT}
     */
    public static final String TENDER_PROJECT_RECORD_TYPE = "srm_tender_project";
    @Resource
    private SrmTenderProjectMapper tenderProjectMapper;
    @Resource
@@ -60,8 +67,13 @@
    private MdmItemApi mdmItemApi;
    @Resource
    private ErpPurchaseOrderApi erpPurchaseOrderApi;
    @Resource
    private SrmSupplierApplyService supplierApplyService;
    @Resource
    private StorageAttachmentApi storageAttachmentApi;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createTenderProject(SrmTenderProjectSaveReqVO createReqVO) {
        SrmTenderProjectDO project = BeanUtils.toBean(createReqVO, SrmTenderProjectDO.class);
        project.setTenderStatus(TenderStatusEnum.DRAFT.getCode());
@@ -69,10 +81,15 @@
            project.setBiddingMode(tenderConfigService.getConfig().getDefaultBiddingMode());
        }
        tenderProjectMapper.insert(project);
        // 绑定资质要求附件
        if (CollUtil.isNotEmpty(createReqVO.getBlobIds())) {
            storageAttachmentApi.bindAttachments("file", TENDER_PROJECT_RECORD_TYPE, project.getId(), createReqVO.getBlobIds());
        }
        return project.getId();
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    @LogRecord(type = LOG_TENDER_TYPE, subType = LOG_TENDER_UPDATE_SUB_TYPE,
            bizNo = "{{#updateReqVO.id}}", success = LOG_TENDER_UPDATE_SUCCESS)
    public void updateTenderProject(SrmTenderProjectSaveReqVO updateReqVO) {
@@ -84,6 +101,8 @@
            SrmTenderProjectDO updateObj = BeanUtils.toBean(updateReqVO, SrmTenderProjectDO.class);
            updateObj.setTenderStatus(status); // 状态不允许前端传入覆盖
            tenderProjectMapper.updateById(updateObj);
            // 增量更新资质要求附件
            storageAttachmentApi.updateAttachments("file", TENDER_PROJECT_RECORD_TYPE, updateReqVO.getId(), updateReqVO.getBlobIds());
            LogRecordContext.putVariable("tenderName", project.getTenderName());
            LogRecordContext.putVariable("oldTenderDesc", project.getTenderDesc());
            LogRecordContext.putVariable("newTenderDesc", updateReqVO.getTenderDesc());
@@ -99,6 +118,8 @@
            updateObj.setTenderDesc(updateReqVO.getTenderDesc());
            updateObj.setQualificationRequirements(updateReqVO.getQualificationRequirements());
            tenderProjectMapper.updateById(updateObj);
            // 增量更新资质要求附件
            storageAttachmentApi.updateAttachments("file", TENDER_PROJECT_RECORD_TYPE, updateReqVO.getId(), updateReqVO.getBlobIds());
            LogRecordContext.putVariable("tenderName", project.getTenderName());
            LogRecordContext.putVariable("oldTenderDesc", project.getTenderDesc());
            LogRecordContext.putVariable("newTenderDesc", updateReqVO.getTenderDesc());
@@ -112,6 +133,7 @@
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTenderProject(Long id) {
        SrmTenderProjectDO project = validateTenderExists(id);
        if (!TenderStatusEnum.DRAFT.getCode().equals(project.getTenderStatus())) {
@@ -121,6 +143,8 @@
        if (tenderBidMapper.countByTenderProjectId(id) > 0) {
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.TENDER_HAS_BIDS_CANNOT_DELETE);
        }
        // 删除资质要求附件
        storageAttachmentApi.deleteAttachmentsByRecord(TENDER_PROJECT_RECORD_TYPE, id);
        tenderMaterialMapper.deleteByTenderProjectId(id);
        tenderProjectMapper.deleteById(id);
    }
@@ -257,7 +281,8 @@
        award.setBidId(bidId);
        award.setAwardNo("SMP" + System.currentTimeMillis());
        award.setAwardStatus(AwardStatusEnum.AWARDED.getCode());
        award.setAwardAmount(bid.getBidTotalAmount());
        // 投标金额为空时,回退到该投标下所有报价行金额之和 = Σ(报价单价 × 物料数量),避免 award_amount 为空导致入库失败
        award.setAwardAmount(resolveAwardAmount(id, bidId, bid.getBidTotalAmount()));
        award.setAwardTime(LocalDateTime.now());
        award.setApprovalTime(LocalDateTime.now());
        bidAwardMapper.insert(award);
@@ -275,6 +300,24 @@
    }
    /**
     * 解析中标金额:投标金额为空时,汇总该投标下所有报价行金额 = Σ(报价单价 × 物料数量)
     */
    private BigDecimal resolveAwardAmount(Long tenderProjectId, Long bidId, BigDecimal bidTotalAmount) {
        if (bidTotalAmount != null) {
            return bidTotalAmount;
        }
        List<SrmTenderMaterialDO> materials = tenderMaterialMapper.selectListByTenderProjectId(tenderProjectId);
        Map<Long, BigDecimal> quantityMap = materials.stream()
                .collect(Collectors.toMap(SrmTenderMaterialDO::getId,
                        material -> material.getQuantity() == null ? BigDecimal.ZERO : material.getQuantity()));
        return supplierQuoteMapper.selectListByBidId(bidId).stream()
                .filter(quote -> quote.getQuotePrice() != null && quote.getTenderMaterialId() != null)
                .map(quote -> quote.getQuotePrice()
                        .multiply(quantityMap.getOrDefault(quote.getTenderMaterialId(), BigDecimal.ZERO)))
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }
    /**
     * 简易模式:根据定标信息自动生成采购订单
     */
    private void generatePurchaseOrderForSimple(SrmBidAwardDO award, SrmTenderProjectDO project) {
@@ -283,6 +326,9 @@
        if (supplier == null) {
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.AWARD_SUPPLIER_NOT_SYNCED);
        }
        // 供应商未准入则自动准入并通过,保证采购订单流程可用
        supplierApplyService.ensureSupplierApproved(supplier.getId());
        // 查招标物料
        List<SrmTenderMaterialDO> materials = tenderMaterialMapper
@@ -309,10 +355,18 @@
        Map<Long, MdmItemRespDTO> itemMap = mdmItemApi.getItemList(productIds).getCheckedData()
                .stream().collect(Collectors.toMap(MdmItemRespDTO::getId, Function.identity()));
        // 计算价格
        BigDecimal totalQuoteAmount = quotes.stream()
                .map(SrmSupplierQuoteDO::getQuotePrice)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        // 计算价格(报价为“单价”口径:行金额 = 报价单价 × 数量;
        // 定标金额与报价合计不一致时按行金额比例分摊,订单行单价仍取报价单价)
        BigDecimal totalQuoteAmount = BigDecimal.ZERO;
        for (SrmTenderMaterialDO material : materials) {
            SrmSupplierQuoteDO quote = quoteMap.get(material.getId());
            BigDecimal quantity = material.getQuantity();
            if (quote == null || quote.getQuotePrice() == null
                    || quantity == null || quantity.compareTo(BigDecimal.ZERO) <= 0) {
                continue;
            }
            totalQuoteAmount = totalQuoteAmount.add(quote.getQuotePrice().multiply(quantity));
        }
        BigDecimal awardAmount = award.getAwardAmount() != null ? award.getAwardAmount() : totalQuoteAmount;
        BigDecimal ratio = totalQuoteAmount.compareTo(BigDecimal.ZERO) > 0
                ? awardAmount.divide(totalQuoteAmount, 8, RoundingMode.HALF_UP)
@@ -325,17 +379,24 @@
            if (quote == null) {
                continue;
            }
            BigDecimal quotePrice = quote.getQuotePrice();
            if (quotePrice == null) {
                continue;
            }
            BigDecimal quantity = material.getQuantity();
            if (quantity == null || quantity.compareTo(BigDecimal.ZERO) <= 0) {
                continue;
            }
            MdmItemRespDTO mdmItem = itemMap.get(material.getProductId());
            if (mdmItem == null) {
                continue;
            }
            BigDecimal materialTotal = quote.getQuotePrice().multiply(ratio);
            BigDecimal unitPrice = materialTotal.divide(material.getQuantity(), 4, RoundingMode.HALF_UP);
            BigDecimal unitPrice = quotePrice.multiply(ratio).setScale(4, RoundingMode.HALF_UP);
            ErpPurchaseOrderCreateReqDTO.Item item = new ErpPurchaseOrderCreateReqDTO.Item();
            item.setProductId(material.getProductId());
            item.setProductUnitId(mdmItem.getUnitMeasureId());
            item.setProductPrice(unitPrice);
            item.setCount(material.getQuantity());
            item.setCount(quantity);
            item.setTaxPercent(quote.getTaxRate());
            item.setRemark("定标编号: " + award.getAwardNo());
            orderItems.add(item);
@@ -368,6 +429,7 @@
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    @LogRecord(type = LOG_TENDER_TYPE, subType = LOG_TENDER_UPDATE_SUB_TYPE,
            bizNo = "{{#updateReqVO.id}}", success = LOG_TENDER_UPDATE_SUCCESS)
    public void updateTenderProjectPartial(SrmTenderProjectPartialUpdateReqVO updateReqVO) {
@@ -380,6 +442,8 @@
        updateObj.setTenderDesc(updateReqVO.getTenderDesc());
        updateObj.setQualificationRequirements(updateReqVO.getQualificationRequirements());
        tenderProjectMapper.updateById(updateObj);
        // 增量更新资质要求附件
        storageAttachmentApi.updateAttachments("file", TENDER_PROJECT_RECORD_TYPE, updateReqVO.getId(), updateReqVO.getBlobIds());
        LogRecordContext.putVariable("tenderName", project.getTenderName());
        LogRecordContext.putVariable("oldTenderDesc", project.getTenderDesc());
        LogRecordContext.putVariable("newTenderDesc", updateReqVO.getTenderDesc());