Browse Source

同步库存

yuanliang 1 year ago
parent
commit
ebd1e32b71
1 changed files with 74 additions and 43 deletions
  1. 74 43
      mec-biz/src/main/java/com/ym/mec/biz/service/impl/GoodsServiceImpl.java

+ 74 - 43
mec-biz/src/main/java/com/ym/mec/biz/service/impl/GoodsServiceImpl.java

@@ -76,11 +76,15 @@ import javax.annotation.Resource;
 import java.io.BufferedWriter;
 import java.io.ByteArrayInputStream;
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.OutputStreamWriter;
 import java.math.BigDecimal;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -1208,34 +1212,57 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 		if (CollectionUtils.isEmpty(goodsSubs)) {
 			return true;
 		}
-		Map<Integer, Integer> stockMap = goodsSubStockModels.stream().collect(Collectors.toMap(GoodsSubStockModel::getSku, GoodsSubStockModel::getStock));
+//		Map<Integer, Integer> stockMap = goodsSubStockModels.stream().collect(Collectors.toMap(GoodsSubStockModel::getSku, GoodsSubStockModel::getStock));
 		List<Integer> goodIds = goodsSubs.stream().map(GoodsSub::getGoodsId).distinct().collect(Collectors.toList());
 		goodsDao.lockGoods(goodIds);
-		List<Goods> goodsList = goodsDao.findGoodsByIds(goodIds.stream().map(String::valueOf).collect(Collectors.joining(",")));
-		Map<Integer, Integer> goodStockMap = goodsList.stream().collect(Collectors.toMap(Goods::getId, Goods::getStockCount));
+//		List<Goods> goodsList = goodsDao.findGoodsByIds(goodIds.stream().map(String::valueOf).collect(Collectors.joining(",")));
+//		Map<Integer, Integer> goodStockMap = goodsList.stream().collect(Collectors.toMap(Goods::getId, Goods::getStockCount));
+
+		queryWrapper = new QueryWrapper<>();
+		queryWrapper.in("goods_id_", goodIds);
+		List<GoodsSub> allGoodsSubs = goodsSubMapper.selectList(queryWrapper);
+		List<String> allSkuIdList = allGoodsSubs.stream().map(next -> next.getSku().toString()).distinct().collect(Collectors.toList());
+		PmsProductQueryParamDto dto = new PmsProductQueryParamDto();
+		dto.setSkuStockIds(String.join(",", allSkuIdList));
+		dto.setPageNum(1);
+		dto.setPageSize(9999);
+		dto.setJson(JSON.toJSONString(dto));
+		Map<Integer, Integer> skuStockMap = mallFeignService.getProductList(dto).getRows().stream().collect(Collectors.toMap(PmsProductDto::getSkuStockId, PmsProductDto::getStock));
+		goodsSubStockModels.forEach(next-> skuStockMap.put(next.getSku(),next.getStock()));
 
-		List<Goods> updateGoods = new ArrayList<>();
-		for (GoodsSub goodsSub : goodsSubs) {
-			Integer goodsId = goodsSub.getGoodsId();
-			Integer stock = stockMap.get(goodsSub.getSku());
-			int updateStock = Math.min(goodStockMap.get(goodsId), stock);
+		Map<Integer, List<GoodsSub>> collect = allGoodsSubs.stream().collect(Collectors.groupingBy(GoodsSub::getGoodsId));
 
+		List<Goods> collect1 = collect.entrySet().stream().map(next -> {
 			Goods goods = new Goods();
-			goods.setId(goodsId);
-			goods.setStockCount(updateStock);
-			updateGoods.add(goods);
-		}
+			goods.setId(next.getKey());
+			Integer minStock = next.getValue().stream().map(o -> skuStockMap.getOrDefault(o.getSku(), 0)).min(Integer::compareTo).get();
+			goods.setStockCount(minStock);
+			return goods;
+		}).collect(Collectors.toList());
+
+
+//		List<Goods> updateGoods = new ArrayList<>();
+//		for (GoodsSub goodsSub : goodsSubs) {
+//			Integer goodsId = goodsSub.getGoodsId();
+//			Integer stock = stockMap.get(goodsSub.getSku());
+//			int updateStock = Math.min(goodStockMap.get(goodsId), stock);
+//
+//			Goods goods = new Goods();
+//			goods.setId(goodsId);
+//			goods.setStockCount(updateStock);
+//			updateGoods.add(goods);
+//		}
 		// 处理同一个商品,多个子商品同时更新库存,取最小的库存
-		Map<Integer, Optional<Goods>> collect = updateGoods.stream().collect(Collectors.groupingBy(Goods::getId, Collectors.minBy(Comparator.comparingInt(Goods::getStockCount))));
-		List<Goods> collect1 = collect.entrySet().stream().map(next -> {
-			if (next.getValue().isPresent()) {
-				Goods goods = new Goods();
-				goods.setId(next.getKey());
-				goods.setStockCount(next.getValue().get().getStockCount());
-				return goods;
-			}
-			return null;
-		}).filter(Objects::nonNull).collect(Collectors.toList());
+//		Map<Integer, Optional<Goods>> collect = updateGoods.stream().collect(Collectors.groupingBy(Goods::getId, Collectors.minBy(Comparator.comparingInt(Goods::getStockCount))));
+//		List<Goods> collect1 = collect2.entrySet().stream().map(next -> {
+//			if (next.getValue().isPresent()) {
+//				Goods goods = new Goods();
+//				goods.setId(next.getKey());
+//				goods.setStockCount(next.getValue().get().getStockCount());
+//				return goods;
+//			}
+//			return null;
+//		}).filter(Objects::nonNull).collect(Collectors.toList());
 		goodsDao.updateStock(collect1);
 		return true;
 	}
@@ -1255,32 +1282,36 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 		return mallFeignService.listWithChildren();
 	}
 
-	private String getExportErrLogFile(List<String> errList){
+	private String getExportErrLogFile(List<String> errList) {
+		if (CollectionUtils.isEmpty(errList)) {
+			return null;
+		}
 
 		String basePath = new ApplicationHome(this.getClass()).getSource().getParentFile().getPath();
 		String format = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
 		File file = new File(basePath + File.separator + "goods_group_import_err" + format + ".csv");
-		if (!file.getParentFile().exists()) {
-			file.getParentFile().mkdirs();
+		try {
+			if (!file.getParentFile().exists()) {
+				file.getParentFile().mkdirs();
+			}
+			file.createNewFile();
+		} catch (IOException e) {
+			throw new RuntimeException(e);
+		}
+		try {
+			BufferedWriter csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(file.toPath()), StandardCharsets.UTF_8), 1024);
+
+			for (String errMsg : errList) {
+				csvFileOutputStream.write(errMsg);
+				csvFileOutputStream.newLine();
+			}
+			csvFileOutputStream.flush();
+			csvFileOutputStream.close();
+			String folder = "daya/download/" + UploadUtil.getFileFloder();
+			return ossPluginContext.getPluginService(TencentOssPlugin.PLUGIN_NAME).uploadFile(folder, file);
+		} catch (IOException e) {
+			e.printStackTrace();
 		}
-		OutputStream ouputStream = null;
-
-//		try {
-//			BufferedWriter output = new BufferedWriter(new FileWriter(file,true));//true 则追加写入text文本
-//			output.write("XH,VL");//表头 -> 对应下方write 写入数据的列数
-//			for(int i = 0;i<errList.size();i++) {//遍历写入数据
-//				output.write("\r\n");//换行
-//				//((CreateCSV)this.list.get(i)).row 这样写是因为this.list.get(i).row报错,所以先将this.list.get(i)转为class类的CreateCSV类型,再取对象中的属性
-//				output.write(((CreateCSV)this.list.get(i)).row + "," + ((CreateCSV)this.list.get(i)).value);
-//			}
-//			String folder = "daya/download/" + UploadUtil.getFileFloder();
-//			String url = ossPluginContext.getPluginService(TencentOssPlugin.PLUGIN_NAME).uploadFile(folder, file);
-//
-//			output.flush();
-//			output.close();
-//		}catch(IOException e) {
-//			e.printStackTrace();
-//		}
 		return "";
 	}
 }