瀏覽代碼

商品导入添加图片支持

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

+ 84 - 28
mec-biz/src/main/java/com/ym/mec/biz/service/impl/GoodsServiceImpl.java

@@ -74,6 +74,8 @@ import org.springframework.util.CollectionUtils;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
 import java.io.*;
 import java.math.BigDecimal;
 import java.nio.charset.StandardCharsets;
@@ -663,6 +665,7 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 		Map<String, Long> brandNameIdMap = mallFeignService.getList().stream().collect(Collectors.toMap(BrandDto::getName, BrandDto::getId));
 
 		List<JSONObject> jsonObjectList = new ArrayList<>();
+		Map<String, byte[]> snImageMap = new HashMap<>();
 		for (Map.Entry<String, List<Map<String, Object>>> sheetData : sheetsListMap.entrySet()) {
 			List<Map<String, Object>> rows = sheetData.getValue();
 			if (rows.isEmpty()) {
@@ -678,7 +681,7 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 			}
 
 			String lineErrMsg = "错误行号:第%s行,%s";
-			Map<String, String> beforeGoods = new HashMap<>();
+			Map<String, Object> beforeGoods = new HashMap<>();
 			Map<String, Set<String>> snSubGoodsMap = new HashMap<>();
 			for (int i = 0; i < rows.size(); i++) {
 				JSONObject objectMap = new JSONObject();
@@ -701,10 +704,10 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 						continue;
 					}
 					String fieldCode = columns.get(fieldName);
-					String value = entry.getValue().toString();
+					Object value = entry.getValue();
 
 					// 非子商品字段为空,往上一行获取值
-					if (StringUtils.isEmpty(value)) {
+					if (StringUtils.isEmpty(value.toString())) {
 						if ("subGoodsName".equals(fieldCode) || "sku".equals(fieldCode) || "subGoodsPrice".equals(fieldCode)) {
 							lineErrList.add("字段‘" + fieldName + "’为空");
 						} else {
@@ -722,13 +725,19 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 							}
 						}
 					} else {
+						if ("image".equals(fieldCode) && value instanceof List) { // 图片数组 取第一个
+							Object data = ((List<?>) value).get(0);
+							if (data instanceof PictureData) {
+								value = ((PictureData) data).getData();
+							}
+						}
 						beforeGoods.put(fieldCode, value);
 					}
 					if ("sn".equals(fieldCode)) {
-						if (existSnList.contains(value)) {
+						if (existSnList.contains(value.toString())) {
 							lineErrList.add("货号[" + value + "]已存在");
 						} else {
-							if (repeatSnList.contains(value)) {
+							if (repeatSnList.contains(value.toString())) {
 								lineErrList.add("货号[" + value + "]重复");
 							}
 						}
@@ -736,8 +745,8 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 					} else if ("marketPrice".equals(fieldCode)
 							|| "discountPrice".equals(fieldCode)
 							|| "groupPurchasePrice".equals(fieldCode)) {
-						if (NumberUtils.isNumber(value)) {
-							BigDecimal prize = new BigDecimal(value);
+						if (NumberUtils.isNumber(value.toString())) {
+							BigDecimal prize = new BigDecimal(value.toString());
 							if (BigDecimal.ZERO.compareTo(prize) > 0) {
 								lineErrList.add("字段‘" + fieldName + "’不能为负数");
 							} else {
@@ -748,7 +757,7 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 
 						}
 					} else if ("type".equals(fieldCode)) {
-						String tempValue = value;
+						String tempValue = value.toString();
 						Optional<GoodsType> first = Arrays.stream(GoodsType.values()).filter(next -> next.getDesc().equals(tempValue)).findFirst();
 						if (first.isPresent()) {
 							objectMap.put(fieldCode, first.get().getCode());
@@ -756,8 +765,8 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 							lineErrList.add("字段‘" + fieldName + "’类型不支持");
 						}
 					} else if ("goodsCategoryName".equals(fieldCode)) {
-						if(categoryNames.contains(value)){
-							objectMap.put("goodsCategoryId", categoryIdNameMap.get(value).intValue());
+						if(categoryNames.contains(value.toString())){
+							objectMap.put("goodsCategoryId", categoryIdNameMap.get(value.toString()));
 						}else {
 							lineErrList.add("字段‘" + fieldName + "’不支持");
 						}
@@ -767,9 +776,9 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 							|| "freeFeeShowOrganName".equals(fieldCode)
 							|| "replacementShowOrganName".equals(fieldCode)
 					) {
-						if (StringUtils.isNotEmpty(value)) {
+						if (StringUtils.isNotEmpty(value.toString())) {
 							List<String> orgIds = new ArrayList<>();
-							for (String orgName : value.split("[,,]")) {
+							for (String orgName : value.toString().split("[,,]")) {
 								if (!orgNameIdMap.containsKey(orgName)) {
 									lineErrList.add("字段‘" + fieldName + "’:‘" + value + "’分部不支持");
 								} else {
@@ -781,7 +790,7 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 							objectMap.put(fieldKey, join);
 						}
 					} else if ("subGoodsName".equals(fieldCode)) {
-						subGoodsName = value;
+						subGoodsName = value.toString();
 						if (StringUtils.isNotEmpty(subGoodsSku)) {
 							String key = subGoodsName + "_" + subGoodsSku;
 							if (skuMap.containsKey(key)) {
@@ -795,7 +804,7 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 							}
 						}
 					} else if ("sku".equals(fieldCode)) {
-						subGoodsSku = value;
+						subGoodsSku = value.toString();
 						if (StringUtils.isNotEmpty(subGoodsName)) {
 							String key = subGoodsName + "_" + subGoodsSku;
 							if (skuMap.containsKey(key)) {
@@ -809,13 +818,13 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 							}
 						}
 					} else if ("subGoodsPrice".equals(fieldCode)) {
-						BigDecimal subGoodsPrice = new BigDecimal(value);
+						BigDecimal subGoodsPrice = new BigDecimal(value.toString());
 						if (BigDecimal.ZERO.compareTo(subGoodsPrice) > 0) {
 							lineErrList.add("字段‘" + fieldName + "’不能为负数");
 						}
 					} else if ("brand".equals(fieldCode)) {
-						if (brandNameIdMap.containsKey(value)) {
-							objectMap.put("brand", brandNameIdMap.get(value));
+						if (brandNameIdMap.containsKey(value.toString())) {
+							objectMap.put("brand", brandNameIdMap.get(value.toString()));
 						} else {
 							lineErrList.add("组合商品品牌‘" + value + "’不支持");
 						}
@@ -835,6 +844,8 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 				// 校验子商品sku是否重复
 				String tempSn = objectMap.get("sn").toString();
 				String tempSku = objectMap.get("sku").toString();
+				byte[] images = objectMap.getBytes("image");
+				snImageMap.put(tempSn, images);
 				if (StringUtils.isNotEmpty(tempSn) && StringUtils.isNotEmpty(tempSku)) {
 					Set<String> set = snSubGoodsMap.getOrDefault(tempSn, new HashSet<>());
 					if (set.contains(tempSku)) {
@@ -862,6 +873,7 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 		}
 
 		List<GoodsWrapper.Goods> goodsList = jsonObjectList.stream().map(next -> {
+			next.remove("image");
 			String jsonString = next.toJSONString();
 			GoodsWrapper.Goods goods = JSON.parseObject(jsonString, GoodsWrapper.Goods.class);
 			goods.setSellCount(0);
@@ -878,8 +890,21 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 //		// 数据合并
 		Map<Integer, Integer> skuIdMap = subGoods.stream().collect(Collectors.toMap(PmsProductDto::getSkuStockId, PmsProductDto::getStock));
 
-		goodsGroup.forEach((key, value) -> {
+		for (Map.Entry<String, List<GoodsWrapper.Goods>> entry : goodsGroup.entrySet()) {
+			List<GoodsWrapper.Goods> value = entry.getValue();
 			GoodsWrapper.Goods good = value.get(0);
+			String sn = good.getSn();
+
+			// 图片上传
+			byte[] images = snImageMap.get(sn);
+			if (images != null && images.length > 0) {
+				File imgFile = createImportGoodsTempFile(UUID.randomUUID().toString().replaceAll("-","") + ".png");
+				BufferedImage read = ImageIO.read(new ByteArrayInputStream((images)));
+				ImageIO.write(read, "png", imgFile);
+				String url = ossPluginContext.getPluginService(TencentOssPlugin.PLUGIN_NAME).uploadFile("daya/" + UploadUtil.getFileFloder(), imgFile);
+				good.setImage(url);
+				imgFile.deleteOnExit();
+			}
 			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);
 
@@ -898,7 +923,29 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 				return sub;
 			}).collect(Collectors.toList());
 			goodsSubMapper.saveBatch(subList);
-		});
+		}
+
+//		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;
 	}
 
@@ -1367,22 +1414,27 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 		return mallFeignService.listWithChildren();
 	}
 
-	private String getExportErrLogFile(List<String> errList) {
-		if (CollectionUtils.isEmpty(errList)) {
-			return null;
-		}
-
+	private File createImportGoodsTempFile(String fileName) {
 		String basePath = new ApplicationHome(this.getClass()).getSource().getParentFile().getPath();
-		String format = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
-		File file = new File(basePath + File.separator + format + ".csv");
+		File file = new File(basePath + File.separator + "daya" + File.separator + "importGoods" + File.separator + fileName);
 		try {
 			if (!file.getParentFile().exists()) {
 				file.getParentFile().mkdirs();
 			}
 			file.createNewFile();
+			return file;
 		} catch (IOException e) {
-			throw new RuntimeException(e);
+			throw new BizException("导入失败,回执文件上传失败");
 		}
+	}
+
+	private String getExportErrLogFile(List<String> errList) {
+		if (CollectionUtils.isEmpty(errList)) {
+			return null;
+		}
+
+		String format = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
+		File file = createImportGoodsTempFile(format + ".csv");
 		try {
 			FileOutputStream out = new FileOutputStream(file);
 			byte[] uft8bom = {(byte) 0xef, (byte) 0xbb, (byte) 0xbf}; // 处理乱码
@@ -1398,7 +1450,11 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 			String folder = "daya/download/" + UploadUtil.getFileFloder();
 			return ossPluginContext.getPluginService(TencentOssPlugin.PLUGIN_NAME).uploadFile(folder, file);
 		} catch (IOException e) {
-			e.printStackTrace();
+			log.error("上传错误回执文件失败:{}",e.getMessage());
+		} finally {
+			if (file.exists()) {
+				file.deleteOnExit();
+			}
 		}
 		return "";
 	}