Authored by unknown

优化vip价格的生成逻辑

1 package com.yoho.search.consumer.service.base; 1 package com.yoho.search.consumer.service.base;
2 2
3 -import com.yoho.search.base.utils.PriceUtil;  
4 -import com.yoho.search.dal.ProductPricePlanMapper;  
5 -import com.yoho.search.dal.model.ProductPricePlan; 3 +import java.util.List;
  4 +
6 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.stereotype.Component; 6 import org.springframework.stereotype.Component;
8 7
9 -import java.math.BigDecimal;  
10 -import java.util.List; 8 +import com.yoho.search.consumer.service.logic.VipPriceLogicServie;
  9 +import com.yoho.search.dal.ProductPricePlanMapper;
  10 +import com.yoho.search.dal.model.ProductPricePlan;
11 11
12 @Component 12 @Component
13 public class ProductPricePlanService { 13 public class ProductPricePlanService {
14 14
15 @Autowired 15 @Autowired
16 private ProductPricePlanMapper productPricePlanMapper; 16 private ProductPricePlanMapper productPricePlanMapper;
  17 + @Autowired
  18 + private VipPriceLogicServie vipPriceLogicServie;
17 19
18 public int count() { 20 public int count() {
19 return productPricePlanMapper.selectCount(); 21 return productPricePlanMapper.selectCount();
20 } 22 }
21 23
22 - public static final int POSITION0 = 0;  
23 - public static final int POSITION1 = 1;  
24 - public static final int POSITION2 = 2;  
25 -  
26 public List<ProductPricePlan> getPageLists(int offset, int limit) { 24 public List<ProductPricePlan> getPageLists(int offset, int limit) {
27 List<ProductPricePlan> list = productPricePlanMapper.selectPageLists(offset,limit); 25 List<ProductPricePlan> list = productPricePlanMapper.selectPageLists(offset,limit);
28 for(ProductPricePlan p:list){ 26 for(ProductPricePlan p:list){
29 - BigDecimal[] vippricearray = PriceUtil.getVipPriceBigDecimalArray(p.getCurrentSaleprice(), p.getVipPrice(), p.getVip1Price(), p.getVip2Price(), p.getVip3Price(), p.getVipDiscountType());  
30 - p.setVip1Price(vippricearray[POSITION0]);  
31 - p.setVip2Price(vippricearray[POSITION1]);  
32 - p.setVip3Price(vippricearray[POSITION2]); 27 + vipPriceLogicServie.fillProductPricePlanVipPrice(p);
33 } 28 }
34 return list; 29 return list;
35 } 30 }
  1 +package com.yoho.search.consumer.service.bo;
  2 +
  3 +import java.math.BigDecimal;
  4 +
  5 +public class VipPriceBO {
  6 +
  7 + private double vip1Price;
  8 + private double vip2Price;
  9 + private double vip3Price;
  10 +
  11 + public VipPriceBO(double vip1Price, double vip2Price, double vip3Price) {
  12 + super();
  13 + this.vip1Price = vip1Price;
  14 + this.vip2Price = vip2Price;
  15 + this.vip3Price = vip3Price;
  16 + }
  17 +
  18 + public double getVip1Price() {
  19 + return vip1Price;
  20 + }
  21 +
  22 + public double getVip2Price() {
  23 + return vip2Price;
  24 + }
  25 +
  26 + public double getVip3Price() {
  27 + return vip3Price;
  28 + }
  29 +
  30 + public BigDecimal getVip1PriceBigDecimal() {
  31 + return new BigDecimal(vip1Price);
  32 + }
  33 +
  34 + public BigDecimal getVip2PriceBigDecimal() {
  35 + return new BigDecimal(vip2Price);
  36 + }
  37 +
  38 + public BigDecimal getVip3PriceBigDecimal() {
  39 + return new BigDecimal(vip3Price);
  40 + }
  41 +
  42 +}
  1 +package com.yoho.search.consumer.service.logic;
  2 +
  3 +import java.math.BigDecimal;
  4 +
  5 +import org.springframework.stereotype.Service;
  6 +
  7 +import com.yoho.search.consumer.service.bo.ProductPriceBO;
  8 +import com.yoho.search.consumer.service.bo.VipPriceBO;
  9 +import com.yoho.search.dal.model.ProductPricePlan;
  10 +
  11 +@Service
  12 +public class VipPriceLogicServie {
  13 +
  14 + /**
  15 + * 设置计划的vip价格
  16 + * @param productPricePlan
  17 + * @return
  18 + */
  19 + public void fillProductPricePlanVipPrice(ProductPricePlan p){
  20 + VipPriceBO vipPriceBO = this.getVipPriceBO(p.getCurrentSaleprice(), p.getVipPrice(), p.getVip1Price(), p.getVip2Price(), p.getVip3Price(), p.getVipDiscountType(), p.getVipDiscount());
  21 + p.setVip1Price(vipPriceBO.getVip1PriceBigDecimal());
  22 + p.setVip2Price(vipPriceBO.getVip2PriceBigDecimal());
  23 + p.setVip3Price(vipPriceBO.getVip3PriceBigDecimal());
  24 + }
  25 +
  26 + /**
  27 + * 计算并重设vip相关价格
  28 + * @param productPriceBO
  29 + */
  30 + public void fillProductPriceVipPrice(ProductPriceBO productPriceBO){
  31 + VipPriceBO vipPriceBO = this.getVipPriceBO(productPriceBO.getSalesPrice(), productPriceBO.getVipPrice(), productPriceBO.getVip1Price(), productPriceBO.getVip2Price(), productPriceBO.getVip3Price(), productPriceBO.getVipDiscountType(),
  32 + productPriceBO.getVipDiscount());
  33 + productPriceBO.setVip1Price(vipPriceBO.getVip1PriceBigDecimal());
  34 + productPriceBO.setVip2Price(vipPriceBO.getVip2PriceBigDecimal());
  35 + productPriceBO.setVip3Price(vipPriceBO.getVip3PriceBigDecimal());
  36 + }
  37 +
  38 + private VipPriceBO getVipPriceBO(BigDecimal salesPrice, BigDecimal vipPrice, BigDecimal vip1Price, BigDecimal vip2Price, BigDecimal vip3Price, Integer type,
  39 + BigDecimal vipDiscount) {
  40 + double salesPricedoubleCeil = Math.ceil(this.getDoubleFromBigDecimal(salesPrice));
  41 + // 默认取销售价
  42 + double vip1PriceResult = salesPricedoubleCeil;
  43 + double vip2PriceResult = salesPricedoubleCeil;
  44 + double vip3PriceResult = salesPricedoubleCeil;
  45 + if (type == null) {
  46 + return new VipPriceBO(vip1PriceResult, vip2PriceResult, vip3PriceResult);
  47 + }
  48 + switch (type) {
  49 + case 1:
  50 + vip1PriceResult = Double.valueOf(String.format("%.2f", salesPricedoubleCeil * 0.95));
  51 + vip2PriceResult = Double.valueOf(String.format("%.2f", salesPricedoubleCeil * 0.9));
  52 + vip3PriceResult = Double.valueOf(String.format("%.2f", salesPricedoubleCeil * 0.88));
  53 + break;
  54 + case 2:
  55 + if (!isVipDiscountLegal(vipDiscount)) {
  56 + vipDiscount = new BigDecimal(0.95);
  57 + }
  58 + BigDecimal price = vipDiscount.multiply(BigDecimal.valueOf(salesPricedoubleCeil));
  59 + vip1PriceResult = Double.valueOf(String.format("%.2f", price));
  60 + vip2PriceResult = Double.valueOf(String.format("%.2f", price));
  61 + vip3PriceResult = Double.valueOf(String.format("%.2f", price));
  62 + break;
  63 + case 3:
  64 + vip1PriceResult = Double.valueOf(String.format("%.2f", salesPricedoubleCeil));
  65 + vip2PriceResult = Double.valueOf(String.format("%.2f", salesPricedoubleCeil));
  66 + vip3PriceResult = Double.valueOf(String.format("%.2f", salesPricedoubleCeil));
  67 + break;
  68 + case 4:
  69 + vip1PriceResult = this.getDoubleFromBigDecimal(vipPrice);
  70 + vip2PriceResult = vip1PriceResult;
  71 + vip3PriceResult = vip1PriceResult;
  72 + break;
  73 + case 5:
  74 + vip1PriceResult = this.getDoubleFromBigDecimal(vip1Price);
  75 + vip2PriceResult = this.getDoubleFromBigDecimal(vip2Price);
  76 + vip3PriceResult = this.getDoubleFromBigDecimal(vip3Price);
  77 + break;
  78 + }
  79 + return new VipPriceBO(vip1PriceResult, vip2PriceResult, vip3PriceResult);
  80 + }
  81 +
  82 + private boolean isVipDiscountLegal(BigDecimal vipDisCount) {
  83 + if (vipDisCount == null) {
  84 + return false;
  85 + }
  86 + boolean gt0 = vipDisCount.compareTo(BigDecimal.ZERO) == 1;
  87 + boolean lt1 = vipDisCount.compareTo(BigDecimal.ONE) == -1;
  88 + if (gt0 && lt1) {
  89 + return true;
  90 + }
  91 + return false;
  92 + }
  93 +
  94 + private double getDoubleFromBigDecimal(BigDecimal bigDecimal) {
  95 + if (bigDecimal == null) {
  96 + return 0d;
  97 + }
  98 + return bigDecimal.doubleValue();
  99 + }
  100 +}
1 package com.yoho.search.consumer.service.logic.productIndex.viewBuilder; 1 package com.yoho.search.consumer.service.logic.productIndex.viewBuilder;
2 2
3 -import com.yoho.search.base.utils.DateUtil;  
4 -import com.yoho.search.base.utils.MathUtils;  
5 -import com.yoho.search.consumer.service.bo.ProductIndexBO;  
6 -import com.yoho.search.consumer.service.bo.ProductPriceBO;  
7 -import com.yoho.search.dal.ProductPriceMapper;  
8 -import com.yoho.search.dal.model.ProductPrice;  
9 -import org.apache.commons.lang.StringUtils;  
10 -import org.slf4j.Logger;  
11 -import org.slf4j.LoggerFactory;  
12 -import org.springframework.beans.factory.annotation.Autowired;  
13 -import org.springframework.stereotype.Component;  
14 -  
15 import java.math.BigDecimal; 3 import java.math.BigDecimal;
16 import java.math.RoundingMode; 4 import java.math.RoundingMode;
17 import java.text.DecimalFormat; 5 import java.text.DecimalFormat;
@@ -21,6 +9,20 @@ import java.util.List; @@ -21,6 +9,20 @@ import java.util.List;
21 import java.util.Map; 9 import java.util.Map;
22 import java.util.stream.Collectors; 10 import java.util.stream.Collectors;
23 11
  12 +import org.apache.commons.lang.StringUtils;
  13 +import org.slf4j.Logger;
  14 +import org.slf4j.LoggerFactory;
  15 +import org.springframework.beans.factory.annotation.Autowired;
  16 +import org.springframework.stereotype.Component;
  17 +
  18 +import com.yoho.search.base.utils.DateUtil;
  19 +import com.yoho.search.base.utils.MathUtils;
  20 +import com.yoho.search.consumer.service.bo.ProductIndexBO;
  21 +import com.yoho.search.consumer.service.bo.ProductPriceBO;
  22 +import com.yoho.search.consumer.service.logic.VipPriceLogicServie;
  23 +import com.yoho.search.dal.ProductPriceMapper;
  24 +import com.yoho.search.dal.model.ProductPrice;
  25 +
24 /** 26 /**
25 * Created by wangnan on 2017/1/6. 27 * Created by wangnan on 2017/1/6.
26 */ 28 */
@@ -31,6 +33,8 @@ public class ProductPriceBuilder implements ViewBuilder { @@ -31,6 +33,8 @@ public class ProductPriceBuilder implements ViewBuilder {
31 33
32 @Autowired 34 @Autowired
33 private ProductPriceMapper productPriceMapper; 35 private ProductPriceMapper productPriceMapper;
  36 + @Autowired
  37 + private VipPriceLogicServie vipPriceLogicServie;
34 38
35 @Override 39 @Override
36 public void build(List<ProductIndexBO> productIndexBOs, List<Integer> ids, List<Integer> sknList) { 40 public void build(List<ProductIndexBO> productIndexBOs, List<Integer> ids, List<Integer> sknList) {
@@ -42,7 +46,7 @@ public class ProductPriceBuilder implements ViewBuilder { @@ -42,7 +46,7 @@ public class ProductPriceBuilder implements ViewBuilder {
42 pi.setSpecialoffer("N"); 46 pi.setSpecialoffer("N");
43 pi.setPromotionDiscount(new BigDecimal(1)); 47 pi.setPromotionDiscount(new BigDecimal(1));
44 pi.setPromotionDiscountInt(10L); 48 pi.setPromotionDiscountInt(10L);
45 - 49 +
46 pi.setIsStudentPrice("N"); 50 pi.setIsStudentPrice("N");
47 pi.setIsStudentRebate("N"); 51 pi.setIsStudentRebate("N");
48 pi.setIsnew("N"); 52 pi.setIsnew("N");
@@ -92,7 +96,7 @@ public class ProductPriceBuilder implements ViewBuilder { @@ -92,7 +96,7 @@ public class ProductPriceBuilder implements ViewBuilder {
92 pi.setIsnew("Y"); 96 pi.setIsnew("Y");
93 } 97 }
94 } catch (Exception e) { 98 } catch (Exception e) {
95 - logger.error(e.getMessage(),e); 99 + logger.error(e.getMessage(), e);
96 } 100 }
97 101
98 } 102 }
@@ -123,84 +127,13 @@ public class ProductPriceBuilder implements ViewBuilder { @@ -123,84 +127,13 @@ public class ProductPriceBuilder implements ViewBuilder {
123 } 127 }
124 } 128 }
125 129
126 - private double getDoubleFromBigDecimal(BigDecimal bigDecimal) {  
127 - if (bigDecimal == null) {  
128 - return 0d;  
129 - }  
130 - return bigDecimal.doubleValue();  
131 - }  
132 -  
133 - private BigDecimal getBigDecimalFromDouble(Double d) {  
134 - if (d == null) {  
135 - return new BigDecimal(0);  
136 - }  
137 - return new BigDecimal(d);  
138 - }  
139 -  
140 - private boolean vipPriceLimit = false;// 是否执行vip价格限制  
141 -  
142 public void fillProductPriceBO(ProductPriceBO productPriceBO) { 130 public void fillProductPriceBO(ProductPriceBO productPriceBO) {
143 - double marketPrice = this.getDoubleFromBigDecimal(productPriceBO.getMarketPrice()); 131 + // 1、计算并重设vip相关价格
  132 + vipPriceLogicServie.fillProductPriceVipPrice(productPriceBO);
  133 +
  134 + // 2、计算折扣相关
144 double salesPrice = this.getDoubleFromBigDecimal(productPriceBO.getSalesPrice()); 135 double salesPrice = this.getDoubleFromBigDecimal(productPriceBO.getSalesPrice());
145 - double vipPrice = vipPriceLimit ? salesPrice : this.getDoubleFromBigDecimal(productPriceBO.getVipPrice());  
146 - double vip1Price = salesPrice;  
147 - double vip2Price = salesPrice;  
148 - double vip3Price = salesPrice;  
149 -  
150 - // 判断是否需要计算vip价格  
151 - boolean needCalVipPrice = productPriceBO.getVipDiscountType() != null;  
152 - // 如果开启了vip限制,  
153 - if (vipPriceLimit) {  
154 - needCalVipPrice = needCalVipPrice && (salesPrice == marketPrice) && (productPriceBO.getProductVipStatus() == 1);  
155 - }  
156 - if (needCalVipPrice) {  
157 - switch (productPriceBO.getVipDiscountType()) {  
158 - case 1:  
159 - vip1Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice) * 0.95));  
160 - vip2Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice) * 0.9));  
161 - vip3Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice) * 0.88));  
162 - break;  
163 - case 2:  
164 - if(productPriceBO.getVipDiscount()!=null){  
165 - boolean gt0 = productPriceBO.getVipDiscount().compareTo(BigDecimal.ZERO)==1;  
166 - boolean lt1 = productPriceBO.getVipDiscount().compareTo(BigDecimal.ONE)==-1;  
167 - if(gt0&&lt1){  
168 - BigDecimal price = productPriceBO.getVipDiscount().multiply(BigDecimal.valueOf(Math.ceil(salesPrice)));  
169 - vip1Price = Double.parseDouble(String.format("%.2f", price));  
170 - vip2Price = Double.parseDouble(String.format("%.2f", price));  
171 - vip3Price = Double.parseDouble(String.format("%.2f", price));  
172 - }  
173 - }  
174 - else{  
175 - vip1Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice) * 0.95));  
176 - vip2Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice) * 0.95));  
177 - vip3Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice) * 0.95));  
178 - }  
179 - break;  
180 - case 3:  
181 - vip1Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice)));  
182 - vip2Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice)));  
183 - vip3Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice)));  
184 - break;  
185 - case 4:  
186 - vip1Price = vipPrice;  
187 - vip2Price = vipPrice;  
188 - vip3Price = vipPrice;  
189 - break;  
190 - case 5:  
191 - vip1Price = this.getDoubleFromBigDecimal(productPriceBO.getVip1Price());  
192 - vip2Price = this.getDoubleFromBigDecimal(productPriceBO.getVip2Price());  
193 - vip3Price = this.getDoubleFromBigDecimal(productPriceBO.getVip3Price());  
194 - break;  
195 - }  
196 - }  
197 - productPriceBO.setSalesPrice(this.getBigDecimalFromDouble(salesPrice));  
198 - productPriceBO.setVipPrice(this.getBigDecimalFromDouble(vipPrice));  
199 - productPriceBO.setVip1Price(this.getBigDecimalFromDouble(vip1Price));  
200 - productPriceBO.setVip2Price(this.getBigDecimalFromDouble(vip2Price));  
201 - productPriceBO.setVip3Price(this.getBigDecimalFromDouble(vip3Price));  
202 -  
203 - // 折扣相关 136 + double marketPrice = this.getDoubleFromBigDecimal(productPriceBO.getMarketPrice());
204 productPriceBO.setIsDiscount(salesPrice < marketPrice ? "Y" : "N"); 137 productPriceBO.setIsDiscount(salesPrice < marketPrice ? "Y" : "N");
205 double specialOffer = this.divide(1, productPriceBO.getSalesPrice(), productPriceBO.getMarketPrice()); 138 double specialOffer = this.divide(1, productPriceBO.getSalesPrice(), productPriceBO.getMarketPrice());
206 if (specialOffer <= 0.5) { 139 if (specialOffer <= 0.5) {
@@ -213,11 +146,14 @@ public class ProductPriceBuilder implements ViewBuilder { @@ -213,11 +146,14 @@ public class ProductPriceBuilder implements ViewBuilder {
213 double promotionDiscount = this.divide(3, productPriceBO.getSalesPrice(), productPriceBO.getMarketPrice()); 146 double promotionDiscount = this.divide(3, productPriceBO.getSalesPrice(), productPriceBO.getMarketPrice());
214 productPriceBO.setPromotionDiscount(this.getBigDecimalFromDouble(promotionDiscount)); 147 productPriceBO.setPromotionDiscount(this.getBigDecimalFromDouble(promotionDiscount));
215 148
216 - // 设置学生价相关属性 149 + // 3、设置学生价相关属性
217 productPriceBO.setIsStudentPrice(productPriceBO.getStudentPrice() != null ? "Y" : "N"); 150 productPriceBO.setIsStudentPrice(productPriceBO.getStudentPrice() != null ? "Y" : "N");
218 productPriceBO.setIsstudentrebate(productPriceBO.getStudentCoinRate() == null || productPriceBO.getStudentCoinRate().compareTo(BigDecimal.ZERO) == 0 ? "N" : "Y"); 151 productPriceBO.setIsstudentrebate(productPriceBO.getStudentCoinRate() == null || productPriceBO.getStudentCoinRate().compareTo(BigDecimal.ZERO) == 0 ? "N" : "Y");
219 152
220 - // 设置vipLevels 153 + // 4、设置vipLevels
  154 + double vip1Price = productPriceBO.getVip1Price().doubleValue();
  155 + double vip2Price = productPriceBO.getVip2Price().doubleValue();
  156 + double vip3Price = productPriceBO.getVip3Price().doubleValue();
221 List<String> vipLevels = new ArrayList<String>(); 157 List<String> vipLevels = new ArrayList<String>();
222 if (vip1Price > 0 && vip1Price < salesPrice) { 158 if (vip1Price > 0 && vip1Price < salesPrice) {
223 vipLevels.add("1"); 159 vipLevels.add("1");
@@ -229,7 +165,6 @@ public class ProductPriceBuilder implements ViewBuilder { @@ -229,7 +165,6 @@ public class ProductPriceBuilder implements ViewBuilder {
229 vipLevels.add("3"); 165 vipLevels.add("3");
230 } 166 }
231 productPriceBO.setVipLevels(StringUtils.join(vipLevels, ",")); 167 productPriceBO.setVipLevels(StringUtils.join(vipLevels, ","));
232 -  
233 } 168 }
234 169
235 private double divide(int scale, BigDecimal a, BigDecimal b) { 170 private double divide(int scale, BigDecimal a, BigDecimal b) {
@@ -246,4 +181,18 @@ public class ProductPriceBuilder implements ViewBuilder { @@ -246,4 +181,18 @@ public class ProductPriceBuilder implements ViewBuilder {
246 return Double.parseDouble(result); 181 return Double.parseDouble(result);
247 } 182 }
248 183
  184 + private double getDoubleFromBigDecimal(BigDecimal bigDecimal) {
  185 + if (bigDecimal == null) {
  186 + return 0d;
  187 + }
  188 + return bigDecimal.doubleValue();
  189 + }
  190 +
  191 + private BigDecimal getBigDecimalFromDouble(Double d) {
  192 + if (d == null) {
  193 + return new BigDecimal(0);
  194 + }
  195 + return new BigDecimal(d);
  196 + }
  197 +
249 } 198 }