|
|
package com.yoho.search.consumer.service.logic.productIndex.viewBuilder;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
|
import java.text.DecimalFormat;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import com.yoho.search.base.utils.DateUtil;
|
|
|
import com.yoho.search.base.utils.MathUtils;
|
|
|
import com.yoho.search.consumer.service.bo.ProductIndexBO;
|
|
|
import com.yoho.search.consumer.service.bo.ProductPriceBO;
|
|
|
import com.yoho.search.consumer.service.logic.SpecialDealLogicService;
|
|
|
import com.yoho.search.consumer.service.logic.VipPriceLogicServie;
|
|
|
import com.yoho.search.consumer.service.logic.VipPriceLogicService;
|
|
|
import com.yoho.search.dal.ProductPriceMapper;
|
|
|
import com.yoho.search.dal.model.ProductPrice;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
|
import java.text.DecimalFormat;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* Created by wangnan on 2017/1/6.
|
...
|
...
|
@@ -35,82 +32,99 @@ public class ProductPriceBuilder implements ViewBuilder { |
|
|
@Autowired
|
|
|
private ProductPriceMapper productPriceMapper;
|
|
|
@Autowired
|
|
|
private VipPriceLogicServie vipPriceLogicServie;
|
|
|
private VipPriceLogicService vipPriceLogicService;
|
|
|
@Autowired
|
|
|
private SpecialDealLogicService specialSearchFieldLogicService;
|
|
|
|
|
|
@Override
|
|
|
public void build(List<ProductIndexBO> productIndexBOs, List<Integer> ids, List<Integer> sknList) {
|
|
|
List<ProductPrice> productPrices = productPriceMapper.selectBySkns(sknList);
|
|
|
List<ProductPrice> productPrices = productPriceMapper.selectBySknList(sknList);
|
|
|
Map<Integer, ProductPrice> productPricesMap = productPrices.stream().collect(Collectors.toMap(ProductPrice::getProductSkn, (p) -> p));
|
|
|
for (ProductIndexBO pi : productIndexBOs) {
|
|
|
// set default
|
|
|
pi.setIsDiscount("N");
|
|
|
pi.setSpecialoffer("N");
|
|
|
pi.setPromotionDiscount(new BigDecimal(1));
|
|
|
pi.setPromotionDiscountInt(10L);
|
|
|
|
|
|
pi.setIsStudentPrice("N");
|
|
|
pi.setIsStudentRebate("N");
|
|
|
pi.setIsnew("N");
|
|
|
|
|
|
for (ProductIndexBO productIndexBO : productIndexBOs) {
|
|
|
try {
|
|
|
// 如果价格不存在,则直接返回
|
|
|
ProductPrice productPrice = productPricesMap.get(pi.getProductSkn());
|
|
|
// 如果该skn对应价格对象不存在,直接跳过
|
|
|
ProductPrice productPrice = productPricesMap.get(productIndexBO.getProductSkn());
|
|
|
if (productPrice == null) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
// 构造ProductPriceBO
|
|
|
ProductPriceBO productPriceBO = new ProductPriceBO(productPrice);
|
|
|
this.fillProductPriceBO(productPriceBO);
|
|
|
this.buildProductPriceBO(productPriceBO);
|
|
|
// 构造ProductIndexBO
|
|
|
this.buildProductIndexBO(productIndexBO, productPriceBO);
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
pi.setMarketPrice(productPriceBO.getMarketPrice());
|
|
|
pi.setVipDiscountType(productPriceBO.getVipDiscountType());
|
|
|
pi.setSalesPrice(productPriceBO.getSalesPrice());
|
|
|
pi.setVipPrice(productPriceBO.getVipPrice());
|
|
|
pi.setVip1Price(productPriceBO.getVip1Price());
|
|
|
pi.setVip2Price(productPriceBO.getVip2Price());
|
|
|
pi.setVip3Price(productPriceBO.getVip3Price());
|
|
|
pi.setStudentPrice(productPriceBO.getStudentPrice());
|
|
|
/**
|
|
|
* 增量+全量都调用
|
|
|
*/
|
|
|
public void buildProductPriceBO(ProductPriceBO productPriceBO) {
|
|
|
// 1、计算并重设vip相关价格
|
|
|
vipPriceLogicService.fillProductPriceVipPrice(productPriceBO);
|
|
|
// 2、计算折扣相关
|
|
|
double salesPrice = this.getDoubleFromBigDecimal(productPriceBO.getSalesPrice());
|
|
|
double marketPrice = this.getDoubleFromBigDecimal(productPriceBO.getMarketPrice());
|
|
|
productPriceBO.setIsDiscount(salesPrice < marketPrice ? "Y" : "N");
|
|
|
double specialOffer = this.divide(1, productPriceBO.getSalesPrice(), productPriceBO.getMarketPrice());
|
|
|
if (specialOffer <= 0.5) {
|
|
|
productPriceBO.setSpecialoffer("Y");
|
|
|
} else {
|
|
|
productPriceBO.setSpecialoffer("N");
|
|
|
}
|
|
|
double promotionDiscountInt = this.divide(1, productPriceBO.getSalesPrice().multiply(new BigDecimal(10)), productPriceBO.getMarketPrice());
|
|
|
productPriceBO.setPromotionDiscountInt((long) promotionDiscountInt);
|
|
|
double promotionDiscount = this.divide(3, productPriceBO.getSalesPrice(), productPriceBO.getMarketPrice());
|
|
|
productPriceBO.setPromotionDiscount(this.getBigDecimalFromDouble(promotionDiscount));
|
|
|
// 3、设置学生价相关属性
|
|
|
productPriceBO.setIsStudentPrice(productPriceBO.getStudentPrice() != null ? "Y" : "N");
|
|
|
productPriceBO.setIsstudentrebate(productPriceBO.getStudentCoinRate() == null || productPriceBO.getStudentCoinRate().compareTo(BigDecimal.ZERO) == 0 ? "N" : "Y");
|
|
|
}
|
|
|
|
|
|
private void buildProductIndexBO(ProductIndexBO productIndexBO, ProductPriceBO productPriceBO) {
|
|
|
productIndexBO.setIsDiscount("N");
|
|
|
productIndexBO.setSpecialoffer("N");
|
|
|
productIndexBO.setPromotionDiscount(new BigDecimal(1));
|
|
|
productIndexBO.setPromotionDiscountInt(10L);
|
|
|
productIndexBO.setIsStudentPrice("N");
|
|
|
productIndexBO.setIsStudentRebate("N");
|
|
|
productIndexBO.setIsnew("N");
|
|
|
productIndexBO.setMarketPrice(productPriceBO.getMarketPrice());
|
|
|
productIndexBO.setVipDiscountType(productPriceBO.getVipDiscountType());
|
|
|
productIndexBO.setSalesPrice(productPriceBO.getSalesPrice());
|
|
|
productIndexBO.setVipPrice(productPriceBO.getVipPrice());
|
|
|
productIndexBO.setVip1Price(productPriceBO.getVip1Price());
|
|
|
productIndexBO.setVip2Price(productPriceBO.getVip2Price());
|
|
|
productIndexBO.setVip3Price(productPriceBO.getVip3Price());
|
|
|
productIndexBO.setStudentPrice(productPriceBO.getStudentPrice());
|
|
|
// 折扣相关
|
|
|
pi.setIsDiscount(productPriceBO.getIsDiscount());
|
|
|
pi.setSpecialoffer(productPriceBO.getSpecialoffer());
|
|
|
pi.setPromotionDiscountInt(productPriceBO.getPromotionDiscountInt());
|
|
|
pi.setPromotionDiscount(productPriceBO.getPromotionDiscount());
|
|
|
|
|
|
productIndexBO.setIsDiscount(productPriceBO.getIsDiscount());
|
|
|
productIndexBO.setSpecialoffer(productPriceBO.getSpecialoffer());
|
|
|
productIndexBO.setPromotionDiscountInt(productPriceBO.getPromotionDiscountInt());
|
|
|
productIndexBO.setPromotionDiscount(productPriceBO.getPromotionDiscount());
|
|
|
// 学生价相关字段
|
|
|
pi.setIsStudentPrice(productPriceBO.getIsStudentPrice());
|
|
|
pi.setIsStudentRebate(productPriceBO.getIsstudentrebate());
|
|
|
|
|
|
productIndexBO.setIsStudentPrice(productPriceBO.getIsStudentPrice());
|
|
|
productIndexBO.setIsStudentRebate(productPriceBO.getIsstudentrebate());
|
|
|
// 设置vipLevels
|
|
|
pi.setVipLevels(productPriceBO.getVipLevels());
|
|
|
|
|
|
productIndexBO.setVipLevels(productPriceBO.getVipLevels());
|
|
|
// 设置价格更新时间
|
|
|
pi.setPriceUpdateTime(productPriceBO.getUpdateTime());
|
|
|
|
|
|
productIndexBO.setPriceUpdateTime(productPriceBO.getUpdateTime());
|
|
|
// 设置最后一次降价时间
|
|
|
pi.setLastReducePriceTime(productPriceBO.getLastReducePriceTime());
|
|
|
|
|
|
productIndexBO.setLastReducePriceTime(productPriceBO.getLastReducePriceTime());
|
|
|
// 计算isNew(规则:首次上架7天内展示且折扣率大于等于88折)
|
|
|
pi.setIsnew("N");
|
|
|
if (this.isNew(pi.getFirstShelveTime(), pi.getMarketPrice(), pi.getSalesPrice())) {
|
|
|
pi.setIsnew("Y");
|
|
|
productIndexBO.setIsnew("N");
|
|
|
if (this.isNew(productIndexBO.getFirstShelveTime(), productIndexBO.getMarketPrice(), productIndexBO.getSalesPrice())) {
|
|
|
productIndexBO.setIsnew("Y");
|
|
|
}
|
|
|
// 计算specialsearchfieldPrice
|
|
|
pi.setSpecialSearchFieldPrice(specialSearchFieldLogicService.getSpecialSearchFieldPrice(productPriceBO));
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
|
// 计算specialSearchFieldPrice
|
|
|
productIndexBO.setSpecialSearchFieldPrice(specialSearchFieldLogicService.getSpecialSearchFieldPrice(productPriceBO));
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* isNew 标签逻辑
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean isNew(Integer firstShelveTime, BigDecimal marketPrice, BigDecimal salesPrice) {
|
|
|
try {
|
...
|
...
|
@@ -132,46 +146,6 @@ public class ProductPriceBuilder implements ViewBuilder { |
|
|
}
|
|
|
}
|
|
|
|
|
|
public void fillProductPriceBO(ProductPriceBO productPriceBO) {
|
|
|
// 1、计算并重设vip相关价格
|
|
|
vipPriceLogicServie.fillProductPriceVipPrice(productPriceBO);
|
|
|
|
|
|
// 2、计算折扣相关
|
|
|
double salesPrice = this.getDoubleFromBigDecimal(productPriceBO.getSalesPrice());
|
|
|
double marketPrice = this.getDoubleFromBigDecimal(productPriceBO.getMarketPrice());
|
|
|
productPriceBO.setIsDiscount(salesPrice < marketPrice ? "Y" : "N");
|
|
|
double specialOffer = this.divide(1, productPriceBO.getSalesPrice(), productPriceBO.getMarketPrice());
|
|
|
if (specialOffer <= 0.5) {
|
|
|
productPriceBO.setSpecialoffer("Y");
|
|
|
} else {
|
|
|
productPriceBO.setSpecialoffer("N");
|
|
|
}
|
|
|
double promotionDiscountInt = this.divide(1, productPriceBO.getSalesPrice().multiply(new BigDecimal(10)), productPriceBO.getMarketPrice());
|
|
|
productPriceBO.setPromotionDiscountInt((long) promotionDiscountInt);
|
|
|
double promotionDiscount = this.divide(3, productPriceBO.getSalesPrice(), productPriceBO.getMarketPrice());
|
|
|
productPriceBO.setPromotionDiscount(this.getBigDecimalFromDouble(promotionDiscount));
|
|
|
|
|
|
// 3、设置学生价相关属性
|
|
|
productPriceBO.setIsStudentPrice(productPriceBO.getStudentPrice() != null ? "Y" : "N");
|
|
|
productPriceBO.setIsstudentrebate(productPriceBO.getStudentCoinRate() == null || productPriceBO.getStudentCoinRate().compareTo(BigDecimal.ZERO) == 0 ? "N" : "Y");
|
|
|
|
|
|
// 4、设置vipLevels
|
|
|
double vip1Price = productPriceBO.getVip1Price().doubleValue();
|
|
|
double vip2Price = productPriceBO.getVip2Price().doubleValue();
|
|
|
double vip3Price = productPriceBO.getVip3Price().doubleValue();
|
|
|
List<String> vipLevels = new ArrayList<String>();
|
|
|
if (vip1Price > 0 && vip1Price < salesPrice) {
|
|
|
vipLevels.add("1");
|
|
|
}
|
|
|
if (vip2Price > 0 && vip2Price < salesPrice) {
|
|
|
vipLevels.add("2");
|
|
|
}
|
|
|
if (vip3Price > 0 && vip3Price < salesPrice) {
|
|
|
vipLevels.add("3");
|
|
|
}
|
|
|
productPriceBO.setVipLevels(StringUtils.join(vipLevels, ","));
|
|
|
}
|
|
|
|
|
|
private double divide(int scale, BigDecimal a, BigDecimal b) {
|
|
|
if (b == null || b.compareTo(BigDecimal.ZERO) == 0) {
|
|
|
return 0;
|
...
|
...
|
@@ -199,5 +173,4 @@ public class ProductPriceBuilder implements ViewBuilder { |
|
|
}
|
|
|
return new BigDecimal(d);
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|