Authored by unknown

优化vip价格的生成逻辑

package com.yoho.search.consumer.service.base;
import com.yoho.search.base.utils.PriceUtil;
import com.yoho.search.dal.ProductPricePlanMapper;
import com.yoho.search.dal.model.ProductPricePlan;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.List;
import com.yoho.search.consumer.service.logic.VipPriceLogicServie;
import com.yoho.search.dal.ProductPricePlanMapper;
import com.yoho.search.dal.model.ProductPricePlan;
@Component
public class ProductPricePlanService {
@Autowired
private ProductPricePlanMapper productPricePlanMapper;
@Autowired
private VipPriceLogicServie vipPriceLogicServie;
public int count() {
return productPricePlanMapper.selectCount();
}
public static final int POSITION0 = 0;
public static final int POSITION1 = 1;
public static final int POSITION2 = 2;
public List<ProductPricePlan> getPageLists(int offset, int limit) {
List<ProductPricePlan> list = productPricePlanMapper.selectPageLists(offset,limit);
for(ProductPricePlan p:list){
BigDecimal[] vippricearray = PriceUtil.getVipPriceBigDecimalArray(p.getCurrentSaleprice(), p.getVipPrice(), p.getVip1Price(), p.getVip2Price(), p.getVip3Price(), p.getVipDiscountType());
p.setVip1Price(vippricearray[POSITION0]);
p.setVip2Price(vippricearray[POSITION1]);
p.setVip3Price(vippricearray[POSITION2]);
vipPriceLogicServie.fillProductPricePlanVipPrice(p);
}
return list;
}
... ...
package com.yoho.search.consumer.service.bo;
import java.math.BigDecimal;
public class VipPriceBO {
private double vip1Price;
private double vip2Price;
private double vip3Price;
public VipPriceBO(double vip1Price, double vip2Price, double vip3Price) {
super();
this.vip1Price = vip1Price;
this.vip2Price = vip2Price;
this.vip3Price = vip3Price;
}
public double getVip1Price() {
return vip1Price;
}
public double getVip2Price() {
return vip2Price;
}
public double getVip3Price() {
return vip3Price;
}
public BigDecimal getVip1PriceBigDecimal() {
return new BigDecimal(vip1Price);
}
public BigDecimal getVip2PriceBigDecimal() {
return new BigDecimal(vip2Price);
}
public BigDecimal getVip3PriceBigDecimal() {
return new BigDecimal(vip3Price);
}
}
... ...
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;
@Service
public class VipPriceLogicServie {
/**
* 设置计划的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());
p.setVip1Price(vipPriceBO.getVip1PriceBigDecimal());
p.setVip2Price(vipPriceBO.getVip2PriceBigDecimal());
p.setVip3Price(vipPriceBO.getVip3PriceBigDecimal());
}
/**
* 计算并重设vip相关价格
* @param productPriceBO
*/
public void fillProductPriceVipPrice(ProductPriceBO productPriceBO){
VipPriceBO vipPriceBO = this.getVipPriceBO(productPriceBO.getSalesPrice(), productPriceBO.getVipPrice(), productPriceBO.getVip1Price(), productPriceBO.getVip2Price(), productPriceBO.getVip3Price(), productPriceBO.getVipDiscountType(),
productPriceBO.getVipDiscount());
productPriceBO.setVip1Price(vipPriceBO.getVip1PriceBigDecimal());
productPriceBO.setVip2Price(vipPriceBO.getVip2PriceBigDecimal());
productPriceBO.setVip3Price(vipPriceBO.getVip3PriceBigDecimal());
}
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 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));
break;
case 2:
if (!isVipDiscountLegal(vipDiscount)) {
vipDiscount = new BigDecimal(0.95);
}
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));
break;
case 4:
vip1PriceResult = this.getDoubleFromBigDecimal(vipPrice);
vip2PriceResult = vip1PriceResult;
vip3PriceResult = vip1PriceResult;
break;
case 5:
vip1PriceResult = this.getDoubleFromBigDecimal(vip1Price);
vip2PriceResult = this.getDoubleFromBigDecimal(vip2Price);
vip3PriceResult = this.getDoubleFromBigDecimal(vip3Price);
break;
}
return new VipPriceBO(vip1PriceResult, vip2PriceResult, vip3PriceResult);
}
private boolean isVipDiscountLegal(BigDecimal vipDisCount) {
if (vipDisCount == null) {
return false;
}
boolean gt0 = vipDisCount.compareTo(BigDecimal.ZERO) == 1;
boolean lt1 = vipDisCount.compareTo(BigDecimal.ONE) == -1;
if (gt0 && lt1) {
return true;
}
return false;
}
private double getDoubleFromBigDecimal(BigDecimal bigDecimal) {
if (bigDecimal == null) {
return 0d;
}
return bigDecimal.doubleValue();
}
}
... ...
package com.yoho.search.consumer.service.logic.productIndex.viewBuilder;
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.dal.ProductPriceMapper;
import com.yoho.search.dal.model.ProductPrice;
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 java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
... ... @@ -21,6 +9,20 @@ 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.VipPriceLogicServie;
import com.yoho.search.dal.ProductPriceMapper;
import com.yoho.search.dal.model.ProductPrice;
/**
* Created by wangnan on 2017/1/6.
*/
... ... @@ -31,6 +33,8 @@ public class ProductPriceBuilder implements ViewBuilder {
@Autowired
private ProductPriceMapper productPriceMapper;
@Autowired
private VipPriceLogicServie vipPriceLogicServie;
@Override
public void build(List<ProductIndexBO> productIndexBOs, List<Integer> ids, List<Integer> sknList) {
... ... @@ -42,7 +46,7 @@ public class ProductPriceBuilder implements ViewBuilder {
pi.setSpecialoffer("N");
pi.setPromotionDiscount(new BigDecimal(1));
pi.setPromotionDiscountInt(10L);
pi.setIsStudentPrice("N");
pi.setIsStudentRebate("N");
pi.setIsnew("N");
... ... @@ -92,7 +96,7 @@ public class ProductPriceBuilder implements ViewBuilder {
pi.setIsnew("Y");
}
} catch (Exception e) {
logger.error(e.getMessage(),e);
logger.error(e.getMessage(), e);
}
}
... ... @@ -123,84 +127,13 @@ public class ProductPriceBuilder implements ViewBuilder {
}
}
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 boolean vipPriceLimit = false;// 是否执行vip价格限制
public void fillProductPriceBO(ProductPriceBO productPriceBO) {
double marketPrice = this.getDoubleFromBigDecimal(productPriceBO.getMarketPrice());
// 1、计算并重设vip相关价格
vipPriceLogicServie.fillProductPriceVipPrice(productPriceBO);
// 2、计算折扣相关
double salesPrice = this.getDoubleFromBigDecimal(productPriceBO.getSalesPrice());
double vipPrice = vipPriceLimit ? salesPrice : this.getDoubleFromBigDecimal(productPriceBO.getVipPrice());
double vip1Price = salesPrice;
double vip2Price = salesPrice;
double vip3Price = salesPrice;
// 判断是否需要计算vip价格
boolean needCalVipPrice = productPriceBO.getVipDiscountType() != null;
// 如果开启了vip限制,
if (vipPriceLimit) {
needCalVipPrice = needCalVipPrice && (salesPrice == marketPrice) && (productPriceBO.getProductVipStatus() == 1);
}
if (needCalVipPrice) {
switch (productPriceBO.getVipDiscountType()) {
case 1:
vip1Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice) * 0.95));
vip2Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice) * 0.9));
vip3Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice) * 0.88));
break;
case 2:
if(productPriceBO.getVipDiscount()!=null){
boolean gt0 = productPriceBO.getVipDiscount().compareTo(BigDecimal.ZERO)==1;
boolean lt1 = productPriceBO.getVipDiscount().compareTo(BigDecimal.ONE)==-1;
if(gt0&&lt1){
BigDecimal price = productPriceBO.getVipDiscount().multiply(BigDecimal.valueOf(Math.ceil(salesPrice)));
vip1Price = Double.parseDouble(String.format("%.2f", price));
vip2Price = Double.parseDouble(String.format("%.2f", price));
vip3Price = Double.parseDouble(String.format("%.2f", price));
}
}
else{
vip1Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice) * 0.95));
vip2Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice) * 0.95));
vip3Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice) * 0.95));
}
break;
case 3:
vip1Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice)));
vip2Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice)));
vip3Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice)));
break;
case 4:
vip1Price = vipPrice;
vip2Price = vipPrice;
vip3Price = vipPrice;
break;
case 5:
vip1Price = this.getDoubleFromBigDecimal(productPriceBO.getVip1Price());
vip2Price = this.getDoubleFromBigDecimal(productPriceBO.getVip2Price());
vip3Price = this.getDoubleFromBigDecimal(productPriceBO.getVip3Price());
break;
}
}
productPriceBO.setSalesPrice(this.getBigDecimalFromDouble(salesPrice));
productPriceBO.setVipPrice(this.getBigDecimalFromDouble(vipPrice));
productPriceBO.setVip1Price(this.getBigDecimalFromDouble(vip1Price));
productPriceBO.setVip2Price(this.getBigDecimalFromDouble(vip2Price));
productPriceBO.setVip3Price(this.getBigDecimalFromDouble(vip3Price));
// 折扣相关
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) {
... ... @@ -213,11 +146,14 @@ public class ProductPriceBuilder implements ViewBuilder {
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");
// 设置vipLevels
// 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");
... ... @@ -229,7 +165,6 @@ public class ProductPriceBuilder implements ViewBuilder {
vipLevels.add("3");
}
productPriceBO.setVipLevels(StringUtils.join(vipLevels, ","));
}
private double divide(int scale, BigDecimal a, BigDecimal b) {
... ... @@ -246,4 +181,18 @@ public class ProductPriceBuilder implements ViewBuilder {
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);
}
}
... ...