Browse Source

perf(cart): 优化获取购物车性能

yuj 7 years ago
parent
commit
206f074a45

+ 9 - 0
src/main/java/com/uas/platform/b2c/prod/commodity/dao/GoodsHistoryDao.java

@@ -69,4 +69,13 @@ public interface GoodsHistoryDao extends JpaSpecificationExecutor<GoodsHistory>,
      */
     List<GoodsHistory> findByEnUUAndCurrencyName(Long enUU, String currencyName);
 
+    /**
+     * 获取历史库存的id的最大值
+     *
+     * @param codes 分组信息
+     * @return
+     */
+    @Query(value = "select max(g.id) from GoodsHistory g where g.batchCode in (:codes) group by g.batchCode")
+    List<Long> findMaxIdByBatchCodes(@Param("codes") List<String> codes);
+
 }

+ 1 - 0
src/main/java/com/uas/platform/b2c/prod/commodity/model/GoodsSimple.java

@@ -562,6 +562,7 @@ public class GoodsSimple {
 	}
 	
 	public GoodsSimple(GoodsHistory goods) {
+		this.id = goods.getId();
 		this.batchCode = goods.getBatchCode();
 		this.original = goods.getOriginal();
 		this.qtyPrice = goods.getQtyPrice();

+ 70 - 7
src/main/java/com/uas/platform/b2c/trade/presale/service/impl/CartServiceImpl.java

@@ -5,6 +5,7 @@ import com.uas.platform.b2c.common.account.model.Enterprise;
 import com.uas.platform.b2c.common.account.model.User;
 import com.uas.platform.b2c.core.support.SystemSession;
 import com.uas.platform.b2c.core.utils.FastjsonUtils;
+import com.uas.platform.b2c.prod.commodity.constant.IntegerConstant;
 import com.uas.platform.b2c.prod.commodity.dao.GoodsDao;
 import com.uas.platform.b2c.prod.commodity.dao.GoodsHistoryDao;
 import com.uas.platform.b2c.prod.commodity.dao.GoodsSimpleDao;
@@ -31,11 +32,13 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Sort;
 import org.springframework.data.jpa.domain.Specification;
+import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
 import org.springframework.util.StringUtils;
 
+import javax.naming.NameParser;
 import javax.persistence.criteria.CriteriaBuilder;
 import javax.persistence.criteria.CriteriaQuery;
 import javax.persistence.criteria.Predicate;
@@ -73,6 +76,9 @@ public class CartServiceImpl implements CartService {
 	@Autowired
 	private EnterpriseDao enterpriseDao;
 
+	@Autowired
+	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
+
 //	@Autowired
 //	public CartServiceImpl(CartDao cartDao, GoodsService goodsService, ComponentService componentService, ComponentDao componentDao, GoodsSimpleDao goodsSimpleDao, GoodsHistoryDao goodsHistoryDao, StoreInDao storeInDao, GoodsDao goodsDao, EnterpriseDao enterpriseDao) {
 //		this.cartDao = cartDao;
@@ -419,18 +425,75 @@ public class CartServiceImpl implements CartService {
 			}
 		}, info);
 		// 将购物车记录按店铺进行分类
+
 		if (!CollectionUtils.isEmpty(carts.getContent())) {
-			for (Cart cart : carts.getContent()) {
-                GoodsHistory goodsHistory = goodsHistoryDao.findNewByBatchCode(cart.getBatchCode());
-                cart.setGoods(new GoodsSimple(goodsHistory));
-                cart.setStatus();
-				Integer count = goodsDao.getSimilarProCount(cart.getCode(), cart.getBatchCode(), Status.AVAILABLE.value());
-				cart.setSimilarCount(count);
-			}
+			cartFillGoodsHistory(carts.getContent());
+			setCartSimilarCount(carts.getContent());
 		}
 		return carts;
 	}
 
+	/**
+	 * 向Cart中添加历史库存(因为担心 卖家删除库存信息,所以从历史库存中找库存信息)
+	 *
+	 * @param carts
+	 */
+	private void cartFillGoodsHistory(List<Cart> carts) {
+		if (org.apache.commons.collections.CollectionUtils.isEmpty(carts)) {
+			return ;
+		}
+		List<String> batchCodes = new ArrayList<>();
+		for (Cart cart : carts) {
+			batchCodes.add(cart.getBatchCode());
+		}
+		//从历史库存中获取上架信息
+		List<Long> historyIds = goodsHistoryDao.findMaxIdByBatchCodes(batchCodes);
+		if (org.apache.commons.collections.CollectionUtils.isNotEmpty(historyIds)) {
+			List<GoodsHistory> histories = goodsHistoryDao.findAll(historyIds);
+			for (GoodsHistory history : histories) {
+				for (Cart cart : carts) {
+					if (cart.getBatchCode().equals(history.getBatchCode())) {
+						cart.setGoods(new GoodsSimple(history));
+						cart.setStatus();
+					}
+				}
+			}
+		}
+	}
+
+	/**
+	 * 从库存信息中找型号相同的库存信息,统计个数
+	 *
+	 * @param carts
+	 */
+	private void setCartSimilarCount(List<Cart> carts) {
+		if (org.apache.commons.collections.CollectionUtils.isEmpty(carts)) {
+			return ;
+		}
+		List<String> codes = new ArrayList<>();
+		for (Cart cart : carts) {
+			cart.setSimilarCount(IntegerConstant.NO_INT);
+			codes.add(cart.getCode());
+		}
+		//获取相似产品
+		String sql = "select cmp_code, go_batchcode from trade$goods where cmp_code in (:codes) and go_status = " + com.uas.platform.b2c.core.constant.Status.AVAILABLE.value();
+		Map<String, Object> codeMaps = new HashMap<>();
+		codeMaps.put("codes", codes);
+		List<Map<String, Object>> maps = namedParameterJdbcTemplate.queryForList(sql, codeMaps);
+		for (Map<String, Object> numMap : maps) {
+			Object code = numMap.get("cmp_code");
+			for (Cart cart : carts) {
+				if (cart.getCode().equals(code)) {
+					if (!cart.getBatchCode().equals(numMap.get("go_batchcode"))) {
+						Integer similarCount = cart.getSimilarCount();
+						cart.setSimilarCount(++similarCount);
+					}
+				}
+			}
+
+		}
+	}
+
 	@Override
 	public boolean addedSameGoods(String batchCode, Long enUU, Long userUU) {
 		Cart existCart = cartDao.findByUuAndBatchCodeAndEnuu(userUU, batchCode, enUU);