|
|
package com.yoho.ufo.service.impl;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.elasticsearch.common.collect.Lists;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import com.yoho.ufo.dal.ChannelSkuCompareMapper;
|
|
|
import com.yoho.ufo.dal.ProductMapper;
|
|
|
import com.yoho.ufo.dal.StorageMapper;
|
|
|
import com.yoho.ufo.dal.StoragePriceMapper;
|
|
|
import com.yoho.ufo.dal.UfoSizeMapper;
|
|
|
import com.yoho.ufo.dal.model.Product;
|
|
|
import com.yoho.ufo.dal.model.Storage;
|
|
|
import com.yoho.ufo.dal.model.StoragePrice;
|
|
|
import com.yoho.ufo.model.ChannelSkuCompare;
|
|
|
import com.yoho.ufo.model.ChannelSkuCompareReq;
|
|
|
import com.yoho.ufo.model.commoditybasicrole.size.Size;
|
|
|
import com.yoho.ufo.service.IChannelSkuCompareService;
|
|
|
import com.yoho.ufo.service.model.PageResponseBO;
|
|
|
import com.yohobuy.ufo.model.resp.product.ChannelSkuCompareRspBo;
|
|
|
|
|
|
/**
|
|
|
* @author caoyan
|
|
|
*/
|
|
|
@Service
|
|
|
public class ChannelSkuCompareServiceImpl implements IChannelSkuCompareService {
|
|
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(ChannelSkuCompareServiceImpl.class);
|
|
|
|
|
|
@Autowired
|
|
|
private ChannelSkuCompareMapper channelSkuCompareMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private ProductMapper productMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private UfoSizeMapper ufoSizeMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private StorageMapper storageMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private StoragePriceMapper storagePriceMapper;
|
|
|
|
|
|
@Override
|
|
|
public PageResponseBO<ChannelSkuCompareRspBo> queryList(ChannelSkuCompareReq req) {
|
|
|
LOGGER.info("queryList in. req is {}", req);
|
|
|
if(!checkAndBuildParam(req)) {
|
|
|
return null;
|
|
|
}
|
|
|
int total = channelSkuCompareMapper.selectTotalByCondition(req);
|
|
|
if(total == 0) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
List<ChannelSkuCompare> cscList = channelSkuCompareMapper.selectByCondition(req);
|
|
|
if(CollectionUtils.isEmpty(cscList)) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
//查询商品名称
|
|
|
List<Integer> productIdList = cscList.stream().map(ChannelSkuCompare::getProductId).collect(Collectors.toList());
|
|
|
List<Product> productList = productMapper.selectProductListByIds(productIdList);
|
|
|
if(CollectionUtils.isEmpty(productList)) {
|
|
|
return null;
|
|
|
}
|
|
|
Map<Integer, Product> productMap = productList.stream().collect(Collectors.toMap(Product::getId, p->p));
|
|
|
|
|
|
//查询尺码名称
|
|
|
List<Integer> sizeIdList = cscList.stream().map(ChannelSkuCompare::getSizeId).collect(Collectors.toList());
|
|
|
List<Size> sizeList = ufoSizeMapper.selectByIdList(sizeIdList);
|
|
|
Map<Integer, String> sizeIdNameMap = sizeList.stream().collect(Collectors.toMap(Size::getId, Size::getSizeName));
|
|
|
|
|
|
//查询平台建议价
|
|
|
List<Integer> skuList = cscList.stream().map(ChannelSkuCompare::getSku).collect(Collectors.toList());
|
|
|
List<Storage> storageList = storageMapper.selectByIds(skuList);
|
|
|
Map<Integer, Storage> storageMap = storageList.stream().collect(Collectors.toMap(Storage::getId, s->s));
|
|
|
|
|
|
//计算UFO当前价,可售sku最低价
|
|
|
List<StoragePrice> storagePriceList = storagePriceMapper.selectMinPriceByStorageIdList(skuList);
|
|
|
Map<Integer, BigDecimal> storageMinPriceMap = storagePriceList.stream().collect(Collectors.toMap(StoragePrice::getStorageId, StoragePrice::getPrice));
|
|
|
|
|
|
List<ChannelSkuCompareRspBo> respList = convertToResp(cscList, productMap, sizeIdNameMap, storageMap, storageMinPriceMap);
|
|
|
|
|
|
PageResponseBO<ChannelSkuCompareRspBo> result=new PageResponseBO<>();
|
|
|
result.setList(respList);
|
|
|
result.setPage(req.getPage());
|
|
|
result.setSize(req.getSize());
|
|
|
result.setTotal(total);
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
private List<ChannelSkuCompareRspBo> convertToResp(List<ChannelSkuCompare> cscList, Map<Integer, Product> productMap,
|
|
|
Map<Integer, String> sizeIdNameMap, Map<Integer, Storage> storageMap, Map<Integer, BigDecimal> storageMinPriceMap){
|
|
|
List<ChannelSkuCompareRspBo> boList = Lists.newArrayList();
|
|
|
for(ChannelSkuCompare csc : cscList) {
|
|
|
ChannelSkuCompareRspBo bo = new ChannelSkuCompareRspBo();
|
|
|
bo.setProductId(csc.getProductId());
|
|
|
bo.setSku(csc.getSku());
|
|
|
bo.setProductName(productMap.get(csc.getProductId()).getProductName());
|
|
|
bo.setSizeName(sizeIdNameMap.get(csc.getSizeId()));
|
|
|
bo.setChannelPrice(getFormatPrice(csc.getChannelPrice()));
|
|
|
bo.setChannelUrl(csc.getChannelUrl());
|
|
|
bo.setLowRate(csc.getLowRate().multiply(BigDecimal.valueOf(100)) + "%");
|
|
|
bo.setHighRate(csc.getHighRate().multiply(BigDecimal.valueOf(100)) + "%");
|
|
|
BigDecimal channelLowPrice = csc.getChannelPrice().multiply(BigDecimal.valueOf(1).subtract(csc.getLowRate()));
|
|
|
BigDecimal channelHighPrice = csc.getChannelPrice().multiply(BigDecimal.valueOf(1).add(csc.getHighRate()));
|
|
|
bo.setChannelPriceRange(getFormatPrice(channelLowPrice) + "~" + getFormatPrice(channelHighPrice));
|
|
|
bo.setUfoCurrentPrice(getFormatPrice(storageMinPriceMap.get(csc.getSku())));
|
|
|
BigDecimal ufoMinPrice = productMap.get(csc.getProductId()).getMinPrice();
|
|
|
bo.setUfoMinPrice(getFormatPrice(ufoMinPrice));
|
|
|
//毒当前价低于UFO价格红线则为异常,那么显示上回的建议价
|
|
|
BigDecimal suggestLowPrice = storageMap.get(csc.getSku()).getSuggestLowPrice();
|
|
|
BigDecimal suggestHighPrice = storageMap.get(csc.getSku()).getSuggestHighPrice();
|
|
|
if(null != suggestLowPrice && null != suggestHighPrice) {
|
|
|
bo.setSuggestPriceRange(getFormatPrice(suggestLowPrice) + "~" + getFormatPrice(suggestHighPrice));
|
|
|
}
|
|
|
if(csc.getChannelPrice().compareTo(ufoMinPrice) < 0) {//异常
|
|
|
bo.setStatus(1);
|
|
|
}else {
|
|
|
bo.setStatus(0);//正常
|
|
|
}
|
|
|
boList.add(bo);
|
|
|
}
|
|
|
|
|
|
return boList;
|
|
|
}
|
|
|
|
|
|
private static String getFormatPrice(BigDecimal price) {
|
|
|
return String.format("%d", price.intValue());
|
|
|
}
|
|
|
|
|
|
private boolean checkAndBuildParam(ChannelSkuCompareReq req) {
|
|
|
if(StringUtils.isNotEmpty(req.getProductName())) {
|
|
|
List<Product> productList = productMapper.selectByProductName(req.getProductName());
|
|
|
if(CollectionUtils.isEmpty(productList)) {
|
|
|
return false;
|
|
|
}else {
|
|
|
req.setProductIdList(productList.stream().map(Product::getId).collect(Collectors.toList()));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(StringUtils.isNotEmpty(req.getSizeName())) {
|
|
|
List<Size> sizeList = ufoSizeMapper.selectBySizeName(req.getSizeName());
|
|
|
if(CollectionUtils.isEmpty(sizeList)) {
|
|
|
return false;
|
|
|
}else {
|
|
|
req.setSizeIdList(sizeList.stream().map(Size::getId).collect(Collectors.toList()));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
} |
...
|
...
|
|