From 51a4ff5d777028d52a19c314a99f796334cb7b51 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期六, 23 十一月 2019 18:30:01 +0800
Subject: [PATCH] 配置文件修改

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/brand/BrandInfoServiceImpl.java |  279 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 251 insertions(+), 28 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/brand/BrandInfoServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/brand/BrandInfoServiceImpl.java
index 8d5665f..1eafeef 100644
--- a/fanli/src/main/java/com/yeshi/fanli/service/impl/brand/BrandInfoServiceImpl.java
+++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/brand/BrandInfoServiceImpl.java
@@ -1,36 +1,51 @@
 package com.yeshi.fanli.service.impl.brand;
 
+import java.io.InputStream;
+import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
+import java.util.UUID;
 
 import javax.annotation.Resource;
 
 import org.springframework.cache.annotation.Cacheable;
+import org.springframework.core.task.TaskExecutor;
 import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+import org.yeshi.utils.tencentcloud.COSManager;
 
-import com.yeshi.fanli.dao.brand.BrandGoodsCaheDao;
 import com.yeshi.fanli.dao.brand.BrandShopCaheDao;
 import com.yeshi.fanli.dao.mybatis.brand.BrandInfoMapper;
+import com.yeshi.fanli.dto.ConfigParamsDTO;
+import com.yeshi.fanli.dto.pdd.PDDGoodsDetail;
+import com.yeshi.fanli.entity.brand.BrandGoodsCahe;
 import com.yeshi.fanli.entity.brand.BrandInfo;
+import com.yeshi.fanli.entity.jd.JDGoods;
+import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
 import com.yeshi.fanli.entity.taobao.TaoBaoShop;
 import com.yeshi.fanli.exception.brand.BrandInfoException;
 import com.yeshi.fanli.service.inter.brand.BrandClassShopService;
+import com.yeshi.fanli.service.inter.brand.BrandGoodsCaheService;
 import com.yeshi.fanli.service.inter.brand.BrandInfoService;
+import com.yeshi.fanli.service.inter.brand.BrandShopCaheService;
 import com.yeshi.fanli.service.inter.goods.TaoBaoGoodsBriefService;
-import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService;
 import com.yeshi.fanli.service.inter.lable.QualityGoodsService;
+import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
 import com.yeshi.fanli.service.inter.taobao.TaoBaoGoodsUpdateService;
+import com.yeshi.fanli.util.Constant;
 import com.yeshi.fanli.util.StringUtil;
+import com.yeshi.fanli.util.factory.goods.GoodsDetailVOFactory;
 import com.yeshi.fanli.vo.brand.BrandInfoVO;
 import com.yeshi.fanli.vo.brand.TaoBaoShopVO;
+import com.yeshi.fanli.vo.goods.GoodsDetailVO;
 
 @Service
 public class BrandInfoServiceImpl implements BrandInfoService {
-	
+
 	@Resource
 	private BrandInfoMapper brandInfoMapper;
-	
+
 	@Resource
 	private HongBaoManageService hongBaoManageService;
 
@@ -42,28 +57,51 @@
 
 	@Resource
 	private TaoBaoGoodsUpdateService taoBaoGoodsUpdateService;
-	
+
 	@Resource
 	private BrandClassShopService brandClassShopService;
 
 	@Resource
 	private BrandShopCaheDao brandShopCaheDao;
-	
+
 	@Resource
-	private BrandGoodsCaheDao brandGoodsCaheDao;
-	
+	private BrandGoodsCaheService brandGoodsCaheService;
+
+	@Resource
+	private BrandShopCaheService brandShopCaheService;
+
+	@Resource(name = "taskExecutor")
+	private TaskExecutor executor;
 	
 	
 	@Override
-	public void saveObject(BrandInfo record) throws BrandInfoException{
+	public void saveObject(MultipartFile file, BrandInfo record) throws BrandInfoException {
 		String name = record.getName();
-		if (name == null || name.trim().length() == 0)
+		if (StringUtil.isNullOrEmpty(name))
 			throw new BrandInfoException(1, "鍚嶇О涓嶈兘涓虹┖");
+
+		String shopKey = record.getShopKey();
+		if (StringUtil.isNullOrEmpty(shopKey))
+			record.setShopKey(name);
+		
+		String searchKey = record.getSearchKey();
+		if (StringUtil.isNullOrEmpty(searchKey))
+			record.setSearchKey(name);
 		
 		Integer state = record.getState();
 		if (state == null)
 			record.setState(0);
 		
+		// 鍥剧墖涓婁紶
+		String picture = null;
+		if (file != null) {
+			try {
+				picture = uploadPicture(file);
+			} catch (Exception e) {
+				throw new BrandInfoException(1, "鍥剧墖涓婁紶澶辫触");
+			}
+		}
+
 		Long id = record.getId();
 		if (id == null) {
 			record.setCreateTime(new Date());
@@ -73,36 +111,102 @@
 			BrandInfo resultObj = brandInfoMapper.selectByPrimaryKey(id);
 			if (resultObj == null)
 				throw new BrandInfoException(1, "淇敼鍐呭宸蹭笉瀛樺湪");
-			
+
+			if (picture != null && picture.trim().length() > 0) {
+				// 鍒犻櫎鑰佸浘
+				if (resultObj.getIcon() != null && resultObj.getIcon().trim().length() > 0 && !Constant.IS_TEST) {
+					COSManager.getInstance().deleteFile(resultObj.getIcon());
+				};
+				// 瀛樺偍鏂板浘
+				record.setIcon(picture);
+			} else {
+				record.setIcon(resultObj.getIcon());
+			}
+			if (state == 0){
+				record.setGoodsTotal(0);
+			} else {
+				record.setGoodsTotal(resultObj.getGoodsTotal());
+			}
 			record.setCreateTime(resultObj.getCreateTime());
 			record.setUpdateTime(new Date());
 			brandInfoMapper.updateByPrimaryKey(record);
+			
+			
+			if (state == 0){
+				executor.execute(new Runnable() {
+					@Override
+					public void run() {
+						brandGoodsCaheService.removeByBrandId(id);
+						brandShopCaheService.removeByBrandId(id);
+					}
+				});
+			}
+		}
+
+		if (state == 1) {
+			executor.execute(new Runnable() {
+				@Override
+				public void run() {
+					int goodsTotal = brandGoodsCaheService.addBrandGoods(record);
+					record.setGoodsTotal(goodsTotal);
+					brandInfoMapper.updateByPrimaryKeySelective(record);
+				}
+			});
 		}
 	}
+
 	
+	/**
+	 * 涓婁紶鍥剧墖
+	 * @param file
+	 * @return
+	 * @throws Exception
+	 */
+	public String uploadPicture(MultipartFile file) throws Exception {
+		
+		// 鏂囦欢瑙f瀽 
+		InputStream inputStream = file.getInputStream();
+		String contentType = file.getContentType();
+		String type = contentType.substring(contentType.indexOf("/") + 1);
+	
+		// 鏂囦欢璺緞
+		String filePath="/img/brand/"+UUID.randomUUID().toString().replace("-", "") + "." + type;
+		// 鎵ц涓婁紶
+		String fileLink= COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
+		
+		return fileLink;
+	}
 	
 	@Override
 	public int deleteBatchByPrimaryKey(List<Long> list) {
+		executor.execute(new Runnable() {
+			@Override
+			public void run() {
+				for (Long id: list) {
+					brandGoodsCaheService.removeByBrandId(id);
+					brandShopCaheService.removeByBrandId(id);
+				}
+			}
+		});
+		
 		return brandInfoMapper.deleteBatchByPrimaryKey(list);
 	}
 
-	
 	@Override
-	public List<BrandInfo> listQuery(long start, int count, String key, Integer state) {
-		return null;
+	public List<BrandInfo> listQuery(long start, int count, String key, Long cid, Integer state) {
+		return brandInfoMapper.listQuery(start, count, key, cid, state);
 	}
 
-
 	@Override
-	public long countQuery(String key, Integer state) {
-		return 0;
+	public long countQuery(String key, Long cid, Integer state) {
+		return brandInfoMapper.countQuery(key, cid, state);
 	}
-	
+
 	@Override
 	@Cacheable(value = "brandCache", key = "'listValidBrandInfoCache-'+#cid")
 	public List<BrandInfo> listValidBrandInfoCache(Long cid) {
 		List<BrandInfo> listInfo = new ArrayList<BrandInfo>();
-		
+
 		List<TaoBaoShop> listShop = brandClassShopService.listEffectiveClassShop(cid);
 		if (listShop == null || listShop.size() == 0)
 			return listInfo;
@@ -115,17 +219,16 @@
 		}
 		return listInfo;
 	}
-	
-	
+
 	@Override
 	@Cacheable(value = "brandCache", key = "'listBrandInfoCache-'+#start+'-'+#start +'-'+#cid")
 	public List<BrandInfoVO> listBrandInfoCache(long start, int count, Long cid) {
 		List<BrandInfoVO> listInfo = new ArrayList<BrandInfoVO>();
-		
+
 		List<TaoBaoShopVO> listShop = brandClassShopService.listEffectiveShop(start, count, cid);
 		if (listShop == null || listShop.size() == 0)
 			return listInfo;
-		
+
 		for (TaoBaoShopVO taoBaoShopVO : listShop) {
 			String shopNameCustom = taoBaoShopVO.getShopNameCustom();
 			if (!StringUtil.isNullOrEmpty(shopNameCustom)) {
@@ -135,7 +238,7 @@
 			if (!StringUtil.isNullOrEmpty(shopIconCustom)) {
 				taoBaoShopVO.setShopIcon(shopIconCustom);
 			}
-			
+
 			BrandInfoVO brandInfoVO = new BrandInfoVO();
 			brandInfoVO.setId(taoBaoShopVO.getId());
 			brandInfoVO.setName(taoBaoShopVO.getShopName());
@@ -146,15 +249,135 @@
 		return listInfo;
 	}
 
-	
 	@Override
-	public long countBrandInfo(Long cid){
+	public long countBrandInfo(Long cid) {
 		return brandClassShopService.countBrandShopinfo(cid);
 	}
+
 	
-	
-	public void addShopAndGoods(BrandInfo record) {
+	@Override
+	public void removeAgoByDate(Date date) {
+		// 鍒犻櫎鍟嗗搧
+		brandGoodsCaheService.removeAgoByDate(date);
 		
+		// 鍒犻櫎搴楅摵
+		brandShopCaheService.removeAgoByDate(date);
+		
+		long count = brandInfoMapper.countValidByCid(null);
+		if (count == 0)
+			return;
+
+		for (int page = 0; page < (count / 100) + 1; page++) {
+			updateTotalGoods(page * 100, 100);
+		}
+	}
+
+	/**
+	 * 鏇存柊鍟嗗搧鏁伴噺
+	 * @param start
+	 * @param count
+	 */
+	private void updateTotalGoods(long start, int count) {
+		List<BrandInfo> list = brandInfoMapper.listValidAll(start, count);
+		if (list == null || list.size() == 0)
+			return;
+		
+		for (BrandInfo brandInfo : list) {
+			long goodsTotal = brandGoodsCaheService.countByBrandId(brandInfo.getId());
+			
+			BrandInfo updateInfo = new BrandInfo();
+			updateInfo.setId(brandInfo.getId());
+			updateInfo.setGoodsTotal((int)goodsTotal);
+			brandInfoMapper.updateByPrimaryKeySelective(updateInfo);
+		}
 	}
 	
+	
+	@Override
+	public void addShopAndGoods(long start, int count) {
+		List<BrandInfo> list = brandInfoMapper.listValidAll(start, count);
+		if (list == null || list.size() == 0)
+			return;
+
+		for (BrandInfo brandInfo : list) {
+			String name = brandInfo.getName();
+			String searchKey = brandInfo.getSearchKey();
+			if (StringUtil.isNullOrEmpty(name) && StringUtil.isNullOrEmpty(searchKey))
+				continue;
+
+			// 娣诲姞鍟嗗搧
+			int goodsTotal = brandGoodsCaheService.addBrandGoods(brandInfo);
+
+			brandInfo.setGoodsTotal(goodsTotal);
+			brandInfo.setUpdateTime(new Date());
+			brandInfoMapper.updateByPrimaryKeySelective(brandInfo);
+		}
+	}
+
+	@Override
+	public long countValidByCid(Long cid) {
+		return brandInfoMapper.countValidByCid(cid);
+	}
+
+	@Override
+	public long countValidByCidToApp(Long cid) {
+		return brandInfoMapper.countValidByCidToApp(cid);
+	}
+
+	@Override
+	@Cacheable(value = "brandCache", key = "'listValidByCidToApp-'+#cid")
+	public List<BrandInfo> listValidByCidToApp(Long cid) {
+		if (cid == null)
+			return null;
+		return brandInfoMapper.listValidByCidToApp(cid);
+	}
+
+//	@Cacheable(value = "brandCache", key = "'listValidToApp-'+#start+'-'+#start +'-'+#cid")
+	@Override
+	public List<BrandInfoVO> listValidToApp(long start, int count, Long cid) {
+		List<BrandInfoVO> list = brandInfoMapper.listBrandInfoVO(start, count, cid);
+		if (list == null || list.size() == 0)
+			return null;
+
+		BigDecimal fanLiRate = hongBaoManageService.getFanLiRate();
+		BigDecimal shareRate = hongBaoManageService.getShareRate();
+		ConfigParamsDTO configParamsDTO = new ConfigParamsDTO(fanLiRate, shareRate, Constant.MAX_REWARD_RATE);
+		List<BrandInfoVO> listInfo = new ArrayList<BrandInfoVO>();
+		for (int i = 0; i < list.size(); i++) {
+			BrandInfoVO brand = list.get(i);
+			if (brand == null)
+				continue;
+			
+			List<BrandGoodsCahe> listGoods = brandGoodsCaheService.getByBrandId(1, 3, brand.getId());
+			if (listGoods == null || listGoods.size() < 3)
+				continue;
+
+			List<GoodsDetailVO> listGoodsVO = new ArrayList<GoodsDetailVO>();
+			for (BrandGoodsCahe brandGoods : listGoods) {
+				JDGoods goodsJD = brandGoods.getGoodsJD();
+				if (goodsJD != null) {
+					listGoodsVO.add(GoodsDetailVOFactory.convertJDGoods(goodsJD, configParamsDTO));
+					continue;
+				}
+
+				TaoBaoGoodsBrief goodsTB = brandGoods.getGoodsTB();
+				if (goodsTB != null) {
+					listGoodsVO.add(GoodsDetailVOFactory.convertTaoBao(goodsTB, configParamsDTO));
+					continue;
+				}
+
+				PDDGoodsDetail goodsPDD = brandGoods.getGoodsPDD();
+				if (goodsPDD != null) {
+					listGoodsVO.add(GoodsDetailVOFactory.convertPDDGoods(goodsPDD, configParamsDTO));
+					continue;
+				}
+			}
+
+			if (listGoodsVO.size() >= 3) {
+				brand.setListGoods(listGoodsVO);
+				listInfo.add(brand);
+			}
+		}
+		return listInfo;
+	}
 }

--
Gitblit v1.8.0