Authored by unknown

fix 价格的构建方式

... ... @@ -2,7 +2,6 @@ package com.yoho.search.consumer.index.increment.productIndex;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
... ... @@ -22,10 +21,10 @@ import com.yoho.search.consumer.index.increment.AbstractIndexMqListener;
import com.yoho.search.consumer.service.base.ProductPriceService;
import com.yoho.search.consumer.service.base.ProductService;
import com.yoho.search.consumer.service.bo.ProductIBO;
import com.yoho.search.consumer.service.bo.ProductPriceBO;
import com.yoho.search.consumer.service.logic.productIndex.ProductPriceBOLogicService;
import com.yoho.search.consumer.service.logic.productIndex.viewBuilder.ProductPriceBuilder;
import com.yoho.search.dal.model.Product;
import com.yoho.search.dal.model.ProductPrice;
import com.yoho.search.dal.model.ProductPriceHistory;
@Component
public class ProductMqListener extends AbstractIndexMqListener {
... ... @@ -43,6 +42,8 @@ public class ProductMqListener extends AbstractIndexMqListener {
private ProductPriceBuilder productPriceBuilder;
@Autowired
private ProductIndexIndexBuilder productIndexIndexBuilder;
@Autowired
private ProductPriceBOLogicService productPriceBOLogicService;
@Override
public String getIndexName() {
... ... @@ -84,7 +85,7 @@ public class ProductMqListener extends AbstractIndexMqListener {
private void updateProductDataToEs(final Integer productId, Integer productSkn, boolean isUpdate) {
CostStatistics costStatistics = new CostStatistics();
// 1、获取数据
// 1、构建ProductIBO
List<Integer> ids = new ArrayList<>();
ids.add(productId);
List<ProductIBO> productIBOs = productIndexIndexBuilder.getProductIBOs(ids);
... ... @@ -95,18 +96,19 @@ public class ProductMqListener extends AbstractIndexMqListener {
logger.info("[step1=getProductIBO][productId={}][cost={}ms]", productId, costStatistics.getCost());
// 2、构建ProductPrice
ProductPrice productPrice = productPriceService.getBySkn(productSkn);
ProductPriceHistory productPriceHistory = productPriceService.selectProductPriceHistory(productSkn);
Map<String, Object> productPriceModelMap = productPriceBuilder.getProductPriceModelMap(productId, productIBO.getFirstShelveTime(), productPrice,productPriceHistory);
logger.info("[step2=getProductPrice][productId={}][cost={}ms]", productId, costStatistics.getCost());
// 3、更新到ES
ProductPriceBO productPriceBO = productPriceBOLogicService.buildProductPriceBOWithNew(productSkn, productIBO.getFirstShelveTime());
logger.info("[step2=getProductPriceBO][productId={}][cost={}ms]", productId, costStatistics.getCost());
// 3、构建更新对象
JSONObject jsonObject = (JSONObject) JSON.toJSON(productIBO);
if (!isUpdate) {
jsonObject.put("salesNum", 0);
}
if(productPriceBO!=null){
jsonObject.putAll((JSONObject) JSON.toJSON(productPriceBO));
}
// 4、更新到ES
try {
JSONObject jsonObject = (JSONObject) JSON.toJSON(productIBO);
if (!isUpdate) {
jsonObject.put("salesNum", 0);
}
jsonObject.putAll(productPriceModelMap);
this.updateProductIndexWithDataMap(jsonObject, productId);
logger.info("[step3=updateToEs][productId={}][cost={}ms]", productId, costStatistics.getCost());
} catch (Exception e) {
... ...
... ... @@ -8,19 +8,18 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.ConvertUtils;
import com.yoho.search.base.utils.EventReportEnum;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.consumer.index.increment.AbstractIndexMqListener;
import com.yoho.search.consumer.service.base.ProductPriceService;
import com.yoho.search.consumer.service.base.ProductService;
import com.yoho.search.consumer.service.logic.SpecialDealLogicService;
import com.yoho.search.consumer.service.logic.productIndex.viewBuilder.ProductPriceBuilder;
import com.yoho.search.consumer.service.bo.ProductPriceBO;
import com.yoho.search.consumer.service.logic.productIndex.ProductPriceBOLogicService;
import com.yoho.search.dal.model.Product;
import com.yoho.search.dal.model.ProductPrice;
import com.yoho.search.dal.model.ProductPriceHistory;
@Component
public class ProductPriceMqListener extends AbstractIndexMqListener {
... ... @@ -31,9 +30,7 @@ public class ProductPriceMqListener extends AbstractIndexMqListener {
@Autowired
private ProductService productService;
@Autowired
private ProductPriceBuilder productPriceBuilder;
@Autowired
private SpecialDealLogicService specialSearchFieldLogicService;
private ProductPriceBOLogicService productPriceBOLogicService;
@Override
protected String getIndexName() {
... ... @@ -57,8 +54,10 @@ public class ProductPriceMqListener extends AbstractIndexMqListener {
logger.info("[func=deleteData][step=deleteFromBb][productSkn={}][cost={}ms]", productSkn, this.getCost(begin));
// 3、清空ProductIndex索引中的价格相关参数
this.updateProductIndexByClearPrice(productId);
ProductPriceBO productPriceBO = new ProductPriceBO(productSkn);// 创建一个空对象
Map<String, Object> indexData = new HashMap<String, Object>();
indexData.putAll((JSONObject) JSON.toJSON(productPriceBO));
this.updateProductIndexWithDataMap(indexData, productId);
}
@Override
... ... @@ -84,41 +83,13 @@ public class ProductPriceMqListener extends AbstractIndexMqListener {
logger.warn("[func=updateData][step=updateProductIndex fail ][product is null ][productSkn is {}]", productSkn);
return;
}
// 构造增量的建到Pi索引里的数据
ProductPriceHistory productPriceHistory = productPriceService.selectProductPriceHistory(productSkn);
Map<String, Object> indexData = productPriceBuilder.getProductPriceModelMap(product.getId(), product.getFirstShelveTime(), pp,productPriceHistory);
ProductPriceBO productPriceBO = productPriceBOLogicService.buildProductPriceBOWithNew(pp, product.getFirstShelveTime());
if (productPriceBO == null) {
return;
}
Map<String, Object> indexData = new HashMap<String, Object>();
indexData.putAll((JSONObject) JSON.toJSON(productPriceBO));
// 更新商品索引
this.updateProductIndexWithDataMap(indexData, product.getId());
}
/**
* 清除商品索引里 相关的价格参数
*/
private void updateProductIndexByClearPrice(Integer productId) {
Map<String, Object> indexData = new HashMap<String, Object>(30);
indexData.put("productId", productId);
indexData.put("marketPrice", 0);
indexData.put("salesPrice", 0);
indexData.put("vipPrice", 0);
indexData.put("vipDiscountType", 0);
indexData.put("vip1Price", 0);
indexData.put("vip2Price", 0);
indexData.put("vip3Price", 0);
indexData.put("studentPrice", 0);
// others
indexData.put("isDiscount", "N");
indexData.put("specialoffer", "N");
indexData.put("promotionDiscountInt", 10);
indexData.put("promotionDiscount", 1);
indexData.put("isStudentPrice", "N");
indexData.put("isstudentrebate", "N");
indexData.put("vipLevels", "");
indexData.put("priceUpdateTime", 0);
indexData.put("isnew", "N");
indexData.put(ProductIndexEsField.specialSearchFieldPrice, "");
indexData.put("isLatestReducePrice", "N");
// 更新商品索引
this.updateProductIndexWithDataMap(indexData, productId);
}
}
... ...
... ... @@ -5,36 +5,45 @@ import java.math.BigDecimal;
import com.yoho.search.dal.model.ProductPrice;
public class ProductPriceBO {
private BigDecimal marketPrice;
private BigDecimal vipPrice;
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 BigDecimal salesPrice;// 构造的时候使用currentSalesPrice构建
private Integer vipDiscountType;// 构造的时候使用currentVipDiscountType构建
private Integer productSkn = 0;
private BigDecimal marketPrice = new BigDecimal(0);
private BigDecimal vipPrice = new BigDecimal(0);
private BigDecimal vip1Price = new BigDecimal(0);
private BigDecimal vip2Price = new BigDecimal(0);
private BigDecimal vip3Price = new BigDecimal(0);
private BigDecimal studentPrice = new BigDecimal(0);
private BigDecimal studentCoinRate = new BigDecimal(0);
private Integer priceUpdateTime = 0;
private Integer lastReducePriceTime = 0;
private BigDecimal basicPrice = new BigDecimal(0);
private Integer productVipStatus = 0;
private BigDecimal vipDiscount = new BigDecimal(0);
private BigDecimal salesPrice = new BigDecimal(0);// 构造的时候使用currentSalesPrice构建
private Integer vipDiscountType = 0;// 构造的时候使用currentVipDiscountType构建
// others
private String isDiscount;
private String specialoffer;
private Long promotionDiscountInt;
private BigDecimal promotionDiscount;
private String isStudentPrice;
private String isstudentrebate;
private String vipLevels;
private String isLatestReducePrice;
private String isnew;
private String isDiscount = "N";
private String specialoffer = "N";
private String isStudentPrice = "N";
private String isstudentrebate = "N";
private String isLatestReducePrice = "N";
private String isnew = "N";
private Long promotionDiscountInt = 10L;
private BigDecimal promotionDiscount = new BigDecimal(1);
private String vipLevels = "";
private String specialSearchFieldPrice = "";
public ProductPriceBO(Integer productSkn) {
this.productSkn = productSkn;
}
public ProductPriceBO(ProductPrice productPrice) {
super();
this.productSkn = productPrice.getProductSkn();
// 使用currentSalesPrice和currentVipDiscountType构建salesPrice和vipDiscountType【综合了变价计划和商品本身价格的结果,生成的逻辑完全由后台控制】
BigDecimal currentPrice = productPrice.getCurrentPrice();//
if (currentPrice != null && currentPrice.compareTo(BigDecimal.ZERO) == 1) {
... ... @@ -55,13 +64,21 @@ public class ProductPriceBO {
this.vip3Price = productPrice.getVip3Price();
this.studentPrice = productPrice.getStudentPrice();
this.studentCoinRate = productPrice.getStudentCoinRate();
this.updateTime = productPrice.getUpdateTime();
this.priceUpdateTime = productPrice.getUpdateTime();
this.lastReducePriceTime = productPrice.getLastReducePriceTime();
this.basicPrice = productPrice.getBasicPrice();
this.productVipStatus = productPrice.getProductVipStatus();
this.vipDiscount = productPrice.getVipDiscount();
}
public Integer getProductSkn() {
return productSkn;
}
public void setProductSkn(Integer productSkn) {
this.productSkn = productSkn;
}
public String getIsLatestReducePrice() {
return isLatestReducePrice;
}
... ... @@ -214,14 +231,14 @@ public class ProductPriceBO {
this.vipLevels = vipLevels;
}
public Integer getUpdateTime() {
return updateTime;
public Integer getPriceUpdateTime() {
return priceUpdateTime;
}
public void setUpdateTime(Integer updateTime) {
this.updateTime = updateTime;
public void setPriceUpdateTime(Integer priceUpdateTime) {
this.priceUpdateTime = priceUpdateTime;
}
public BigDecimal getBasicPrice() {
return basicPrice;
}
... ... @@ -238,6 +255,14 @@ public class ProductPriceBO {
this.productVipStatus = productVipStatus;
}
public String getSpecialSearchFieldPrice() {
return specialSearchFieldPrice;
}
public void setSpecialSearchFieldPrice(String specialSearchFieldPrice) {
this.specialSearchFieldPrice = specialSearchFieldPrice;
}
public String getIsnew() {
return isnew;
}
... ...
... ... @@ -4,6 +4,7 @@ import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
... ... @@ -15,6 +16,7 @@ import org.springframework.stereotype.Service;
import com.yoho.search.base.utils.DateUtil;
import com.yoho.search.base.utils.MathUtils;
import com.yoho.search.consumer.service.base.ProductPriceService;
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.VipPriceLogicService;
... ... @@ -31,13 +33,29 @@ public class ProductPriceBOLogicService {
@Autowired
private VipPriceLogicService vipPriceLogicService;
/**
* 构造skn的价格相关的bo
*
* @param productSkns
* @return
*/
public List<ProductPriceBO> buildProductPriceBO(List<Integer> productSkns) {
public ProductPriceBO buildProductPriceBOWithNew(ProductPrice productPrice, Integer firstShelveTime) {
ProductPriceHistory productPriceHistory = productPriceService.selectProductPriceHistory(productPrice.getProductSkn());
// 构造ProductPriceBO
ProductPriceBO productPriceBO = new ProductPriceBO(productPrice);
// 构造ProductIndexBO
this.buildProductPriceBO(productPriceBO, productPrice, productPriceHistory);
// 构造isNew
this.buildisNew(productPriceBO, firstShelveTime);
return productPriceBO;
}
public ProductPriceBO buildProductPriceBOWithNew(Integer productSkn, Integer firstShelveTime) {
List<ProductPriceBO> productPriceBOs = this.buildProductPriceBOs(Arrays.asList(productSkn));
if (productPriceBOs == null || productPriceBOs.isEmpty()) {
return null;
}
ProductPriceBO productPriceBO = productPriceBOs.get(0);
// 构造isNew
this.buildisNew(productPriceBO, firstShelveTime);
return productPriceBO;
}
public List<ProductPriceBO> buildProductPriceBOs(List<Integer> productSkns) {
List<ProductPrice> productPrices = productPriceService.getProductPrices(productSkns);
List<ProductPriceHistory> productPriceHistorys = productPriceService.selectProductPriceHistorys(productSkns);
Map<Integer, ProductPriceHistory> productPriceHistoryMap = productPriceHistorys.stream().collect(Collectors.toMap(ProductPriceHistory::getProductSkn, (p) -> p));
... ... @@ -52,7 +70,7 @@ public class ProductPriceBOLogicService {
}
return results;
}
private void buildProductPriceBO(ProductPriceBO productPriceBO, ProductPrice productPrice, ProductPriceHistory productPriceHistory) {
// 1、计算并重设vip相关价格
vipPriceLogicService.fillProductPriceVipPrice(productPriceBO);
... ... @@ -74,40 +92,10 @@ public class ProductPriceBOLogicService {
BigDecimal studentPrice = productPriceBO.getStudentPrice();
productPriceBO.setIsStudentPrice(studentPrice == null || studentPrice.compareTo(BigDecimal.ZERO) == 0 ? "N" : "Y");
productPriceBO.setIsstudentrebate(productPriceBO.getStudentCoinRate() == null || productPriceBO.getStudentCoinRate().compareTo(BigDecimal.ZERO) == 0 ? "N": "Y");
// 4、设置最新降价
// 4、设置
productPriceBO.setSpecialSearchFieldPrice(specialDealLogicService.getSpecialSearchFieldPrice(productPriceBO));
// 5、设置最新降价
productPriceBO.setIsLatestReducePrice(this.isLatestReducePrice(productPrice, productPriceHistory));
// TODO fill new
}
private void fillIsNew(Integer firstShelveTime, ProductPriceBO productPriceBO) {
BigDecimal marketPrice = productPriceBO.getMarketPrice();
BigDecimal salesPrice = productPriceBO.getSalesPrice();
boolean isNew = this.isNew(firstShelveTime, marketPrice, salesPrice);
productPriceBO.setIsnew(isNew?"Y":"N");
}
/**
*@计算isNew
*@首次上架7天内展示且折扣率大于等于88折)
*/
private 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 String isLatestReducePrice(ProductPrice productPrice, ProductPriceHistory productPriceHistory) {
... ... @@ -149,4 +137,38 @@ public class ProductPriceBOLogicService {
return new BigDecimal(d);
}
/**
* @计算isNew
* @首次上架7天内展示且折扣率大于等于88折)
*/
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 void buildisNew(ProductPriceBO productPriceBO, Integer firstShelveTime) {
boolean isNew = this.isNew(firstShelveTime, productPriceBO.getMarketPrice(), productPriceBO.getSalesPrice());
productPriceBO.setIsnew(isNew ? "Y" : "N");
}
public void buildisNew(ProductIndexBO productIndexBO, ProductPriceBO productPriceBO) {
boolean isNew = this.isNew(productIndexBO.getFirstShelveTime(), productPriceBO.getMarketPrice(), productPriceBO.getSalesPrice());
productPriceBO.setIsnew(isNew ? "Y" : "N");
}
}
... ...
package com.yoho.search.consumer.service.logic.productIndex.viewBuilder;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
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.base.utils.ProductIndexEsField;
import com.yoho.search.consumer.service.base.ProductPriceService;
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.VipPriceLogicService;
import com.yoho.search.dal.model.ProductPrice;
import com.yoho.search.dal.model.ProductPriceHistory;
import com.yoho.search.consumer.service.logic.productIndex.ProductPriceBOLogicService;
/**
* Created by wangnan on 2017/1/6.
... ... @@ -32,34 +23,25 @@ import com.yoho.search.dal.model.ProductPriceHistory;
public class ProductPriceBuilder implements ViewBuilder {
private final Logger logger = LoggerFactory.getLogger(ProductPriceBuilder.class);
@Autowired
private ProductPriceService productPriceService;
@Autowired
private VipPriceLogicService vipPriceLogicService;
@Autowired
private SpecialDealLogicService specialSearchFieldLogicService;
private ProductPriceBOLogicService productPriceBOLogicService;
@Override
public void build(List<ProductIndexBO> productIndexBOs, List<Integer> ids, List<Integer> sknList) {
List<ProductPrice> productPrices = productPriceService.getProductPrices(sknList);
Map<Integer, ProductPrice> productPricesMap = productPrices.stream().collect(Collectors.toMap(ProductPrice::getProductSkn, (p) -> p));
List<ProductPriceHistory> productPriceHistorys = productPriceService.selectProductPriceHistorys(sknList);
Map<Integer, ProductPriceHistory> productPriceHistoryMap = productPriceHistorys.stream().collect(Collectors.toMap(ProductPriceHistory::getProductSkn, (p) -> p));
List<ProductPriceBO> productPrices = productPriceBOLogicService.buildProductPriceBOs(sknList);
Map<Integer, ProductPriceBO> productPricesMap = productPrices.stream().collect(Collectors.toMap(ProductPriceBO::getProductSkn, (p) -> p));
for (ProductIndexBO productIndexBO : productIndexBOs) {
try {
// 如果该skn对应价格对象不存在,直接跳过
ProductPrice productPrice = productPricesMap.get(productIndexBO.getProductSkn());
if (productPrice == null) {
ProductPriceBO productPriceBO = productPricesMap.get(productIndexBO.getProductSkn());
if (productPriceBO == null) {
continue;
}
// 取价格的历史记录
ProductPriceHistory productPriceHistory = productPriceHistoryMap.get(productIndexBO.getProductSkn());
// 构造ProductPriceBO
ProductPriceBO productPriceBO = new ProductPriceBO(productPrice);
// 构造ProductIndexBO
this.buildProductPriceBO(productPriceBO, productPrice,productPriceHistory);
// 构造ProductIndexBO
this.buildProductIndexBO(productIndexBO, productPriceBO);
} catch (Exception e) {
... ... @@ -68,168 +50,9 @@ public class ProductPriceBuilder implements ViewBuilder {
}
}
/**
* 增量+全量都调用
*/
public void buildProductPriceBO(ProductPriceBO productPriceBO, ProductPrice productPrice,ProductPriceHistory productPriceHistory) {
// 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、设置学生价相关属性
BigDecimal studentPrice = productPriceBO.getStudentPrice();
productPriceBO.setIsStudentPrice(studentPrice== null || studentPrice.compareTo(BigDecimal.ZERO)==0 ? "N" : "Y");
productPriceBO.setIsstudentrebate(productPriceBO.getStudentCoinRate() == null || productPriceBO.getStudentCoinRate().compareTo(BigDecimal.ZERO) == 0 ? "N" : "Y");
// 4、设置最新降价
productPriceBO.setIsLatestReducePrice(this.isLatestReducePrice(productPrice,productPriceHistory));
}
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));
productIndexBO.setIsLatestReducePrice(productPriceBO.getIsLatestReducePrice());
}
public Map<String, Object> getProductPriceModelMap(Integer productId, Integer firstShelveTime, ProductPrice pp,ProductPriceHistory productPriceHistory) {
Map<String, Object> indexData = new HashMap<String, Object>();
indexData.put("isnew", "N");
if (pp == null) {
return indexData;
}
// 构造ProductPriceBO
ProductPriceBO productPriceBO = new ProductPriceBO(pp);
this.buildProductPriceBO(productPriceBO, pp,productPriceHistory);
indexData.put("productId", productId);
indexData.put("marketPrice", this.getDoubleFromBigDecimal(productPriceBO.getMarketPrice()));
indexData.put("salesPrice", this.getDoubleFromBigDecimal(productPriceBO.getSalesPrice()));
indexData.put("vipPrice", this.getDoubleFromBigDecimal(productPriceBO.getVipPrice()));
indexData.put("vipDiscountType", productPriceBO.getVipDiscountType());
indexData.put("vip1Price", this.getDoubleFromBigDecimal(productPriceBO.getVip1Price()));
indexData.put("vip2Price", this.getDoubleFromBigDecimal(productPriceBO.getVip2Price()));
indexData.put("vip3Price", this.getDoubleFromBigDecimal(productPriceBO.getVip3Price()));
indexData.put("studentPrice", this.getDoubleFromBigDecimal(productPriceBO.getStudentPrice()));
indexData.put("isDiscount", productPriceBO.getIsDiscount());
indexData.put("specialoffer", productPriceBO.getSpecialoffer());
indexData.put("promotionDiscountInt", productPriceBO.getPromotionDiscountInt());
indexData.put("promotionDiscount", this.getDoubleFromBigDecimal(productPriceBO.getPromotionDiscount()));
indexData.put("isStudentPrice", productPriceBO.getIsStudentPrice());
indexData.put("isstudentrebate", productPriceBO.getIsstudentrebate());
indexData.put("vipLevels", productPriceBO.getVipLevels());
indexData.put("priceUpdateTime", productPriceBO.getUpdateTime());
if (this.isNew(firstShelveTime, productPriceBO.getMarketPrice(), productPriceBO.getSalesPrice())) {
indexData.put("isnew", "Y");
}
indexData.put(ProductIndexEsField.specialSearchFieldPrice, specialSearchFieldLogicService.getSpecialSearchFieldPrice(productPriceBO));
indexData.put("isLatestReducePrice", productPriceBO.getIsLatestReducePrice());
return indexData;
}
private String isLatestReducePrice(ProductPrice productPrice,ProductPriceHistory productPriceHistory) {
BigDecimal newPrice = productPrice.getCurrentPrice();
if (newPrice == null || productPriceHistory == null) {
return "N";
}
if (newPrice.compareTo(productPriceHistory.getCurrentSalesPrice()) == 0) {
return newPrice.compareTo(productPriceHistory.getHistorySalesPrice()) < 0 ? "Y" : "N";
}
return newPrice.compareTo(productPriceHistory.getCurrentSalesPrice()) < 0 ? "Y" : "N";
}
/**
* 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);
BeanUtils.copyProperties(productPriceBO, productIndexBO);
//全量建索引时单独设置isnew
productPriceBOLogicService.buildisNew(productIndexBO, productPriceBO);
}
}
... ...