瀏覽代碼

下发待采购

yzc 11 月之前
父節點
當前提交
3559b238e2

+ 12 - 231
hx-mes/src/main/java/com/fjhx/mes/service/production/impl/ProduceOrderServiceImpl.java

@@ -4,14 +4,11 @@ import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.util.ObjectUtil;
 import com.alibaba.fastjson2.JSONObject;
 import com.baomidou.dynamic.datasource.annotation.DSTransactional;
-import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.fjhx.common.entity.AvailableStockBo;
 import com.fjhx.common.enums.PushBusinessTypeEnum;
 import com.fjhx.common.utils.Assert;
 import com.fjhx.item.entity.product.po.ProductInfo;
-import com.fjhx.item.enums.ProductAvailableRecordType;
 import com.fjhx.item.service.product.ProductInfoService;
 import com.fjhx.mes.entity.MaterialBalanceBo;
 import com.fjhx.mes.entity.production.dto.ProduceOrderSelectDto;
@@ -28,14 +25,12 @@ import com.fjhx.mes.service.material.MaterialPreparationService;
 import com.fjhx.mes.service.production.ProduceOrderDetailService;
 import com.fjhx.mes.service.production.ProduceOrderService;
 import com.fjhx.mes.service.production.ProductionTaskProgressService;
-import com.fjhx.purchase.entity.subscribe.po.SubscribeDetail;
 import com.fjhx.purchase.service.subscribe.SubscribeDetailService;
 import com.fjhx.sale.entity.arrival.po.ArrivalDetail;
 import com.fjhx.sale.entity.arrival.vo.ArrivalDetailVo;
 import com.fjhx.sale.entity.contract.dto.ContractSelectDto;
 import com.fjhx.sale.entity.contract.po.Contract;
 import com.fjhx.sale.entity.contract.po.ContractProduct;
-import com.fjhx.sale.entity.contract.po.ContractProductBom;
 import com.fjhx.sale.entity.contract.vo.ContractProductBomVo;
 import com.fjhx.sale.entity.contract.vo.ContractProductVo;
 import com.fjhx.sale.entity.contract.vo.ContractVo;
@@ -72,8 +67,6 @@ import org.springframework.stereotype.Service;
 import java.math.BigDecimal;
 import java.text.SimpleDateFormat;
 import java.util.*;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.function.Function;
 import java.util.stream.Collectors;
 
 
@@ -343,223 +336,6 @@ public class ProduceOrderServiceImpl extends ServiceImpl<ProduceOrderMapper, Pro
         this.updateById(productionOrder);
     }
 
-    /**
-     * 创建合同产品 物料待采购信息
-     */
-    private void createMaterialWaitPurchase(ProductionOrder productionOrder) {
-        //根据归属公司将数据赋值给指定公司
-        Long companyId = productionOrder.getCompanyId();
-        Long contractId = productionOrder.getContractId();
-
-        //获取合同产品信息
-        List<ContractProductVo> contractProductList = contractProductService.getList(IWrapper.<ContractProduct>getWrapper()
-                .eq("cp", ContractProduct::getContractId, contractId)
-                .eq("pi.company_id", companyId)
-        );
-        Map<Long, ContractProduct> productMap = contractProductList.stream().collect(Collectors.toMap(ContractProduct::getId, Function.identity()));
-
-        //获取物料列表
-        List<Long> cpIds = contractProductList.stream().map(ContractProduct::getId).collect(Collectors.toList());
-        List<ContractProductBom> cpBList = contractProductBomService.list(q -> q
-                .in(ContractProductBom::getContractProductId, cpIds)
-                .ne(ContractProductBom::getType, 3)//创建待采购忽略工序类型的数据
-        );
-
-        //合并所有相同物料
-        Map<Long, AvailableStockBo> inOutBoMap = new ConcurrentHashMap<>();
-        for (ContractProductBom bom : cpBList) {
-            Long materialId = bom.getMaterialId();
-//            ContractProduct contractProduct = productMap.get(bom.getContractProductId());
-//            BigDecimal multiply = bom.getQuantity().multiply(contractProduct.getQuantity());
-            BigDecimal multiply = bom.getQuantity();
-
-            //合并相同物料
-            AvailableStockBo inOutBo = inOutBoMap.get(materialId);
-            if (ObjectUtil.isEmpty(inOutBo)) {
-                inOutBo = new AvailableStockBo();
-                inOutBo.setProductId(materialId);
-                inOutBo.setQuantity(BigDecimal.ZERO);
-            }
-            inOutBo.setQuantity(inOutBo.getQuantity().add(multiply));
-            inOutBoMap.put(materialId, inOutBo);
-        }
-
-        //计算并生成待出库数据
-        List<SubscribeDetail> subscribeDetailList = new ArrayList<>();
-        //获取待采购物料列表
-        List<AvailableStockBo> collect = inOutBoMap.values().stream().collect(Collectors.toList());
-        //操作可用库存计数需要采购量
-        productInfoService.editAvailableQuantity(collect, contractId, ProductAvailableRecordType.SALE_PASS, companyId);
-        for (AvailableStockBo inOutBo : collect) {
-            Long materialId = inOutBo.getProductId();
-            BigDecimal requiredQuantity = inOutBo.getQuantity();
-
-            //需要采购的数量大于0生成待采购数据
-            if (requiredQuantity.compareTo(BigDecimal.ZERO) > 0) {
-                SubscribeDetail subscribeDetail = new SubscribeDetail();
-                subscribeDetail.setProductId(materialId);
-                subscribeDetail.setCount(requiredQuantity);
-                subscribeDetail.setStatus(15);//待采购
-                subscribeDetail.setContractId(contractId);
-                subscribeDetail.setDataType(1);
-                subscribeDetail.setCompanyId(companyId);
-                subscribeDetail.setProdOrderId(productionOrder.getId());
-                subscribeDetailList.add(subscribeDetail);
-            }
-        }
-        //保存待采购明细
-        subscribeDetailService.saveBatch(subscribeDetailList);
-    }
-
-    void updateMaterialWaitPurchase(ProductionOrder productionOrder) {
-        Long newContractId = productionOrder.getContractId();
-        Contract newContract = contractService.getById(newContractId);
-        Long oldContractId = newContract.getOldContractId();
-        Long companyId = newContract.getCompanyId();
-        //---------------------------------------------------------------
-        List<ContractProductVo> oldContractProductList = contractProductService.getList(IWrapper.<ContractProduct>getWrapper()
-                .eq("cp", ContractProduct::getContractId, oldContractId)
-                //过滤当前生产公司的产品
-                .eq("pi.company_id", productionOrder.getCompanyId())
-        );
-        List<Long> oldCpIds = oldContractProductList.stream().map(ContractProduct::getId).collect(Collectors.toList());
-        List<ContractProductBomVo> oldProductBomListSum = contractProductBomService.getContractProductBomQuantitySum(IWrapper.getWrapper()
-                .eq("cpb.contract_id", oldContractId)
-                .in("cp.id", oldCpIds)
-                .ne("cpb.type", 3)
-        );
-
-        //获取新合同物料数量Map
-        List<ContractProductVo> newContractProductList = contractProductService.getList(IWrapper.<ContractProduct>getWrapper()
-                .eq("cp", ContractProduct::getContractId, newContract)
-                //过滤当前生产公司的产品
-                .eq("pi.company_id", productionOrder.getCompanyId())
-        );
-        List<Long> newCpIds = newContractProductList.stream().map(ContractProduct::getId).collect(Collectors.toList());
-        List<ContractProductBomVo> contractProductBomQuantitySum = contractProductBomService.getContractProductBomQuantitySum(IWrapper.getWrapper()
-                .eq("cpb.contract_id", newContractId)
-                .in("cp.id", newCpIds)
-                .ne("cpb.type", 3)
-        );
-        Map<Long, BigDecimal> newQuantityMap = contractProductBomQuantitySum.stream().collect(Collectors.toMap(ContractProductBom::getMaterialId, ContractProductBom::getQuantity));
-
-
-        List<AvailableStockBo> availableStockBoList = new ArrayList<>();
-
-        //获取已采购数量
-        List<EhsdPurchaseProductVo> purchaseQuantitySumByContractId = ehsdPurchaseProductMapper.getPurchaseQuantitySumByContractId(newContractId, companyId);
-        Map<Long, BigDecimal> collect = purchaseQuantitySumByContractId.stream().collect(Collectors.toMap(EhsdPurchaseProduct::getProductId, EhsdPurchaseProduct::getQuantity));
-
-        //旧数据处理
-        for (ContractProductBomVo oldContractProductBom : oldProductBomListSum) {
-            Long materialId = oldContractProductBom.getMaterialId();
-
-            //新数量
-            BigDecimal newQuantity = newQuantityMap.get(materialId);
-            //已采购数量
-            BigDecimal purchaseQuantity = collect.getOrDefault(materialId, BigDecimal.ZERO);
-
-            //获取原需采购量
-            SubscribeDetail subscribeDetail = subscribeDetailService.getOne(q -> q
-                    .eq(SubscribeDetail::getContractId, newContractId)
-                    .eq(SubscribeDetail::getProductId, materialId)
-                    .eq(SubscribeDetail::getCompanyId, productionOrder.getCompanyId())
-            );
-            BigDecimal demandPurchaseCount = BigDecimal.ZERO;
-            if (ObjectUtils.isNotEmpty(subscribeDetail)) {
-                demandPurchaseCount = subscribeDetail.getCount();
-            }
-
-
-            //生成操作可用库存的实体
-            AvailableStockBo availableStockBo = new AvailableStockBo();
-            availableStockBo.setProductId(materialId);
-            availableStockBo.setQuantity(oldContractProductBom.getQuantity());
-            availableStockBo.setNewQuantity(newQuantity);
-            availableStockBo.setInStockQuantity(purchaseQuantity);
-            availableStockBo.setDemandPurchaseCount(demandPurchaseCount);
-
-            availableStockBoList.add(availableStockBo);
-
-        }
-
-        //新添加的数据处理
-        Map<Long, BigDecimal> oldQuantityMap = oldProductBomListSum.stream().collect(Collectors.toMap(ContractProductBom::getMaterialId, ContractProductBom::getQuantity));
-        for (ContractProductBomVo newPurchaseProduct : contractProductBomQuantitySum) {
-            Long materialId = newPurchaseProduct.getMaterialId();
-
-            BigDecimal bigDecimal = oldQuantityMap.get(materialId);
-            //忽略掉变更前有的物料
-            if (ObjectUtils.isNotEmpty(bigDecimal)) {
-                continue;
-            }
-
-            //生成操作可用库存的实体
-            AvailableStockBo availableStockBo = new AvailableStockBo();
-            availableStockBo.setProductId(materialId);
-            availableStockBo.setQuantity(BigDecimal.ZERO);
-            availableStockBo.setNewQuantity(newPurchaseProduct.getQuantity());
-            availableStockBo.setInStockQuantity(BigDecimal.ZERO);
-            availableStockBo.setDemandPurchaseCount(BigDecimal.ZERO);
-
-            availableStockBoList.add(availableStockBo);
-        }
-
-
-        //修改可用库存
-        productInfoService.editAvailableQuantity(availableStockBoList, newContractId, ProductAvailableRecordType.SALE_UPDATE, companyId);
-
-
-        //修该待采购量为需采购量
-        for (AvailableStockBo availableStockBo : availableStockBoList) {
-
-            Long materialId = availableStockBo.getProductId();
-            SubscribeDetail subscribeDetail = subscribeDetailService.getOne(q -> q.eq(SubscribeDetail::getContractId, newContractId)
-                    .eq(SubscribeDetail::getProductId, materialId)
-                    .eq(SubscribeDetail::getCompanyId, productionOrder.getCompanyId())
-            );
-
-            BigDecimal requiredQuantity = availableStockBo.getNewQuantity();
-
-
-            //需采购量>0
-            if (requiredQuantity.compareTo(BigDecimal.ZERO) > 0) {
-                if (ObjectUtils.isEmpty(subscribeDetail)) {
-                    //生成待采购
-                    subscribeDetail = new SubscribeDetail();
-                    subscribeDetail.setProductId(materialId);
-                    subscribeDetail.setCount(BigDecimal.ZERO);
-                    subscribeDetail.setStatus(15);//待采购
-                    subscribeDetail.setContractId(newContractId);
-                    subscribeDetail.setDataType(1);
-                    subscribeDetail.setCompanyId(companyId);
-                    subscribeDetail.setProdOrderId(productionOrder.getId());
-                }
-
-                //修改待采购量
-                subscribeDetail.setCount(requiredQuantity);
-
-                //计算状态
-                BigDecimal purchaseQuantity = availableStockBo.getInStockQuantity();
-                int status = purchaseQuantity.compareTo(requiredQuantity) >= 0 ? 20 : 30;
-                if (purchaseQuantity.compareTo(BigDecimal.ZERO) == 0) {
-                    status = 15;
-                }
-                subscribeDetail.setStatus(status);
-
-                subscribeDetailService.saveOrUpdate(subscribeDetail);
-            } else {
-                //需采购==0不显示或删除,<不存在
-                if (ObjectUtils.isNotEmpty(subscribeDetail)) {
-                    subscribeDetailService.removeById(subscribeDetail);
-                }
-            }
-
-
-        }
-
-        //---------------------------------------------------------------
-    }
 
     /**
      * 创建生产任务
@@ -785,15 +561,20 @@ public class ProduceOrderServiceImpl extends ServiceImpl<ProduceOrderMapper, Pro
             Assert.notEmpty(contract, "查询不到合同信息!");
             //所有订单交期确认, 合同为已下发, 才生成数据
             if (count == 0 && ObjectUtil.equals(contract.getOrderDistributeStatus(), 1)) {
+
+                //创建/更新 待采购
+                contractService.createWaitMaterial(contract.getId());
+
                 List<ProductionOrder> productionOrderList = this.list(q -> q.eq(ProductionOrder::getContractId, productionOrder.getContractId()));
                 for (ProductionOrder productionOrder1 : productionOrderList) {
-                    if (produceOrderDetailService.count(q -> q.eq(ProductionOrderDetail::getProduceOrderId, productionOrder1.getId())) > 0) {
-                        //修改待采购
-                        updateMaterialWaitPurchase(productionOrder1);
-                    } else {
-                        //创建待采购
-                        createMaterialWaitPurchase(productionOrder1);
-                    }
+
+//                    if (produceOrderDetailService.count(q -> q.eq(ProductionOrderDetail::getProduceOrderId, productionOrder1.getId())) > 0) {
+//                        //修改待采购
+//                        updateMaterialWaitPurchase(productionOrder1.getCompanyId(), productionOrder1.getContractId());
+//                    } else {
+//                        //创建待采购
+//                        createMaterialWaitPurchase(productionOrder1.getCompanyId(), productionOrder1.getContractId());
+//                    }
 
                     //创建生产任务
                     createOrderDetails(productionOrder1);

+ 8 - 0
hx-sale/src/main/java/com/fjhx/sale/controller/contract/ContractController.java

@@ -381,4 +381,12 @@ public class ContractController {
         contractService.editSettleDateRange(dto);
     }
 
+    /**
+     * 下发待采购
+     */
+    @PostMapping("/createWaitMaterial")
+    public void createWaitMaterial(@RequestBody ContractDto dto) {
+        contractService.createWaitMaterial(dto.getId());
+    }
+
 }

+ 5 - 0
hx-sale/src/main/java/com/fjhx/sale/entity/contract/po/Contract.java

@@ -562,6 +562,11 @@ public class Contract extends BasePo {
      * 样品单关联合同id
      */
     private Integer linkContractId;
+
+    /**
+     * 待采购状态
+     */
+    private Integer waitPurchaseStatus;
     //========================================================================
     /**
      * 交接单附件列表

+ 2 - 0
hx-sale/src/main/java/com/fjhx/sale/service/contract/ContractService.java

@@ -269,4 +269,6 @@ public interface ContractService extends BaseService<Contract> {
     void excelExport(HttpServletResponse httpServletResponse, ContractSelectDto dto);
 
     void editSettleDateRange(ContractDto dto);
+
+    void createWaitMaterial(Long contractId);
 }

+ 270 - 3
hx-sale/src/main/java/com/fjhx/sale/service/contract/impl/ContractServiceImpl.java

@@ -10,6 +10,7 @@ import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.IdWorker;
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -72,6 +73,7 @@ import com.fjhx.sale.entity.purchase.vo.EhsdPurchaseProductVo;
 import com.fjhx.sale.entity.sale.vo.SaleQuotationVo;
 import com.fjhx.sale.mapper.contract.ContractMapper;
 import com.fjhx.sale.mapper.contract.ContractProductBomMapper;
+import com.fjhx.sale.mapper.purchase.EhsdPurchaseProductMapper;
 import com.fjhx.sale.service.SaleService;
 import com.fjhx.sale.service.claim.ClaimContractService;
 import com.fjhx.sale.service.contract.*;
@@ -104,7 +106,6 @@ import com.ruoyi.common.utils.wrapper.IWrapper;
 import com.ruoyi.common.utils.wrapper.SqlField;
 import com.ruoyi.framework.config.ThreadPoolConfig;
 import com.ruoyi.system.service.ISysDeptService;
-import com.ruoyi.system.service.ISysRoleService;
 import com.ruoyi.system.service.ISysUserService;
 import com.ruoyi.system.utils.UserUtil;
 import org.apache.commons.collections4.MapUtils;
@@ -121,6 +122,7 @@ import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.util.*;
 import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.function.Function;
 import java.util.stream.Collectors;
@@ -231,7 +233,7 @@ public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract>
     @Autowired
     private CodingRuleService codingRuleService;
     @Autowired
-    private ISysRoleService roleService;
+    private EhsdPurchaseProductMapper ehsdPurchaseProductMapper;
 
     /**
      * 合同和样品单 下拉分页
@@ -1419,6 +1421,14 @@ public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract>
 
     @Override
     public void saveOrEdit(ContractDto contract) {
+        //赋值待采购下发状态
+        if (ObjectUtil.equals(contract.getProcessingMethod(), 10)) {
+            //业务自采无需手动下发采购
+            contract.setWaitPurchaseStatus(1);
+        } else {
+            contract.setWaitPurchaseStatus(0);
+        }
+
         //退回发起人处理关联样品单
         if (ObjectUtil.isNotEmpty(contract.getId())) {
             Contract oldCo = this.getById(contract.getId());
@@ -1471,7 +1481,7 @@ public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract>
                 contractProductBom.setContractProductId(cp.getId());
                 contractProductBom.setProductId(cp.getProductId());
             }
-             contractProductBomService.editLinked(contractProductBomList, ContractProductBom::getContractProductId, cp.getId());
+            contractProductBomService.editLinked(contractProductBomList, ContractProductBom::getContractProductId, cp.getId());
 
             //保存BOM附件
             for (ContractProductBom contractProductBom : contractProductBomList) {
@@ -3411,4 +3421,261 @@ public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract>
         );
     }
 
+    @DSTransactional
+    @Override
+    public void createWaitMaterial(Long contractId) {
+        Contract contract = contractService.getById(contractId);
+        Assert.notEmpty(contract, "查询不到合同信息,请检查!");
+        Contract oldContract = contractService.getById(contract.getOldContractId());
+
+        //原待采购产品
+        List<ContractProductVo> oldContractProductList0 = new ArrayList<>();
+        if (ObjectUtil.isNotEmpty(oldContract)) {
+            oldContractProductList0 = contractProductService.getList(IWrapper.<ContractProduct>getWrapper()
+                    .eq("cp", ContractProduct::getContractId, oldContract.getId())
+            );
+        }
+        //新待采购产品
+        List<ContractProductVo> newContractProductList0 = contractProductService.getList(IWrapper.<ContractProduct>getWrapper()
+                .eq("cp", ContractProduct::getContractId, contractId)
+        );
+
+        //获取所有相关公司
+        List<Long> productIds = oldContractProductList0.stream().map(ContractProductVo::getProductId).collect(Collectors.toList());
+        productIds.addAll(newContractProductList0.stream().map(ContractProductVo::getProductId).collect(Collectors.toList()));
+        List<Long> pCompanyIds = productInfoService.listObject(ProductInfo::getCompanyId, q -> q.in(ProductInfo::getId, productIds));
+
+        //根据公司创建待采购数据
+        for (Long companyId : pCompanyIds) {
+            //如果当前合同已下采购发报错
+            if (ObjectUtil.equals(contract.getWaitPurchaseStatus(), 1)) {
+                return;
+            }
+            //if 旧合同不存在 || 旧合同 下发采购状态 != 1 添加 else 修改
+            if (ObjectUtil.isEmpty(oldContract) || ObjectUtil.notEqual(oldContract.getWaitPurchaseStatus(), 1)) {
+                //创建待采购
+                createMaterialWaitPurchase(companyId, contractId);
+            } else {
+                //修改待采购
+                updateMaterialWaitPurchase(companyId, contractId);
+            }
+        }
+
+        //更新下发状态
+        contractService.update(q -> q.eq(Contract::getId, contract.getId()).set(Contract::getWaitPurchaseStatus, 1));
+    }
+
+    /**
+     * 创建合同产品 物料待采购信息
+     */
+    private void createMaterialWaitPurchase(Long companyId, Long contractId) {
+        //获取合同产品信息
+        List<ContractProductVo> contractProductList = contractProductService.getList(IWrapper.<ContractProduct>getWrapper()
+                .eq("cp", ContractProduct::getContractId, contractId)
+                .eq("pi.company_id", companyId)
+        );
+        Map<Long, ContractProduct> productMap = contractProductList.stream().collect(Collectors.toMap(ContractProduct::getId, Function.identity()));
+
+        //获取物料列表
+        List<Long> cpIds = contractProductList.stream().map(ContractProduct::getId).collect(Collectors.toList());
+        List<ContractProductBom> cpBList = contractProductBomService.list(q -> q
+                .in(ContractProductBom::getContractProductId, cpIds)
+                .ne(ContractProductBom::getType, 3)//创建待采购忽略工序类型的数据
+        );
+
+        //合并所有相同物料
+        Map<Long, AvailableStockBo> inOutBoMap = new ConcurrentHashMap<>();
+        for (ContractProductBom bom : cpBList) {
+            Long materialId = bom.getMaterialId();
+//            ContractProduct contractProduct = productMap.get(bom.getContractProductId());
+//            BigDecimal multiply = bom.getQuantity().multiply(contractProduct.getQuantity());
+            BigDecimal multiply = bom.getQuantity();
+
+            //合并相同物料
+            AvailableStockBo inOutBo = inOutBoMap.get(materialId);
+            if (ObjectUtil.isEmpty(inOutBo)) {
+                inOutBo = new AvailableStockBo();
+                inOutBo.setProductId(materialId);
+                inOutBo.setQuantity(BigDecimal.ZERO);
+            }
+            inOutBo.setQuantity(inOutBo.getQuantity().add(multiply));
+            inOutBoMap.put(materialId, inOutBo);
+        }
+
+        //计算并生成待出库数据
+        List<SubscribeDetail> subscribeDetailList = new ArrayList<>();
+        //获取待采购物料列表
+        List<AvailableStockBo> collect = inOutBoMap.values().stream().collect(Collectors.toList());
+        //操作可用库存计数需要采购量
+        productInfoService.editAvailableQuantity(collect, contractId, ProductAvailableRecordType.SALE_PASS, companyId);
+        for (AvailableStockBo inOutBo : collect) {
+            Long materialId = inOutBo.getProductId();
+            BigDecimal requiredQuantity = inOutBo.getQuantity();
+
+            //需要采购的数量大于0生成待采购数据
+            if (requiredQuantity.compareTo(BigDecimal.ZERO) > 0) {
+                SubscribeDetail subscribeDetail = new SubscribeDetail();
+                subscribeDetail.setProductId(materialId);
+                subscribeDetail.setCount(requiredQuantity);
+                subscribeDetail.setStatus(15);//待采购
+                subscribeDetail.setContractId(contractId);
+                subscribeDetail.setDataType(1);
+                subscribeDetail.setCompanyId(companyId);
+//                subscribeDetail.setProdOrderId(productionOrder.getId());
+                subscribeDetailList.add(subscribeDetail);
+            }
+        }
+        //保存待采购明细
+        subscribeDetailService.saveBatch(subscribeDetailList);
+    }
+
+    void updateMaterialWaitPurchase(Long companyId, Long newContractId) {
+        Contract newContract = contractService.getById(newContractId);
+        Long oldContractId = newContract.getOldContractId();
+
+        //---------------------------------------------------------------
+        List<ContractProductVo> oldContractProductList = contractProductService.getList(IWrapper.<ContractProduct>getWrapper()
+                .eq("cp", ContractProduct::getContractId, oldContractId)
+                //过滤当前生产公司的产品
+                .eq("pi.company_id", companyId)
+        );
+        List<Long> oldCpIds = oldContractProductList.stream().map(ContractProduct::getId).collect(Collectors.toList());
+        List<ContractProductBomVo> oldProductBomListSum = contractProductBomService.getContractProductBomQuantitySum(IWrapper.getWrapper()
+                .eq("cpb.contract_id", oldContractId)
+                .in("cp.id", oldCpIds)
+                .ne("cpb.type", 3)
+        );
+
+        //获取新合同物料数量Map
+        List<ContractProductVo> newContractProductList = contractProductService.getList(IWrapper.<ContractProduct>getWrapper()
+                .eq("cp", ContractProduct::getContractId, newContract)
+                //过滤当前生产公司的产品
+                .eq("pi.company_id", companyId)
+        );
+        List<Long> newCpIds = newContractProductList.stream().map(ContractProduct::getId).collect(Collectors.toList());
+        List<ContractProductBomVo> contractProductBomQuantitySum = contractProductBomService.getContractProductBomQuantitySum(IWrapper.getWrapper()
+                .eq("cpb.contract_id", newContractId)
+                .in("cp.id", newCpIds)
+                .ne("cpb.type", 3)
+        );
+        Map<Long, BigDecimal> newQuantityMap = contractProductBomQuantitySum.stream().collect(Collectors.toMap(ContractProductBom::getMaterialId, ContractProductBom::getQuantity));
+
+
+        List<AvailableStockBo> availableStockBoList = new ArrayList<>();
+
+        //获取已采购数量
+        List<EhsdPurchaseProductVo> purchaseQuantitySumByContractId = ehsdPurchaseProductMapper.getPurchaseQuantitySumByContractId(newContractId, companyId);
+        Map<Long, BigDecimal> collect = purchaseQuantitySumByContractId.stream().collect(Collectors.toMap(EhsdPurchaseProduct::getProductId, EhsdPurchaseProduct::getQuantity));
+
+        //旧数据处理
+        for (ContractProductBomVo oldContractProductBom : oldProductBomListSum) {
+            Long materialId = oldContractProductBom.getMaterialId();
+
+            //新数量
+            BigDecimal newQuantity = newQuantityMap.get(materialId);
+            //已采购数量
+            BigDecimal purchaseQuantity = collect.getOrDefault(materialId, BigDecimal.ZERO);
+
+            //获取原需采购量
+            SubscribeDetail subscribeDetail = subscribeDetailService.getOne(q -> q
+                    .eq(SubscribeDetail::getContractId, newContractId)
+                    .eq(SubscribeDetail::getProductId, materialId)
+                    .eq(SubscribeDetail::getCompanyId, companyId)
+            );
+            BigDecimal demandPurchaseCount = BigDecimal.ZERO;
+            if (ObjectUtils.isNotEmpty(subscribeDetail)) {
+                demandPurchaseCount = subscribeDetail.getCount();
+            }
+
+
+            //生成操作可用库存的实体
+            AvailableStockBo availableStockBo = new AvailableStockBo();
+            availableStockBo.setProductId(materialId);
+            availableStockBo.setQuantity(oldContractProductBom.getQuantity());
+            availableStockBo.setNewQuantity(newQuantity);
+            availableStockBo.setInStockQuantity(purchaseQuantity);
+            availableStockBo.setDemandPurchaseCount(demandPurchaseCount);
+
+            availableStockBoList.add(availableStockBo);
+
+        }
+
+        //新添加的数据处理
+        Map<Long, BigDecimal> oldQuantityMap = oldProductBomListSum.stream().collect(Collectors.toMap(ContractProductBom::getMaterialId, ContractProductBom::getQuantity));
+        for (ContractProductBomVo newPurchaseProduct : contractProductBomQuantitySum) {
+            Long materialId = newPurchaseProduct.getMaterialId();
+
+            BigDecimal bigDecimal = oldQuantityMap.get(materialId);
+            //忽略掉变更前有的物料
+            if (ObjectUtils.isNotEmpty(bigDecimal)) {
+                continue;
+            }
+
+            //生成操作可用库存的实体
+            AvailableStockBo availableStockBo = new AvailableStockBo();
+            availableStockBo.setProductId(materialId);
+            availableStockBo.setQuantity(BigDecimal.ZERO);
+            availableStockBo.setNewQuantity(newPurchaseProduct.getQuantity());
+            availableStockBo.setInStockQuantity(BigDecimal.ZERO);
+            availableStockBo.setDemandPurchaseCount(BigDecimal.ZERO);
+
+            availableStockBoList.add(availableStockBo);
+        }
+
+
+        //修改可用库存
+        productInfoService.editAvailableQuantity(availableStockBoList, newContractId, ProductAvailableRecordType.SALE_UPDATE, companyId);
+
+
+        //修该待采购量为需采购量
+        for (AvailableStockBo availableStockBo : availableStockBoList) {
+
+            Long materialId = availableStockBo.getProductId();
+            SubscribeDetail subscribeDetail = subscribeDetailService.getOne(q -> q.eq(SubscribeDetail::getContractId, newContractId)
+                    .eq(SubscribeDetail::getProductId, materialId)
+                    .eq(SubscribeDetail::getCompanyId, companyId)
+            );
+
+            BigDecimal requiredQuantity = availableStockBo.getNewQuantity();
+
+
+            //需采购量>0
+            if (requiredQuantity.compareTo(BigDecimal.ZERO) > 0) {
+                if (ObjectUtils.isEmpty(subscribeDetail)) {
+                    //生成待采购
+                    subscribeDetail = new SubscribeDetail();
+                    subscribeDetail.setProductId(materialId);
+                    subscribeDetail.setCount(BigDecimal.ZERO);
+                    subscribeDetail.setStatus(15);//待采购
+                    subscribeDetail.setContractId(newContractId);
+                    subscribeDetail.setDataType(1);
+                    subscribeDetail.setCompanyId(companyId);
+//                    subscribeDetail.setProdOrderId(productionOrder.getId());
+                }
+
+                //修改待采购量
+                subscribeDetail.setCount(requiredQuantity);
+
+                //计算状态
+                BigDecimal purchaseQuantity = availableStockBo.getInStockQuantity();
+                int status = purchaseQuantity.compareTo(requiredQuantity) >= 0 ? 20 : 30;
+                if (purchaseQuantity.compareTo(BigDecimal.ZERO) == 0) {
+                    status = 15;
+                }
+                subscribeDetail.setStatus(status);
+
+                subscribeDetailService.saveOrUpdate(subscribeDetail);
+            } else {
+                //需采购==0不显示或删除,<不存在
+                if (ObjectUtils.isNotEmpty(subscribeDetail)) {
+                    subscribeDetailService.removeById(subscribeDetail);
+                }
+            }
+
+
+        }
+
+        //---------------------------------------------------------------
+    }
+
 }