瀏覽代碼

商品导入校验

yuanliang 1 年之前
父節點
當前提交
4ae25fa3f3
共有 1 個文件被更改,包括 37 次插入15 次删除
  1. 37 15
      mec-biz/src/main/java/com/ym/mec/biz/service/impl/GoodsServiceImpl.java

+ 37 - 15
mec-biz/src/main/java/com/ym/mec/biz/service/impl/GoodsServiceImpl.java

@@ -630,7 +630,11 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 		goodsCategoryQueryInfo.setRows(9999);
 		goodsCategoryQueryInfo.setRows(9999);
 
 
 		List<ProductCategoryDto> categoryList = mallFeignService.listWithChildren();
 		List<ProductCategoryDto> categoryList = mallFeignService.listWithChildren();
-		List<ProductCategoryDto> allCategoryList = categoryList.stream().map(ProductCategoryDto::getChildren).filter(next->!next.isEmpty()).flatMap(Collection::stream).collect(Collectors.toList());
+		List<ProductCategoryDto> allCategoryList = categoryList.stream().map(next -> {
+			List<ProductCategoryDto> children = next.getChildren();
+			children.forEach(child -> child.setName(next.getName() + "/" + child.getName()));
+			return children;
+		}).filter(next -> !next.isEmpty()).flatMap(Collection::stream).collect(Collectors.toList());
 		Map<String, Long> categoryIdNameMap = allCategoryList
 		Map<String, Long> categoryIdNameMap = allCategoryList
 				.stream().collect(Collectors.toMap(ProductCategoryDto::getName, ProductCategoryDto::getId));
 				.stream().collect(Collectors.toMap(ProductCategoryDto::getName, ProductCategoryDto::getId));
 		Set<String> categoryNames = categoryIdNameMap.keySet();
 		Set<String> categoryNames = categoryIdNameMap.keySet();
@@ -668,8 +672,13 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 			}
 			}
 		});
 		});
 
 
-		List<String> snSet = firstSheet.stream().map(next -> next.get("组合商品货号").toString()).filter(StringUtils::isNotEmpty).distinct().collect(Collectors.toList());
-		List<String> existSnList = goodsDao.findBySns(snSet).stream().map(Goods::getSn).distinct().collect(Collectors.toList());
+		// excel中所有的sn
+		List<String> snSet = firstSheet.stream().map(next -> next.get("组合商品货号").toString()).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
+		// 获取重复的货号
+		List<String> repeatSnList = snSet.stream().collect(Collectors.groupingBy(e -> e, Collectors.counting()))
+				.entrySet().stream().filter(e -> e.getValue() > 1)
+				.map(Map.Entry::getKey).collect(Collectors.toList());
+		List<String> existSnList = goodsDao.findBySns(snSet.stream().distinct().collect(Collectors.toList())).stream().map(Goods::getSn).distinct().collect(Collectors.toList());
 
 
 		Map<String, Long> brandNameIdMap = mallFeignService.getList().stream().collect(Collectors.toMap(BrandDto::getName, BrandDto::getId));
 		Map<String, Long> brandNameIdMap = mallFeignService.getList().stream().collect(Collectors.toMap(BrandDto::getName, BrandDto::getId));
 
 
@@ -690,8 +699,7 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 
 
 			String lineErrMsg = "错误行号:第%s行,%s";
 			String lineErrMsg = "错误行号:第%s行,%s";
 			Map<String, String> beforeGoods = new HashMap<>();
 			Map<String, String> beforeGoods = new HashMap<>();
-
-			Map<String, String> snNameMap = new HashMap<>();
+			Map<String, Set<String>> snSubGoodsMap = new HashMap<>();
 			for (int i = 0; i < rows.size(); i++) {
 			for (int i = 0; i < rows.size(); i++) {
 				JSONObject objectMap = new JSONObject();
 				JSONObject objectMap = new JSONObject();
 				int rowNum = i + 2;
 				int rowNum = i + 2;
@@ -699,6 +707,11 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 				if (row.size() == 0) {
 				if (row.size() == 0) {
 					continue;
 					continue;
 				}
 				}
+				// 新商品标志
+				String sn = row.get("组合商品货号").toString();
+				if (StringUtils.isNotEmpty(sn)) {
+					beforeGoods.clear();
+				}
 				String subGoodsName = null;
 				String subGoodsName = null;
 				String subGoodsSku = null;
 				String subGoodsSku = null;
 				List<String> lineErrList = new ArrayList<>();
 				List<String> lineErrList = new ArrayList<>();
@@ -731,21 +744,16 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 					} else {
 					} else {
 						beforeGoods.put(fieldCode, value);
 						beforeGoods.put(fieldCode, value);
 					}
 					}
-					if("sn".equals(fieldCode)){
+					if ("sn".equals(fieldCode)) {
 						if (existSnList.contains(value)) {
 						if (existSnList.contains(value)) {
 							lineErrList.add("货号[" + value + "]已存在");
 							lineErrList.add("货号[" + value + "]已存在");
 						} else {
 						} else {
-							String goodsName = row.get("组合商品名称").toString();
-							if (snNameMap.containsKey(value) && !snNameMap.get(value).equals(goodsName)) {
+							if (repeatSnList.contains(value)) {
 								lineErrList.add("货号[" + value + "]重复");
 								lineErrList.add("货号[" + value + "]重复");
-							} else {
-								snNameMap.put(value, goodsName);
 							}
 							}
-							objectMap.put(fieldCode, value);
 						}
 						}
-					}
-					else
-					if ("marketPrice".equals(fieldCode)
+						objectMap.put(fieldCode, value);
+					} else if ("marketPrice".equals(fieldCode)
 							|| "discountPrice".equals(fieldCode)
 							|| "discountPrice".equals(fieldCode)
 							|| "groupPurchasePrice".equals(fieldCode)) {
 							|| "groupPurchasePrice".equals(fieldCode)) {
 						if (NumberUtils.isNumber(value)) {
 						if (NumberUtils.isNumber(value)) {
@@ -842,8 +850,21 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 						!objectMap.containsKey("memberFeeShowOrganId") &&
 						!objectMap.containsKey("memberFeeShowOrganId") &&
 						!objectMap.containsKey("freeFeeShowOrganId") &&
 						!objectMap.containsKey("freeFeeShowOrganId") &&
 						!objectMap.containsKey("replacementShowOrganId")) {
 						!objectMap.containsKey("replacementShowOrganId")) {
-					lineErrList.add("可售分部至少需要选择一个");
+					lineErrList.add("可售分部至少需要填写一个");
 				}
 				}
+				// 校验子商品sku是否重复
+				String tempSn = objectMap.get("sn").toString();
+				String tempSku = objectMap.get("sku").toString();
+				if (StringUtils.isNotEmpty(tempSn) && StringUtils.isNotEmpty(tempSku)) {
+					Set<String> set = snSubGoodsMap.getOrDefault(tempSn, new HashSet<>());
+					if (set.contains(tempSku)) {
+						lineErrList.add("子商品SKU重复");
+					} else {
+						set.add(tempSku);
+						snSubGoodsMap.put(tempSn, set);
+					}
+				}
+
 				if (!lineErrList.isEmpty()) {
 				if (!lineErrList.isEmpty()) {
 					errList.add(String.format(lineErrMsg, rowNum, String.join(",", lineErrList)));
 					errList.add(String.format(lineErrMsg, rowNum, String.join(",", lineErrList)));
 				}
 				}
@@ -863,6 +884,7 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 		List<GoodsWrapper.Goods> goodsList = jsonObjectList.stream().map(next -> {
 		List<GoodsWrapper.Goods> goodsList = jsonObjectList.stream().map(next -> {
 			String jsonString = next.toJSONString();
 			String jsonString = next.toJSONString();
 			GoodsWrapper.Goods goods = JSON.parseObject(jsonString, GoodsWrapper.Goods.class);
 			GoodsWrapper.Goods goods = JSON.parseObject(jsonString, GoodsWrapper.Goods.class);
+			goods.setSellCount(0);
 			GoodsWrapper.GoodsSub goodsSub = JSON.parseObject(jsonString, GoodsWrapper.GoodsSub.class);
 			GoodsWrapper.GoodsSub goodsSub = JSON.parseObject(jsonString, GoodsWrapper.GoodsSub.class);
 			ArrayList<GoodsWrapper.GoodsSub> arrayList = new ArrayList<>();
 			ArrayList<GoodsWrapper.GoodsSub> arrayList = new ArrayList<>();
 			arrayList.add(goodsSub);
 			arrayList.add(goodsSub);