Authored by unknown

修复增量更新时的价格问题

... ... @@ -2,6 +2,7 @@ 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;
... ... @@ -82,7 +83,7 @@ public class ProductMqListener extends AbstractIndexMqListener {
private void updateProductDataToEs(final Integer productId, Integer productSkn, boolean isUpdate) {
CostStatistics costStatistics = new CostStatistics();
// 3、获取数据
// 1、获取数据
List<Integer> ids = new ArrayList<>();
ids.add(productId);
List<ProductIBO> productIBOs = productIndexIndexBuilder.getProductIBOs(ids);
... ... @@ -90,24 +91,24 @@ public class ProductMqListener extends AbstractIndexMqListener {
return;
}
ProductIBO productIBO = productIBOs.get(0);
// 处理isNew(规则:首次上架7天内展示且折扣率小于88折)
productIBO.setIsnew("N");
logger.info("[step1=getProductIBO][productId={}][cost={}ms]", productId, costStatistics.getCost());
// 2、构建ProductPrice
ProductPrice productPrice = productPriceService.getBySkn(productSkn);
if (productPrice != null && productPriceBuilder.isNew(productIBO.getFirstShelveTime(), productPrice.getRetailPrice(), productPrice.getSalesPrice())) {
productIBO.setIsnew("Y");
}
logger.info("[step3=getProductI][productId={}][cost={}ms]", productId, costStatistics.getCost());
Map<String, Object> productPriceModelMap = productPriceBuilder.getProductPriceModelMap(productId, productIBO.getFirstShelveTime(), productPrice);
logger.info("[step2=getProductPrice][productId={}][cost={}ms]", productId, costStatistics.getCost());
// 4、更新到ES
// 3、更新到ES
try {
JSONObject jsonObject = (JSONObject) JSON.toJSON(productIBO);
if (!isUpdate) {
jsonObject.put("salesNum", 0);
}
jsonObject.putAll(productPriceModelMap);
this.updateProductIndexWithDataMap(jsonObject, productId);
logger.info("[step4=updateToEs][productId={}][cost={}ms]", productId, costStatistics.getCost());
logger.info("[step3=updateToEs][productId={}][cost={}ms]", productId, costStatistics.getCost());
} catch (Exception e) {
logger.error("[step4=updateToEs][productId={}][cost={}ms][execption={}]", productId, costStatistics.getCost(), e.getMessage());
logger.error("[step3=updateToEs][productId={}][cost={}ms][execption={}]", productId, costStatistics.getCost(), e.getMessage());
productIndexMqListener.sendProductIndexMessage(productId);
}
}
... ...
package com.yoho.search.consumer.index.increment.productIndex;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
... ... @@ -17,7 +16,6 @@ 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.bo.ProductPriceBO;
import com.yoho.search.consumer.service.logic.SpecialDealLogicService;
import com.yoho.search.consumer.service.logic.productIndex.viewBuilder.ProductPriceBuilder;
import com.yoho.search.dal.model.Product;
... ... @@ -75,51 +73,20 @@ public class ProductPriceMqListener extends AbstractIndexMqListener {
// 2、更新ProductIndex
begin = System.currentTimeMillis();
Integer productId = productService.selectProductIdBySkn(productPrice.getProductSkn());
this.updateProductIndex(productId, productPrice);
this.updateProductIndex(productPrice.getProductSkn(), productPrice);
logger.info("[func=updateData][step=updateProductIndex][productSkn={}][cost={}ms]", productPrice.getProductSkn(), this.getCost(begin));
}
private double getDoubleFromBigDecimal(BigDecimal bigDecimal) {
if (bigDecimal == null) {
return 0d;
}
return bigDecimal.doubleValue();
}
private void updateProductIndex(Integer productId, ProductPrice pp) {
Map<String, Object> indexData = new HashMap<String, Object>();
// 构造ProductPriceBO
ProductPriceBO productPriceBO = new ProductPriceBO(pp);
productPriceBuilder.buildProductPriceBO(productPriceBO);
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());
Product product = productService.getById(productId);
if (product != null) {
indexData.put("isnew", productPriceBuilder.isNew(product.getFirstShelveTime(), productPriceBO.getMarketPrice(), productPriceBO.getSalesPrice()));
} else {
indexData.put("isnew", "N");
private void updateProductIndex(Integer productSkn, ProductPrice pp) {
Product product = productService.getBySkn(productSkn);
if (product == null) {
logger.warn("[func=updateData][step=updateProductIndex fail ][product is null ][productSkn is {}]", productSkn);
return;
}
indexData.put(ProductIndexEsField.specialSearchFieldPrice, specialSearchFieldLogicService.getSpecialSearchFieldPrice(productPriceBO));
// 构造增量的建到Pi索引里的数据
Map<String, Object> indexData = productPriceBuilder.getProductPriceModelMap(product.getId(), product.getFirstShelveTime(), pp);
// 更新商品索引
this.updateProductIndexWithDataMap(indexData, productId);
this.updateProductIndexWithDataMap(indexData, product.getId());
}
/**
... ... @@ -128,14 +95,14 @@ public class ProductPriceMqListener extends AbstractIndexMqListener {
private void updateProductIndexByClearPrice(Integer productId) {
Map<String, Object> indexData = new HashMap<String, Object>(30);
indexData.put("productId", productId);
indexData.put("marketPrice", null);
indexData.put("salesPrice", null);
indexData.put("vipPrice", null);
indexData.put("vipDiscountType", null);
indexData.put("vip1Price", null);
indexData.put("vip2Price", null);
indexData.put("vip3Price", null);
indexData.put("studentPrice", null);
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");
... ...
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.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.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.ProductPriceMapper;
import com.yoho.search.dal.model.ProductPrice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Created by wangnan on 2017/1/6.
... ... @@ -122,6 +125,39 @@ public class ProductPriceBuilder implements ViewBuilder {
productIndexBO.setSpecialSearchFieldPrice(specialSearchFieldLogicService.getSpecialSearchFieldPrice(productPriceBO));
}
public Map<String, Object> getProductPriceModelMap(Integer productId, Integer firstShelveTime, ProductPrice pp) {
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);
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 (firstShelveTime != null) {
indexData.put("isnew", this.isNew(firstShelveTime, productPriceBO.getMarketPrice(), productPriceBO.getSalesPrice()));
}
indexData.put(ProductIndexEsField.specialSearchFieldPrice, specialSearchFieldLogicService.getSpecialSearchFieldPrice(productPriceBO));
return indexData;
}
/**
* isNew 标签逻辑
... ...