瀏覽代碼

状态同步更新

yuanliang 1 年之前
父節點
當前提交
6feea5139d

+ 2 - 1
mec-application/src/main/java/com/ym/mec/web/controller/GoodsController.java

@@ -15,6 +15,7 @@ import com.ym.mec.biz.service.SysUserService;
 import com.ym.mec.common.controller.BaseController;
 import com.ym.mec.common.dto.BrandDto;
 import com.ym.mec.common.dto.ProductAttributeCategoryDto;
+import com.ym.mec.common.dto.ProductCategoryDto;
 import com.ym.mec.common.page.PageInfo;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -127,7 +128,7 @@ public class GoodsController extends BaseController {
         List<Goods> rows = page.getRows();
         if (!rows.isEmpty()) {
             Map<String, String> brandIdNameMap = goodsService.queryGoodsBrandList().stream().collect(Collectors.toMap(next -> next.getId().toString(), BrandDto::getName));
-            Map<Integer, String> categoryIdNameMap = goodsService.queryGoodsCategoryList().stream().collect(Collectors.toMap(next -> next.getId().intValue(), ProductAttributeCategoryDto::getName));
+            Map<Integer, String> categoryIdNameMap = goodsService.queryGoodsCategoryList().stream().collect(Collectors.toMap(next -> next.getId().intValue(), ProductCategoryDto::getName));
 
             for (Goods row : rows) {
                 row.setBrandName(brandIdNameMap.getOrDefault(row.getBrand(), row.getBrand()));

+ 2 - 2
mec-biz/src/main/java/com/ym/mec/biz/service/GoodsService.java

@@ -156,9 +156,9 @@ public interface GoodsService extends BaseService<Integer, Goods> {
 
     Boolean goodsStockCountSynchronize(List<GoodsSubStockModel> goodsSubStockModels);
 
-    List<ProductCategoryDto> queryGoodsTypeList();
+    List<ProductAttributeCategoryDto> queryGoodsTypeList();
 
     List<BrandDto> queryGoodsBrandList();
 
-    List<ProductAttributeCategoryDto> queryGoodsCategoryList();
+    List<ProductCategoryDto> queryGoodsCategoryList();
 }

+ 67 - 64
mec-biz/src/main/java/com/ym/mec/biz/service/impl/GoodsServiceImpl.java

@@ -576,7 +576,7 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 		Map<String, List<Map<String, Object>>> sheetsListMap = POIUtil.importExcel(new ByteArrayInputStream(file.getBytes()), 2, file.getOriginalFilename());
 		if (sheetsListMap.values().isEmpty()) {
 			errList.add("excel未解析到数据");
-			return "";
+			return getImportErrLogFile(errList);
 		}
 
 		// 查询商品分类
@@ -599,7 +599,7 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 		List<Map<String, Object>> firstSheet = sheetsListMap.get(0);
 		List<String> skuList = firstSheet.stream().map(next -> next.get("SKU").toString()).distinct().collect(Collectors.toList());
 
-		List<PmsProductDto> subGoods = new ArrayList<>();
+		List<PmsProductDto> subGoods;
 		try {
 			// 查询SKU对应商品
 			PmsProductQueryParamDto dto = PmsProductQueryParamDto.builder()
@@ -608,13 +608,15 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 					.pageSize(9999)
 					.build();
 			dto.setJson(JSON.toJSONString(dto));
-			subGoods.addAll(mallFeignService.getProductList(dto).getRows());
+			subGoods = new ArrayList<>(mallFeignService.getProductList(dto).getRows());
 		} catch (Exception e) {
 			errList.add("商城服务调用失败");
+			return getImportErrLogFile(errList);
 		}
 		Map<String, PmsProductDto>skuMap = subGoods.stream().collect(Collectors.toMap(next -> (next.getName() + "_" + next.getSkuCode()), Function.identity()));
 
-		List<GoodsWrapper.Goods> goodsList = new ArrayList<>();
+//		List<GoodsWrapper.Goods> goodsList = new ArrayList<>();
+		List<JSONObject> jsonObjectList = new ArrayList<>();
 		for (Map.Entry<String, List<Map<String, Object>>> sheetData : sheetsListMap.entrySet()) {
 			List<Map<String, Object>> rows = sheetData.getValue();
 			if (rows.isEmpty()) {
@@ -626,7 +628,7 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 			List<String> defaultField = templateFields.stream().filter(firstRow::containsKey).collect(Collectors.toList());
 			if (!defaultField.isEmpty()) {
 				errList.add("导入字段缺省:" + String.join(",", defaultField));
-				break;
+				return getImportErrLogFile(errList);
 			}
 
 			String lineErrMsg = "第%s行存在错误:%s";
@@ -643,6 +645,7 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 				if (row.size() == 0) {
 					continue;
 				}
+				List<String> lineErrList = new ArrayList<>();
 				for (Map.Entry<String, Object> entry : row.entrySet()) {
 					String fieldName = entry.getKey();
 					if (!templateFields.contains(fieldName)) {
@@ -654,10 +657,10 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 					// 非子商品字段为空,往上一行获取值
 					if (StringUtils.isEmpty(value)) {
 						if ("subGoodsName".equals(fieldCode) || "sku".equals(fieldCode) || "subGoodsPrice".equals(fieldCode)) {
-							errList.add(String.format(lineErrMsg, rowNum, "字段‘" + fieldName + "’为空"));
+							lineErrList.add("字段‘" + fieldName + "’为空");
 						} else {
 							if (!beforeGoods.containsKey(fieldCode)) {
-								errList.add(String.format(lineErrMsg, rowNum, "字段‘" + fieldName + "’为空"));
+								lineErrList.add("字段‘" + fieldName + "’为空");
 								continue;
 							} else {
 								value = beforeGoods.get(fieldCode);
@@ -672,12 +675,13 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 						if (NumberUtils.isNumber(value)) {
 							BigDecimal prize = new BigDecimal(value);
 							if (BigDecimal.ZERO.compareTo(prize) > 0) {
-								errList.add(String.format(lineErrMsg, rowNum, "字段‘" + fieldName + "’不能为负数"));
+								lineErrList.add("字段‘" + fieldName + "’不能为负数");
 							} else {
 								objectMap.put(fieldCode, value);
 							}
 						} else {
-							errList.add(String.format(lineErrMsg, rowNum, "字段‘" + fieldName + "’类型错误"));
+							lineErrList.add("字段‘" + fieldName + "’类型错误");
+
 						}
 					} else if ("type".equals(fieldCode)) {
 						String tempValue = value;
@@ -685,13 +689,13 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 						if (match) {
 							objectMap.put(fieldCode, value);
 						} else {
-							errList.add(String.format(lineErrMsg, rowNum, "字段‘" + fieldName + "’类型不支持"));
+							lineErrList.add("字段‘" + fieldName + "’类型不支持");
 						}
 					} else if ("goodsCategoryName".equals(fieldCode)) {
 						if(categoryNames.contains(value)){
 							objectMap.put("goodsCategoryId", categoryIdNameMap.get(value));
 						}else {
-							errList.add(String.format(lineErrMsg, rowNum, "字段‘" + fieldName + "’类型不支持"));
+							lineErrList.add("字段‘" + fieldName + "’类型不支持");
 						}
 					} else if ("educationShowOrganName".equals(fieldCode)
 							|| "courseFeeShowOrganName".equals(fieldCode)
@@ -702,7 +706,7 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 						List<String> orgIds = new ArrayList<>();
 						for (String orgName : value.split(",")) {
 							if (!orgNameIdMap.containsKey(orgName)) {
-								errList.add(String.format(lineErrMsg, rowNum, "字段‘" + fieldName + "’:‘" + value + "’分部不支持"));
+								lineErrList.add("字段‘" + fieldName + "’:‘" + value + "’分部不支持");
 							} else {
 								orgIds.add(orgNameIdMap.get(orgName).toString());
 							}
@@ -710,17 +714,6 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 						String join = String.join(",", orgIds);
 						String fieldKey = fieldCode.replaceAll("Name", "Id");
 						objectMap.put(fieldKey, join);
-//						if("educationShowOrganName".equals(fieldCode)){
-//							goods.setEducationShowOrganId(join);
-//						}else if("courseFeeShowOrganName".equals(fieldCode)){
-//							goods.setCourseFeeShowOrganId(join);
-//						}else if("memberFeeShowOrganName".equals(fieldCode)){
-//							goods.setMemberFeeShowOrganId(join);
-//						}else if("freeFeeShowOrganName".equals(fieldCode)){
-//							goods.setFreeFeeShowOrganId(join);
-//						}else if("replacementShowOrganName".equals(fieldCode)){
-//							goods.setReplacementShowOrganId(join);
-//						}
 					} else if ("subGoodsName".equals(fieldCode)) {
 						subGoodsName = value;
 						if (StringUtils.isNotEmpty(subGoodsSku)) {
@@ -732,7 +725,7 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 								objectMap.put("stock", dto.getStock());
 								objectMap.put("goodsStatus", dto.getPublishStatus()==1);
 							} else {
-								errList.add(String.format(lineErrMsg, rowNum, "子商品‘" + value + "[" + subGoodsSku + "]’不存在"));
+								lineErrList.add("子商品‘" + value + "[" + subGoodsSku + "]’不存在");
 							}
 						}
 					} else if ("sku".equals(fieldCode)) {
@@ -746,60 +739,65 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 								objectMap.put("stock", dto.getStock());
 								objectMap.put("goodsStatus", dto.getPublishStatus()==1);
 							} else {
-								errList.add(String.format(lineErrMsg, rowNum, "子商品‘" + value + "[" + subGoodsSku + "]’不存在"));
+								lineErrList.add("子商品‘" + value + "[" + subGoodsSku + "]’不存在");
 							}
 						}
 					} else if ("subGoodsPrice".equals(fieldCode)) {
 						BigDecimal subGoodsPrice = new BigDecimal(value);
 						if (BigDecimal.ZERO.compareTo(subGoodsPrice) > 0) {
-							errList.add(String.format(lineErrMsg, rowNum, "字段‘" + fieldName + "’不能为负数"));
+							lineErrList.add("字段‘" + fieldName + "’不能为负数");
 						}
 						goodsSub.setGoodsPrice(subGoodsPrice);
 					} else {
 						objectMap.put(fieldCode, value);
 					}
 				}
-				goods.setGoodsSubList(Collections.singletonList(goodsSub));
-				goodsList.add(goods);
+				if (!lineErrList.isEmpty()) {
+					errList.add(String.format(lineErrMsg, rowNum, String.join(",", lineErrList)));
+				}
+				jsonObjectList.add(objectMap);
+//				goods.setGoodsSubList(Collections.singletonList(goodsSub));
+//				goodsList.add(goods);
 			}
 		}
 
-		if (goodsList.isEmpty()) {
+		if (jsonObjectList.isEmpty()) {
 			errList.add("未解析到数据");
 		}
 
 		if (!errList.isEmpty()) {
 			// 存在错误信息,添加错误日志文件返回
-			return "";
+			return getImportErrLogFile(errList);
 		}
 
-		Map<String, List<GoodsWrapper.Goods>> goodsGroup = goodsList.stream().collect(Collectors.groupingBy(next -> next.getName() + "_" + next.getSn()));
-
 
-		// 数据合并
-		Map<Integer, Integer> skuIdMap = subGoods.stream().collect(Collectors.toMap(PmsProductDto::getSkuStockId, PmsProductDto::getStock));
-
-		goodsGroup.forEach((key, value) -> {
-			GoodsWrapper.Goods good = value.get(0);
-			List<GoodsWrapper.GoodsSub> goodsSubList = value.stream().map(GoodsWrapper.Goods::getGoodsSubList).flatMap(Collection::stream).collect(Collectors.toList());
-			Integer stock = goodsSubList.stream().map(next -> skuIdMap.get(next.getSku())).min(Integer::compareTo).orElse(0);
-
-			Goods goods = JSON.parseObject(JSON.toJSONString(good), Goods.class);
-			goods.setStatus(YesOrNoEnum.NO);
-			goods.setStockCount(stock);
-			this.insert(goods);
-
-			List<GoodsSub> subList = goodsSubList.stream().map(next -> {
-				GoodsSub sub = new GoodsSub();
-				sub.setGoodsId(goods.getId());
-				sub.setMallGoodsId(next.getMallGoodsId());
-				sub.setSku(next.getSku());
-				sub.setGoodsStatus(next.getGoodsStatus());
-				sub.setGoodsPrice(next.getGoodsPrice());
-				return sub;
-			}).collect(Collectors.toList());
-			goodsSubMapper.saveBatch(subList);
-		});
+//		Map<String, List<GoodsWrapper.Goods>> goodsGroup = jsonObjectList.stream().collect(Collectors.groupingBy(next -> next.getClass() + "_" + next.getSn()));
+//
+//
+//		// 数据合并
+//		Map<Integer, Integer> skuIdMap = subGoods.stream().collect(Collectors.toMap(PmsProductDto::getSkuStockId, PmsProductDto::getStock));
+//
+//		goodsGroup.forEach((key, value) -> {
+//			GoodsWrapper.Goods good = value.get(0);
+//			List<GoodsWrapper.GoodsSub> goodsSubList = value.stream().map(GoodsWrapper.Goods::getGoodsSubList).flatMap(Collection::stream).collect(Collectors.toList());
+//			Integer stock = goodsSubList.stream().map(next -> skuIdMap.get(next.getSku())).min(Integer::compareTo).orElse(0);
+//
+//			Goods goods = JSON.parseObject(JSON.toJSONString(good), Goods.class);
+//			goods.setStatus(YesOrNoEnum.NO);
+//			goods.setStockCount(stock);
+//			this.insert(goods);
+//
+//			List<GoodsSub> subList = goodsSubList.stream().map(next -> {
+//				GoodsSub sub = new GoodsSub();
+//				sub.setGoodsId(goods.getId());
+//				sub.setMallGoodsId(next.getMallGoodsId());
+//				sub.setSku(next.getSku());
+//				sub.setGoodsStatus(next.getGoodsStatus());
+//				sub.setGoodsPrice(next.getGoodsPrice());
+//				return sub;
+//			}).collect(Collectors.toList());
+//			goodsSubMapper.saveBatch(subList);
+//		});
 		return null;
 	}
 
@@ -1142,16 +1140,16 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 			return false;
 		}
 		log.info(Thread.currentThread().getName() + "开始同步商品状态:" + JSON.toJSONString(goodsSubModelList));
-		List<Integer> goodIds = goodsSubModelList.stream().map(GoodsSubModel::getMallGoodsId).distinct().collect(Collectors.toList());
+		List<Integer> skuIds = goodsSubModelList.stream().map(GoodsSubModel::getSkuId).distinct().collect(Collectors.toList());
 		QueryWrapper<GoodsSub> queryWrapper = new QueryWrapper<>();
-		queryWrapper.in("mall_goods_id_", goodIds);
+		queryWrapper.in("sku_", skuIds);
 		List<GoodsSub> goodsSubs = goodsSubMapper.selectList(queryWrapper);
 		if (CollectionUtils.isEmpty(goodsSubs)) {
 			return true;
 		}
-		Map<String, Boolean> keyStatusMap = goodsSubModelList.stream().collect(Collectors.toMap(next -> next.getMallGoodsId() + "_" + next.getSkuId(), GoodsSubModel::getGoodsStatus));
+		Map<Integer, Boolean> keyStatusMap = goodsSubModelList.stream().collect(Collectors.toMap(GoodsSubModel::getSkuId, GoodsSubModel::getGoodsStatus));
 		log.info(Thread.currentThread().getName() + "开始同步商品状态:0/" + goodsSubs.size());
-		List<GoodsSub> goodsSubList = goodsSubs.stream().peek(next -> next.setGoodsStatus(keyStatusMap.get(next.getMallGoodsId() + "_" + next.getSku()))).collect(Collectors.toList());
+		List<GoodsSub> goodsSubList = goodsSubs.stream().peek(next -> next.setGoodsStatus(keyStatusMap.get(next.getSku()))).collect(Collectors.toList());
 		// 下架的商品
 		List<GoodsSub> downGoods = goodsSubList.stream().filter(next -> Boolean.FALSE.equals(next.getGoodsStatus())).collect(Collectors.toList());
 		if(!CollectionUtils.isEmpty(downGoods)){
@@ -1216,8 +1214,8 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 	}
 
 	@Override
-	public List<ProductCategoryDto> queryGoodsTypeList() {
-		return mallFeignService.listWithChildren();
+	public List<ProductAttributeCategoryDto> queryGoodsTypeList() {
+		return mallFeignService.getProductAttributeCategoryList();
 	}
 
 	@Override
@@ -1226,7 +1224,12 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 	}
 
 	@Override
-	public List<ProductAttributeCategoryDto> queryGoodsCategoryList() {
-		return mallFeignService.getProductAttributeCategoryList();
+	public List<ProductCategoryDto> queryGoodsCategoryList() {
+		return mallFeignService.listWithChildren();
+	}
+
+	private String getImportErrLogFile(List<String> errList){
+
+		return "";
 	}
 }