From 2d3afb55aed07f9780ab46aefbdc7d520cdff576 Mon Sep 17 00:00:00 2001
From: yujian <yujian@163.com>
Date: 星期二, 26 十一月 2019 15:44:22 +0800
Subject: [PATCH] 品牌排序

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/brand/BrandInfoServiceImpl.java |   82 ++++++++++++++++++++++++++++++-----------
 1 files changed, 60 insertions(+), 22 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 6e2cf5f..38fcb3d 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,6 +19,7 @@
 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;
@@ -81,6 +82,10 @@
 		if (StringUtil.isNullOrEmpty(name))
 			throw new BrandInfoException(1, "鍚嶇О涓嶈兘涓虹┖");
 
+		BrandClass brandClass = record.getBrandClass();
+		if (brandClass ==null || brandClass.getId() == null)
+			throw new BrandInfoException(1, "鍒嗙被涓嶈兘涓虹┖");
+		
 		String shopKey = record.getShopKey();
 		if (StringUtil.isNullOrEmpty(shopKey))
 			record.setShopKey(name);
@@ -90,9 +95,10 @@
 			record.setSearchKey(name);
 		
 		Integer state = record.getState();
-		if (state == null)
-			record.setState(0);
-		
+		if (state == null) {
+			state = 0;
+			record.setState(state);
+		}
 		// 鍥剧墖涓婁紶
 		String picture = null;
 		if (file != null) {
@@ -106,8 +112,15 @@
 		Long id = record.getId();
 		if (id == null) {
 			record.setCreateTime(new Date());
+			record.setGoodsTotal(0);
+			record.setShopTotal(0);
 			record.setUpdateTime(new Date());
 			brandInfoMapper.insert(record);
+			
+			BrandInfo resultObj = new BrandInfo();
+			resultObj.setId(record.getId());
+			resultObj.setWeight(Double.valueOf(record.getId()));
+			brandInfoMapper.updateByPrimaryKeySelective(resultObj);
 		} else {
 			BrandInfo resultObj = brandInfoMapper.selectByPrimaryKey(id);
 			if (resultObj == null)
@@ -123,15 +136,19 @@
 			} else {
 				record.setIcon(resultObj.getIcon());
 			}
+			
 			if (state == 0){
 				record.setGoodsTotal(0);
+				record.setShopTotal(0);
 			} else {
 				record.setGoodsTotal(resultObj.getGoodsTotal());
+				record.setShopTotal(resultObj.getShopTotal());
 			}
+			
+			record.setWeight(resultObj.getWeight());
 			record.setCreateTime(resultObj.getCreateTime());
 			record.setUpdateTime(new Date());
 			brandInfoMapper.updateByPrimaryKey(record);
-			
 			
 			if (state == 0){
 				executor.execute(new Runnable() {
@@ -148,16 +165,13 @@
 			executor.execute(new Runnable() {
 				@Override
 				public void run() {
-					int goodsTotal = brandGoodsCaheService.addBrandGoods(record);
-					record.setGoodsTotal(goodsTotal);
+					BrandInfo numInfo = brandGoodsCaheService.addBrandGoods(record);
 					
-					List<BrandShopCahe> listshop = brandShopCaheService.getByBrandId(record.getId());
-					if (listshop == null) {
-						record.setShopTotal(0);
-					} else {
-						record.setShopTotal(listshop.size());
-					}
-					brandInfoMapper.updateByPrimaryKeySelective(record);
+					BrandInfo update = new BrandInfo();
+					update.setId(record.getId());
+					update.setGoodsTotal(numInfo.getGoodsTotal());
+					update.setShopTotal(numInfo.getShopTotal());
+					brandInfoMapper.updateByPrimaryKeySelective(update);
 				}
 			});
 		}
@@ -199,6 +213,36 @@
 		
 		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, "鎿嶄綔鏁版嵁宸蹭笉瀛樺湪");
+		
+		Double oldOrder = resultObj.getWeight();
+		BrandInfo changeObj = brandInfoMapper.getByAdjoinOrder(resultObj.getBrandClass().getId(),oldOrder, moveType);
+		if (changeObj == null )
+			throw new BrandInfoException(1, "宸茬粡鍦ㄦ渶杈圭紭锛屾棤鍙氦鎹㈢殑浣嶇疆");
+		
+		BrandInfo changeObj1 = new BrandInfo();
+		changeObj1.setId(id);
+		changeObj1.setWeight(changeObj.getWeight());
+		
+		BrandInfo changeObj2 = new BrandInfo();
+		changeObj2.setId(changeObj.getId());
+		changeObj2.setWeight(oldOrder);
+		
+		brandInfoMapper.updateByPrimaryKeySelective(changeObj1);
+		brandInfoMapper.updateByPrimaryKeySelective(changeObj2);
+	}
+	
 
 	@Override
 	public List<BrandInfo> listQuery(long start, int count, String key, Long cid, Integer state) {
@@ -321,17 +365,11 @@
 				continue;
 
 			// 娣诲姞鍟嗗搧
-			int goodsTotal = brandGoodsCaheService.addBrandGoods(brandInfo);
+			BrandInfo numInfo = brandGoodsCaheService.addBrandGoods(brandInfo);
 
-			brandInfo.setGoodsTotal(goodsTotal);
+			brandInfo.setGoodsTotal(numInfo.getGoodsTotal());
+			brandInfo.setShopTotal(numInfo.getShopTotal());
 			brandInfo.setUpdateTime(new Date());
-			
-			List<BrandShopCahe> listshop = brandShopCaheService.getByBrandId(brandInfo.getId());
-			if (listshop == null) {
-				brandInfo.setShopTotal(0);
-			} else {
-				brandInfo.setShopTotal(listshop.size());
-			}
 			brandInfoMapper.updateByPrimaryKeySelective(brandInfo);
 		}
 	}

--
Gitblit v1.8.0