|
|
package com.yoho.ufo.service.impl;
|
|
|
|
|
|
import com.yoho.core.rest.client.ServiceCaller;
|
|
|
import com.yoho.product.model.GoodsBo;
|
|
|
import com.yoho.product.model.GoodsImagesBo;
|
|
|
import com.yoho.product.model.ProductBo;
|
|
|
import com.yoho.product.request.BatchBaseRequest;
|
|
|
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.service.Yoho2ufoProductService;
|
|
|
import com.yoho.ufo.util.CollectionUtil;
|
|
|
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.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
public class Yoho2ufoProductServiceImpl implements Yoho2ufoProductService {
|
|
|
|
|
|
@Resource
|
|
|
Yoho2ufoProductMapper yoho2ufoProductMapper;
|
|
|
|
|
|
@Resource
|
|
|
ServiceCaller serviceCaller;
|
|
|
|
|
|
@Resource
|
|
|
SearchSortRelationMapper searchSortRelationMapper;
|
|
|
|
|
|
@Resource
|
|
|
private ProductSortMapper productSortMapper;
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse<PageResponseBO<Yoho2ufoProductRespBo>> list(Yoho2ufoProductReqBo bo) {
|
|
|
int count = yoho2ufoProductMapper.selectCount(bo.getProductSkn(), bo.getProductName(), bo.getBrandName());
|
|
|
List<Yoho2ufoProductRespBo> respBoList = new ArrayList<>();
|
|
|
Map<Integer, ProductBo> productBoMap = new HashMap<>();
|
|
|
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<ProductBo> productBoList = getProductBoBySknList(sknList);
|
|
|
if (productBoList != null && productBoList.size() > 0) {
|
|
|
//yoho大分类
|
|
|
List<Integer> yohoMaxSortList = CollectionUtil.distinct(productBoList, ProductBo::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, ProductBo::getSmallSortId);
|
|
|
List<SearchSortRelation> searchSmallSortRelations = searchSortRelationMapper.selectByYohoSortIds(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 = 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::getYohoSmallSortId);
|
|
|
List<ProductSort> productSmallSortList = productSortMapper.selectSortByIdList(ufoSmallSortList);
|
|
|
Map<Integer, ProductSort> ufoSmallSortMap = CollectionUtils.isEmpty(productSmallSortList) ? new HashMap<>() :
|
|
|
CollectionUtil.extractMap(productSmallSortList, ProductSort::getId);
|
|
|
|
|
|
|
|
|
productBoList.forEach(p -> {
|
|
|
Integer skn = p.getProductPriceBo().getProductSkn();
|
|
|
productBoMap.put(skn, p);
|
|
|
});
|
|
|
|
|
|
productList.forEach(p -> {
|
|
|
ProductBo yohoProductBo = productBoMap.get(p.getProductSkn());
|
|
|
String yohoSortStr = yohoProductBo.getMaxSortName() + ":" + yohoProductBo.getMiddleSortName() + ":"
|
|
|
+ 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 = ufoMaxSort == null ? "":(ufoMaxSort.getSortName() + ":" + ufoSmallSort.getSortName());
|
|
|
Yoho2ufoProductRespBo yoho2ufoProductRespBo = new Yoho2ufoProductRespBo();
|
|
|
yoho2ufoProductRespBo.setProductSkn(p.getProductSkn());
|
|
|
yoho2ufoProductRespBo.setProductName(p.getProductName());
|
|
|
yoho2ufoProductRespBo.setBrandName(p.getBrandName());
|
|
|
yoho2ufoProductRespBo.setId(p.getId());
|
|
|
yoho2ufoProductRespBo.setYohoSortStr(yohoSortStr);
|
|
|
yoho2ufoProductRespBo.setUfoSortStr(ufoSortStr);
|
|
|
yoho2ufoProductRespBo.setYohoImage(getProductUrl(yohoProductBo));
|
|
|
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<ProductBo> productBoList = getProductBoBySknList(skns);
|
|
|
Yoho2ufoProductRespBo yoho2ufoProductRespBo = new Yoho2ufoProductRespBo();
|
|
|
if(CollectionUtils.isEmpty(productBoList)){
|
|
|
return new ApiResponse<>();
|
|
|
}
|
|
|
ProductBo yohoProductBo = productBoList.get(0);
|
|
|
Yoho2ufoProduct yoho2ufoProductFromDB = yoho2ufoProductMapper.selectByProductSkn(skn);
|
|
|
if(yoho2ufoProductFromDB != null){
|
|
|
yoho2ufoProductRespBo.setUfoImage(yoho2ufoProductFromDB.getUfoImage());
|
|
|
}
|
|
|
yoho2ufoProductRespBo.setProductName(yohoProductBo.getProductName());
|
|
|
yoho2ufoProductRespBo.setBrandName(yohoProductBo.getBrand().getBrandName());
|
|
|
yoho2ufoProductRespBo.setYohoImage(getProductUrl(yohoProductBo));
|
|
|
String yohoSortStr = yohoProductBo.getMaxSortName() + ":" + yohoProductBo.getMiddleSortName() + ":"
|
|
|
+ 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 = searchMaxSortRelation == null ? null :
|
|
|
productSortMapper.selectOneById(searchSmallSortRelation.getUfoSortId());
|
|
|
String ufoSortStr = (ufoMaxSort == null ? "" : ufoMaxSort.getSortName()) +":" +
|
|
|
(ufoSmallSort == null ? "" : ufoSmallSort.getSortName());
|
|
|
yoho2ufoProductRespBo.setUfoSortStr(ufoSortStr);
|
|
|
return new ApiResponse<>(yoho2ufoProductRespBo);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据skn集合调用服务查询对应商品详情.
|
|
|
*
|
|
|
* @param sknList skn集合list
|
|
|
* @return ProductBo集合
|
|
|
*/
|
|
|
public List<ProductBo> getProductBoBySknList(Collection<Integer> sknList) {
|
|
|
|
|
|
// 构建请求对象
|
|
|
BatchBaseRequest<Integer> request = new BatchBaseRequest<>();
|
|
|
request.setParams(new ArrayList<>(sknList));
|
|
|
|
|
|
return Arrays.asList(serviceCaller.call("product.batchQueryProductsBySkns", request, ProductBo[].class));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse<Void> deleteYoho2ufoProductById(Integer id) {
|
|
|
yoho2ufoProductMapper.deleteByPrimaryKey(id);
|
|
|
return new ApiResponse<>();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse<Void> deleteBatchYoho2ufoProduct(String ids) {
|
|
|
yoho2ufoProductMapper.deleteBatchByIds(ids);
|
|
|
return new ApiResponse<>();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse<Void> addOrUpdate(Yoho2ufoProductReqBo reqBo) {
|
|
|
List<Integer> skns = new ArrayList<>();
|
|
|
skns.add(reqBo.getProductSkn());
|
|
|
List<ProductBo> productBoList = getProductBoBySknList(skns);
|
|
|
if(CollectionUtils.isEmpty(productBoList)){
|
|
|
return new ApiResponse<>();
|
|
|
}
|
|
|
ProductBo yohoProductBo = productBoList.get(0);
|
|
|
Yoho2ufoProduct yoho2ufoProduct = new Yoho2ufoProduct();
|
|
|
yoho2ufoProduct.setUfoImage(reqBo.getUfoImage());
|
|
|
yoho2ufoProduct.setProductSkn(reqBo.getProductSkn());
|
|
|
yoho2ufoProduct.setProductName(yohoProductBo.getProductName());
|
|
|
yoho2ufoProduct.setBrandName(yohoProductBo.getBrand().getBrandName());
|
|
|
yoho2ufoProduct.setYohoImage(getProductUrl(yohoProductBo));
|
|
|
yoho2ufoProduct.setUpdateTime((int) (System.currentTimeMillis() / 1000));
|
|
|
Yoho2ufoProduct yoho2ufoProductFromDB = yoho2ufoProductMapper.selectByProductSkn(reqBo.getProductSkn());
|
|
|
if(yoho2ufoProductFromDB != null){
|
|
|
yoho2ufoProduct.setId(reqBo.getId());
|
|
|
yoho2ufoProductMapper.updateByPrimaryKeySelective(yoho2ufoProduct);
|
|
|
}else{
|
|
|
yoho2ufoProductMapper.insert(yoho2ufoProduct);
|
|
|
}
|
|
|
return new ApiResponse<>();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse<Void> insertBatch(String sknStr) {
|
|
|
Map<Integer, ProductBo> productBoMap = new HashMap<>();
|
|
|
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<ProductBo> productBoList = getProductBoBySknList(sknList);
|
|
|
if (productBoList != null && productBoList.size() > 0) {
|
|
|
productBoList.forEach(p -> {
|
|
|
Integer skn = p.getProductPriceBo().getProductSkn();
|
|
|
productBoMap.put(skn, p);
|
|
|
});
|
|
|
|
|
|
sknList.forEach(skn -> {
|
|
|
ProductBo yohoProductBo = productBoMap.get(skn);
|
|
|
Yoho2ufoProduct yoho2ufoProduct = new Yoho2ufoProduct();
|
|
|
yoho2ufoProduct.setProductSkn(skn);
|
|
|
yoho2ufoProduct.setProductName(yohoProductBo.getProductName());
|
|
|
yoho2ufoProduct.setBrandName(yohoProductBo.getBrand().getBrandName());
|
|
|
yoho2ufoProduct.setYohoImage(getProductUrl(yohoProductBo));
|
|
|
yoho2ufoProduct.setUpdateTime((int) (System.currentTimeMillis() / 1000));
|
|
|
yoho2ufoProductList.add(yoho2ufoProduct);
|
|
|
});
|
|
|
yoho2ufoProductMapper.batchInsert(yoho2ufoProductList);
|
|
|
}
|
|
|
return new ApiResponse<>();
|
|
|
}
|
|
|
|
|
|
//获取商品图片的url
|
|
|
private String getProductUrl(ProductBo productBo) {
|
|
|
List<GoodsBo> goodsList = productBo.getGoodsList();
|
|
|
String imageUrl = "";
|
|
|
if (goodsList != null && goodsList.size() > 0) {
|
|
|
for (GoodsBo goodsBo : goodsList) {
|
|
|
if ("Y".equals(goodsBo.getIsDefault())) {
|
|
|
List<GoodsImagesBo> goodsImagesBoList = goodsBo.getGoodsImagesList();
|
|
|
if (goodsImagesBoList != null && goodsImagesBoList.size() > 0) {
|
|
|
for (GoodsImagesBo goodsImagesBo : goodsImagesBoList) {
|
|
|
if ("Y".equals(goodsImagesBo.getIsDefault())) {
|
|
|
imageUrl = goodsImagesBo.getImageUrl();
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
if (StringUtils.isEmpty(imageUrl)) {
|
|
|
List<GoodsImagesBo> goodsImagesBoList = goodsList.get(0).getGoodsImagesList();
|
|
|
if (goodsImagesBoList != null && goodsImagesBoList.size() > 0) {
|
|
|
imageUrl = goodsImagesBoList.get(0).getImageUrl();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return imageUrl;
|
|
|
}
|
|
|
|
|
|
|
|
|
} |
...
|
...
|
|