Authored by unknown

fix 价格的构建方式

@@ -2,7 +2,6 @@ package com.yoho.search.consumer.index.increment.productIndex; @@ -2,7 +2,6 @@ package com.yoho.search.consumer.index.increment.productIndex;
2 2
3 import java.util.ArrayList; 3 import java.util.ArrayList;
4 import java.util.List; 4 import java.util.List;
5 -import java.util.Map;  
6 5
7 import org.apache.commons.collections.CollectionUtils; 6 import org.apache.commons.collections.CollectionUtils;
8 import org.slf4j.Logger; 7 import org.slf4j.Logger;
@@ -22,10 +21,10 @@ import com.yoho.search.consumer.index.increment.AbstractIndexMqListener; @@ -22,10 +21,10 @@ import com.yoho.search.consumer.index.increment.AbstractIndexMqListener;
22 import com.yoho.search.consumer.service.base.ProductPriceService; 21 import com.yoho.search.consumer.service.base.ProductPriceService;
23 import com.yoho.search.consumer.service.base.ProductService; 22 import com.yoho.search.consumer.service.base.ProductService;
24 import com.yoho.search.consumer.service.bo.ProductIBO; 23 import com.yoho.search.consumer.service.bo.ProductIBO;
  24 +import com.yoho.search.consumer.service.bo.ProductPriceBO;
  25 +import com.yoho.search.consumer.service.logic.productIndex.ProductPriceBOLogicService;
25 import com.yoho.search.consumer.service.logic.productIndex.viewBuilder.ProductPriceBuilder; 26 import com.yoho.search.consumer.service.logic.productIndex.viewBuilder.ProductPriceBuilder;
26 import com.yoho.search.dal.model.Product; 27 import com.yoho.search.dal.model.Product;
27 -import com.yoho.search.dal.model.ProductPrice;  
28 -import com.yoho.search.dal.model.ProductPriceHistory;  
29 28
30 @Component 29 @Component
31 public class ProductMqListener extends AbstractIndexMqListener { 30 public class ProductMqListener extends AbstractIndexMqListener {
@@ -43,6 +42,8 @@ public class ProductMqListener extends AbstractIndexMqListener { @@ -43,6 +42,8 @@ public class ProductMqListener extends AbstractIndexMqListener {
43 private ProductPriceBuilder productPriceBuilder; 42 private ProductPriceBuilder productPriceBuilder;
44 @Autowired 43 @Autowired
45 private ProductIndexIndexBuilder productIndexIndexBuilder; 44 private ProductIndexIndexBuilder productIndexIndexBuilder;
  45 + @Autowired
  46 + private ProductPriceBOLogicService productPriceBOLogicService;
46 47
47 @Override 48 @Override
48 public String getIndexName() { 49 public String getIndexName() {
@@ -84,7 +85,7 @@ public class ProductMqListener extends AbstractIndexMqListener { @@ -84,7 +85,7 @@ public class ProductMqListener extends AbstractIndexMqListener {
84 85
85 private void updateProductDataToEs(final Integer productId, Integer productSkn, boolean isUpdate) { 86 private void updateProductDataToEs(final Integer productId, Integer productSkn, boolean isUpdate) {
86 CostStatistics costStatistics = new CostStatistics(); 87 CostStatistics costStatistics = new CostStatistics();
87 - // 1、获取数据 88 + // 1、构建ProductIBO
88 List<Integer> ids = new ArrayList<>(); 89 List<Integer> ids = new ArrayList<>();
89 ids.add(productId); 90 ids.add(productId);
90 List<ProductIBO> productIBOs = productIndexIndexBuilder.getProductIBOs(ids); 91 List<ProductIBO> productIBOs = productIndexIndexBuilder.getProductIBOs(ids);
@@ -95,18 +96,19 @@ public class ProductMqListener extends AbstractIndexMqListener { @@ -95,18 +96,19 @@ public class ProductMqListener extends AbstractIndexMqListener {
95 logger.info("[step1=getProductIBO][productId={}][cost={}ms]", productId, costStatistics.getCost()); 96 logger.info("[step1=getProductIBO][productId={}][cost={}ms]", productId, costStatistics.getCost());
96 97
97 // 2、构建ProductPrice 98 // 2、构建ProductPrice
98 - ProductPrice productPrice = productPriceService.getBySkn(productSkn);  
99 - ProductPriceHistory productPriceHistory = productPriceService.selectProductPriceHistory(productSkn);  
100 - Map<String, Object> productPriceModelMap = productPriceBuilder.getProductPriceModelMap(productId, productIBO.getFirstShelveTime(), productPrice,productPriceHistory);  
101 - logger.info("[step2=getProductPrice][productId={}][cost={}ms]", productId, costStatistics.getCost()); 99 + ProductPriceBO productPriceBO = productPriceBOLogicService.buildProductPriceBOWithNew(productSkn, productIBO.getFirstShelveTime());
  100 + logger.info("[step2=getProductPriceBO][productId={}][cost={}ms]", productId, costStatistics.getCost());
102 101
103 - // 3、更新到ES  
104 - try { 102 + // 3、构建更新对象
105 JSONObject jsonObject = (JSONObject) JSON.toJSON(productIBO); 103 JSONObject jsonObject = (JSONObject) JSON.toJSON(productIBO);
106 if (!isUpdate) { 104 if (!isUpdate) {
107 jsonObject.put("salesNum", 0); 105 jsonObject.put("salesNum", 0);
108 } 106 }
109 - jsonObject.putAll(productPriceModelMap); 107 + if(productPriceBO!=null){
  108 + jsonObject.putAll((JSONObject) JSON.toJSON(productPriceBO));
  109 + }
  110 + // 4、更新到ES
  111 + try {
110 this.updateProductIndexWithDataMap(jsonObject, productId); 112 this.updateProductIndexWithDataMap(jsonObject, productId);
111 logger.info("[step3=updateToEs][productId={}][cost={}ms]", productId, costStatistics.getCost()); 113 logger.info("[step3=updateToEs][productId={}][cost={}ms]", productId, costStatistics.getCost());
112 } catch (Exception e) { 114 } catch (Exception e) {
@@ -8,19 +8,18 @@ import org.slf4j.LoggerFactory; @@ -8,19 +8,18 @@ import org.slf4j.LoggerFactory;
8 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.stereotype.Component; 9 import org.springframework.stereotype.Component;
10 10
  11 +import com.alibaba.fastjson.JSON;
11 import com.alibaba.fastjson.JSONObject; 12 import com.alibaba.fastjson.JSONObject;
12 import com.yoho.search.base.utils.ConvertUtils; 13 import com.yoho.search.base.utils.ConvertUtils;
13 import com.yoho.search.base.utils.EventReportEnum; 14 import com.yoho.search.base.utils.EventReportEnum;
14 import com.yoho.search.base.utils.ISearchConstants; 15 import com.yoho.search.base.utils.ISearchConstants;
15 -import com.yoho.search.base.utils.ProductIndexEsField;  
16 import com.yoho.search.consumer.index.increment.AbstractIndexMqListener; 16 import com.yoho.search.consumer.index.increment.AbstractIndexMqListener;
17 import com.yoho.search.consumer.service.base.ProductPriceService; 17 import com.yoho.search.consumer.service.base.ProductPriceService;
18 import com.yoho.search.consumer.service.base.ProductService; 18 import com.yoho.search.consumer.service.base.ProductService;
19 -import com.yoho.search.consumer.service.logic.SpecialDealLogicService;  
20 -import com.yoho.search.consumer.service.logic.productIndex.viewBuilder.ProductPriceBuilder; 19 +import com.yoho.search.consumer.service.bo.ProductPriceBO;
  20 +import com.yoho.search.consumer.service.logic.productIndex.ProductPriceBOLogicService;
21 import com.yoho.search.dal.model.Product; 21 import com.yoho.search.dal.model.Product;
22 import com.yoho.search.dal.model.ProductPrice; 22 import com.yoho.search.dal.model.ProductPrice;
23 -import com.yoho.search.dal.model.ProductPriceHistory;  
24 23
25 @Component 24 @Component
26 public class ProductPriceMqListener extends AbstractIndexMqListener { 25 public class ProductPriceMqListener extends AbstractIndexMqListener {
@@ -31,9 +30,7 @@ public class ProductPriceMqListener extends AbstractIndexMqListener { @@ -31,9 +30,7 @@ public class ProductPriceMqListener extends AbstractIndexMqListener {
31 @Autowired 30 @Autowired
32 private ProductService productService; 31 private ProductService productService;
33 @Autowired 32 @Autowired
34 - private ProductPriceBuilder productPriceBuilder;  
35 - @Autowired  
36 - private SpecialDealLogicService specialSearchFieldLogicService; 33 + private ProductPriceBOLogicService productPriceBOLogicService;
37 34
38 @Override 35 @Override
39 protected String getIndexName() { 36 protected String getIndexName() {
@@ -57,8 +54,10 @@ public class ProductPriceMqListener extends AbstractIndexMqListener { @@ -57,8 +54,10 @@ public class ProductPriceMqListener extends AbstractIndexMqListener {
57 logger.info("[func=deleteData][step=deleteFromBb][productSkn={}][cost={}ms]", productSkn, this.getCost(begin)); 54 logger.info("[func=deleteData][step=deleteFromBb][productSkn={}][cost={}ms]", productSkn, this.getCost(begin));
58 55
59 // 3、清空ProductIndex索引中的价格相关参数 56 // 3、清空ProductIndex索引中的价格相关参数
60 - this.updateProductIndexByClearPrice(productId);  
61 - 57 + ProductPriceBO productPriceBO = new ProductPriceBO(productSkn);// 创建一个空对象
  58 + Map<String, Object> indexData = new HashMap<String, Object>();
  59 + indexData.putAll((JSONObject) JSON.toJSON(productPriceBO));
  60 + this.updateProductIndexWithDataMap(indexData, productId);
62 } 61 }
63 62
64 @Override 63 @Override
@@ -84,41 +83,13 @@ public class ProductPriceMqListener extends AbstractIndexMqListener { @@ -84,41 +83,13 @@ public class ProductPriceMqListener extends AbstractIndexMqListener {
84 logger.warn("[func=updateData][step=updateProductIndex fail ][product is null ][productSkn is {}]", productSkn); 83 logger.warn("[func=updateData][step=updateProductIndex fail ][product is null ][productSkn is {}]", productSkn);
85 return; 84 return;
86 } 85 }
87 - // 构造增量的建到Pi索引里的数据  
88 - ProductPriceHistory productPriceHistory = productPriceService.selectProductPriceHistory(productSkn);  
89 - Map<String, Object> indexData = productPriceBuilder.getProductPriceModelMap(product.getId(), product.getFirstShelveTime(), pp,productPriceHistory);  
90 - // 更新商品索引  
91 - this.updateProductIndexWithDataMap(indexData, product.getId()); 86 + ProductPriceBO productPriceBO = productPriceBOLogicService.buildProductPriceBOWithNew(pp, product.getFirstShelveTime());
  87 + if (productPriceBO == null) {
  88 + return;
92 } 89 }
93 -  
94 - /**  
95 - * 清除商品索引里 相关的价格参数  
96 - */  
97 - private void updateProductIndexByClearPrice(Integer productId) {  
98 - Map<String, Object> indexData = new HashMap<String, Object>(30);  
99 - indexData.put("productId", productId);  
100 - indexData.put("marketPrice", 0);  
101 - indexData.put("salesPrice", 0);  
102 - indexData.put("vipPrice", 0);  
103 - indexData.put("vipDiscountType", 0);  
104 - indexData.put("vip1Price", 0);  
105 - indexData.put("vip2Price", 0);  
106 - indexData.put("vip3Price", 0);  
107 - indexData.put("studentPrice", 0);  
108 - // others  
109 - indexData.put("isDiscount", "N");  
110 - indexData.put("specialoffer", "N");  
111 - indexData.put("promotionDiscountInt", 10);  
112 - indexData.put("promotionDiscount", 1);  
113 - indexData.put("isStudentPrice", "N");  
114 - indexData.put("isstudentrebate", "N");  
115 - indexData.put("vipLevels", "");  
116 - indexData.put("priceUpdateTime", 0);  
117 - indexData.put("isnew", "N");  
118 - indexData.put(ProductIndexEsField.specialSearchFieldPrice, "");  
119 - indexData.put("isLatestReducePrice", "N"); 90 + Map<String, Object> indexData = new HashMap<String, Object>();
  91 + indexData.putAll((JSONObject) JSON.toJSON(productPriceBO));
120 // 更新商品索引 92 // 更新商品索引
121 - this.updateProductIndexWithDataMap(indexData, productId); 93 + this.updateProductIndexWithDataMap(indexData, product.getId());
122 } 94 }
123 -  
124 } 95 }
@@ -6,35 +6,44 @@ import com.yoho.search.dal.model.ProductPrice; @@ -6,35 +6,44 @@ import com.yoho.search.dal.model.ProductPrice;
6 6
7 public class ProductPriceBO { 7 public class ProductPriceBO {
8 8
9 - private BigDecimal marketPrice;  
10 - private BigDecimal vipPrice;  
11 - private BigDecimal vip1Price;  
12 - private BigDecimal vip2Price;  
13 - private BigDecimal vip3Price;  
14 - private BigDecimal studentPrice;  
15 - private BigDecimal studentCoinRate;  
16 - private Integer updateTime;  
17 - private Integer lastReducePriceTime;  
18 - private BigDecimal basicPrice;  
19 - private Integer productVipStatus;  
20 - private BigDecimal vipDiscount;  
21 -  
22 - private BigDecimal salesPrice;// 构造的时候使用currentSalesPrice构建  
23 - private Integer vipDiscountType;// 构造的时候使用currentVipDiscountType构建 9 + private Integer productSkn = 0;
  10 + private BigDecimal marketPrice = new BigDecimal(0);
  11 + private BigDecimal vipPrice = new BigDecimal(0);
  12 + private BigDecimal vip1Price = new BigDecimal(0);
  13 + private BigDecimal vip2Price = new BigDecimal(0);
  14 + private BigDecimal vip3Price = new BigDecimal(0);
  15 + private BigDecimal studentPrice = new BigDecimal(0);
  16 + private BigDecimal studentCoinRate = new BigDecimal(0);
  17 + private Integer priceUpdateTime = 0;
  18 + private Integer lastReducePriceTime = 0;
  19 + private BigDecimal basicPrice = new BigDecimal(0);
  20 + private Integer productVipStatus = 0;
  21 + private BigDecimal vipDiscount = new BigDecimal(0);
  22 +
  23 + private BigDecimal salesPrice = new BigDecimal(0);// 构造的时候使用currentSalesPrice构建
  24 + private Integer vipDiscountType = 0;// 构造的时候使用currentVipDiscountType构建
24 25
25 // others 26 // others
26 - private String isDiscount;  
27 - private String specialoffer;  
28 - private Long promotionDiscountInt;  
29 - private BigDecimal promotionDiscount;  
30 - private String isStudentPrice;  
31 - private String isstudentrebate;  
32 - private String vipLevels;  
33 - private String isLatestReducePrice;  
34 - private String isnew; 27 + private String isDiscount = "N";
  28 + private String specialoffer = "N";
  29 + private String isStudentPrice = "N";
  30 + private String isstudentrebate = "N";
  31 + private String isLatestReducePrice = "N";
  32 + private String isnew = "N";
  33 +
  34 + private Long promotionDiscountInt = 10L;
  35 + private BigDecimal promotionDiscount = new BigDecimal(1);
  36 +
  37 + private String vipLevels = "";
  38 + private String specialSearchFieldPrice = "";
  39 +
  40 + public ProductPriceBO(Integer productSkn) {
  41 + this.productSkn = productSkn;
  42 + }
35 43
36 public ProductPriceBO(ProductPrice productPrice) { 44 public ProductPriceBO(ProductPrice productPrice) {
37 super(); 45 super();
  46 + this.productSkn = productPrice.getProductSkn();
38 // 使用currentSalesPrice和currentVipDiscountType构建salesPrice和vipDiscountType【综合了变价计划和商品本身价格的结果,生成的逻辑完全由后台控制】 47 // 使用currentSalesPrice和currentVipDiscountType构建salesPrice和vipDiscountType【综合了变价计划和商品本身价格的结果,生成的逻辑完全由后台控制】
39 BigDecimal currentPrice = productPrice.getCurrentPrice();// 48 BigDecimal currentPrice = productPrice.getCurrentPrice();//
40 if (currentPrice != null && currentPrice.compareTo(BigDecimal.ZERO) == 1) { 49 if (currentPrice != null && currentPrice.compareTo(BigDecimal.ZERO) == 1) {
@@ -55,13 +64,21 @@ public class ProductPriceBO { @@ -55,13 +64,21 @@ public class ProductPriceBO {
55 this.vip3Price = productPrice.getVip3Price(); 64 this.vip3Price = productPrice.getVip3Price();
56 this.studentPrice = productPrice.getStudentPrice(); 65 this.studentPrice = productPrice.getStudentPrice();
57 this.studentCoinRate = productPrice.getStudentCoinRate(); 66 this.studentCoinRate = productPrice.getStudentCoinRate();
58 - this.updateTime = productPrice.getUpdateTime(); 67 + this.priceUpdateTime = productPrice.getUpdateTime();
59 this.lastReducePriceTime = productPrice.getLastReducePriceTime(); 68 this.lastReducePriceTime = productPrice.getLastReducePriceTime();
60 this.basicPrice = productPrice.getBasicPrice(); 69 this.basicPrice = productPrice.getBasicPrice();
61 this.productVipStatus = productPrice.getProductVipStatus(); 70 this.productVipStatus = productPrice.getProductVipStatus();
62 this.vipDiscount = productPrice.getVipDiscount(); 71 this.vipDiscount = productPrice.getVipDiscount();
63 } 72 }
64 73
  74 + public Integer getProductSkn() {
  75 + return productSkn;
  76 + }
  77 +
  78 + public void setProductSkn(Integer productSkn) {
  79 + this.productSkn = productSkn;
  80 + }
  81 +
65 public String getIsLatestReducePrice() { 82 public String getIsLatestReducePrice() {
66 return isLatestReducePrice; 83 return isLatestReducePrice;
67 } 84 }
@@ -214,12 +231,12 @@ public class ProductPriceBO { @@ -214,12 +231,12 @@ public class ProductPriceBO {
214 this.vipLevels = vipLevels; 231 this.vipLevels = vipLevels;
215 } 232 }
216 233
217 - public Integer getUpdateTime() {  
218 - return updateTime; 234 + public Integer getPriceUpdateTime() {
  235 + return priceUpdateTime;
219 } 236 }
220 237
221 - public void setUpdateTime(Integer updateTime) {  
222 - this.updateTime = updateTime; 238 + public void setPriceUpdateTime(Integer priceUpdateTime) {
  239 + this.priceUpdateTime = priceUpdateTime;
223 } 240 }
224 241
225 public BigDecimal getBasicPrice() { 242 public BigDecimal getBasicPrice() {
@@ -238,6 +255,14 @@ public class ProductPriceBO { @@ -238,6 +255,14 @@ public class ProductPriceBO {
238 this.productVipStatus = productVipStatus; 255 this.productVipStatus = productVipStatus;
239 } 256 }
240 257
  258 + public String getSpecialSearchFieldPrice() {
  259 + return specialSearchFieldPrice;
  260 + }
  261 +
  262 + public void setSpecialSearchFieldPrice(String specialSearchFieldPrice) {
  263 + this.specialSearchFieldPrice = specialSearchFieldPrice;
  264 + }
  265 +
241 public String getIsnew() { 266 public String getIsnew() {
242 return isnew; 267 return isnew;
243 } 268 }
@@ -4,6 +4,7 @@ import java.math.BigDecimal; @@ -4,6 +4,7 @@ import java.math.BigDecimal;
4 import java.math.RoundingMode; 4 import java.math.RoundingMode;
5 import java.text.DecimalFormat; 5 import java.text.DecimalFormat;
6 import java.util.ArrayList; 6 import java.util.ArrayList;
  7 +import java.util.Arrays;
7 import java.util.Date; 8 import java.util.Date;
8 import java.util.List; 9 import java.util.List;
9 import java.util.Map; 10 import java.util.Map;
@@ -15,6 +16,7 @@ import org.springframework.stereotype.Service; @@ -15,6 +16,7 @@ import org.springframework.stereotype.Service;
15 import com.yoho.search.base.utils.DateUtil; 16 import com.yoho.search.base.utils.DateUtil;
16 import com.yoho.search.base.utils.MathUtils; 17 import com.yoho.search.base.utils.MathUtils;
17 import com.yoho.search.consumer.service.base.ProductPriceService; 18 import com.yoho.search.consumer.service.base.ProductPriceService;
  19 +import com.yoho.search.consumer.service.bo.ProductIndexBO;
18 import com.yoho.search.consumer.service.bo.ProductPriceBO; 20 import com.yoho.search.consumer.service.bo.ProductPriceBO;
19 import com.yoho.search.consumer.service.logic.SpecialDealLogicService; 21 import com.yoho.search.consumer.service.logic.SpecialDealLogicService;
20 import com.yoho.search.consumer.service.logic.VipPriceLogicService; 22 import com.yoho.search.consumer.service.logic.VipPriceLogicService;
@@ -31,13 +33,29 @@ public class ProductPriceBOLogicService { @@ -31,13 +33,29 @@ public class ProductPriceBOLogicService {
31 @Autowired 33 @Autowired
32 private VipPriceLogicService vipPriceLogicService; 34 private VipPriceLogicService vipPriceLogicService;
33 35
34 - /**  
35 - * 构造skn的价格相关的bo  
36 - *  
37 - * @param productSkns  
38 - * @return  
39 - */  
40 - public List<ProductPriceBO> buildProductPriceBO(List<Integer> productSkns) { 36 + public ProductPriceBO buildProductPriceBOWithNew(ProductPrice productPrice, Integer firstShelveTime) {
  37 + ProductPriceHistory productPriceHistory = productPriceService.selectProductPriceHistory(productPrice.getProductSkn());
  38 + // 构造ProductPriceBO
  39 + ProductPriceBO productPriceBO = new ProductPriceBO(productPrice);
  40 + // 构造ProductIndexBO
  41 + this.buildProductPriceBO(productPriceBO, productPrice, productPriceHistory);
  42 + // 构造isNew
  43 + this.buildisNew(productPriceBO, firstShelveTime);
  44 + return productPriceBO;
  45 + }
  46 +
  47 + public ProductPriceBO buildProductPriceBOWithNew(Integer productSkn, Integer firstShelveTime) {
  48 + List<ProductPriceBO> productPriceBOs = this.buildProductPriceBOs(Arrays.asList(productSkn));
  49 + if (productPriceBOs == null || productPriceBOs.isEmpty()) {
  50 + return null;
  51 + }
  52 + ProductPriceBO productPriceBO = productPriceBOs.get(0);
  53 + // 构造isNew
  54 + this.buildisNew(productPriceBO, firstShelveTime);
  55 + return productPriceBO;
  56 + }
  57 +
  58 + public List<ProductPriceBO> buildProductPriceBOs(List<Integer> productSkns) {
41 List<ProductPrice> productPrices = productPriceService.getProductPrices(productSkns); 59 List<ProductPrice> productPrices = productPriceService.getProductPrices(productSkns);
42 List<ProductPriceHistory> productPriceHistorys = productPriceService.selectProductPriceHistorys(productSkns); 60 List<ProductPriceHistory> productPriceHistorys = productPriceService.selectProductPriceHistorys(productSkns);
43 Map<Integer, ProductPriceHistory> productPriceHistoryMap = productPriceHistorys.stream().collect(Collectors.toMap(ProductPriceHistory::getProductSkn, (p) -> p)); 61 Map<Integer, ProductPriceHistory> productPriceHistoryMap = productPriceHistorys.stream().collect(Collectors.toMap(ProductPriceHistory::getProductSkn, (p) -> p));
@@ -74,40 +92,10 @@ public class ProductPriceBOLogicService { @@ -74,40 +92,10 @@ public class ProductPriceBOLogicService {
74 BigDecimal studentPrice = productPriceBO.getStudentPrice(); 92 BigDecimal studentPrice = productPriceBO.getStudentPrice();
75 productPriceBO.setIsStudentPrice(studentPrice == null || studentPrice.compareTo(BigDecimal.ZERO) == 0 ? "N" : "Y"); 93 productPriceBO.setIsStudentPrice(studentPrice == null || studentPrice.compareTo(BigDecimal.ZERO) == 0 ? "N" : "Y");
76 productPriceBO.setIsstudentrebate(productPriceBO.getStudentCoinRate() == null || productPriceBO.getStudentCoinRate().compareTo(BigDecimal.ZERO) == 0 ? "N": "Y"); 94 productPriceBO.setIsstudentrebate(productPriceBO.getStudentCoinRate() == null || productPriceBO.getStudentCoinRate().compareTo(BigDecimal.ZERO) == 0 ? "N": "Y");
77 - // 4、设置最新降价 95 + // 4、设置
  96 + productPriceBO.setSpecialSearchFieldPrice(specialDealLogicService.getSpecialSearchFieldPrice(productPriceBO));
  97 + // 5、设置最新降价
78 productPriceBO.setIsLatestReducePrice(this.isLatestReducePrice(productPrice, productPriceHistory)); 98 productPriceBO.setIsLatestReducePrice(this.isLatestReducePrice(productPrice, productPriceHistory));
79 - // TODO fill new  
80 - }  
81 -  
82 - private void fillIsNew(Integer firstShelveTime, ProductPriceBO productPriceBO) {  
83 - BigDecimal marketPrice = productPriceBO.getMarketPrice();  
84 - BigDecimal salesPrice = productPriceBO.getSalesPrice();  
85 - boolean isNew = this.isNew(firstShelveTime, marketPrice, salesPrice);  
86 - productPriceBO.setIsnew(isNew?"Y":"N");  
87 - }  
88 -  
89 - /**  
90 - *@计算isNew  
91 - *@首次上架7天内展示且折扣率大于等于88折)  
92 - */  
93 - private boolean isNew(Integer firstShelveTime, BigDecimal marketPrice, BigDecimal salesPrice) {  
94 - try {  
95 - if (firstShelveTime == null || firstShelveTime <= 0 || salesPrice == null || marketPrice == null) {  
96 - return false;  
97 - }  
98 - long dateCount = DateUtil.daysBetween(new Date(firstShelveTime * 1000L), new Date());  
99 - if (dateCount > 7) {  
100 - return false;  
101 - }  
102 - // 计算折扣  
103 - Double discount = MathUtils.getDevideValue(salesPrice, marketPrice, 2, RoundingMode.DOWN);  
104 - if (discount >= 0.88) {  
105 - return true;  
106 - }  
107 - return false;  
108 - } catch (Exception e) {  
109 - return false;  
110 - }  
111 } 99 }
112 100
113 private String isLatestReducePrice(ProductPrice productPrice, ProductPriceHistory productPriceHistory) { 101 private String isLatestReducePrice(ProductPrice productPrice, ProductPriceHistory productPriceHistory) {
@@ -149,4 +137,38 @@ public class ProductPriceBOLogicService { @@ -149,4 +137,38 @@ public class ProductPriceBOLogicService {
149 return new BigDecimal(d); 137 return new BigDecimal(d);
150 } 138 }
151 139
  140 + /**
  141 + * @计算isNew
  142 + * @首次上架7天内展示且折扣率大于等于88折)
  143 + */
  144 + public boolean isNew(Integer firstShelveTime, BigDecimal marketPrice, BigDecimal salesPrice) {
  145 + try {
  146 + if (firstShelveTime == null || firstShelveTime <= 0 || salesPrice == null || marketPrice == null) {
  147 + return false;
  148 + }
  149 + long dateCount = DateUtil.daysBetween(new Date(firstShelveTime * 1000L), new Date());
  150 + if (dateCount > 7) {
  151 + return false;
  152 + }
  153 + // 计算折扣
  154 + Double discount = MathUtils.getDevideValue(salesPrice, marketPrice, 2, RoundingMode.DOWN);
  155 + if (discount >= 0.88) {
  156 + return true;
  157 + }
  158 + return false;
  159 + } catch (Exception e) {
  160 + return false;
  161 + }
  162 + }
  163 +
  164 + private void buildisNew(ProductPriceBO productPriceBO, Integer firstShelveTime) {
  165 + boolean isNew = this.isNew(firstShelveTime, productPriceBO.getMarketPrice(), productPriceBO.getSalesPrice());
  166 + productPriceBO.setIsnew(isNew ? "Y" : "N");
  167 + }
  168 +
  169 + public void buildisNew(ProductIndexBO productIndexBO, ProductPriceBO productPriceBO) {
  170 + boolean isNew = this.isNew(productIndexBO.getFirstShelveTime(), productPriceBO.getMarketPrice(), productPriceBO.getSalesPrice());
  171 + productPriceBO.setIsnew(isNew ? "Y" : "N");
  172 + }
  173 +
152 } 174 }
1 package com.yoho.search.consumer.service.logic.productIndex.viewBuilder; 1 package com.yoho.search.consumer.service.logic.productIndex.viewBuilder;
2 2
3 -import java.math.BigDecimal;  
4 -import java.math.RoundingMode;  
5 -import java.text.DecimalFormat;  
6 -import java.util.Date;  
7 -import java.util.HashMap;  
8 import java.util.List; 3 import java.util.List;
9 import java.util.Map; 4 import java.util.Map;
10 import java.util.stream.Collectors; 5 import java.util.stream.Collectors;
11 6
12 import org.slf4j.Logger; 7 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory; 8 import org.slf4j.LoggerFactory;
  9 +import org.springframework.beans.BeanUtils;
14 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.stereotype.Component; 11 import org.springframework.stereotype.Component;
16 12
17 -import com.yoho.search.base.utils.DateUtil;  
18 -import com.yoho.search.base.utils.MathUtils;  
19 -import com.yoho.search.base.utils.ProductIndexEsField;  
20 import com.yoho.search.consumer.service.base.ProductPriceService; 13 import com.yoho.search.consumer.service.base.ProductPriceService;
21 import com.yoho.search.consumer.service.bo.ProductIndexBO; 14 import com.yoho.search.consumer.service.bo.ProductIndexBO;
22 import com.yoho.search.consumer.service.bo.ProductPriceBO; 15 import com.yoho.search.consumer.service.bo.ProductPriceBO;
23 -import com.yoho.search.consumer.service.logic.SpecialDealLogicService;  
24 import com.yoho.search.consumer.service.logic.VipPriceLogicService; 16 import com.yoho.search.consumer.service.logic.VipPriceLogicService;
25 -import com.yoho.search.dal.model.ProductPrice;  
26 -import com.yoho.search.dal.model.ProductPriceHistory; 17 +import com.yoho.search.consumer.service.logic.productIndex.ProductPriceBOLogicService;
27 18
28 /** 19 /**
29 * Created by wangnan on 2017/1/6. 20 * Created by wangnan on 2017/1/6.
@@ -38,28 +29,19 @@ public class ProductPriceBuilder implements ViewBuilder { @@ -38,28 +29,19 @@ public class ProductPriceBuilder implements ViewBuilder {
38 @Autowired 29 @Autowired
39 private VipPriceLogicService vipPriceLogicService; 30 private VipPriceLogicService vipPriceLogicService;
40 @Autowired 31 @Autowired
41 - private SpecialDealLogicService specialSearchFieldLogicService; 32 + private ProductPriceBOLogicService productPriceBOLogicService;
42 33
43 @Override 34 @Override
44 public void build(List<ProductIndexBO> productIndexBOs, List<Integer> ids, List<Integer> sknList) { 35 public void build(List<ProductIndexBO> productIndexBOs, List<Integer> ids, List<Integer> sknList) {
45 - List<ProductPrice> productPrices = productPriceService.getProductPrices(sknList);  
46 - Map<Integer, ProductPrice> productPricesMap = productPrices.stream().collect(Collectors.toMap(ProductPrice::getProductSkn, (p) -> p));  
47 - List<ProductPriceHistory> productPriceHistorys = productPriceService.selectProductPriceHistorys(sknList);  
48 - Map<Integer, ProductPriceHistory> productPriceHistoryMap = productPriceHistorys.stream().collect(Collectors.toMap(ProductPriceHistory::getProductSkn, (p) -> p));  
49 - 36 + List<ProductPriceBO> productPrices = productPriceBOLogicService.buildProductPriceBOs(sknList);
  37 + Map<Integer, ProductPriceBO> productPricesMap = productPrices.stream().collect(Collectors.toMap(ProductPriceBO::getProductSkn, (p) -> p));
50 for (ProductIndexBO productIndexBO : productIndexBOs) { 38 for (ProductIndexBO productIndexBO : productIndexBOs) {
51 try { 39 try {
52 // 如果该skn对应价格对象不存在,直接跳过 40 // 如果该skn对应价格对象不存在,直接跳过
53 - ProductPrice productPrice = productPricesMap.get(productIndexBO.getProductSkn());  
54 - if (productPrice == null) { 41 + ProductPriceBO productPriceBO = productPricesMap.get(productIndexBO.getProductSkn());
  42 + if (productPriceBO == null) {
55 continue; 43 continue;
56 } 44 }
57 - // 取价格的历史记录  
58 - ProductPriceHistory productPriceHistory = productPriceHistoryMap.get(productIndexBO.getProductSkn());  
59 - // 构造ProductPriceBO  
60 - ProductPriceBO productPriceBO = new ProductPriceBO(productPrice);  
61 - // 构造ProductIndexBO  
62 - this.buildProductPriceBO(productPriceBO, productPrice,productPriceHistory);  
63 // 构造ProductIndexBO 45 // 构造ProductIndexBO
64 this.buildProductIndexBO(productIndexBO, productPriceBO); 46 this.buildProductIndexBO(productIndexBO, productPriceBO);
65 } catch (Exception e) { 47 } catch (Exception e) {
@@ -68,168 +50,9 @@ public class ProductPriceBuilder implements ViewBuilder { @@ -68,168 +50,9 @@ public class ProductPriceBuilder implements ViewBuilder {
68 } 50 }
69 } 51 }
70 52
71 - /**  
72 - * 增量+全量都调用  
73 - */  
74 - public void buildProductPriceBO(ProductPriceBO productPriceBO, ProductPrice productPrice,ProductPriceHistory productPriceHistory) {  
75 - // 1、计算并重设vip相关价格  
76 - vipPriceLogicService.fillProductPriceVipPrice(productPriceBO);  
77 - // 2、计算折扣相关  
78 - double salesPrice = this.getDoubleFromBigDecimal(productPriceBO.getSalesPrice());  
79 - double marketPrice = this.getDoubleFromBigDecimal(productPriceBO.getMarketPrice());  
80 - productPriceBO.setIsDiscount(salesPrice < marketPrice ? "Y" : "N");  
81 - double specialOffer = this.divide(1, productPriceBO.getSalesPrice(), productPriceBO.getMarketPrice());  
82 - if (specialOffer <= 0.5) {  
83 - productPriceBO.setSpecialoffer("Y");  
84 - } else {  
85 - productPriceBO.setSpecialoffer("N");  
86 - }  
87 - double promotionDiscountInt = this.divide(1, productPriceBO.getSalesPrice().multiply(new BigDecimal(10)), productPriceBO.getMarketPrice());  
88 - productPriceBO.setPromotionDiscountInt((long) promotionDiscountInt);  
89 - double promotionDiscount = this.divide(3, productPriceBO.getSalesPrice(), productPriceBO.getMarketPrice());  
90 - productPriceBO.setPromotionDiscount(this.getBigDecimalFromDouble(promotionDiscount));  
91 - // 3、设置学生价相关属性  
92 - BigDecimal studentPrice = productPriceBO.getStudentPrice();  
93 - productPriceBO.setIsStudentPrice(studentPrice== null || studentPrice.compareTo(BigDecimal.ZERO)==0 ? "N" : "Y");  
94 - productPriceBO.setIsstudentrebate(productPriceBO.getStudentCoinRate() == null || productPriceBO.getStudentCoinRate().compareTo(BigDecimal.ZERO) == 0 ? "N" : "Y");  
95 - // 4、设置最新降价  
96 - productPriceBO.setIsLatestReducePrice(this.isLatestReducePrice(productPrice,productPriceHistory));  
97 - }  
98 -  
99 private void buildProductIndexBO(ProductIndexBO productIndexBO, ProductPriceBO productPriceBO) { 53 private void buildProductIndexBO(ProductIndexBO productIndexBO, ProductPriceBO productPriceBO) {
100 - productIndexBO.setIsDiscount("N");  
101 - productIndexBO.setSpecialoffer("N");  
102 - productIndexBO.setPromotionDiscount(new BigDecimal(1));  
103 - productIndexBO.setPromotionDiscountInt(10L);  
104 - productIndexBO.setIsStudentPrice("N");  
105 - productIndexBO.setIsStudentRebate("N");  
106 - productIndexBO.setIsnew("N");  
107 - productIndexBO.setMarketPrice(productPriceBO.getMarketPrice());  
108 - productIndexBO.setVipDiscountType(productPriceBO.getVipDiscountType());  
109 - productIndexBO.setSalesPrice(productPriceBO.getSalesPrice());  
110 - productIndexBO.setVipPrice(productPriceBO.getVipPrice());  
111 - productIndexBO.setVip1Price(productPriceBO.getVip1Price());  
112 - productIndexBO.setVip2Price(productPriceBO.getVip2Price());  
113 - productIndexBO.setVip3Price(productPriceBO.getVip3Price());  
114 - productIndexBO.setStudentPrice(productPriceBO.getStudentPrice());  
115 - // 折扣相关  
116 - productIndexBO.setIsDiscount(productPriceBO.getIsDiscount());  
117 - productIndexBO.setSpecialoffer(productPriceBO.getSpecialoffer());  
118 - productIndexBO.setPromotionDiscountInt(productPriceBO.getPromotionDiscountInt());  
119 - productIndexBO.setPromotionDiscount(productPriceBO.getPromotionDiscount());  
120 - // 学生价相关字段  
121 - productIndexBO.setIsStudentPrice(productPriceBO.getIsStudentPrice());  
122 - productIndexBO.setIsStudentRebate(productPriceBO.getIsstudentrebate());  
123 - // 设置vipLevels  
124 - productIndexBO.setVipLevels(productPriceBO.getVipLevels());  
125 - // 设置价格更新时间  
126 - productIndexBO.setPriceUpdateTime(productPriceBO.getUpdateTime());  
127 - // 设置最后一次降价时间  
128 - productIndexBO.setLastReducePriceTime(productPriceBO.getLastReducePriceTime());  
129 - // 计算isNew(规则:首次上架7天内展示且折扣率大于等于88折)  
130 - productIndexBO.setIsnew("N");  
131 - if (this.isNew(productIndexBO.getFirstShelveTime(), productIndexBO.getMarketPrice(), productIndexBO.getSalesPrice())) {  
132 - productIndexBO.setIsnew("Y");  
133 - }  
134 - // 计算specialSearchFieldPrice  
135 - productIndexBO.setSpecialSearchFieldPrice(specialSearchFieldLogicService.getSpecialSearchFieldPrice(productPriceBO));  
136 - productIndexBO.setIsLatestReducePrice(productPriceBO.getIsLatestReducePrice());  
137 - }  
138 -  
139 - public Map<String, Object> getProductPriceModelMap(Integer productId, Integer firstShelveTime, ProductPrice pp,ProductPriceHistory productPriceHistory) {  
140 - Map<String, Object> indexData = new HashMap<String, Object>();  
141 - indexData.put("isnew", "N");  
142 - if (pp == null) {  
143 - return indexData;  
144 - }  
145 - // 构造ProductPriceBO  
146 - ProductPriceBO productPriceBO = new ProductPriceBO(pp);  
147 - this.buildProductPriceBO(productPriceBO, pp,productPriceHistory);  
148 - indexData.put("productId", productId);  
149 - indexData.put("marketPrice", this.getDoubleFromBigDecimal(productPriceBO.getMarketPrice()));  
150 - indexData.put("salesPrice", this.getDoubleFromBigDecimal(productPriceBO.getSalesPrice()));  
151 - indexData.put("vipPrice", this.getDoubleFromBigDecimal(productPriceBO.getVipPrice()));  
152 - indexData.put("vipDiscountType", productPriceBO.getVipDiscountType());  
153 - indexData.put("vip1Price", this.getDoubleFromBigDecimal(productPriceBO.getVip1Price()));  
154 - indexData.put("vip2Price", this.getDoubleFromBigDecimal(productPriceBO.getVip2Price()));  
155 - indexData.put("vip3Price", this.getDoubleFromBigDecimal(productPriceBO.getVip3Price()));  
156 - indexData.put("studentPrice", this.getDoubleFromBigDecimal(productPriceBO.getStudentPrice()));  
157 -  
158 - indexData.put("isDiscount", productPriceBO.getIsDiscount());  
159 - indexData.put("specialoffer", productPriceBO.getSpecialoffer());  
160 - indexData.put("promotionDiscountInt", productPriceBO.getPromotionDiscountInt());  
161 - indexData.put("promotionDiscount", this.getDoubleFromBigDecimal(productPriceBO.getPromotionDiscount()));  
162 - indexData.put("isStudentPrice", productPriceBO.getIsStudentPrice());  
163 - indexData.put("isstudentrebate", productPriceBO.getIsstudentrebate());  
164 - indexData.put("vipLevels", productPriceBO.getVipLevels());  
165 - indexData.put("priceUpdateTime", productPriceBO.getUpdateTime());  
166 - if (this.isNew(firstShelveTime, productPriceBO.getMarketPrice(), productPriceBO.getSalesPrice())) {  
167 - indexData.put("isnew", "Y");  
168 - }  
169 - indexData.put(ProductIndexEsField.specialSearchFieldPrice, specialSearchFieldLogicService.getSpecialSearchFieldPrice(productPriceBO));  
170 - indexData.put("isLatestReducePrice", productPriceBO.getIsLatestReducePrice());  
171 - return indexData;  
172 - }  
173 -  
174 - private String isLatestReducePrice(ProductPrice productPrice,ProductPriceHistory productPriceHistory) {  
175 - BigDecimal newPrice = productPrice.getCurrentPrice();  
176 - if (newPrice == null || productPriceHistory == null) {  
177 - return "N";  
178 - }  
179 - if (newPrice.compareTo(productPriceHistory.getCurrentSalesPrice()) == 0) {  
180 - return newPrice.compareTo(productPriceHistory.getHistorySalesPrice()) < 0 ? "Y" : "N";  
181 - }  
182 - return newPrice.compareTo(productPriceHistory.getCurrentSalesPrice()) < 0 ? "Y" : "N";  
183 - }  
184 -  
185 - /**  
186 - * isNew 标签逻辑  
187 - */  
188 - public boolean isNew(Integer firstShelveTime, BigDecimal marketPrice, BigDecimal salesPrice) {  
189 - try {  
190 - if (firstShelveTime == null || firstShelveTime <= 0 || salesPrice == null || marketPrice == null) {  
191 - return false;  
192 - }  
193 - long dateCount = DateUtil.daysBetween(new Date(firstShelveTime * 1000L), new Date());  
194 - if (dateCount > 7) {  
195 - return false;  
196 - }  
197 - // 计算折扣  
198 - Double discount = MathUtils.getDevideValue(salesPrice, marketPrice, 2, RoundingMode.DOWN);  
199 - if (discount >= 0.88) {  
200 - return true;  
201 - }  
202 - return false;  
203 - } catch (Exception e) {  
204 - return false;  
205 - }  
206 - }  
207 -  
208 - private double divide(int scale, BigDecimal a, BigDecimal b) {  
209 - if (b == null || b.compareTo(BigDecimal.ZERO) == 0) {  
210 - return 0;  
211 - }  
212 - StringBuilder sb = new StringBuilder("0.0");  
213 - for (int i = 1; i < scale; i++) {  
214 - sb.append('0');  
215 - }  
216 - BigDecimal divideResult = a.divide(b, scale, RoundingMode.DOWN);  
217 - DecimalFormat decimalFormat = new DecimalFormat(sb.toString());  
218 - String result = decimalFormat.format(divideResult);  
219 - return Double.parseDouble(result);  
220 - }  
221 -  
222 - private double getDoubleFromBigDecimal(BigDecimal bigDecimal) {  
223 - if (bigDecimal == null) {  
224 - return 0d;  
225 - }  
226 - return bigDecimal.doubleValue();  
227 - }  
228 -  
229 - private BigDecimal getBigDecimalFromDouble(Double d) {  
230 - if (d == null) {  
231 - return new BigDecimal(0);  
232 - }  
233 - return new BigDecimal(d); 54 + BeanUtils.copyProperties(productPriceBO, productIndexBO);
  55 + //全量建索引时单独设置isnew
  56 + productPriceBOLogicService.buildisNew(productIndexBO, productPriceBO);
234 } 57 }
235 } 58 }