From c8041ec0544bf122e6819e6bf698997ccbf30aaf Mon Sep 17 00:00:00 2001
From: yujian <yujian@163.com>
Date: 星期二, 10 十二月 2019 15:32:34 +0800
Subject: [PATCH] 小黄条

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/brand/BrandInfoServiceImpl.java |  185 ++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 152 insertions(+), 33 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 3227405..34364e4 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
@@ -19,12 +19,15 @@
 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.BrandClass;
 import com.yeshi.fanli.entity.brand.BrandGoodsCahe;
 import com.yeshi.fanli.entity.brand.BrandInfo;
+import com.yeshi.fanli.entity.brand.BrandShopCahe;
 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.log.LogHelper;
 import com.yeshi.fanli.service.inter.brand.BrandClassShopService;
 import com.yeshi.fanli.service.inter.brand.BrandGoodsCaheService;
 import com.yeshi.fanli.service.inter.brand.BrandInfoService;
@@ -80,29 +83,63 @@
 		if (StringUtil.isNullOrEmpty(name))
 			throw new BrandInfoException(1, "鍚嶇О涓嶈兘涓虹┖");
 
-		String searchKey = record.getSearchKey();
-		if (StringUtil.isNullOrEmpty(searchKey))
-			record.setSearchKey(name);
+		BrandClass brandClass = record.getBrandClass();
+		if (brandClass ==null || brandClass.getId() == null)
+			throw new BrandInfoException(1, "鍒嗙被涓嶈兘涓虹┖");
+		
+		BrandInfo existBrand = brandInfoMapper.selectByUniqueName(name.trim(), brandClass.getId(), record.getId());
+		if (existBrand != null)
+			throw new BrandInfoException(1, "璇ュ垎绫讳腑宸插瓨鍦細" + name.trim());
 		
 		Integer state = record.getState();
-		if (state == null)
-			record.setState(0);
+		if (state == null) {
+			state = 0;
+			record.setState(state);
+		}
 		
-		// 鍥剧墖涓婁紶
+	
 		String picture = null;
 		if (file != null) {
-			try {
+			try { // 鍥剧墖涓婁紶
 				picture = uploadPicture(file);
 			} catch (Exception e) {
 				throw new BrandInfoException(1, "鍥剧墖涓婁紶澶辫触");
 			}
 		}
-
+		
+		String searchKey = record.getSearchKey();
+		if (!StringUtil.isNullOrEmpty(searchKey)) {
+			if (StringUtil.isNullOrEmpty(record.getSearchKeyJD()))
+				record.setSearchKeyJD(searchKey);
+			
+			if (StringUtil.isNullOrEmpty(record.getSearchKeyPDD()))
+				record.setSearchKeyPDD(searchKey);
+		}
+		
+		String shopKey = record.getShopKey();
+		if (!StringUtil.isNullOrEmpty(shopKey)) {
+			if (StringUtil.isNullOrEmpty(record.getShopKeyJD()))
+				record.setShopKeyJD(shopKey);
+			
+			if (StringUtil.isNullOrEmpty(record.getShopKeyPDD()))
+				record.setShopKeyPDD(shopKey);
+		}
+		record.setName(name.trim());
+		record.setUpdateTime(new Date());
+		
 		Long id = record.getId();
 		if (id == null) {
+			record.setIcon(picture);
+			record.setWeight(500.00);
+			record.setGoodsTotal(0);
+			record.setShopTotal(0);
 			record.setCreateTime(new Date());
-			record.setUpdateTime(new Date());
 			brandInfoMapper.insert(record);
+			
+			BrandInfo resultObj = new BrandInfo();
+			resultObj.setId(record.getId());
+			resultObj.setOrder(Integer.valueOf(record.getId().toString()));
+			brandInfoMapper.updateByPrimaryKeySelective(resultObj);
 		} else {
 			BrandInfo resultObj = brandInfoMapper.selectByPrimaryKey(id);
 			if (resultObj == null)
@@ -110,8 +147,8 @@
 
 			if (picture != null && picture.trim().length() > 0) {
 				// 鍒犻櫎鑰佸浘
-				if (picture != null && picture.trim().length() > 0 && !Constant.IS_TEST) {
-					COSManager.getInstance().deleteFile(picture);
+				if (resultObj.getIcon() != null && resultObj.getIcon().trim().length() > 0 && !Constant.IS_TEST) {
+					COSManager.getInstance().deleteFile(resultObj.getIcon());
 				};
 				// 瀛樺偍鏂板浘
 				record.setIcon(picture);
@@ -119,11 +156,17 @@
 				record.setIcon(resultObj.getIcon());
 			}
 			
-			record.setGoodsTotal(resultObj.getGoodsTotal());
+			if (state == 0){
+				record.setGoodsTotal(0);
+				record.setShopTotal(0);
+			} else {
+				record.setGoodsTotal(resultObj.getGoodsTotal());
+				record.setShopTotal(resultObj.getShopTotal());
+			}
+			record.setOrder(resultObj.getOrder());
+			record.setWeight(resultObj.getWeight());
 			record.setCreateTime(resultObj.getCreateTime());
-			record.setUpdateTime(new Date());
 			brandInfoMapper.updateByPrimaryKey(record);
-			
 			
 			if (state == 0){
 				executor.execute(new Runnable() {
@@ -140,9 +183,13 @@
 			executor.execute(new Runnable() {
 				@Override
 				public void run() {
-					int goodsTotal = brandGoodsCaheService.addBrandGoods(record);
-					record.setGoodsTotal(goodsTotal);
-					brandInfoMapper.updateByPrimaryKeySelective(record);
+					BrandInfo numInfo = brandGoodsCaheService.addBrandGoods(record);
+					
+					BrandInfo update = new BrandInfo();
+					update.setId(record.getId());
+					update.setGoodsTotal(numInfo.getGoodsTotal());
+					update.setShopTotal(numInfo.getShopTotal());
+					brandInfoMapper.updateByPrimaryKeySelective(update);
 				}
 			});
 		}
@@ -184,15 +231,45 @@
 		
 		return brandInfoMapper.deleteBatchByPrimaryKey(list);
 	}
+	
+	@Override
+	public void updateOrder(Long id, Integer moveType) throws BrandInfoException {
+		if (moveType == null || (!moveType.equals(1) && !moveType.equals(-1)))
+			throw new BrandInfoException(1, "浼犻�掔殑绫诲瀷涓嶆纭�");
+		
+		if (id == null)
+			throw new BrandInfoException(1, "ID涓嶈兘涓虹┖");
+		
+		BrandInfo resultObj = brandInfoMapper.selectByPrimaryKey(id);
+		if (resultObj == null) 
+			throw new BrandInfoException(1, "鎿嶄綔鏁版嵁宸蹭笉瀛樺湪");
+		
+		Integer oldOrder = resultObj.getOrder();
+		BrandInfo changeObj = brandInfoMapper.getByAdjoinOrder(resultObj.getBrandClass().getId(),oldOrder, moveType);
+		if (changeObj == null )
+			throw new BrandInfoException(1, "宸茬粡鍦ㄦ渶杈圭紭锛屾棤鍙氦鎹㈢殑浣嶇疆");
+		
+		BrandInfo changeObj1 = new BrandInfo();
+		changeObj1.setId(id);
+		changeObj1.setOrder(changeObj.getOrder());
+		
+		BrandInfo changeObj2 = new BrandInfo();
+		changeObj2.setId(changeObj.getId());
+		changeObj2.setOrder(oldOrder);
+		
+		brandInfoMapper.updateByPrimaryKeySelective(changeObj1);
+		brandInfoMapper.updateByPrimaryKeySelective(changeObj2);
+	}
+	
 
 	@Override
-	public List<BrandInfo> listQuery(long start, int count, String key, Long cid, Integer state) {
-		return brandInfoMapper.listQuery(start, count, key, cid, state);
+	public List<BrandInfo> listQuery(long start, int count, List<String> keys, Long cid, Integer state, Integer showState) {
+		return brandInfoMapper.listQuery(start, count, keys, cid, state,showState);
 	}
 
 	@Override
-	public long countQuery(String key, Long cid, Integer state) {
-		return brandInfoMapper.countQuery(key, cid, state);
+	public long countQuery(List<String> keys, Long cid, Integer state, Integer showState) {
+		return brandInfoMapper.countQuery(keys, cid, state, showState);
 	}
 
 	@Override
@@ -255,8 +332,42 @@
 		
 		// 鍒犻櫎搴楅摵
 		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);
+			List<BrandShopCahe> listshop = brandShopCaheService.getByBrandId(brandInfo.getId());
+			if (listshop == null) {
+				updateInfo.setShopTotal(0);
+			} else {
+				updateInfo.setShopTotal(listshop.size());
+			}
+			updateInfo.setWeight(Math.random() * 1000);
+			brandInfoMapper.updateByPrimaryKeySelective(updateInfo);
+		}
+	}
 	
 	
 	@Override
@@ -266,17 +377,25 @@
 			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);
+			try {
+				String name = brandInfo.getName();
+				String searchKey = brandInfo.getSearchKey();
+				if (StringUtil.isNullOrEmpty(name) && StringUtil.isNullOrEmpty(searchKey))
+					continue;
+				// 娣诲姞鍟嗗搧
+				BrandInfo numInfo = brandGoodsCaheService.addBrandGoods(brandInfo);
+				
+				BrandInfo updateInfo = new BrandInfo(); 
+				updateInfo.setId(brandInfo.getId());
+				updateInfo.setWeight(Math.random() * 1000);
+				updateInfo.setGoodsTotal(numInfo.getGoodsTotal());
+				updateInfo.setShopTotal(numInfo.getShopTotal());
+				updateInfo.setUpdateTime(new Date());
+				brandInfoMapper.updateByPrimaryKeySelective(updateInfo);
+			} catch (Exception e) {
+				LogHelper.errorDetailInfo(e);
+			}
+			
 		}
 	}
 
@@ -298,7 +417,7 @@
 		return brandInfoMapper.listValidByCidToApp(cid);
 	}
 
-	@Cacheable(value = "brandCache", key = "'listValidToApp-'+#start+'-'+#start +'-'+#cid")
+	@Cacheable(value = "brandCache", key = "'listValidToApp-'+#start+'-'+#cid")
 	@Override
 	public List<BrandInfoVO> listValidToApp(long start, int count, Long cid) {
 		List<BrandInfoVO> list = brandInfoMapper.listBrandInfoVO(start, count, cid);

--
Gitblit v1.8.0