Authored by WN\wangnan

currentvipdiscounttype需求

... ... @@ -16,7 +16,7 @@ public interface ProductPriceMapper {
List<ProductPrice> selectPageLists(@Param(value="offset")Integer offset, @Param(value="pageSize")Integer pageSize);
List<ProductPrice> selectBySkns(List<Integer> skns);
List<ProductPrice> selectBySknList(List<Integer> sknList);
int selectCount();
}
\ No newline at end of file
... ...
... ... @@ -9,6 +9,7 @@
<result column="current_price" property="currentPrice" jdbcType="DECIMAL" />
<result column="purchase_price" property="purchasePrice" jdbcType="DECIMAL" />
<result column="vip_discount_type" property="vipDiscountType" jdbcType="INTEGER" />
<result column="current_vip_discount_type" property="currentVipDiscountType" jdbcType="INTEGER" />
<result column="vip_discount" property="vipDiscount" jdbcType="DECIMAL" />
<result column="vip_price" property="vipPrice" jdbcType="DECIMAL" />
<result column="vip1_price" property="vip1Price" jdbcType="DECIMAL" />
... ... @@ -25,7 +26,7 @@
<sql id="Base_Column_List">
product_skn,retail_price, sales_price,current_price, purchase_price,
vip_discount_type, vip_discount, vip_price,
vip_discount_type, current_vip_discount_type, vip_discount, vip_price,
vip1_price, vip2_price,
vip3_price,student_price,
update_time,student_coin_rate,lastReducePriceTime,
... ... @@ -51,13 +52,13 @@
timeout="20000">
insert ignore into product_price (
retail_price, sales_price,current_price, purchase_price,
vip_discount_type, vip_discount, vip_price,
vip_discount_type, current_vip_discount_type, vip_discount, vip_price,
vip1_price, vip2_price, vip3_price,student_price,
product_skn, update_time, student_coin_rate,lastReducePriceTime,
product_vip_status,basic_price,purchase_discount
)values (
#{retailPrice,jdbcType=DECIMAL},#{salesPrice,jdbcType=DECIMAL},#{currentPrice,jdbcType=DECIMAL},
#{purchasePrice,jdbcType=DECIMAL},#{vipDiscountType,jdbcType=INTEGER},
#{purchasePrice,jdbcType=DECIMAL},#{vipDiscountType,jdbcType=INTEGER},#{currentVipDiscountType,jdbcType=INTEGER},
#{vipDiscount,jdbcType=DECIMAL}, #{vipPrice,jdbcType=DECIMAL},
#{vip1Price,jdbcType=DECIMAL},
#{vip2Price,jdbcType=DECIMAL},#{vip3Price,jdbcType=DECIMAL},#{studentPrice,jdbcType=DECIMAL},
... ... @@ -86,6 +87,9 @@
<if test="vipDiscountType != null">
vip_discount_type = #{vipDiscountType,jdbcType=INTEGER},
</if>
<if test="currentVipDiscountType != null">
current_vip_discount_type = #{currentVipDiscountType,jdbcType=INTEGER},
</if>
<if test="vipDiscount != null">
vip_discount = #{vipDiscount,jdbcType=DECIMAL},
</if>
... ... @@ -136,7 +140,7 @@
from product_price limit #{offset},#{pageSize}
</select>
<select id="selectBySkns" resultMap="BaseResultMap">
<select id="selectBySknList" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from product_price where product_skn in
... ...
... ... @@ -10,7 +10,7 @@ import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.consumer.index.common.IYohoIndexService;
import com.yoho.search.consumer.index.increment.AbstractIndexMqListener;
import com.yoho.search.consumer.service.base.ProductPricePlanService;
import com.yoho.search.consumer.service.logic.VipPriceLogicServie;
import com.yoho.search.consumer.service.logic.VipPriceLogicService;
import com.yoho.search.dal.model.ProductPricePlan;
@Component
... ... @@ -21,7 +21,7 @@ public class ProductPricePlanMqListener extends AbstractIndexMqListener {
@Autowired
private IYohoIndexService indexService;
@Autowired
private VipPriceLogicServie vipPriceLogicServie;
private VipPriceLogicService vipPriceLogicService;
@Override
public String getIndexName() {
... ... @@ -47,7 +47,7 @@ public class ProductPricePlanMqListener extends AbstractIndexMqListener {
}
productPricePlanService.saveOrUpdate(productPricePlan);
// 填充价格相关参数
vipPriceLogicServie.fillProductPricePlanVipPrice(productPricePlan);
vipPriceLogicService.fillProductPricePlanVipPrice(productPricePlan);
String idValue = String.valueOf(productPricePlan.getId());
indexService.updateIndexData(this.getIndexName(), idValue, (JSONObject) JSONObject.toJSON(productPricePlan));
}
... ...
... ... @@ -92,7 +92,7 @@ public class ProductPriceMqListener extends AbstractIndexMqListener {
// 构造ProductPriceBO
ProductPriceBO productPriceBO = new ProductPriceBO(pp);
productPriceBuilder.fillProductPriceBO(productPriceBO);
productPriceBuilder.buildProductPriceBO(productPriceBO);
indexData.put("productId", productId);
indexData.put("marketPrice", this.getDoubleFromBigDecimal(productPriceBO.getMarketPrice()));
indexData.put("salesPrice", this.getDoubleFromBigDecimal(productPriceBO.getSalesPrice()));
... ...
... ... @@ -5,7 +5,7 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.yoho.search.consumer.service.logic.VipPriceLogicServie;
import com.yoho.search.consumer.service.logic.VipPriceLogicService;
import com.yoho.search.dal.ProductPricePlanMapper;
import com.yoho.search.dal.model.ProductPricePlan;
... ... @@ -15,7 +15,7 @@ public class ProductPricePlanService {
@Autowired
private ProductPricePlanMapper productPricePlanMapper;
@Autowired
private VipPriceLogicServie vipPriceLogicServie;
private VipPriceLogicService vipPriceLogicService;
public int count() {
return productPricePlanMapper.selectCount();
... ... @@ -24,7 +24,7 @@ public class ProductPricePlanService {
public List<ProductPricePlan> getPageLists(int offset, int limit) {
List<ProductPricePlan> list = productPricePlanMapper.selectPageLists(offset,limit);
for(ProductPricePlan p:list){
vipPriceLogicServie.fillProductPricePlanVipPrice(p);
vipPriceLogicService.fillProductPricePlanVipPrice(p);
}
return list;
}
... ...
... ... @@ -6,220 +6,229 @@ import java.math.BigDecimal;
public class ProductPriceBO {
private BigDecimal marketPrice;
private BigDecimal salesPrice;
private BigDecimal vipPrice;
private Integer vipDiscountType;
private BigDecimal vip1Price;
private BigDecimal vip2Price;
private BigDecimal vip3Price;
private BigDecimal studentPrice;
private BigDecimal studentCoinRate;
private Integer updateTime;
private Integer lastReducePriceTime;
private BigDecimal basicPrice;
private Integer productVipStatus;
private BigDecimal vipDiscount;
// others
private String isDiscount;
private String specialoffer;
private Long promotionDiscountInt;
private BigDecimal promotionDiscount;
private String isStudentPrice;
private String isstudentrebate;
private String vipLevels;
public ProductPriceBO(ProductPrice productPrice) {
super();
this.marketPrice = productPrice.getRetailPrice();// 吊牌价
BigDecimal currentPrice = productPrice.getCurrentPrice();// 综合了变价计划和商品售价的结果,生成的逻辑完全由后台控制。
if (currentPrice != null && currentPrice.compareTo(BigDecimal.ZERO) == 1) {
this.salesPrice = currentPrice;
} else {
this.salesPrice = productPrice.getSalesPrice();// 兜底方案
}
this.vipPrice = productPrice.getVipPrice();
this.vipDiscountType = productPrice.getVipDiscountType();
this.vip1Price = productPrice.getVip1Price();
this.vip2Price = productPrice.getVip2Price();
this.vip3Price = productPrice.getVip3Price();
this.studentPrice = productPrice.getStudentPrice();
this.studentCoinRate = productPrice.getStudentCoinRate();
this.updateTime = productPrice.getUpdateTime();
this.lastReducePriceTime = productPrice.getLastReducePriceTime();
this.basicPrice = productPrice.getBasicPrice();
this.productVipStatus = productPrice.getProductVipStatus();
this.vipDiscount = productPrice.getVipDiscount();
}
public BigDecimal getVipDiscount() {
return vipDiscount;
}
public void setVipDiscount(BigDecimal vipDiscount) {
this.vipDiscount = vipDiscount;
}
public Integer getLastReducePriceTime() {
return lastReducePriceTime;
}
public void setLastReducePriceTime(Integer lastReducePriceTime) {
this.lastReducePriceTime = lastReducePriceTime;
}
public BigDecimal getMarketPrice() {
return marketPrice;
}
public void setMarketPrice(BigDecimal marketPrice) {
this.marketPrice = marketPrice;
}
public BigDecimal getSalesPrice() {
return salesPrice;
}
public void setSalesPrice(BigDecimal salesPrice) {
this.salesPrice = salesPrice;
}
public BigDecimal getVipPrice() {
return vipPrice;
}
public void setVipPrice(BigDecimal vipPrice) {
this.vipPrice = vipPrice;
}
public Integer getVipDiscountType() {
return vipDiscountType;
}
public void setVipDiscountType(Integer vipDiscountType) {
this.vipDiscountType = vipDiscountType;
}
public BigDecimal getVip1Price() {
return vip1Price;
}
public void setVip1Price(BigDecimal vip1Price) {
this.vip1Price = vip1Price;
}
public BigDecimal getVip2Price() {
return vip2Price;
}
public void setVip2Price(BigDecimal vip2Price) {
this.vip2Price = vip2Price;
}
public BigDecimal getVip3Price() {
return vip3Price;
}
public void setVip3Price(BigDecimal vip3Price) {
this.vip3Price = vip3Price;
}
public BigDecimal getStudentPrice() {
return studentPrice;
}
public void setStudentPrice(BigDecimal studentPrice) {
this.studentPrice = studentPrice;
}
public BigDecimal getStudentCoinRate() {
return studentCoinRate;
}
public void setStudentCoinRate(BigDecimal studentCoinRate) {
this.studentCoinRate = studentCoinRate;
}
public String getIsDiscount() {
return isDiscount;
}
public void setIsDiscount(String isDiscount) {
this.isDiscount = isDiscount;
}
public String getSpecialoffer() {
return specialoffer;
}
private BigDecimal marketPrice;
private BigDecimal salesPrice;
private BigDecimal vipPrice;
private Integer vipDiscountType;
private BigDecimal vip1Price;
private BigDecimal vip2Price;
private BigDecimal vip3Price;
private BigDecimal studentPrice;
private BigDecimal studentCoinRate;
private Integer updateTime;
private Integer lastReducePriceTime;
private BigDecimal basicPrice;
private Integer productVipStatus;
private BigDecimal vipDiscount;
private Integer currentVipDiscountType;
// others
private String isDiscount;
private String specialoffer;
private Long promotionDiscountInt;
private BigDecimal promotionDiscount;
private String isStudentPrice;
private String isstudentrebate;
private String vipLevels;
public ProductPriceBO(ProductPrice productPrice) {
super();
this.marketPrice = productPrice.getRetailPrice();// 吊牌价
BigDecimal currentPrice = productPrice.getCurrentPrice();// 综合了变价计划和商品售价的结果,生成的逻辑完全由后台控制。
if (currentPrice != null && currentPrice.compareTo(BigDecimal.ZERO) == 1) {
this.salesPrice = currentPrice;
} else {
this.salesPrice = productPrice.getSalesPrice();// 兜底方案
}
this.vipPrice = productPrice.getVipPrice();
this.vipDiscountType = productPrice.getVipDiscountType();
this.vip1Price = productPrice.getVip1Price();
this.vip2Price = productPrice.getVip2Price();
this.vip3Price = productPrice.getVip3Price();
this.studentPrice = productPrice.getStudentPrice();
this.studentCoinRate = productPrice.getStudentCoinRate();
this.updateTime = productPrice.getUpdateTime();
this.lastReducePriceTime = productPrice.getLastReducePriceTime();
this.basicPrice = productPrice.getBasicPrice();
this.productVipStatus = productPrice.getProductVipStatus();
this.vipDiscount = productPrice.getVipDiscount();
this.currentVipDiscountType = productPrice.getCurrentVipDiscountType();
}
public Integer getCurrentVipDiscountType() {
return currentVipDiscountType;
}
public void setCurrentVipDiscountType(Integer currentVipDiscountType) {
this.currentVipDiscountType = currentVipDiscountType;
}
public BigDecimal getVipDiscount() {
return vipDiscount;
}
public void setVipDiscount(BigDecimal vipDiscount) {
this.vipDiscount = vipDiscount;
}
public Integer getLastReducePriceTime() {
return lastReducePriceTime;
}
public void setLastReducePriceTime(Integer lastReducePriceTime) {
this.lastReducePriceTime = lastReducePriceTime;
}
public BigDecimal getMarketPrice() {
return marketPrice;
}
public void setMarketPrice(BigDecimal marketPrice) {
this.marketPrice = marketPrice;
}
public BigDecimal getSalesPrice() {
return salesPrice;
}
public void setSalesPrice(BigDecimal salesPrice) {
this.salesPrice = salesPrice;
}
public BigDecimal getVipPrice() {
return vipPrice;
}
public void setVipPrice(BigDecimal vipPrice) {
this.vipPrice = vipPrice;
}
public Integer getVipDiscountType() {
return vipDiscountType;
}
public void setVipDiscountType(Integer vipDiscountType) {
this.vipDiscountType = vipDiscountType;
}
public BigDecimal getVip1Price() {
return vip1Price;
}
public void setVip1Price(BigDecimal vip1Price) {
this.vip1Price = vip1Price;
}
public BigDecimal getVip2Price() {
return vip2Price;
}
public void setVip2Price(BigDecimal vip2Price) {
this.vip2Price = vip2Price;
}
public BigDecimal getVip3Price() {
return vip3Price;
}
public void setVip3Price(BigDecimal vip3Price) {
this.vip3Price = vip3Price;
}
public BigDecimal getStudentPrice() {
return studentPrice;
}
public void setStudentPrice(BigDecimal studentPrice) {
this.studentPrice = studentPrice;
}
public BigDecimal getStudentCoinRate() {
return studentCoinRate;
}
public void setStudentCoinRate(BigDecimal studentCoinRate) {
this.studentCoinRate = studentCoinRate;
}
public String getIsDiscount() {
return isDiscount;
}
public void setSpecialoffer(String specialoffer) {
this.specialoffer = specialoffer;
}
public void setIsDiscount(String isDiscount) {
this.isDiscount = isDiscount;
}
public Long getPromotionDiscountInt() {
return promotionDiscountInt;
}
public String getSpecialoffer() {
return specialoffer;
}
public void setPromotionDiscountInt(Long promotionDiscountInt) {
this.promotionDiscountInt = promotionDiscountInt;
}
public void setSpecialoffer(String specialoffer) {
this.specialoffer = specialoffer;
}
public BigDecimal getPromotionDiscount() {
return promotionDiscount;
}
public Long getPromotionDiscountInt() {
return promotionDiscountInt;
}
public void setPromotionDiscount(BigDecimal promotionDiscount) {
this.promotionDiscount = promotionDiscount;
}
public void setPromotionDiscountInt(Long promotionDiscountInt) {
this.promotionDiscountInt = promotionDiscountInt;
}
public String getIsStudentPrice() {
return isStudentPrice;
}
public BigDecimal getPromotionDiscount() {
return promotionDiscount;
}
public void setIsStudentPrice(String isStudentPrice) {
this.isStudentPrice = isStudentPrice;
}
public void setPromotionDiscount(BigDecimal promotionDiscount) {
this.promotionDiscount = promotionDiscount;
}
public String getIsstudentrebate() {
return isstudentrebate;
}
public String getIsStudentPrice() {
return isStudentPrice;
}
public void setIsstudentrebate(String isstudentrebate) {
this.isstudentrebate = isstudentrebate;
}
public void setIsStudentPrice(String isStudentPrice) {
this.isStudentPrice = isStudentPrice;
}
public String getVipLevels() {
return vipLevels;
}
public String getIsstudentrebate() {
return isstudentrebate;
}
public void setVipLevels(String vipLevels) {
this.vipLevels = vipLevels;
}
public void setIsstudentrebate(String isstudentrebate) {
this.isstudentrebate = isstudentrebate;
}
public Integer getUpdateTime() {
return updateTime;
}
public String getVipLevels() {
return vipLevels;
}
public void setUpdateTime(Integer updateTime) {
this.updateTime = updateTime;
}
public void setVipLevels(String vipLevels) {
this.vipLevels = vipLevels;
}
public BigDecimal getBasicPrice() {
return basicPrice;
}
public Integer getUpdateTime() {
return updateTime;
}
public void setBasicPrice(BigDecimal basicPrice) {
this.basicPrice = basicPrice;
}
public void setUpdateTime(Integer updateTime) {
this.updateTime = updateTime;
}
public Integer getProductVipStatus() {
return productVipStatus;
}
public BigDecimal getBasicPrice() {
return basicPrice;
}
public void setProductVipStatus(Integer productVipStatus) {
this.productVipStatus = productVipStatus;
}
public void setBasicPrice(BigDecimal basicPrice) {
this.basicPrice = basicPrice;
}
public Integer getProductVipStatus() {
return productVipStatus;
}
public void setProductVipStatus(Integer productVipStatus) {
this.productVipStatus = productVipStatus;
}
}
... ...
package com.yoho.search.consumer.service.logic;
import java.math.BigDecimal;
import org.springframework.stereotype.Service;
import com.yoho.search.consumer.service.bo.ProductPriceBO;
import com.yoho.search.consumer.service.bo.VipPriceBO;
import com.yoho.search.dal.model.ProductPricePlan;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@Service
public class VipPriceLogicServie {
public class VipPriceLogicService {
/**
* 设置计划的vip价格
* @param productPricePlan
* @return
*/
public void fillProductPricePlanVipPrice(ProductPricePlan p){
VipPriceBO vipPriceBO = this.getVipPriceBO(p.getCurrentSaleprice(), p.getVipPrice(), p.getVip1Price(), p.getVip2Price(), p.getVip3Price(), p.getVipDiscountType(), p.getVipDiscount());
... ... @@ -25,45 +25,65 @@ public class VipPriceLogicServie {
/**
* 计算并重设vip相关价格
* @param productPriceBO
*/
public void fillProductPriceVipPrice(ProductPriceBO productPriceBO){
VipPriceBO vipPriceBO = this.getVipPriceBO(productPriceBO.getSalesPrice(), productPriceBO.getVipPrice(), productPriceBO.getVip1Price(), productPriceBO.getVip2Price(), productPriceBO.getVip3Price(), productPriceBO.getVipDiscountType(),
VipPriceBO vipPriceBO = this.getVipPriceBO(productPriceBO.getSalesPrice(), productPriceBO.getVipPrice(), productPriceBO.getVip1Price(), productPriceBO.getVip2Price(), productPriceBO.getVip3Price(), productPriceBO.getCurrentVipDiscountType(),
productPriceBO.getVipDiscount());
productPriceBO.setVip1Price(vipPriceBO.getVip1PriceBigDecimal());
productPriceBO.setVip2Price(vipPriceBO.getVip2PriceBigDecimal());
productPriceBO.setVip3Price(vipPriceBO.getVip3PriceBigDecimal());
//设置vipLevels
double salesPrice = this.getDoubleFromBigDecimal(productPriceBO.getSalesPrice());
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, ","));
}
/**
* salesPrice等于productPrice表的currentPrice,type是productPrice表的currentVipDiscountType
*/
private VipPriceBO getVipPriceBO(BigDecimal salesPrice, BigDecimal vipPrice, BigDecimal vip1Price, BigDecimal vip2Price, BigDecimal vip3Price, Integer type,
BigDecimal vipDiscount) {
double salesPricedoubleCeil = Math.ceil(this.getDoubleFromBigDecimal(salesPrice));
double salesPriceDoubleCeil = Math.ceil(this.getDoubleFromBigDecimal(salesPrice));
// 默认取销售价
double vip1PriceResult = salesPricedoubleCeil;
double vip2PriceResult = salesPricedoubleCeil;
double vip3PriceResult = salesPricedoubleCeil;
double vip1PriceResult = salesPriceDoubleCeil;
double vip2PriceResult = salesPriceDoubleCeil;
double vip3PriceResult = salesPriceDoubleCeil;
if (type == null) {
return new VipPriceBO(vip1PriceResult, vip2PriceResult, vip3PriceResult);
}
switch (type) {
case 1:
vip1PriceResult = Double.valueOf(String.format("%.2f", salesPricedoubleCeil * 0.95));
vip2PriceResult = Double.valueOf(String.format("%.2f", salesPricedoubleCeil * 0.9));
vip3PriceResult = Double.valueOf(String.format("%.2f", salesPricedoubleCeil * 0.88));
vip1PriceResult = Double.valueOf(String.format("%.2f", salesPriceDoubleCeil * 0.95));
vip2PriceResult = Double.valueOf(String.format("%.2f", salesPriceDoubleCeil * 0.9));
vip3PriceResult = Double.valueOf(String.format("%.2f", salesPriceDoubleCeil * 0.88));
break;
case 2:
if (!isVipDiscountLegal(vipDiscount)) {
vipDiscount = new BigDecimal(0.95);
}
BigDecimal price = vipDiscount.multiply(BigDecimal.valueOf(salesPricedoubleCeil));
BigDecimal price = vipDiscount.multiply(BigDecimal.valueOf(salesPriceDoubleCeil));
vip1PriceResult = Double.valueOf(String.format("%.2f", price));
vip2PriceResult = Double.valueOf(String.format("%.2f", price));
vip3PriceResult = Double.valueOf(String.format("%.2f", price));
break;
case 3:
vip1PriceResult = Double.valueOf(String.format("%.2f", salesPricedoubleCeil));
vip2PriceResult = Double.valueOf(String.format("%.2f", salesPricedoubleCeil));
vip3PriceResult = Double.valueOf(String.format("%.2f", salesPricedoubleCeil));
vip1PriceResult = Double.valueOf(String.format("%.2f", salesPriceDoubleCeil));
vip2PriceResult = Double.valueOf(String.format("%.2f", salesPriceDoubleCeil));
vip3PriceResult = Double.valueOf(String.format("%.2f", salesPriceDoubleCeil));
break;
case 4:
vip1PriceResult = this.getDoubleFromBigDecimal(vipPrice);
... ...
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.
... ... @@ -30,174 +27,150 @@ import com.yoho.search.dal.model.ProductPrice;
@Component
public class ProductPriceBuilder implements ViewBuilder {
private final Logger logger = LoggerFactory.getLogger(ProductPriceBuilder.class);
@Autowired
private ProductPriceMapper productPriceMapper;
@Autowired
private VipPriceLogicServie vipPriceLogicServie;
@Autowired
private SpecialDealLogicService specialSearchFieldLogicService;
@Override
public void build(List<ProductIndexBO> productIndexBOs, List<Integer> ids, List<Integer> sknList) {
List<ProductPrice> productPrices = productPriceMapper.selectBySkns(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");
try {
// 如果价格不存在,则直接返回
ProductPrice productPrice = productPricesMap.get(pi.getProductSkn());
if (productPrice == null) {
continue;
}
// 构造ProductPriceBO
ProductPriceBO productPriceBO = new ProductPriceBO(productPrice);
this.fillProductPriceBO(productPriceBO);
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());
// 折扣相关
pi.setIsDiscount(productPriceBO.getIsDiscount());
pi.setSpecialoffer(productPriceBO.getSpecialoffer());
pi.setPromotionDiscountInt(productPriceBO.getPromotionDiscountInt());
pi.setPromotionDiscount(productPriceBO.getPromotionDiscount());
// 学生价相关字段
pi.setIsStudentPrice(productPriceBO.getIsStudentPrice());
pi.setIsStudentRebate(productPriceBO.getIsstudentrebate());
// 设置vipLevels
pi.setVipLevels(productPriceBO.getVipLevels());
// 设置价格更新时间
pi.setPriceUpdateTime(productPriceBO.getUpdateTime());
// 设置最后一次降价时间
pi.setLastReducePriceTime(productPriceBO.getLastReducePriceTime());
// 计算isNew(规则:首次上架7天内展示且折扣率大于等于88折)
pi.setIsnew("N");
if (this.isNew(pi.getFirstShelveTime(), pi.getMarketPrice(), pi.getSalesPrice())) {
pi.setIsnew("Y");
}
// 计算specialsearchfieldPrice
pi.setSpecialSearchFieldPrice(specialSearchFieldLogicService.getSpecialSearchFieldPrice(productPriceBO));
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
}
/**
* isNew 标签逻辑
*
* @return
*/
public boolean isNew(Integer firstShelveTime, BigDecimal marketPrice, BigDecimal salesPrice) {
try {
if (firstShelveTime == null || firstShelveTime <= 0 || salesPrice == null || marketPrice == null) {
return false;
}
long dateCount = DateUtil.daysBetween(new Date(firstShelveTime * 1000L), new Date());
if (dateCount > 7) {
return false;
}
// 计算折扣
Double discount = MathUtils.getDevideValue(salesPrice, marketPrice, 2, RoundingMode.DOWN);
if (discount >= 0.88) {
return true;
}
return false;
} catch (Exception e) {
return false;
}
}
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;
}
StringBuilder sb = new StringBuilder("0.0");
for (int i = 1; i < scale; i++) {
sb.append('0');
}
BigDecimal divideResult = a.divide(b, scale, RoundingMode.DOWN);
DecimalFormat decimalFormat = new DecimalFormat(sb.toString());
String result = decimalFormat.format(divideResult);
return Double.parseDouble(result);
}
private double getDoubleFromBigDecimal(BigDecimal bigDecimal) {
if (bigDecimal == null) {
return 0d;
}
return bigDecimal.doubleValue();
}
private BigDecimal getBigDecimalFromDouble(Double d) {
if (d == null) {
return new BigDecimal(0);
}
return new BigDecimal(d);
}
private final Logger logger = LoggerFactory.getLogger(ProductPriceBuilder.class);
@Autowired
private ProductPriceMapper productPriceMapper;
@Autowired
private VipPriceLogicService vipPriceLogicService;
@Autowired
private SpecialDealLogicService specialSearchFieldLogicService;
@Override
public void build(List<ProductIndexBO> productIndexBOs, List<Integer> ids, List<Integer> sknList) {
List<ProductPrice> productPrices = productPriceMapper.selectBySknList(sknList);
Map<Integer, ProductPrice> productPricesMap = productPrices.stream().collect(Collectors.toMap(ProductPrice::getProductSkn, (p) -> p));
for (ProductIndexBO productIndexBO : productIndexBOs) {
try {
// 如果该skn对应价格对象不存在,直接跳过
ProductPrice productPrice = productPricesMap.get(productIndexBO.getProductSkn());
if (productPrice == null) {
continue;
}
// 构造ProductPriceBO
ProductPriceBO productPriceBO = new ProductPriceBO(productPrice);
this.buildProductPriceBO(productPriceBO);
// 构造ProductIndexBO
this.buildProductIndexBO(productIndexBO, productPriceBO);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
}
/**
* 增量+全量都调用
*/
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());
// 折扣相关
productIndexBO.setIsDiscount(productPriceBO.getIsDiscount());
productIndexBO.setSpecialoffer(productPriceBO.getSpecialoffer());
productIndexBO.setPromotionDiscountInt(productPriceBO.getPromotionDiscountInt());
productIndexBO.setPromotionDiscount(productPriceBO.getPromotionDiscount());
// 学生价相关字段
productIndexBO.setIsStudentPrice(productPriceBO.getIsStudentPrice());
productIndexBO.setIsStudentRebate(productPriceBO.getIsstudentrebate());
// 设置vipLevels
productIndexBO.setVipLevels(productPriceBO.getVipLevels());
// 设置价格更新时间
productIndexBO.setPriceUpdateTime(productPriceBO.getUpdateTime());
// 设置最后一次降价时间
productIndexBO.setLastReducePriceTime(productPriceBO.getLastReducePriceTime());
// 计算isNew(规则:首次上架7天内展示且折扣率大于等于88折)
productIndexBO.setIsnew("N");
if (this.isNew(productIndexBO.getFirstShelveTime(), productIndexBO.getMarketPrice(), productIndexBO.getSalesPrice())) {
productIndexBO.setIsnew("Y");
}
// 计算specialSearchFieldPrice
productIndexBO.setSpecialSearchFieldPrice(specialSearchFieldLogicService.getSpecialSearchFieldPrice(productPriceBO));
}
/**
* isNew 标签逻辑
*/
public boolean isNew(Integer firstShelveTime, BigDecimal marketPrice, BigDecimal salesPrice) {
try {
if (firstShelveTime == null || firstShelveTime <= 0 || salesPrice == null || marketPrice == null) {
return false;
}
long dateCount = DateUtil.daysBetween(new Date(firstShelveTime * 1000L), new Date());
if (dateCount > 7) {
return false;
}
// 计算折扣
Double discount = MathUtils.getDevideValue(salesPrice, marketPrice, 2, RoundingMode.DOWN);
if (discount >= 0.88) {
return true;
}
return false;
} catch (Exception e) {
return false;
}
}
private double divide(int scale, BigDecimal a, BigDecimal b) {
if (b == null || b.compareTo(BigDecimal.ZERO) == 0) {
return 0;
}
StringBuilder sb = new StringBuilder("0.0");
for (int i = 1; i < scale; i++) {
sb.append('0');
}
BigDecimal divideResult = a.divide(b, scale, RoundingMode.DOWN);
DecimalFormat decimalFormat = new DecimalFormat(sb.toString());
String result = decimalFormat.format(divideResult);
return Double.parseDouble(result);
}
private double getDoubleFromBigDecimal(BigDecimal bigDecimal) {
if (bigDecimal == null) {
return 0d;
}
return bigDecimal.doubleValue();
}
private BigDecimal getBigDecimalFromDouble(Double d) {
if (d == null) {
return new BigDecimal(0);
}
return new BigDecimal(d);
}
}
... ...