Authored by wangnan

代码重构

... ... @@ -3,6 +3,7 @@ package com.yoho.search.consumer.service.logic.productIndex;
import com.yoho.search.consumer.service.bo.ProductActivitiesBO;
import com.yoho.search.dal.ProductActivityMapper;
import com.yoho.search.dal.model.ProductActivity;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
... ... @@ -20,11 +21,14 @@ public class ProductActivitiesLogicService {
@Autowired
private ProductActivityMapper productActivityMapper;
public List<ProductActivitiesBO> getData(List<Integer> skns) {
//构建ProductActivity 视图
List<ProductActivity> productActivities = productActivityMapper.getProductActivity(skns);
//构建ProductActivities视图
//group by product_skn
/**
* 根据productSkn查询activity_product表的每一条记录,然后根据productSkn分组把多条记录拼装成一个JSONArray
*/
public List<ProductActivitiesBO> getData(List<Integer> productSknList) {
List<ProductActivity> productActivities = productActivityMapper.getProductActivity(productSknList);
if(CollectionUtils.isEmpty(productActivities)){
return new ArrayList<>();
}
Map<Integer, String> productActivitiesMap = new HashMap<>();
for (ProductActivity p : productActivities) {
if (productActivitiesMap.containsKey(p.getProductSkn())) {
... ... @@ -33,15 +37,14 @@ public class ProductActivitiesLogicService {
productActivitiesMap.put(p.getProductSkn(), p.getActivity());
}
}
//塞入ProductActivities,顺便把Activities字段用[]包裹
List<ProductActivitiesBO> productActivitiesBOs = new ArrayList<>();
List<ProductActivitiesBO> productActivitiesBOList = new ArrayList<>();
for (Map.Entry<Integer, String> entry : productActivitiesMap.entrySet()) {
String activities = "[" + entry.getValue() + "]";
ProductActivitiesBO productActivitiesBO1 = new ProductActivitiesBO();
productActivitiesBO1.setProductSkn(entry.getKey());
productActivitiesBO1.setActivities(activities);
productActivitiesBOs.add(productActivitiesBO1);
ProductActivitiesBO productActivitiesBO = new ProductActivitiesBO();
productActivitiesBO.setProductSkn(entry.getKey());
productActivitiesBO.setActivities(activities);
productActivitiesBOList.add(productActivitiesBO);
}
return productActivitiesBOs;
return productActivitiesBOList;
}
}
... ...
... ... @@ -38,77 +38,80 @@ public class ProductGoodsLogicService {
private StorageService storageService;
/**
* @return
*
*/
public List<ProductGoodsBO> getData(List<Integer> productIds) {
// 解析product_good视图
//构建productColorsMap
List<ProductColor> productColors = productColorMapper.getAll();
if(CollectionUtils.isEmpty(productColors)){
return new ArrayList<>();
}
Map<Integer, String> productColorsMap = new HashMap<>();
for (ProductColor p : productColors) {
productColorsMap.put(p.getId(), p.getColorCode() == null ? "" : p.getColorCode());
}
// 获取Goods数据
List<Goods> goodss = goodsMapper.selectListByProductIds(productIds);
// 解析goods_cover_image 视图
List<Goods> goodsList = goodsMapper.selectListByProductIds(productIds);
List<Integer> goodsIds = goodsMapper.getIdByProductIds(productIds);
List<GoodsCoverImage> goodsCoverImages = new ArrayList<GoodsCoverImage>();
List<Storage> storages = new ArrayList<>();
List<GoodsCoverImage> goodsCoverImageList = new ArrayList<GoodsCoverImage>();
List<Storage> storageList = new ArrayList<>();
if (goodsIds != null && !goodsIds.isEmpty()) {
goodsCoverImages = goodsCoverImageMapper.selectGoodsCoverImage(goodsIds);
storages = storageService.getStoragesByGoodsIds(goodsIds);
goodsCoverImageList = goodsCoverImageMapper.selectGoodsCoverImage(goodsIds);
storageList = storageService.getStoragesByGoodsIds(goodsIds);
}
//获取goods对应的库存
Map<Integer, Integer> storagesMap = new HashMap<>();
if (!CollectionUtils.isEmpty(storages)) {
for (Storage storage : storages) {
if (storagesMap.containsKey(storage.getGoodsId())) {
storagesMap.put(storage.getGoodsId(), storage.getStorageNum() + storagesMap.get(storage.getGoodsId()));
//构建storageListMap
Map<Integer, Integer> storageListMap = new HashMap<>();
if (!CollectionUtils.isEmpty(storageList)) {
for (Storage storage : storageList) {
if (storageListMap.containsKey(storage.getGoodsId())) {
storageListMap.put(storage.getGoodsId(), storage.getStorageNum() + storageListMap.get(storage.getGoodsId()));
} else {
storagesMap.put(storage.getGoodsId(), storage.getStorageNum());
storageListMap.put(storage.getGoodsId(), storage.getStorageNum());
}
}
}
//构建goodsCoverImagesMap
Map<Integer, GoodsCoverImage> goodsCoverImagesMap = new HashMap<>();
// 用goodsId作为key
for (GoodsCoverImage g : goodsCoverImages) {
if (goodsCoverImagesMap.containsKey(g.getGoodsId())) {
GoodsCoverImage goodsCoverImage = goodsCoverImagesMap.get(g.getGoodsId());
if (StringUtils.isBlank(goodsCoverImage.getCover_1()) && !StringUtils.isBlank((g.getCover_1()))) {
goodsCoverImage.setCover_1(g.getCover_1());
for (GoodsCoverImage coverImage : goodsCoverImageList) {
if (goodsCoverImagesMap.containsKey(coverImage.getGoodsId())) {
GoodsCoverImage goodsCoverImage = goodsCoverImagesMap.get(coverImage.getGoodsId());
if (StringUtils.isBlank(goodsCoverImage.getCover_1()) && !StringUtils.isBlank((coverImage.getCover_1()))) {
goodsCoverImage.setCover_1(coverImage.getCover_1());
}
if (StringUtils.isBlank(goodsCoverImage.getCover_2()) && !StringUtils.isBlank((g.getCover_2()))) {
goodsCoverImage.setCover_2(g.getCover_2());
if (StringUtils.isBlank(goodsCoverImage.getCover_2()) && !StringUtils.isBlank((coverImage.getCover_2()))) {
goodsCoverImage.setCover_2(coverImage.getCover_2());
}
} else {
goodsCoverImagesMap.put(g.getGoodsId(), g);
goodsCoverImagesMap.put(coverImage.getGoodsId(), coverImage);
}
}
// 构造product_good视图
//根据productId分组
List<ProductGoodBO> productGoodBOs = new ArrayList<>();
for (Goods g : goodss) {
for (Goods goods : goodsList) {
ProductGoodBO productGoodBO = new ProductGoodBO();
productGoodBO.setProduct_id(g.getProductId());
productGoodBO.setIsDefault(g.getIsDefault() == null ? "" : g.getIsDefault());
this.buildGoods(g, productGoodBO, productColorsMap, goodsCoverImagesMap, storagesMap);
productGoodBO.setProduct_id(goods.getProductId());
productGoodBO.setIsDefault(goods.getIsDefault() == null ? "" : goods.getIsDefault());
this.buildGoods(goods, productGoodBO, productColorsMap, goodsCoverImagesMap, storageListMap);
productGoodBOs.add(productGoodBO);
}
// 构建product_goods视图
Map<Integer, JSONArray> productGoodsMap = new HashMap<>();
for (ProductGoodBO p : productGoodBOs) {
if (productGoodsMap.containsKey(p.getProduct_id())) {
if (p.getGoodList() != null) {
JSONArray jsonArray = productGoodsMap.get(p.getProduct_id());
jsonArray.add(p.getGoodList());
productGoodsMap.put(p.getProduct_id(), jsonArray);
for (ProductGoodBO productGoodBO : productGoodBOs) {
if (productGoodsMap.containsKey(productGoodBO.getProduct_id())) {
if (productGoodBO.getGoodList() != null) {
JSONArray jsonArray = productGoodsMap.get(productGoodBO.getProduct_id());
jsonArray.add(productGoodBO.getGoodList());
productGoodsMap.put(productGoodBO.getProduct_id(), jsonArray);
}
} else {
JSONArray jsonArray = new JSONArray();
jsonArray.add(p.getGoodList());
productGoodsMap.put(p.getProduct_id(), jsonArray);
jsonArray.add(productGoodBO.getGoodList());
productGoodsMap.put(productGoodBO.getProduct_id(), jsonArray);
}
}
List<ProductGoodsBO> productGoodsBOs = new ArrayList<>();
// 塞入productGoodss 顺便把GoodList加上[]包裹
for (Map.Entry<Integer, JSONArray> entry : productGoodsMap.entrySet()) {
ProductGoodsBO pg = new ProductGoodsBO();
pg.setProductId(entry.getKey());
... ... @@ -119,30 +122,34 @@ public class ProductGoodsLogicService {
}
/**
* 拼装goods_list字段
* 拼装goodsList字段
*/
private void buildGoods(Goods g, ProductGoodBO productGoodBO, Map<Integer, String> productColorsMap, Map<Integer, GoodsCoverImage> goodsCoverImagesMap, Map<Integer, Integer> storagesMap) {
private void buildGoods(Goods goods, ProductGoodBO productGoodBO, Map<Integer, String> productColorsMap, Map<Integer, GoodsCoverImage> goodsCoverImagesMap, Map<Integer, Integer> storageMap) {
JSONObject jsonObj = new JSONObject();
jsonObj.put("goods_id", String.valueOf(g.getId()));
jsonObj.put("color_name", StringUtils.isBlank(g.getColorName())? "" : g.getColorName());
//来自Goods
jsonObj.put("goods_id", String.valueOf(goods.getId()));
jsonObj.put("color_name", StringUtils.isBlank(goods.getColorName())? "" : goods.getColorName());
jsonObj.put("images_url", StringUtils.isBlank(goods.getColorImage()) ? "" : goods.getColorImage());
jsonObj.put("is_default", StringUtils.isBlank(goods.getIsDefault())? "" : goods.getIsDefault());
jsonObj.put("color_id", goods.getColorId() == null ? "" : goods.getColorId().toString());
jsonObj.put("status", goods.getStatus() == null ? "" : goods.getStatus().toString());
jsonObj.put("product_skc", goods.getProductSkc() == null ? "" : goods.getProductSkc());
jsonObj.put("color_value", "");
//来自productColorsMap
String colorCode = productColorsMap.get(goods.getColorId());
jsonObj.put("color_code", StringUtils.isBlank(colorCode) ? "" : colorCode);
//来自goodsCoverImagesMap
GoodsCoverImage goodsCoverImage = goodsCoverImagesMap.get(goods.getId());
String cover_1 = "";
String cover_2 = "";
GoodsCoverImage goodsCoverImage = goodsCoverImagesMap.get(g.getId());
if (goodsCoverImage != null) {
cover_1 = goodsCoverImage.getCover_1();
cover_2 = goodsCoverImage.getCover_2();
}
jsonObj.put("cover_1", StringUtils.isBlank(cover_1) ? "" : cover_1);
jsonObj.put("cover_2", StringUtils.isBlank(cover_2) ? "" : cover_2);
jsonObj.put("color_id", g.getColorId() == null ? "" : g.getColorId().toString());
jsonObj.put("status", g.getStatus() == null ? "" : g.getStatus().toString());
String colorCode = productColorsMap.get(g.getColorId());
jsonObj.put("color_code", StringUtils.isBlank(colorCode) ? "" : colorCode);
jsonObj.put("color_value", "");
jsonObj.put("images_url", StringUtils.isBlank(g.getColorImage()) ? "" : g.getColorImage());
jsonObj.put("product_skc", g.getProductSkc() == null ? "" : g.getProductSkc());
jsonObj.put("is_default", StringUtils.isBlank(g.getIsDefault())? "" : g.getIsDefault());
jsonObj.put("storage_num", storagesMap.get(g.getId()) == null ? 0 : storagesMap.get(g.getId()));
//来自storageMap
jsonObj.put("storage_num", storageMap.get(goods.getId()) == null ? 0 : storageMap.get(goods.getId()));
productGoodBO.setGoodList(jsonObj);
}
... ...
... ... @@ -19,22 +19,15 @@ import java.util.stream.Collectors;
@Component
public class ProductSearchBrandWeightLogicService {
//数据量17万
@Autowired
private ProductSearchMapper productSearchMapper;
@Autowired
private ProductSearchBrandMapper productSearchBrandMapper;
public List<ProductSearchBrandWeightBO> getData(List<Integer> skns) {
//构建ProductSearchBrand 视图
List<ProductSearchBrand> productSearchBrands = productSearchBrandMapper.getProductSearchBrand(skns);
//构建ProductSearchBrandWeight 视图
//获取ProductSearch数据
List<ProductSearch> productSearches = productSearchMapper.getByIds(skns);
public List<ProductSearchBrandWeightBO> getData(List<Integer> productSknList) {
List<ProductSearchBrand> productSearchBrands = productSearchBrandMapper.getProductSearchBrand(productSknList);
List<ProductSearch> productSearches = productSearchMapper.getByIds(productSknList);
Map<Integer, Long> productSearchesMap = productSearches.stream().collect(Collectors.toMap(ProductSearch::getId, ProductSearch::getIntValue));
List<ProductSearchBrand> productSearchBrandsNew = new ArrayList<>();
for (ProductSearchBrand p : productSearchBrands) {
if (productSearchesMap.containsKey(p.getBrandWeightId())) {
... ...
... ... @@ -21,19 +21,13 @@ public class ProductSearchShopWeightLogicService {
@Autowired
private ProductSearchMapper productSearchMapper;
@Autowired
private ProductSearchShopMapper productSearchShopMapper;
public List<ProductSearchShopWeightBO> getData(List<Integer> skns) {
//构建ProductSearchBrand 视图
List<ProductSearchShop> productSearchBrands = productSearchShopMapper.getProductSearchShop(skns);
//构建ProductSearchBrandWeight 视图
//获取ProductSearch数据
List<ProductSearch> productSearches = productSearchMapper.getByIds(skns);
public List<ProductSearchShopWeightBO> getData(List<Integer> productSknList) {
List<ProductSearchShop> productSearchBrands = productSearchShopMapper.getProductSearchShop(productSknList);
List<ProductSearch> productSearches = productSearchMapper.getByIds(productSknList);
Map<Integer, Long> productSearchesMap = productSearches.stream().collect(Collectors.toMap(ProductSearch::getId, ProductSearch::getIntValue));
List<ProductSearchShop> productSearchBrandsNew = new ArrayList<>();
for (ProductSearchShop p : productSearchBrands) {
if (productSearchesMap.containsKey(p.getShopWeightId())) {
... ...
... ... @@ -21,20 +21,13 @@ public class ProductSearchSortWeightLogicService {
@Autowired
private ProductSearchMapper productSearchMapper;
@Autowired
private ProductSearchSortMapper productSearchSortMapper;
public List<ProductSearchSortWeightBO> getData(List<Integer> skns) {
// 构建ProductSearchSort 视图
List<ProductSearchSort> productSearchSorts = productSearchSortMapper.getProductSearchSort(skns);
// 构建ProductSearchSortWeight 视图
// 获取ProductSearch数据
List<ProductSearch> productSearches = productSearchMapper.getByIds(skns);
public List<ProductSearchSortWeightBO> getData(List<Integer> productSknList) {
List<ProductSearchSort> productSearchSorts = productSearchSortMapper.getProductSearchSort(productSknList);
List<ProductSearch> productSearches = productSearchMapper.getByIds(productSknList);
Map<Integer, Long> productSearchesMap = productSearches.stream().collect(Collectors.toMap(ProductSearch::getId, ProductSearch::getIntValue));
List<ProductSearchSort> productSearchSortsNew = new ArrayList<>();
for (ProductSearchSort p : productSearchSorts) {
if (productSearchesMap.containsKey(p.getSortWeightId())) {
... ... @@ -51,5 +44,4 @@ public class ProductSearchSortWeightLogicService {
}
return productSearchSortWeightBOs;
}
}
... ...
... ... @@ -25,17 +25,14 @@ public class ProductStandardsLogicService {
// 数据量300
@Autowired
private StandardMapper standardMapper;
// 数据量5000
@Autowired
private ParameterMakeMapper parameterMakeMapper;
// 数据量90万,需要多线程
@Autowired
private ProductStandardRelationMapper productStandardRelationMapper;
/**
* @return
*/
public List<ProductStandardsBO> getData(List<Integer> ids) {
... ... @@ -89,7 +86,7 @@ public class ProductStandardsLogicService {
}
}
// 构造ProductStandards列表
List<ProductStandardsBO> productStandardseBOs = new ArrayList<>();
List<ProductStandardsBO> productStandardsBOS = new ArrayList<>();
for (Map.Entry<Integer, String[]> entry : productStandardRelationsMap.entrySet()) {
ProductStandardsBO productStandardsBO = new ProductStandardsBO();
productStandardsBO.setProductId(entry.getKey());
... ... @@ -97,10 +94,10 @@ public class ProductStandardsLogicService {
productStandardsBO.setStandardIds(valueArray[0]);
productStandardsBO.setStandardNames(valueArray[1]);
productStandardsBO.setStandardOnlyNames(valueArray[2]);
productStandardseBOs.add(productStandardsBO);
productStandardsBOS.add(productStandardsBO);
}
return productStandardseBOs;
return productStandardsBOS;
}
... ...
... ... @@ -34,19 +34,17 @@ public class StorageUpdateTimeLogicService {
@Autowired
private ProductService productService;
public List<StorageUpdateTime> getData(List<Integer> productIds, List<Integer> productSkns) {
if (CollectionUtils.isEmpty(productIds) || CollectionUtils.isEmpty(productSkns)) {
return new ArrayList<StorageUpdateTime>();
public List<StorageUpdateTime> getData(List<Integer> productIds, List<Integer> productSknList) {
if (CollectionUtils.isEmpty(productIds) || CollectionUtils.isEmpty(productSknList)) {
return new ArrayList<>();
}
//查询Product对应的StorageUpdateTime
List<StorageUpdateTime> storageUpdateTimes = storageService.getStorageUpdateTime(productIds);
if (CollectionUtils.isEmpty(storageUpdateTimes)) {
logger.warn("[class=StorageUpdateTimeLogicService][fun=getData][message={}]", "storageUpdateTimes is empty");
return new ArrayList<StorageUpdateTime>();
return new ArrayList<>();
}
List<ProductTiming> productTimings = productTimingService.selectBySkns(productSkns);
List<ProductTiming> productTimings = productTimingService.selectBySkns(productSknList);
Map<Integer, Integer> outSaleTimeMap = new HashMap<>();
for (ProductTiming productTiming : productTimings) {
if (outSaleTimeMap.containsKey(productTiming.getProductSkn())) {
... ...