|
|
package com.yoho.ufo.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.core.rest.client.ServiceCaller;
|
|
|
import com.yoho.ufo.dal.ProductSortMapper;
|
|
|
import com.yoho.ufo.dal.SearchSortRelationMapper;
|
|
|
import com.yoho.ufo.dal.Yoho2ufoProductMapper;
|
|
|
import com.yoho.ufo.dal.model.SearchSortRelation;
|
|
|
import com.yoho.ufo.dal.model.Yoho2ufoProduct;
|
|
|
import com.yoho.ufo.model.commoditybasicrole.category.ProductSort;
|
|
|
import com.yoho.ufo.model.goodsmanage.YohoProductInfo;
|
|
|
import com.yoho.ufo.service.Yoho2ufoProductService;
|
|
|
import com.yoho.ufo.util.CollectionUtil;
|
|
|
import com.yoho.ufo.util.ImagesConstant;
|
|
|
import com.yoho.ufo.util.ImagesHelper;
|
|
|
import com.yohobuy.ufo.model.common.ApiResponse;
|
|
|
import com.yohobuy.ufo.model.common.PageResponseBO;
|
|
|
import com.yohobuy.ufo.model.request.yoho2ufoproduct.Yoho2ufoProductReqBo;
|
|
|
import com.yohobuy.ufo.model.response.yoho2ufoproduct.Yoho2ufoProductRespBo;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
public class Yoho2ufoProductServiceImpl implements Yoho2ufoProductService {
|
|
|
|
|
|
@Resource
|
|
|
Yoho2ufoProductMapper yoho2ufoProductMapper;
|
|
|
|
|
|
@Resource
|
|
|
ServiceCaller serviceCaller;
|
|
|
|
|
|
@Resource
|
|
|
SearchSortRelationMapper searchSortRelationMapper;
|
|
|
|
|
|
@Resource
|
|
|
ProductSortMapper productSortMapper;
|
|
|
|
|
|
@Value("${search.server.address}")
|
|
|
private String searchServerAddress;
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(Yoho2ufoProductServiceImpl.class);
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse<PageResponseBO<Yoho2ufoProductRespBo>> list(Yoho2ufoProductReqBo bo) {
|
|
|
int count = yoho2ufoProductMapper.selectCount(bo.getProductSkn(), bo.getProductName(), bo.getBrandName());
|
|
|
List<Yoho2ufoProductRespBo> respBoList = new ArrayList<>();
|
|
|
if (count > 0) {
|
|
|
List<Yoho2ufoProduct> productList = yoho2ufoProductMapper.selectPage(bo.getProductSkn(),
|
|
|
bo.getProductName(),
|
|
|
bo.getBrandName(),
|
|
|
bo.getStartIndex(),
|
|
|
bo.getRows());
|
|
|
List<Integer> sknList = CollectionUtil.distinct(productList, Yoho2ufoProduct::getProductSkn);
|
|
|
//调用搜索的接口批量查询产品信息
|
|
|
List<YohoProductInfo> productBoList = getProductBoBySknList(sknList);
|
|
|
if (productBoList.size() > 0) {
|
|
|
//yoho大分类
|
|
|
List<Integer> yohoMaxSortList = CollectionUtil.distinct(productBoList, YohoProductInfo::getMaxSortId);
|
|
|
List<SearchSortRelation> searchMaxSortRelations = searchSortRelationMapper.selectByYohoSortIds(yohoMaxSortList);
|
|
|
Map<Integer, SearchSortRelation> searchMaxSortRelationsMap = CollectionUtils.isEmpty(searchMaxSortRelations) ? new HashMap<>() :
|
|
|
CollectionUtil.extractMap(searchMaxSortRelations, SearchSortRelation::getYohoSortId);
|
|
|
//yoho小分类
|
|
|
List<Integer> yohoSmallSortList = CollectionUtil.distinct(productBoList, YohoProductInfo::getSmallSortId);
|
|
|
List<SearchSortRelation> searchSmallSortRelations = searchSortRelationMapper.selectByYohoSmallSortIds(yohoSmallSortList);
|
|
|
Map<Integer, SearchSortRelation> searchSmallSortRelationsMap = CollectionUtils.isEmpty(searchSmallSortRelations) ? new HashMap<>() :
|
|
|
CollectionUtil.extractMap(searchSmallSortRelations, SearchSortRelation::getYohoSmallSortId);
|
|
|
//ufo大分类
|
|
|
List<Integer> ufoMaxSortList = CollectionUtil.distinct(searchMaxSortRelations, SearchSortRelation::getUfoSortId);
|
|
|
List<ProductSort> productMaxSortList = new ArrayList<>();
|
|
|
if(!CollectionUtils.isEmpty(ufoMaxSortList)){
|
|
|
productMaxSortList = productSortMapper.selectSortByIdList(ufoMaxSortList);
|
|
|
}
|
|
|
Map<Integer, ProductSort> ufoMaxSortMap = CollectionUtils.isEmpty(productMaxSortList) ? new HashMap<>() :
|
|
|
CollectionUtil.extractMap(productMaxSortList, ProductSort::getId);
|
|
|
//ufo小分类
|
|
|
List<Integer> ufoSmallSortList = CollectionUtil.distinct(searchSmallSortRelations, SearchSortRelation::getUfoSortId);
|
|
|
List<ProductSort> productSmallSortList = new ArrayList<>();
|
|
|
if(!CollectionUtils.isEmpty(ufoSmallSortList)){
|
|
|
productSmallSortList = productSortMapper.selectSortByIdList(ufoSmallSortList);
|
|
|
}
|
|
|
Map<Integer, ProductSort> ufoSmallSortMap = CollectionUtils.isEmpty(productSmallSortList) ? new HashMap<>() :
|
|
|
CollectionUtil.extractMap(productSmallSortList, ProductSort::getId);
|
|
|
|
|
|
Map<Integer, YohoProductInfo> productBoMap = CollectionUtil.extractMap(productBoList, YohoProductInfo::getProductSkn);
|
|
|
|
|
|
productList.forEach(p -> {
|
|
|
YohoProductInfo yohoProductBo = productBoMap.get(p.getProductSkn());
|
|
|
String yohoSortStr = "";
|
|
|
if(!StringUtils.isEmpty(yohoProductBo.getMaxSortName())){
|
|
|
yohoSortStr += yohoProductBo.getMaxSortName();
|
|
|
}
|
|
|
if(!StringUtils.isEmpty(yohoProductBo.getMiddleSortName())){
|
|
|
yohoSortStr += "/" + yohoProductBo.getMiddleSortName();
|
|
|
}
|
|
|
if(!StringUtils.isEmpty(yohoProductBo.getSmallSortName())){
|
|
|
yohoSortStr += "/" + yohoProductBo.getSmallSortName();
|
|
|
}
|
|
|
SearchSortRelation searchMaxSortRelation = searchMaxSortRelationsMap.get(yohoProductBo.getMaxSortId());
|
|
|
ProductSort ufoMaxSort = searchMaxSortRelation == null ? null :
|
|
|
ufoMaxSortMap.get(searchMaxSortRelation.getUfoSortId());
|
|
|
SearchSortRelation searchSmallSortRelation = searchSmallSortRelationsMap.get(yohoProductBo.getSmallSortId());
|
|
|
ProductSort ufoSmallSort = searchSmallSortRelation == null ? null :
|
|
|
ufoSmallSortMap.get(searchSmallSortRelation.getUfoSortId());
|
|
|
String ufoSortStr = "";
|
|
|
if(ufoMaxSort != null && (!StringUtils.isEmpty(ufoMaxSort.getSortName()))){
|
|
|
ufoSortStr += ufoMaxSort.getSortName();
|
|
|
}
|
|
|
if(ufoSmallSort != null && (!StringUtils.isEmpty(ufoSmallSort.getSortName()))){
|
|
|
ufoSortStr += "/" + ufoSmallSort.getSortName();
|
|
|
}
|
|
|
|
|
|
Yoho2ufoProductRespBo yoho2ufoProductRespBo = new Yoho2ufoProductRespBo();
|
|
|
yoho2ufoProductRespBo.setProductSkn(p.getProductSkn());
|
|
|
yoho2ufoProductRespBo.setProductName(yohoProductBo.getProductName());
|
|
|
yoho2ufoProductRespBo.setBrandName(yohoProductBo.getBrandName());
|
|
|
yoho2ufoProductRespBo.setId(p.getId());
|
|
|
yoho2ufoProductRespBo.setYohoSortStr(yohoSortStr);
|
|
|
yoho2ufoProductRespBo.setUfoSortStr(ufoSortStr);
|
|
|
yoho2ufoProductRespBo.setYohoImage(yohoProductBo.getImageUrl());
|
|
|
yoho2ufoProductRespBo.setUfoImage(p.getUfoImage());
|
|
|
respBoList.add(yoho2ufoProductRespBo);
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
|
|
|
PageResponseBO<Yoho2ufoProductRespBo> pageBo = new PageResponseBO<>(count, respBoList, bo.getPage(), bo.getRows());
|
|
|
return new ApiResponse<>(pageBo);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse<Yoho2ufoProductRespBo> getYoho2ufoProductBySkn(Integer skn) {
|
|
|
List<Integer> skns = new ArrayList<>();
|
|
|
skns.add(skn);
|
|
|
List<YohoProductInfo> productBoList = getProductBoBySknList(skns);
|
|
|
Yoho2ufoProductRespBo yoho2ufoProductRespBo = new Yoho2ufoProductRespBo();
|
|
|
if(CollectionUtils.isEmpty(productBoList)){
|
|
|
return new ApiResponse<>(yoho2ufoProductRespBo);
|
|
|
}
|
|
|
YohoProductInfo yohoProductBo = productBoList.get(0);
|
|
|
Yoho2ufoProduct yoho2ufoProductFromDB = yoho2ufoProductMapper.selectByProductSkn(skn);
|
|
|
if(yoho2ufoProductFromDB != null){
|
|
|
yoho2ufoProductRespBo.setUfoImage(yoho2ufoProductFromDB.getUfoImage());
|
|
|
}
|
|
|
yoho2ufoProductRespBo.setProductSkn(skn);
|
|
|
yoho2ufoProductRespBo.setProductName(yohoProductBo.getProductName());
|
|
|
yoho2ufoProductRespBo.setBrandName(yohoProductBo.getBrandName());
|
|
|
yoho2ufoProductRespBo.setYohoImage(yohoProductBo.getImageUrl());
|
|
|
String yohoSortStr = "";
|
|
|
if(!StringUtils.isEmpty(yohoProductBo.getMaxSortName())){
|
|
|
yohoSortStr += yohoProductBo.getMaxSortName();
|
|
|
}
|
|
|
if(!StringUtils.isEmpty(yohoProductBo.getMiddleSortName())){
|
|
|
yohoSortStr += "/" + yohoProductBo.getMiddleSortName();
|
|
|
}
|
|
|
if(!StringUtils.isEmpty(yohoProductBo.getSmallSortName())){
|
|
|
yohoSortStr += "/" + yohoProductBo.getSmallSortName();
|
|
|
}
|
|
|
yoho2ufoProductRespBo.setYohoSortStr(yohoSortStr);
|
|
|
//ufo大分类
|
|
|
SearchSortRelation searchMaxSortRelation = searchSortRelationMapper.selectByYohoSortId(yohoProductBo.getMaxSortId());
|
|
|
ProductSort ufoMaxSort = searchMaxSortRelation == null ? null :
|
|
|
productSortMapper.selectOneById(searchMaxSortRelation.getUfoSortId());
|
|
|
//ufo小分类
|
|
|
SearchSortRelation searchSmallSortRelation = searchSortRelationMapper.selectByYohoSmallSortId(yohoProductBo.getSmallSortId());
|
|
|
ProductSort ufoSmallSort = searchSmallSortRelation == null ? null :
|
|
|
productSortMapper.selectOneById(searchSmallSortRelation.getUfoSortId());
|
|
|
String ufoSortStr = "";
|
|
|
if(ufoMaxSort != null && (!StringUtils.isEmpty(ufoMaxSort.getSortName()))){
|
|
|
ufoSortStr += ufoMaxSort.getSortName();
|
|
|
}
|
|
|
if(ufoSmallSort != null && (!StringUtils.isEmpty(ufoSmallSort.getSortName()))){
|
|
|
ufoSortStr += "/" + ufoSmallSort.getSortName();
|
|
|
}
|
|
|
yoho2ufoProductRespBo.setUfoSortStr(ufoSortStr);
|
|
|
return new ApiResponse<>(yoho2ufoProductRespBo);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据skn集合调用服务查询对应商品详情
|
|
|
* 调用搜索的接口批量查询产品信息
|
|
|
*
|
|
|
* @param sknList skn集合list
|
|
|
* @return ProductBo集合
|
|
|
*/
|
|
|
public List<YohoProductInfo> getProductBoBySknList(Collection<Integer> sknList) {
|
|
|
|
|
|
List<YohoProductInfo> productBoList = new ArrayList<>();
|
|
|
//调用搜索的接口批量查询产品信息
|
|
|
String url = searchServerAddress + "productindex/productListBySkn.json?product_skn=" +
|
|
|
org.apache.commons.lang.StringUtils.join(sknList, ",");
|
|
|
JSONObject jsonObject = serviceCaller.get("", url, null, JSONObject.class, null).get(1);
|
|
|
if(jsonObject != null && jsonObject.getJSONObject("data") != null){
|
|
|
JSONArray productArray = jsonObject.getJSONObject("data").getJSONArray("product_list");
|
|
|
if(org.apache.commons.collections.CollectionUtils.isNotEmpty(productArray)){
|
|
|
for(int index = 0; index < productArray.size(); index++){
|
|
|
JSONObject jsonObject1 = (JSONObject)productArray.get(index);
|
|
|
YohoProductInfo yohoProductInfo = new YohoProductInfo();
|
|
|
yohoProductInfo.setBrandName(jsonObject1.getString("brand_name"));
|
|
|
yohoProductInfo.setImageUrl(ImagesHelper.getImageAbsoluteUrl(jsonObject1.getString("default_images"), ImagesConstant.BUCKET_GOODS_IMG));
|
|
|
yohoProductInfo.setMaxSortId(jsonObject1.getInteger("max_sort_id"));
|
|
|
yohoProductInfo.setMiddleSortId(jsonObject1.getInteger("middle_sort_id"));
|
|
|
yohoProductInfo.setMaxSortName(jsonObject1.getString("max_sort_name"));
|
|
|
yohoProductInfo.setMiddleSortName(jsonObject1.getString("middle_sort_name"));
|
|
|
yohoProductInfo.setSmallSortId(jsonObject1.getInteger("small_sort_id"));
|
|
|
yohoProductInfo.setSmallSortName(jsonObject1.getString("small_sort_name"));
|
|
|
yohoProductInfo.setProductName(jsonObject1.getString("product_name"));
|
|
|
yohoProductInfo.setProductSkn(jsonObject1.getInteger("product_skn"));
|
|
|
productBoList.add(yohoProductInfo);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
return productBoList;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse<Void> deleteYoho2ufoProductById(Integer id) {
|
|
|
yoho2ufoProductMapper.deleteByPrimaryKey(id);
|
|
|
return new ApiResponse<>();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse<Void> deleteBatchYoho2ufoProduct(String skns) {
|
|
|
if(!StringUtils.isEmpty(skns)){
|
|
|
yoho2ufoProductMapper.deleteBatchBySkns(skns);
|
|
|
}
|
|
|
return new ApiResponse<>();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse<Void> addOrUpdate(Yoho2ufoProduct reqBo) {
|
|
|
List<Integer> skns = new ArrayList<>();
|
|
|
skns.add(reqBo.getProductSkn());
|
|
|
List<YohoProductInfo> productBoList = getProductBoBySknList(skns);
|
|
|
if(CollectionUtils.isEmpty(productBoList)){
|
|
|
return new ApiResponse<>();
|
|
|
}
|
|
|
YohoProductInfo yohoProductBo = productBoList.get(0);
|
|
|
Yoho2ufoProduct yoho2ufoProduct = new Yoho2ufoProduct();
|
|
|
yoho2ufoProduct.setUfoImage(reqBo.getUfoImage());
|
|
|
yoho2ufoProduct.setProductSkn(reqBo.getProductSkn());
|
|
|
yoho2ufoProduct.setProductName(yohoProductBo.getProductName());
|
|
|
yoho2ufoProduct.setBrandName(yohoProductBo.getBrandName());
|
|
|
yoho2ufoProduct.setYohoImage(yohoProductBo.getImageUrl());
|
|
|
yoho2ufoProduct.setUpdateTime((int) (System.currentTimeMillis() / 1000));
|
|
|
Yoho2ufoProduct yoho2ufoProductFromDB = yoho2ufoProductMapper.selectByProductSkn(reqBo.getProductSkn());
|
|
|
if(reqBo.getId() != null && reqBo.getId() > 0){ //更新操作
|
|
|
yoho2ufoProduct.setId(reqBo.getId());
|
|
|
yoho2ufoProductMapper.updateByPrimaryKeySelective(yoho2ufoProduct);
|
|
|
}else if(yoho2ufoProductFromDB == null){ //新增
|
|
|
yoho2ufoProductMapper.insert(yoho2ufoProduct);
|
|
|
}else{
|
|
|
return new ApiResponse<>(201,"该skn数据库已存在");
|
|
|
}
|
|
|
return new ApiResponse<>();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse<Void> insertBatch(String sknStr) {
|
|
|
Map<Integer, YohoProductInfo> productBoMap;
|
|
|
List<Yoho2ufoProduct> yoho2ufoProductList = new ArrayList<>();
|
|
|
String skns = sknStr.trim();
|
|
|
String addskns = skns.replaceAll("\\s*", "");
|
|
|
String[] addSknArray = addskns.split(",");
|
|
|
List<String> sknStrList = Arrays.asList(addSknArray);
|
|
|
List<Integer> sknList = sknStrList.stream().map(Integer::valueOf).collect(Collectors.toList());
|
|
|
List<Yoho2ufoProduct> yoho2ufoProductExsit = yoho2ufoProductMapper.selectByProductSkns(sknList);
|
|
|
if(!CollectionUtils.isEmpty(yoho2ufoProductExsit)){
|
|
|
List<Integer> exsitSkns = yoho2ufoProductExsit.stream().map(Yoho2ufoProduct::getProductSkn)
|
|
|
.collect(Collectors.toList());
|
|
|
sknList = sknList.stream().filter(item -> !exsitSkns.contains(item))
|
|
|
.collect(Collectors.toList());
|
|
|
}
|
|
|
List<YohoProductInfo> productBoList = getProductBoBySknList(sknList);
|
|
|
if (productBoList != null && productBoList.size() > 0) {
|
|
|
productBoMap = CollectionUtil.extractMap
|
|
|
(productBoList, YohoProductInfo::getProductSkn);
|
|
|
|
|
|
sknList.forEach(skn -> {
|
|
|
YohoProductInfo yohoProductBo = productBoMap.get(skn);
|
|
|
Yoho2ufoProduct yoho2ufoProduct = new Yoho2ufoProduct();
|
|
|
yoho2ufoProduct.setProductSkn(skn);
|
|
|
yoho2ufoProduct.setProductName(yohoProductBo.getProductName());
|
|
|
yoho2ufoProduct.setBrandName(yohoProductBo.getBrandName());
|
|
|
yoho2ufoProduct.setYohoImage(yohoProductBo.getImageUrl());
|
|
|
yoho2ufoProduct.setUpdateTime((int) (System.currentTimeMillis() / 1000));
|
|
|
yoho2ufoProductList.add(yoho2ufoProduct);
|
|
|
});
|
|
|
yoho2ufoProductMapper.batchInsert(yoho2ufoProductList);
|
|
|
}
|
|
|
return new ApiResponse<>();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse<Yoho2ufoProductRespBo> getYoho2ufoProductById(Integer id) {
|
|
|
Yoho2ufoProduct yoho2ufoProduct = yoho2ufoProductMapper.selectByPrimaryKey(id);
|
|
|
if(yoho2ufoProduct != null){
|
|
|
return getYoho2ufoProductBySkn(yoho2ufoProduct.getProductSkn());
|
|
|
}
|
|
|
return new ApiResponse<>();
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|