|
|
package com.yoho.search.consumer.service.logicService;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.search.consumer.service.bo.ProductGoodBO;
|
|
|
import com.yoho.search.consumer.service.bo.ProductGoodsBO;
|
|
|
import com.yoho.search.consumer.service.daoService.StorageService;
|
|
|
import com.yoho.search.consumer.service.logicService.cache.BasicDataCacheService;
|
|
|
import com.yoho.search.dal.GoodsCoverImageMapper;
|
|
|
import com.yoho.search.dal.GoodsMapper;
|
|
|
import com.yoho.search.dal.model.Goods;
|
|
|
import com.yoho.search.dal.model.GoodsCoverImage;
|
|
|
import com.yoho.search.dal.model.Storage;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* Created by wangnan on 2016/6/30.
|
|
|
*/
|
|
|
@Component
|
|
|
public class ProductGoodsLogicService {
|
|
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
|
|
|
@Autowired
|
|
|
private GoodsMapper goodsMapper;
|
|
|
@Autowired
|
|
|
private GoodsCoverImageMapper goodsCoverImageMapper;
|
|
|
@Autowired
|
|
|
private StorageService storageService;
|
|
|
@Autowired
|
|
|
private BasicDataCacheService basicDataCacheService;
|
|
|
|
|
|
/**
|
|
|
* 全量增量都有调用
|
|
|
*/
|
|
|
public List<ProductGoodsBO> getProductGoodsBOList(List<Integer> productIds) {
|
|
|
// 构建productColorsMap
|
|
|
try {
|
|
|
if (productIds == null || productIds.isEmpty()) {
|
|
|
return new ArrayList<ProductGoodsBO>();
|
|
|
}
|
|
|
List<Goods> goodsList = goodsMapper.selectListByProductIds(productIds);
|
|
|
List<Integer> goodsIds = goodsList.stream().map(Goods::getId).collect(Collectors.toList());
|
|
|
List<GoodsCoverImage> goodsCoverImageList = new ArrayList<GoodsCoverImage>();
|
|
|
List<Storage> storageList = new ArrayList<>();
|
|
|
if (goodsIds != null && !goodsIds.isEmpty()) {
|
|
|
goodsCoverImageList = goodsCoverImageMapper.selectGoodsCoverImage(goodsIds);
|
|
|
storageList = storageService.getStoragesByGoodsIds(goodsIds);
|
|
|
}
|
|
|
// 构建goodStorageMap
|
|
|
Map<Integer, Integer> goodStorageMap = new HashMap<>();
|
|
|
if (!CollectionUtils.isEmpty(storageList)) {
|
|
|
for (Storage storage : storageList) {
|
|
|
if (goodStorageMap.containsKey(storage.getGoodsId())) {
|
|
|
goodStorageMap.put(storage.getGoodsId(), storage.getStorageNum() + goodStorageMap.get(storage.getGoodsId()));
|
|
|
} else {
|
|
|
goodStorageMap.put(storage.getGoodsId(), storage.getStorageNum());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 构建goodsCoverImagesMap
|
|
|
Map<Integer, GoodsCoverImage> goodsCoverImagesMap = new HashMap<>();
|
|
|
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((coverImage.getCover_2()))) {
|
|
|
goodsCoverImage.setCover_2(coverImage.getCover_2());
|
|
|
}
|
|
|
} else {
|
|
|
goodsCoverImagesMap.put(coverImage.getGoodsId(), coverImage);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 根据productId分组
|
|
|
List<ProductGoodBO> productGoodBOs = new ArrayList<>();
|
|
|
for (Goods goods : goodsList) {
|
|
|
ProductGoodBO productGoodBO = new ProductGoodBO();
|
|
|
productGoodBO.setProduct_id(goods.getProductId());
|
|
|
JSONObject goodsJSONObject = this.fillGoodJSONObject(goods, basicDataCacheService.getColor(), goodsCoverImagesMap, goodStorageMap);
|
|
|
productGoodBO.setGoodJSONObject(goodsJSONObject);
|
|
|
productGoodBOs.add(productGoodBO);
|
|
|
}
|
|
|
Map<Integer, JSONArray> productGoodsMap = new HashMap<>();
|
|
|
for (ProductGoodBO productGoodBO : productGoodBOs) {
|
|
|
if (productGoodsMap.containsKey(productGoodBO.getProduct_id())) {
|
|
|
if (productGoodBO.getGoodJSONObject() != null) {
|
|
|
JSONArray jsonArray = productGoodsMap.get(productGoodBO.getProduct_id());
|
|
|
jsonArray.add(productGoodBO.getGoodJSONObject());
|
|
|
productGoodsMap.put(productGoodBO.getProduct_id(), jsonArray);
|
|
|
}
|
|
|
} else {
|
|
|
JSONArray jsonArray = new JSONArray();
|
|
|
jsonArray.add(productGoodBO.getGoodJSONObject());
|
|
|
productGoodsMap.put(productGoodBO.getProduct_id(), jsonArray);
|
|
|
}
|
|
|
}
|
|
|
List<ProductGoodsBO> productGoodsBOs = new ArrayList<>();
|
|
|
for (Map.Entry<Integer, JSONArray> entry : productGoodsMap.entrySet()) {
|
|
|
ProductGoodsBO pg = new ProductGoodsBO();
|
|
|
pg.setProductId(entry.getKey());
|
|
|
pg.setGoodsList(entry.getValue());
|
|
|
productGoodsBOs.add(pg);
|
|
|
}
|
|
|
return productGoodsBOs;
|
|
|
} catch (ExecutionException e) {
|
|
|
logger.error(e.getMessage(),e);
|
|
|
}
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取有库存且状态正常的color
|
|
|
*/
|
|
|
public void getColorSet(JSONArray goodsListJsonArray, Set<String> colorIdSet, Set<String> colorNameSet) {
|
|
|
for (int i = 0; i < goodsListJsonArray.size(); i++) {
|
|
|
JSONObject jsonObject = goodsListJsonArray.getJSONObject(i);
|
|
|
// 去除无库存和非正常状态的
|
|
|
if (!jsonObject.containsKey("status") || !jsonObject.get("status").toString().equals("1")) {
|
|
|
continue;
|
|
|
}
|
|
|
if (!jsonObject.containsKey("storage_num") || ((Integer) jsonObject.get("storage_num")) < 1) {
|
|
|
continue;
|
|
|
}
|
|
|
if (jsonObject.containsKey("color_id")) {
|
|
|
colorIdSet.add(jsonObject.get("color_id").toString());
|
|
|
}
|
|
|
if (jsonObject.containsKey("color_name")) {
|
|
|
colorNameSet.add(jsonObject.get("color_name").toString());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 转成good对象
|
|
|
*/
|
|
|
private JSONObject fillGoodJSONObject(Goods goods, Map<Integer, String> productColorsMap, Map<Integer, GoodsCoverImage> goodsCoverImagesMap, Map<Integer, Integer> goodStorageMap) {
|
|
|
JSONObject goodJSONObject = new JSONObject();
|
|
|
// 来自Goods
|
|
|
goodJSONObject.put("goods_id", String.valueOf(goods.getId()));
|
|
|
goodJSONObject.put("color_name", StringUtils.isBlank(goods.getColorName()) ? "" : goods.getColorName());
|
|
|
goodJSONObject.put("images_url", StringUtils.isBlank(goods.getColorImage()) ? "" : goods.getColorImage());
|
|
|
goodJSONObject.put("is_default", StringUtils.isBlank(goods.getIsDefault()) ? "" : goods.getIsDefault());
|
|
|
goodJSONObject.put("color_id", goods.getColorId() == null ? "" : goods.getColorId().toString());
|
|
|
goodJSONObject.put("status", goods.getStatus() == null ? "" : goods.getStatus().toString());
|
|
|
goodJSONObject.put("product_skc", goods.getProductSkc() == null ? "" : goods.getProductSkc());
|
|
|
goodJSONObject.put("color_value", "");
|
|
|
// 来自productColorsMap
|
|
|
String colorCode = productColorsMap.get(goods.getColorId());
|
|
|
goodJSONObject.put("color_code", StringUtils.isBlank(colorCode) ? "" : colorCode);
|
|
|
// 来自goodsCoverImagesMap
|
|
|
GoodsCoverImage goodsCoverImage = goodsCoverImagesMap.get(goods.getId());
|
|
|
String cover_1 = "";
|
|
|
String cover_2 = "";
|
|
|
if (goodsCoverImage != null) {
|
|
|
cover_1 = goodsCoverImage.getCover_1();
|
|
|
cover_2 = goodsCoverImage.getCover_2();
|
|
|
}
|
|
|
goodJSONObject.put("cover_1", StringUtils.isBlank(cover_1) ? "" : cover_1);
|
|
|
goodJSONObject.put("cover_2", StringUtils.isBlank(cover_2) ? "" : cover_2);
|
|
|
// 来自storageMap
|
|
|
Integer storage_num = goodStorageMap.get(goods.getId());
|
|
|
goodJSONObject.put("storage_num", storage_num == null ? 0 : storage_num);
|
|
|
return goodJSONObject;
|
|
|
}
|
|
|
} |
|
|
package com.yoho.search.consumer.service.logicService;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.search.consumer.service.bo.ProductGoodBO;
|
|
|
import com.yoho.search.consumer.service.bo.ProductGoodsBO;
|
|
|
import com.yoho.search.consumer.service.daoService.StorageService;
|
|
|
import com.yoho.search.consumer.service.logicService.cache.BasicDataCacheService;
|
|
|
import com.yoho.search.dal.GoodsMapper;
|
|
|
import com.yoho.search.dal.model.Goods;
|
|
|
import com.yoho.search.dal.model.GoodsCoverImage;
|
|
|
import com.yoho.search.dal.model.Storage;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* Created by wangnan on 2016/6/30.
|
|
|
*/
|
|
|
@Component
|
|
|
public class ProductGoodsLogicService {
|
|
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
|
|
|
@Autowired
|
|
|
private GoodsMapper goodsMapper;
|
|
|
@Autowired
|
|
|
private GoodsCoverImageLogicService goodsCoverImageLogicService;
|
|
|
@Autowired
|
|
|
private StorageService storageService;
|
|
|
@Autowired
|
|
|
private BasicDataCacheService basicDataCacheService;
|
|
|
|
|
|
/**
|
|
|
* 全量增量都有调用
|
|
|
*/
|
|
|
public List<ProductGoodsBO> getProductGoodsBOList(List<Integer> productIds) {
|
|
|
// 构建productColorsMap
|
|
|
try {
|
|
|
if (productIds == null || productIds.isEmpty()) {
|
|
|
return new ArrayList<ProductGoodsBO>();
|
|
|
}
|
|
|
List<Goods> goodsList = goodsMapper.selectListByProductIds(productIds);
|
|
|
List<Integer> goodsIds = goodsList.stream().map(Goods::getId).collect(Collectors.toList());
|
|
|
List<Storage> storageList = new ArrayList<>();
|
|
|
if (goodsIds != null && !goodsIds.isEmpty()) {
|
|
|
storageList = storageService.getStoragesByGoodsIds(goodsIds);
|
|
|
}
|
|
|
// 构建goodStorageMap
|
|
|
Map<Integer, Integer> goodStorageMap = new HashMap<>();
|
|
|
if (!CollectionUtils.isEmpty(storageList)) {
|
|
|
for (Storage storage : storageList) {
|
|
|
if (goodStorageMap.containsKey(storage.getGoodsId())) {
|
|
|
goodStorageMap.put(storage.getGoodsId(), storage.getStorageNum() + goodStorageMap.get(storage.getGoodsId()));
|
|
|
} else {
|
|
|
goodStorageMap.put(storage.getGoodsId(), storage.getStorageNum());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 构建goodsCoverImagesMap
|
|
|
Map<Integer, GoodsCoverImage> goodsCoverImagesMap = goodsCoverImageLogicService.selectGoodsCoverImagesByProductIds(productIds);
|
|
|
|
|
|
// 根据productId分组
|
|
|
List<ProductGoodBO> productGoodBOs = new ArrayList<>();
|
|
|
for (Goods goods : goodsList) {
|
|
|
ProductGoodBO productGoodBO = new ProductGoodBO();
|
|
|
productGoodBO.setProduct_id(goods.getProductId());
|
|
|
JSONObject goodsJSONObject = this.fillGoodJSONObject(goods, basicDataCacheService.getColor(), goodsCoverImagesMap, goodStorageMap);
|
|
|
productGoodBO.setGoodJSONObject(goodsJSONObject);
|
|
|
productGoodBOs.add(productGoodBO);
|
|
|
}
|
|
|
Map<Integer, JSONArray> productGoodsMap = new HashMap<>();
|
|
|
for (ProductGoodBO productGoodBO : productGoodBOs) {
|
|
|
if (productGoodsMap.containsKey(productGoodBO.getProduct_id())) {
|
|
|
if (productGoodBO.getGoodJSONObject() != null) {
|
|
|
JSONArray jsonArray = productGoodsMap.get(productGoodBO.getProduct_id());
|
|
|
jsonArray.add(productGoodBO.getGoodJSONObject());
|
|
|
productGoodsMap.put(productGoodBO.getProduct_id(), jsonArray);
|
|
|
}
|
|
|
} else {
|
|
|
JSONArray jsonArray = new JSONArray();
|
|
|
jsonArray.add(productGoodBO.getGoodJSONObject());
|
|
|
productGoodsMap.put(productGoodBO.getProduct_id(), jsonArray);
|
|
|
}
|
|
|
}
|
|
|
List<ProductGoodsBO> productGoodsBOs = new ArrayList<>();
|
|
|
for (Map.Entry<Integer, JSONArray> entry : productGoodsMap.entrySet()) {
|
|
|
ProductGoodsBO pg = new ProductGoodsBO();
|
|
|
pg.setProductId(entry.getKey());
|
|
|
pg.setGoodsList(entry.getValue());
|
|
|
productGoodsBOs.add(pg);
|
|
|
}
|
|
|
return productGoodsBOs;
|
|
|
} catch (ExecutionException e) {
|
|
|
logger.error(e.getMessage(),e);
|
|
|
}
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取有库存且状态正常的color
|
|
|
*/
|
|
|
public void getColorSet(JSONArray goodsListJsonArray, Set<String> colorIdSet, Set<String> colorNameSet) {
|
|
|
for (int i = 0; i < goodsListJsonArray.size(); i++) {
|
|
|
JSONObject jsonObject = goodsListJsonArray.getJSONObject(i);
|
|
|
// 去除无库存和非正常状态的
|
|
|
if (!jsonObject.containsKey("status") || !jsonObject.get("status").toString().equals("1")) {
|
|
|
continue;
|
|
|
}
|
|
|
if (!jsonObject.containsKey("storage_num") || ((Integer) jsonObject.get("storage_num")) < 1) {
|
|
|
continue;
|
|
|
}
|
|
|
if (jsonObject.containsKey("color_id")) {
|
|
|
colorIdSet.add(jsonObject.get("color_id").toString());
|
|
|
}
|
|
|
if (jsonObject.containsKey("color_name")) {
|
|
|
colorNameSet.add(jsonObject.get("color_name").toString());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 转成good对象
|
|
|
*/
|
|
|
private JSONObject fillGoodJSONObject(Goods goods, Map<Integer, String> productColorsMap, Map<Integer, GoodsCoverImage> goodsCoverImagesMap, Map<Integer, Integer> goodStorageMap) {
|
|
|
JSONObject goodJSONObject = new JSONObject();
|
|
|
// 来自Goods
|
|
|
goodJSONObject.put("goods_id", String.valueOf(goods.getId()));
|
|
|
goodJSONObject.put("color_name", StringUtils.isBlank(goods.getColorName()) ? "" : goods.getColorName());
|
|
|
goodJSONObject.put("images_url", StringUtils.isBlank(goods.getColorImage()) ? "" : goods.getColorImage());
|
|
|
goodJSONObject.put("is_default", StringUtils.isBlank(goods.getIsDefault()) ? "" : goods.getIsDefault());
|
|
|
goodJSONObject.put("color_id", goods.getColorId() == null ? "" : goods.getColorId().toString());
|
|
|
goodJSONObject.put("status", goods.getStatus() == null ? "" : goods.getStatus().toString());
|
|
|
goodJSONObject.put("product_skc", goods.getProductSkc() == null ? "" : goods.getProductSkc());
|
|
|
goodJSONObject.put("color_value", "");
|
|
|
// 来自productColorsMap
|
|
|
String colorCode = productColorsMap.get(goods.getColorId());
|
|
|
goodJSONObject.put("color_code", StringUtils.isBlank(colorCode) ? "" : colorCode);
|
|
|
// 来自goodsCoverImagesMap
|
|
|
GoodsCoverImage goodsCoverImage = goodsCoverImagesMap.get(goods.getId());
|
|
|
String cover_1 = "";
|
|
|
String cover_2 = "";
|
|
|
if (goodsCoverImage != null) {
|
|
|
cover_1 = goodsCoverImage.getCover_1();
|
|
|
cover_2 = goodsCoverImage.getCover_2();
|
|
|
}
|
|
|
goodJSONObject.put("cover_1", StringUtils.isBlank(cover_1) ? "" : cover_1);
|
|
|
goodJSONObject.put("cover_2", StringUtils.isBlank(cover_2) ? "" : cover_2);
|
|
|
// 来自storageMap
|
|
|
Integer storage_num = goodStorageMap.get(goods.getId());
|
|
|
goodJSONObject.put("storage_num", storage_num == null ? 0 : storage_num);
|
|
|
return goodJSONObject;
|
|
|
}
|
|
|
} |
...
|
...
|
|