...
|
...
|
@@ -4,144 +4,177 @@ 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.consumer.index.common.IYohoIndexService;
|
|
|
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.productIndex.viewBuilder.ProductPriceBuilder;
|
|
|
import com.yoho.search.dal.ProductPriceMapper;
|
|
|
import com.yoho.search.dal.ProductPricePlanMapper;
|
|
|
import com.yoho.search.dal.model.Product;
|
|
|
import com.yoho.search.dal.model.ProductPrice;
|
|
|
import com.yoho.search.dal.model.ProductPricePlan;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Component
|
|
|
public class ProductPriceMqListener extends AbstractIndexMqListener {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ProductPriceMqListener.class);
|
|
|
@Autowired
|
|
|
private ProductPriceService productPriceService;
|
|
|
@Autowired
|
|
|
private ProductService productService;
|
|
|
@Autowired
|
|
|
private ProductPriceBuilder productPriceBuilder;
|
|
|
|
|
|
@Override
|
|
|
protected String getIndexName() {
|
|
|
return ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected EventReportEnum getEventReportEnum() {
|
|
|
return EventReportEnum.PRODUCTPRICEMQLISTENER_ONMESSAGE;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void deleteData(String id) throws Exception {
|
|
|
// 1、获取productId
|
|
|
Integer productId = Integer.valueOf(id);
|
|
|
|
|
|
// 2、删除数据库
|
|
|
long begin = System.currentTimeMillis();
|
|
|
productPriceService.delete(productId);
|
|
|
logger.info("[func=deleteData][step=deleteFromBb][productId={}][cost={}ms]", id, this.getCost(begin));
|
|
|
|
|
|
// 3、清空ProductIndex索引中的价格相关参数
|
|
|
begin = System.currentTimeMillis();
|
|
|
this.updateProductIndexByClearPrice(productId);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void updateData(JSONObject data) throws Exception {
|
|
|
ProductPrice productPrice = (ProductPrice) ConvertUtils.transMap2Bean(ProductPrice.class, data);
|
|
|
if (productPrice == null || productPrice.getProductId() == null) {
|
|
|
return;
|
|
|
}
|
|
|
// 1、保存至数据库
|
|
|
long begin = System.currentTimeMillis();
|
|
|
productPriceService.saveOrUpdate(productPrice);
|
|
|
logger.info("[func=updateData][step=saveToBb][productId={}][cost={}ms]", productPrice.getProductId(), this.getCost(begin));
|
|
|
|
|
|
// 2、更新ProductIndex
|
|
|
begin = System.currentTimeMillis();
|
|
|
this.updateProductIndex(productPrice.getProductId(), productPrice);
|
|
|
logger.info("[func=updateData][step=updateProductIndex][productId={}][cost={}ms]", productPrice.getProductId(), 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>(30);
|
|
|
|
|
|
// 构造ProductPriceBO
|
|
|
ProductPriceBO productPriceBO = new ProductPriceBO(pp);
|
|
|
productPriceBuilder.fillProductPriceBO(productPriceBO);
|
|
|
indexData.put("productId", productPriceBO.getProductId());
|
|
|
indexData.put("specialPrice", this.getDoubleFromBigDecimal(productPriceBO.getSpecialPrice()));
|
|
|
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");
|
|
|
}
|
|
|
// 更新商品索引
|
|
|
this.updateProductIndexWithDataMap(indexData, productId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 清除商品索引里 相关的价格参数
|
|
|
*/
|
|
|
private void updateProductIndexByClearPrice(Integer productId) {
|
|
|
Map<String, Object> indexData = new HashMap<String, Object>(30);
|
|
|
indexData.put("productId", productId);
|
|
|
indexData.put("specialPrice", null);
|
|
|
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);
|
|
|
// 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");
|
|
|
// 更新商品索引
|
|
|
this.updateProductIndexWithDataMap(indexData, productId);
|
|
|
}
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ProductPriceMqListener.class);
|
|
|
@Autowired
|
|
|
private ProductPriceService productPriceService;
|
|
|
@Autowired
|
|
|
private ProductService productService;
|
|
|
@Autowired
|
|
|
private ProductPriceBuilder productPriceBuilder;
|
|
|
@Autowired
|
|
|
private IYohoIndexService indexService;
|
|
|
@Autowired
|
|
|
private ProductPricePlanMapper productPricePlanMapper;
|
|
|
@Autowired
|
|
|
private ProductPriceMapper productPriceMapper;
|
|
|
|
|
|
|
|
|
@Override
|
|
|
protected String getIndexName() {
|
|
|
return ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected EventReportEnum getEventReportEnum() {
|
|
|
return EventReportEnum.PRODUCTPRICEMQLISTENER_ONMESSAGE;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void deleteData(String id) throws Exception {
|
|
|
// 1、获取productId
|
|
|
Integer productId = Integer.valueOf(id);
|
|
|
|
|
|
// 2、删除数据库
|
|
|
long begin = System.currentTimeMillis();
|
|
|
productPriceService.delete(productId);
|
|
|
logger.info("[func=deleteData][step=deleteFromBb][productId={}][cost={}ms]", id, this.getCost(begin));
|
|
|
|
|
|
// 3、清空ProductIndex索引中的价格相关参数
|
|
|
begin = System.currentTimeMillis();
|
|
|
this.updateProductIndexByClearPrice(productId);
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void updateData(JSONObject data) throws Exception {
|
|
|
ProductPrice productPrice = (ProductPrice) ConvertUtils.transMap2Bean(ProductPrice.class, data);
|
|
|
if (productPrice == null || productPrice.getProductId() == null) {
|
|
|
return;
|
|
|
}
|
|
|
// 1、保存至数据库
|
|
|
long begin = System.currentTimeMillis();
|
|
|
productPriceService.saveOrUpdate(productPrice);
|
|
|
logger.info("[func=updateData][step=saveToBb][productId={}][cost={}ms]", productPrice.getProductId(), this.getCost(begin));
|
|
|
|
|
|
// 2、更新ProductIndex
|
|
|
begin = System.currentTimeMillis();
|
|
|
this.updateProductIndex(productPrice.getProductId(), productPrice);
|
|
|
this.updateProductPricePlan(productPrice);
|
|
|
logger.info("[func=updateData][step=updateProductIndex][productId={}][cost={}ms]", productPrice.getProductId(), this.getCost(begin));
|
|
|
}
|
|
|
|
|
|
private double getDoubleFromBigDecimal(BigDecimal bigDecimal) {
|
|
|
if (bigDecimal == null) {
|
|
|
return 0d;
|
|
|
}
|
|
|
return bigDecimal.doubleValue();
|
|
|
}
|
|
|
|
|
|
private void updateProductPricePlan(ProductPrice pp) throws Exception {
|
|
|
ProductPricePlan productPricePlan = productPricePlanMapper.selectBySkn(pp.getProductSkn());
|
|
|
if (productPricePlan == null) {
|
|
|
return;
|
|
|
}
|
|
|
List<Integer> sknList = new ArrayList<>(1);
|
|
|
sknList.add(pp.getProductSkn());
|
|
|
List<ProductPrice> productPriceList = productPriceMapper.selectBySkns(sknList);
|
|
|
if (CollectionUtils.isEmpty(productPriceList)) {
|
|
|
return;
|
|
|
}
|
|
|
ProductPrice productPrice = productPriceList.get(0);
|
|
|
Map<String, Object> indexData = new HashMap<>(1);
|
|
|
indexData.put("vipDiscount", productPrice.getVipDiscount());
|
|
|
indexService.updateIndexData(ISearchConstants.INDEX_NAME_PRODUCT_PRICE_PLAN, productPricePlan.getId().toString(), indexData);
|
|
|
}
|
|
|
|
|
|
private void updateProductIndex(Integer productId, ProductPrice pp) {
|
|
|
Map<String, Object> indexData = new HashMap<String, Object>(30);
|
|
|
|
|
|
// 构造ProductPriceBO
|
|
|
ProductPriceBO productPriceBO = new ProductPriceBO(pp);
|
|
|
productPriceBuilder.fillProductPriceBO(productPriceBO);
|
|
|
indexData.put("productId", productPriceBO.getProductId());
|
|
|
indexData.put("specialPrice", this.getDoubleFromBigDecimal(productPriceBO.getSpecialPrice()));
|
|
|
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");
|
|
|
}
|
|
|
// 更新商品索引
|
|
|
this.updateProductIndexWithDataMap(indexData, productId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 清除商品索引里 相关的价格参数
|
|
|
*/
|
|
|
private void updateProductIndexByClearPrice(Integer productId) {
|
|
|
Map<String, Object> indexData = new HashMap<String, Object>(30);
|
|
|
indexData.put("productId", productId);
|
|
|
indexData.put("specialPrice", null);
|
|
|
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);
|
|
|
// 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");
|
|
|
// 更新商品索引
|
|
|
this.updateProductIndexWithDataMap(indexData, productId);
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|