|
|
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.alibaba.fastjson.JSONException;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.core.common.utils.DateUtil;
|
|
|
import com.yoho.error.exception.ServiceException;
|
|
|
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.ChannelSkuCompareRspBo;
|
|
|
import com.yoho.ufo.model.commoditybasicrole.size.Size;
|
|
|
import com.yoho.ufo.service.IBusinessExportService;
|
|
|
import com.yoho.ufo.service.IChannelSkuCompareService;
|
|
|
import com.yoho.ufo.service.model.PageResponseBO;
|
|
|
|
|
|
/**
|
|
|
* @author caoyan
|
|
|
*/
|
|
|
@Service("channelSkuCompareServiceImpl")
|
|
|
public class ChannelSkuCompareServiceImpl implements IChannelSkuCompareService, IBusinessExportService {
|
|
|
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int updateSuggestPrice(ChannelSkuCompareReq req) {
|
|
|
if(null == req.getStatus() || req.getStatus().intValue() != 0 || null == req.getId()) {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
ChannelSkuCompare csc = channelSkuCompareMapper.selectById(req.getId());
|
|
|
if(null == csc) {
|
|
|
LOGGER.error("id not exist! id is {}", req.getId());
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
Storage storage = storageMapper.selectByPrimaryKey(csc.getSku());
|
|
|
if(null == storage) {
|
|
|
LOGGER.error("storageId not exist! storageId is {}", csc.getSku());
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
//更新
|
|
|
BigDecimal suggestLowPrice = csc.getChannelPrice().multiply(BigDecimal.valueOf(1).subtract(csc.getLowRate()));
|
|
|
BigDecimal suggestHighPrice = csc.getChannelPrice().multiply(BigDecimal.valueOf(1).add(csc.getHighRate()));
|
|
|
//记录变更人信息
|
|
|
channelSkuCompareMapper.updateChangeStatusById(req.getId(), req.getStatus(), DateUtil.getCurrentTimeSecond(), new UserHelper().getUserId());
|
|
|
|
|
|
return storageMapper.updateSuggestPriceById(csc.getSku(), suggestLowPrice, suggestHighPrice);
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Class getDataClass() {
|
|
|
return ChannelSkuCompareRspBo.class;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<? extends Object> batchExport(String confStr) {
|
|
|
try {
|
|
|
ChannelSkuCompareReq request = new ChannelSkuCompareReq();
|
|
|
if(StringUtils.isNotEmpty(confStr)) {
|
|
|
request = JSONObject.parseObject(confStr, ChannelSkuCompareReq.class);
|
|
|
}
|
|
|
request.setSize(5000);
|
|
|
PageResponseBO<ChannelSkuCompareRspBo> pageResponseBO = queryList(request);
|
|
|
List<ChannelSkuCompareRspBo> boList = pageResponseBO.getList();
|
|
|
if (CollectionUtils.isEmpty(boList)) {
|
|
|
throw new ServiceException(400, "没有要导出的数据");
|
|
|
}
|
|
|
return boList;
|
|
|
} catch (JSONException e) {
|
|
|
LOGGER.warn("parse confStr error: confStr {}, e {}", confStr, e);
|
|
|
throw new ServiceException(400, "传入数据格式错误");
|
|
|
} catch (com.yoho.error.exception.ServiceException e) {
|
|
|
LOGGER.warn("make url error params is confStr {}, e {}", confStr, e);
|
|
|
throw new ServiceException(e.getCode(), e.getErrorMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
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.setId(csc.getId());
|
|
|
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));
|
|
|
}
|
|
|
bo.setStatus(csc.getStatus());
|
|
|
bo.setStatusStr(bo.getStatus().intValue()==0 ? "正常" : "异常");
|
|
|
boList.add(bo);
|
|
|
}
|
|
|
|
|
|
return boList;
|
|
|
}
|
|
|
|
|
|
private static String getFormatPrice(BigDecimal price) {
|
|
|
return String.format("%.2f", price.doubleValue());
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
} |
...
|
...
|
|