|
@@ -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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //---------------------------------------------------------------
|
|
|
+ }
|
|
|
+
|
|
|
}
|