| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | @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 |
| | |
| | | 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()); |
| | |
| | | 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) { |
| | |
| | | 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()); |
| | |
| | | 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()); |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void deleteTenderProject(Long id) { |
| | | SrmTenderProjectDO project = validateTenderExists(id); |
| | | if (!TenderStatusEnum.DRAFT.getCode().equals(project.getTenderStatus())) { |
| | |
| | | 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); |
| | | } |
| | |
| | | award.setBidId(bidId); |
| | | award.setAwardNo("SMP" + System.currentTimeMillis()); |
| | | award.setAwardStatus(AwardStatusEnum.AWARDED.getCode()); |
| | | award.setAwardAmount(bid.getBidTotalAmount()); |
| | | // 投标金额为空时,回退到该投标下所有报价之和,避免 award_amount 为空导致入库失败 |
| | | award.setAwardAmount(resolveAwardAmount(bidId, bid.getBidTotalAmount())); |
| | | award.setAwardTime(LocalDateTime.now()); |
| | | award.setApprovalTime(LocalDateTime.now()); |
| | | bidAwardMapper.insert(award); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 解析中标金额:投标金额为空时,汇总该投标下所有报价 |
| | | */ |
| | | private BigDecimal resolveAwardAmount(Long bidId, BigDecimal bidTotalAmount) { |
| | | if (bidTotalAmount != null) { |
| | | return bidTotalAmount; |
| | | } |
| | | List<SrmSupplierQuoteDO> quotes = supplierQuoteMapper.selectListByBidId(bidId); |
| | | return quotes.stream() |
| | | .map(SrmSupplierQuoteDO::getQuotePrice) |
| | | .filter(Objects::nonNull) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | } |
| | | |
| | | /** |
| | | * 简易模式:根据定标信息自动生成采购订单 |
| | | */ |
| | | private void generatePurchaseOrderForSimple(SrmBidAwardDO award, SrmTenderProjectDO project) { |
| | |
| | | if (supplier == null) { |
| | | throw ServiceExceptionUtil.exception(ErrorCodeConstants.AWARD_SUPPLIER_NOT_SYNCED); |
| | | } |
| | | |
| | | // 供应商未准入则自动准入并通过,保证采购订单流程可用 |
| | | supplierApplyService.ensureSupplierApproved(supplier.getId()); |
| | | |
| | | // 查招标物料 |
| | | List<SrmTenderMaterialDO> materials = tenderMaterialMapper |
| | |
| | | // 计算价格 |
| | | BigDecimal totalQuoteAmount = quotes.stream() |
| | | .map(SrmSupplierQuoteDO::getQuotePrice) |
| | | .filter(Objects::nonNull) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | BigDecimal awardAmount = award.getAwardAmount() != null ? award.getAwardAmount() : totalQuoteAmount; |
| | | BigDecimal ratio = totalQuoteAmount.compareTo(BigDecimal.ZERO) > 0 |
| | |
| | | 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 materialTotal = quotePrice.multiply(ratio); |
| | | BigDecimal unitPrice = materialTotal.divide(quantity, 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); |
| | |
| | | } |
| | | |
| | | @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) { |
| | |
| | | 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()); |