From 4cdcede97b0d7e13aaf8c8b52004d82a291783f1 Mon Sep 17 00:00:00 2001 From: whycxzp <perryhsu@163.com> Date: 星期一, 13 三月 2023 12:19:58 +0800 Subject: [PATCH] bom添加附带物料新增时去重重复物料 --- src/main/java/com/whyc/service/ProductService.java | 41 +++++++++++++++++++++++++++++++---------- 1 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/whyc/service/ProductService.java b/src/main/java/com/whyc/service/ProductService.java index f184bf2..9c9d737 100644 --- a/src/main/java/com/whyc/service/ProductService.java +++ b/src/main/java/com/whyc/service/ProductService.java @@ -8,11 +8,8 @@ import com.whyc.dto.Response; import com.whyc.mapper.ProductMapper; import com.whyc.pojo.*; -import com.whyc.util.ActionUtil; -import com.whyc.util.CommonUtil; import com.whyc.util.DateUtil; -import com.whyc.util.FileUtil; -import com.whyc.util.Zip4jUtil; +import com.whyc.util.*; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.*; import org.springframework.beans.factory.annotation.Autowired; @@ -24,6 +21,9 @@ import java.io.*; import java.text.SimpleDateFormat; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; +import java.util.function.Predicate; import java.util.stream.Collectors; @Service @@ -615,8 +615,8 @@ List<String> subCodeList = materialExistList.stream().map(material -> material.getSubCode()+"/"+material.getSubModel()).collect(Collectors.toList()); List<Material> materialList = new LinkedList<>(); List<Material> materialUpdateList = new LinkedList<>(); - bomList.forEach(bom -> { - if (!subCodeList.contains(bom.getSubCode()+"/"+bom.getSubModel())) { + for (ProductBom bom : bomList) { + if (!subCodeList.contains(bom.getSubCode() + "/" + bom.getSubModel())) { Material material = new Material(); material.setCreateDate(date); material.setDwgUrl(bom.getDwgUrl()); @@ -631,7 +631,7 @@ material.setUnit(bom.getUnit()); materialList.add(material); - }else{ //鐗╂枡宸茬粡瀛樺湪鐨� + } else { //鐗╂枡宸茬粡瀛樺湪鐨� Material material = new Material(); if (bom.getPictureUrl() != null) { material.setPictureUrl(bom.getPictureUrl()); @@ -639,13 +639,16 @@ if (bom.getDwgUrl() != null) { material.setDwgUrl(bom.getDwgUrl()); } - if(material.getPictureUrl()!=null || material.getDwgUrl()!=null){ + if (material.getPictureUrl() != null || material.getDwgUrl() != null) { material.setSubCode(bom.getSubCode()); material.setSubModel(bom.getSubModel()); materialUpdateList.add(material); } } - }); + } + //瀵规柊澧炲埌鐗╂枡琛ㄤ腑鐨勭墿鏂�,鏍规嵁 鐗╂枡缂栫爜+鐗╂枡鍨嬪彿 杩涜鍘婚噸澶勭悊 + materialList = materialList.stream().filter(distinctByKey(m->m.getSubCode()+"/"+m.getSubModel())).collect(Collectors.toList()); + //姣嶆枡鏄惁瀛樺湪 Material materialDB = mService.getByCodeAndModel(product.getParentCode(), product.getParentModel()); if (materialDB == null) { @@ -668,7 +671,20 @@ // product鐨勪富閿部鐢ㄥ搴攑roduct_history鐨� //鏍规嵁缂栫爜鍜屽瀷鍙风‘瀹氱墿鏂檌d骞跺搴攓uantity,瀛樺叆鏁版嵁搴�. //List<Material> bomMaterialList = mService.getListByCodeAndModelList2(bomList); - List<ProductBom> bomMaterialList = pbService.getListByCodeAndModelList2(bomList); + //List<ProductBom> bomMaterialList = pbService.getListByCodeAndModelList2(bomList); + List<Material> existMaterialListWithSameSubCodeAndModel = mService.getSameSubCodeAndModel(bomList); + List<ProductBom> bomMaterialList; + bomList.forEach(bom->{ + String codeModelUnionStr = bom.getSubCode() + "/" + bom.getSubModel(); + for (int i = 0; i < existMaterialListWithSameSubCodeAndModel.size(); i++) { + Material materialExists = existMaterialListWithSameSubCodeAndModel.get(i); + if(codeModelUnionStr.equals(materialExists.getSubCode() + "/" + materialExists.getSubModel())){ + bom.setId(materialExists.getId()); + break; + } + } + }); + bomMaterialList = bomList; // -> product_history ProductHistory productHistory = new ProductHistory(); productHistory.setParentCode(product.getParentCode()); @@ -784,4 +800,9 @@ List list=mapper.selectList(null); return new Response().setII(1,list.size()>0,list,"浜у搧瀵规瘮涓嬫媺閫変腑-鏌ヨ褰撳墠浣跨敤鐨勬墍鏈夌殑浜у搧"); } + public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) { + Map<Object, Boolean> seen = new ConcurrentHashMap<>(); + return object -> seen.putIfAbsent(keyExtractor.apply(object), Boolean.TRUE) == null; + } + } -- Gitblit v1.9.1