Browse Source

状态同步更新

yuanliang 1 year ago
parent
commit
324ae975aa
1 changed files with 86 additions and 35 deletions
  1. 86 35
      mec-biz/src/main/java/com/ym/mec/biz/service/impl/GoodsServiceImpl.java

+ 86 - 35
mec-biz/src/main/java/com/ym/mec/biz/service/impl/GoodsServiceImpl.java

@@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+import com.microsvc.toolkit.middleware.oss.OssPluginContext;
+import com.microsvc.toolkit.middleware.oss.impl.TencentOssPlugin;
 import com.ym.mec.biz.dal.dao.GoodsDao;
 import com.ym.mec.biz.dal.dao.GoodsProcurementDao;
 import com.ym.mec.biz.dal.dao.OrganizationDao;
@@ -54,14 +56,15 @@ import com.ym.mec.thirdparty.message.MessageSenderPluginContext;
 import com.ym.mec.util.collection.MapUtil;
 import com.ym.mec.util.excel.POIUtil;
 import com.ym.mec.util.ini.IniFileUtil;
+import com.ym.mec.util.upload.UploadUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang.math.NumberUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.poi.ss.usermodel.PictureData;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.system.ApplicationHome;
 import org.springframework.core.io.ClassPathResource;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Isolation;
@@ -70,9 +73,15 @@ import org.springframework.util.CollectionUtils;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
+import java.io.BufferedWriter;
 import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.math.BigDecimal;
+import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.function.Function;
@@ -109,6 +118,9 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 
 	@Autowired
 	private GoodsCategoryService goodsCategoryService;
+
+	@Autowired
+	private OssPluginContext ossPluginContext;
 	@Override
 	public BaseDAO<Integer, Goods> getDAO() {
 		return goodsDao;
@@ -576,7 +588,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 getImportErrLogFile(errList);
+			return getExportErrLogFile(errList);
 		}
 
 		// 查询商品分类
@@ -611,12 +623,13 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 			subGoods = new ArrayList<>(mallFeignService.getProductList(dto).getRows());
 		} catch (Exception e) {
 			errList.add("商城服务调用失败");
-			return getImportErrLogFile(errList);
+			return getExportErrLogFile(errList);
 		}
 		Map<String, PmsProductDto>skuMap = subGoods.stream().collect(Collectors.toMap(next -> (next.getName() + "_" + next.getSkuCode()), Function.identity()));
 
 //		List<GoodsWrapper.Goods> goodsList = new ArrayList<>();
 		List<JSONObject> jsonObjectList = new ArrayList<>();
+		Set<String> snSet = new HashSet<>();
 		for (Map.Entry<String, List<Map<String, Object>>> sheetData : sheetsListMap.entrySet()) {
 			List<Map<String, Object>> rows = sheetData.getValue();
 			if (rows.isEmpty()) {
@@ -628,7 +641,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));
-				return getImportErrLogFile(errList);
+				return getExportErrLogFile(errList);
 			}
 
 			String lineErrMsg = "第%s行存在错误:%s";
@@ -639,8 +652,8 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 			String subGoodsSku = null;
 			for (int i = 0; i < rows.size(); i++) {
 				int rowNum = i + 2;
-				GoodsWrapper.Goods goods = new GoodsWrapper.Goods();
-				GoodsWrapper.GoodsSub goodsSub = new GoodsWrapper.GoodsSub();
+//				GoodsWrapper.Goods goods = new GoodsWrapper.Goods();
+//				GoodsWrapper.GoodsSub goodsSub = new GoodsWrapper.GoodsSub();
 				Map<String, Object> row = rows.get(i);
 				if (row.size() == 0) {
 					continue;
@@ -669,6 +682,14 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 					} else {
 						beforeGoods.put(fieldCode, value);
 					}
+//					if("sn".equals(fieldCode)){
+//						if (snSet.contains(value)) {
+//							lineErrList.add("货号[" + value + "]重复");
+//						} else {
+//							snSet.add(value);
+//						}
+//					}
+//					else
 					if ("marketPrice".equals(fieldCode)
 							|| "discountPrice".equals(fieldCode)
 							|| "groupPurchasePrice".equals(fieldCode)) {
@@ -747,7 +768,6 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 						if (BigDecimal.ZERO.compareTo(subGoodsPrice) > 0) {
 							lineErrList.add("字段‘" + fieldName + "’不能为负数");
 						}
-						goodsSub.setGoodsPrice(subGoodsPrice);
 					} else {
 						objectMap.put(fieldCode, value);
 					}
@@ -756,8 +776,6 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 					errList.add(String.format(lineErrMsg, rowNum, String.join(",", lineErrList)));
 				}
 				jsonObjectList.add(objectMap);
-//				goods.setGoodsSubList(Collections.singletonList(goodsSub));
-//				goodsList.add(goods);
 			}
 		}
 
@@ -767,37 +785,46 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 
 		if (!errList.isEmpty()) {
 			// 存在错误信息,添加错误日志文件返回
-			return getImportErrLogFile(errList);
+			return getExportErrLogFile(errList);
 		}
 
+		List<GoodsWrapper.Goods> goodsList = jsonObjectList.stream().map(next -> {
+			String jsonString = next.toJSONString();
+			GoodsWrapper.Goods goods = JSON.parseObject(jsonString, GoodsWrapper.Goods.class);
+			GoodsWrapper.GoodsSub goodsSub = JSON.parseObject(jsonString, GoodsWrapper.GoodsSub.class);
+			ArrayList<GoodsWrapper.GoodsSub> arrayList = new ArrayList<>();
+			arrayList.add(goodsSub);
+			goods.setGoodsSubList(arrayList);
+			return goods;
+		}).collect(Collectors.toList());
 
-//		Map<String, List<GoodsWrapper.Goods>> goodsGroup = jsonObjectList.stream().collect(Collectors.groupingBy(next -> next.getClass() + "_" + next.getSn()));
+		Map<String, List<GoodsWrapper.Goods>> goodsGroup = goodsList.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);
-//		});
+		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;
 	}
 
@@ -1228,8 +1255,32 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 		return mallFeignService.listWithChildren();
 	}
 
-	private String getImportErrLogFile(List<String> errList){
+	private String getExportErrLogFile(List<String> errList){
 
+		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();
+		}
+		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 "";
 	}
 }