From c6037befc88e7e92e93fb928ce20078b6fe293d8 Mon Sep 17 00:00:00 2001
From: yj <Administrator@192>
Date: 星期六, 07 三月 2020 12:02:24 +0800
Subject: [PATCH] 发圈倒计时

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/dynamic/GoodsEvaluateServiceImpl.java |  253 ++++++++++++++++++++++++++++++--------------------
 1 files changed, 152 insertions(+), 101 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/dynamic/GoodsEvaluateServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/dynamic/GoodsEvaluateServiceImpl.java
index 9e33f5b..3b7c417 100644
--- a/fanli/src/main/java/com/yeshi/fanli/service/impl/dynamic/GoodsEvaluateServiceImpl.java
+++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/dynamic/GoodsEvaluateServiceImpl.java
@@ -16,6 +16,7 @@
 import javax.annotation.Resource;
 import javax.imageio.ImageIO;
 
+import org.apache.commons.beanutils.PropertyUtils;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.core.task.TaskExecutor;
 import org.springframework.stereotype.Service;
@@ -186,8 +187,33 @@
 				+ type;
 		return COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
 	}
-	
-	private String uploadPicture(File file,String contentType) throws Exception {
+
+	private ImgInfo uploadVideoPicture(MultipartFile filevideo) {
+		try {
+			File tempFile = new File(FileUtil.getCacheDir() + "/" + filevideo.getOriginalFilename());
+			if (tempFile.exists())
+				tempFile.delete();
+			tempFile.createNewFile();
+			filevideo.transferTo(tempFile);
+			File destFile = new File(FileUtil.getCacheDir() + "/temp_" + filevideo.getOriginalFilename());
+			// 瑁佸壀瑙嗛灏侀潰锛屽楂樻瘮涓�2:1
+			ImageCropUtil.centerCrop(tempFile, destFile, 2.0f);
+			BufferedImage sourceImg = ImageIO.read(new FileInputStream(destFile));
+			ImgInfo info = new ImgInfo();
+			int width = sourceImg.getWidth();
+			int height = sourceImg.getHeight();
+			String picLink = uploadPicture(destFile, filevideo.getContentType());
+			info.setH(height);
+			info.setW(width);
+			info.setUrl(picLink);
+			return info;
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		return null;
+	}
+
+	private String uploadPicture(File file, String contentType) throws Exception {
 		InputStream inputStream = new FileInputStream(file);
 		String type = contentType.substring(contentType.indexOf("/") + 1);
 		String filePath = FilePathEnum.goodsEvaluate.getPath() + UUID.randomUUID().toString().replace("-", "") + "."
@@ -208,7 +234,7 @@
 	}
 
 	@Override
-	public void saveSingleGoods(String pid, Long goodsId, Integer goodsType, String videoUrl, Integer picNum,
+	public String saveSingleGoods(String pid, Long goodsId, Integer goodsType, String videoUrl, Integer picNum,
 			String picUrls, MultipartHttpServletRequest fileRequest) throws GoodsEvaluateException, Exception {
 		if (StringUtil.isNullOrEmpty(pid)) {
 			throw new GoodsEvaluateException(1, "璇蜂繚瀛樼涓�閮ㄥ垎淇℃伅");
@@ -247,36 +273,28 @@
 				if (StringUtil.isNullOrEmpty(videoUrl)) {
 					throw new GoodsEvaluateException(1, "瑙嗛閾炬帴涓嶈兘涓虹┖");
 				}
-				InputStream inputStream = filevideo.getInputStream();
-				File destFile = new File(FileUtil.getCacheDir() + "/temp_" + filevideo.getName());
-				// 瑁佸壀瑙嗛灏侀潰锛屽楂樻瘮涓�2:1
-				ImageCropUtil.centerCrop(inputStream, destFile, 2.0f);
-
-				BufferedImage sourceImg = ImageIO.read(new FileInputStream(destFile));
-				
-				int width = sourceImg.getWidth();
-				int height = sourceImg.getHeight();
-
-				String picLink =  uploadPicture(destFile,filevideo.getContentType());
+				ImgInfo info = uploadVideoPicture(filevideo);
+				if (info == null)
+					throw new GoodsEvaluateException(2, "瑙嗛鍥剧墖涓婁紶澶辫触");
 				if (imgVideo != null) {
 					String url = imgVideo.getUrl();
 					if (!StringUtil.isNullOrEmpty(url)) {
 						listDel.add(url);
 					}
-					imgVideo.setUrl(picLink);
-					imgVideo.setUrlHD(picLink);
+					imgVideo.setUrl(info.getUrl());
+					imgVideo.setUrlHD(info.getUrl());
 				} else {
 					imgVideo = new ImgInfo();
 					imgVideo.setId(UUID.randomUUID().toString().replace("-", ""));
 					imgVideo.setLarge(true);
 					imgVideo.setPid(pid);
-					imgVideo.setUrl(picLink);
-					imgVideo.setUrlHD(picLink);
+					imgVideo.setUrl(info.getUrl());
+					imgVideo.setUrlHD(info.getUrl());
 					imgVideo.setVideoUrl(videoUrl);
 					imgVideo.setType(ImgEnum.video);
 				}
-				imgVideo.setW(width);
-				imgVideo.setH(height);
+				imgVideo.setW(info.getW());
+				imgVideo.setH(info.getH());
 			}
 		}
 
@@ -443,8 +461,8 @@
 		 * = false; }
 		 */
 
+		String commentText = "";
 		if (addComment) {
-			String commentText = "";
 			if (goodsType == Constant.SOURCE_TYPE_TAOBAO) {
 				TaoBaoLink taoBaoLink = shareGoodsService.getTaoBaoLinkForShare(1L, goodsId, "0");
 				String template = configService.get(ConfigKeyEnum.quickShareTBCommentText.getKey());
@@ -494,6 +512,8 @@
 				}
 			}
 		}
+		
+		return commentText;
 	}
 
 	private String createTokenAndLink(GoodsDetailVO goodsDetailVO) throws Exception {
@@ -597,35 +617,29 @@
 					throw new GoodsEvaluateException(1, "瑙嗛閾炬帴涓嶈兘涓虹┖");
 				}
 
-				InputStream inputStream = filevideo.getInputStream();
-				File destFile = new File(FileUtil.getCacheDir() + "/temp_" + filevideo.getName());
-				// 瑁佸壀瑙嗛灏侀潰锛屽楂樻瘮涓�2:1
-				ImageCropUtil.centerCrop(inputStream, destFile, 2.0f);
+				ImgInfo info = uploadVideoPicture(filevideo);
+				if (info == null)
+					throw new GoodsEvaluateException(2, "瑙嗛鍥剧墖涓婁紶澶辫触");
 
-				BufferedImage sourceImg = ImageIO.read(new FileInputStream(destFile));
-				int width = sourceImg.getWidth();
-				int height = sourceImg.getHeight();
-
-				String picLink = uploadPicture(destFile,filevideo.getContentType());
 				if (imgVideo != null) {
 					String url = imgVideo.getUrl();
 					if (!StringUtil.isNullOrEmpty(url)) {
 						listDel.add(url);
 					}
-					imgVideo.setUrl(picLink);
-					imgVideo.setUrlHD(picLink);
+					imgVideo.setUrl(info.getUrl());
+					imgVideo.setUrlHD(info.getUrl());
 				} else {
 					imgVideo = new ImgInfo();
 					imgVideo.setId(UUID.randomUUID().toString().replace("-", ""));
 					imgVideo.setLarge(true);
 					imgVideo.setPid(pid);
-					imgVideo.setUrl(picLink);
-					imgVideo.setUrlHD(picLink);
+					imgVideo.setUrl(info.getUrl());
+					imgVideo.setUrlHD(info.getUrl());
 					imgVideo.setVideoUrl(videoUrl);
 					imgVideo.setType(ImgEnum.video);
 				}
-				imgVideo.setW(width);
-				imgVideo.setH(height);
+				imgVideo.setW(info.getW());
+				imgVideo.setH(info.getH());
 			}
 		}
 
@@ -1013,17 +1027,12 @@
 			if (filevideo != null) {
 				removePicture(videoPic);
 
-				InputStream inputStream = filevideo.getInputStream();
-				File destFile = new File(FileUtil.getCacheDir() + "/temp_" + filevideo.getName());
-				// 瑁佸壀瑙嗛灏侀潰锛屽楂樻瘮涓�2:1
-				ImageCropUtil.centerCrop(inputStream, destFile, 2.0f);
-
-				BufferedImage sourceImg = ImageIO.read(new FileInputStream(destFile));
-				
-				width = sourceImg.getWidth();
-				height = sourceImg.getHeight();
-				String picLink =  uploadPicture(destFile,filevideo.getContentType());
-				videoPic = picLink;
+				ImgInfo info = uploadVideoPicture(filevideo);
+				if (info == null)
+					throw new GoodsEvaluateException(2, "瑙嗛鍥剧墖涓婁紶澶辫触");
+				videoPic = info.getUrl();
+				width=info.getW();
+				height=info.getH();
 			}
 
 			MultipartFile activityfile = fileRequest.getFile("activityfile");
@@ -1063,6 +1072,7 @@
 			listImg.add(imgVideo);
 		}
 
+		int totalImg = 0;
 		if (!StringUtil.isNullOrEmpty(activityPic) || !StringUtil.isNullOrEmpty(imgInfo.getActivityUrl())) {
 			if (imgactivity != null) {
 				imgactivity.setUrl(activityPic);
@@ -1086,8 +1096,21 @@
 				imgactivity.setH(imgactivity.getH());
 			}
 			listImg.add(imgactivity);
+			totalImg ++;
 		}
-		resultObj.setLineNum(1);
+		
+		int lineNum = 0;
+		if (totalImg > 0) {
+			if (totalImg == 1) {
+				lineNum = 1;
+			} else if (totalImg <= 4) {
+				lineNum = 2;
+			} else {
+				lineNum = 3;
+			}
+		}
+
+		resultObj.setLineNum(lineNum);
 		resultObj.setImgList(listImg);
 		goodsEvaluateDao.save(resultObj);
 	}
@@ -1136,39 +1159,29 @@
 			if (filevideo != null) {
 				if (StringUtil.isNullOrEmpty(videoUrl)) {
 					throw new GoodsEvaluateException(1, "瑙嗛閾炬帴涓嶈兘涓虹┖");
-				}	
-				InputStream inputStream = filevideo.getInputStream();
-				File destFile = new File(FileUtil.getCacheDir() + "/temp_" + filevideo.getName());
-				// 瑁佸壀瑙嗛灏侀潰锛屽楂樻瘮涓�2:1
-				ImageCropUtil.centerCrop(inputStream, destFile, 2.0f);
-
-				BufferedImage sourceImg = ImageIO.read(new FileInputStream(destFile));
-				
-				int width = sourceImg.getWidth();
-				int height = sourceImg.getHeight();
-				
-				
-
-				String picLink =  uploadPicture(destFile,filevideo.getContentType());
+				}
+				ImgInfo info = uploadVideoPicture(filevideo);
+				if (info == null)
+					throw new GoodsEvaluateException(2, "瑙嗛鍥剧墖涓婁紶澶辫触");
 				if (imgVideo != null) {
 					String url = imgVideo.getUrl();
 					if (!StringUtil.isNullOrEmpty(url)) {
 						listDel.add(url);
 					}
-					imgVideo.setUrl(picLink);
-					imgVideo.setUrlHD(picLink);
+					imgVideo.setUrl(info.getUrl());
+					imgVideo.setUrlHD(info.getUrl());
 				} else {
 					imgVideo = new ImgInfo();
 					imgVideo.setId(UUID.randomUUID().toString().replace("-", ""));
 					imgVideo.setLarge(true);
 					imgVideo.setPid(pid);
-					imgVideo.setUrl(picLink);
-					imgVideo.setUrlHD(picLink);
+					imgVideo.setUrl(info.getUrl());
+					imgVideo.setUrlHD(info.getUrl());
 					imgVideo.setVideoUrl(videoUrl);
 					imgVideo.setType(ImgEnum.video);
 				}
-				imgVideo.setW(width);
-				imgVideo.setH(height);
+				imgVideo.setW(info.getW());
+				imgVideo.setH(info.getH());
 			}
 
 			activityFile = fileRequest.getFile("activityFile");
@@ -1214,6 +1227,7 @@
 			listImg.add(imgActivity);
 		}
 
+		int totalImg = 0;
 		// 缂栬緫鍥剧墖
 		if (!StringUtil.isNullOrEmpty(picUrls)) {
 			String[] pics = picUrls.split(",");
@@ -1232,9 +1246,10 @@
 							imgInfo0.setUrl(picLink);
 							imgInfo0.setUrlHD(picLink);
 							imgInfo0.setType(ImgEnum.img);
-							imgVideo.setW(1);
-							imgVideo.setH(1);
+							imgInfo0.setW(1);
+							imgInfo0.setH(1);
 							listImg.add(imgInfo0);
+							totalImg ++;
 							continue;
 						}
 					}
@@ -1245,6 +1260,7 @@
 								String url = imgInfo.getUrl();
 								if (picLink.equals(url)) {
 									listImg.add(imgInfo);
+									totalImg ++;
 									break;
 								}
 							}
@@ -1268,7 +1284,19 @@
 					listDel.add(listOld.get(j));
 			}
 		}
-		resultObj.setLineNum(1);
+		
+		int lineNum = 0;
+		if (totalImg > 0) {
+			if (totalImg == 1) {
+				lineNum = 1;
+			} else if (totalImg <= 4) {
+				lineNum = 2;
+			} else {
+				lineNum = 3;
+			}
+		}
+
+		resultObj.setLineNum(lineNum);
 		resultObj.setImgList(listImg);
 		goodsEvaluateDao.save(resultObj);
 
@@ -1447,57 +1475,69 @@
 	@Cacheable(value = "dynamicCache", key = "'queryMaterialsCache-'+#start+'-'+#type")
 	public List<GoodsEvaluate> queryMaterialsCache(int start, int count, int type) throws Exception {
 		List<GoodsEvaluate> list = goodsEvaluateDao.queryValid(start, count, type);
-
-		List<GoodsEvaluate> listOBJ = list;
-		// 鏇存柊鍟嗗搧淇℃伅
-		executor.execute(new Runnable() {
-			@Override
-			public void run() {
-				updateGoodInfo(listOBJ);
-			}
-		});
-
 		if (list == null) {
 			list = new ArrayList<>();
 		}
 		Date now = new Date();
-		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
+		List<GoodsEvaluate> listNew = new ArrayList<>();
 		for (GoodsEvaluate goodsEvaluate : list) {
 
+			GoodsEvaluate evaluateNew = new GoodsEvaluate();
+			try {
+				PropertyUtils.copyProperties(evaluateNew, goodsEvaluate);
+			} catch (Exception e) {
+				e.printStackTrace();
+				continue;
+			}
+
 			int comment = 0;
-			List<CommentInfo> comments = goodsEvaluate.getComments();
+			List<CommentInfo> comments = evaluateNew.getComments();
 			if (comments != null && comments.size() > 0) {
-				EvaluateEnum typeEnum = goodsEvaluate.getType();
+				EvaluateEnum typeEnum = evaluateNew.getType();
 				List<CommentInfo> commentNew = new ArrayList<>();
 
 				for (CommentInfo commentInfo : comments) {
+					if (StringUtil.isNullOrEmpty(commentInfo.getContent()) && 
+							StringUtil.isNullOrEmpty(commentInfo.getCoupon())) {
+						continue;
+					}
+					
+					CommentInfo commentInfoNew = new CommentInfo();
+					try {
+						PropertyUtils.copyProperties(commentInfoNew, commentInfo);
+					} catch (Exception e) {
+						e.printStackTrace();
+						continue;
+					}
+					
 					comment++;
-
-					CommentInfoEnum typeComment = commentInfo.getTypeEnum();
+					CommentInfoEnum typeComment = commentInfoNew.getTypeEnum();
 					if (typeComment != null && typeComment == CommentInfoEnum.goodsCoupon) {
-						GoodsDetailVO goods = goodsEvaluate.getGoods();
-						if (goods.isHasCoupon()) {
-							commentNew.add(commentInfo);
+						GoodsDetailVO goods = evaluateNew.getGoods();
+						if (goods != null && goods.isHasCoupon()) {
+							commentNew.add(commentInfoNew);
 							continue;
 						}
 					}
 
-					String coupon = commentInfo.getCoupon();
-					String endTime = commentInfo.getEndTime();
+					String coupon = commentInfoNew.getCoupon();
+					String endTime = commentInfoNew.getEndTime();
 					if (!StringUtil.isNullOrEmpty(coupon) && !StringUtil.isNullOrEmpty(endTime)) {
 						Date endDay = sdf.parse(endTime);
 						if (endDay.getTime() > now.getTime()) {
-							int daysBetween = DateUtil.daysBetween(now, endDay);
-							if (daysBetween > 0) {
+							String daysBetween = DateUtil.dateDiff5(now, endDay);
+							if (!StringUtil.isNullOrEmpty(daysBetween)) {
 								ClientTextStyleVO styleVO = new ClientTextStyleVO();
 								styleVO.setColor("#E5005C");
-								styleVO.setContent(daysBetween + "澶╁悗杩囨湡");
+								styleVO.setContent(daysBetween + "鍚庤繃鏈�");
 
-								List<ClientTextStyleVO> tagList = commentInfo.getTagList();
+								List<ClientTextStyleVO> tagList = commentInfoNew.getTagList();
 								tagList.add(styleVO);
-								commentInfo.setTagList(tagList);
+								commentInfoNew.setTagList(tagList);
 
-								commentNew.add(commentInfo);
+								commentNew.add(commentInfoNew);
 								continue;
 							}
 						}
@@ -1505,17 +1545,17 @@
 
 					commentNew.add(commentInfo);
 				}
-				goodsEvaluate.setComments(commentNew);
+				evaluateNew.setComments(commentNew);
 
 				if (typeEnum != null && typeEnum == EvaluateEnum.single) {
 					if (comment > 0 && commentNew.size() == 0) { // 鎵�鏈夊埜璇勮涓嬫灦
-						GoodsDetailVO goods = goodsEvaluate.getGoods();
+						GoodsDetailVO goods = evaluateNew.getGoods();
 						if (goods != null) {
 							goods.setState(1); // 璇勮涓嶅瓨鍦� 涓嬫灦
 							LogHelper.test("鑾峰彇鍒楄〃鍟嗗搧涓嬫灦1");
 						}
 
-						List<ImgInfo> imgList = goodsEvaluate.getImgList();
+						List<ImgInfo> imgList = evaluateNew.getImgList();
 						if (imgList != null) {
 							for (ImgInfo imgInfo : imgList) {
 								SimpleGoods simpleGoods = imgInfo.getGoods();
@@ -1529,9 +1569,20 @@
 					}
 				}
 			}
+
+			listNew.add(evaluateNew);
 		}
 
-		return list;
+		List<GoodsEvaluate> listOBJ = list;
+		// 鏇存柊鍟嗗搧淇℃伅
+		executor.execute(new Runnable() {
+			@Override
+			public void run() {
+				updateGoodInfo(listOBJ);
+			}
+		});
+
+		return listNew;
 	}
 
 	@Override

--
Gitblit v1.8.0