Authored by unknown

Merge branch 'master' into ge_skn_block

... ... @@ -17,6 +17,8 @@ public interface ProductPriceMapper {
List<ProductPrice> selectPageLists(@Param(value="offset")Integer offset, @Param(value="pageSize")Integer pageSize);
List<ProductPrice> selectByIds(List<Integer> ids);
List<ProductPrice> selectBySkns(List<Integer> skns);
int selectCount();
}
\ No newline at end of file
... ...
package com.yoho.search.dal;
import java.util.List;
import com.yoho.search.dal.model.ProductPricePlan;
import org.apache.ibatis.annotations.Param;
import com.yoho.search.dal.model.ProductPricePlan;
import java.util.List;
public interface ProductPricePlanMapper {
... ... @@ -16,6 +15,8 @@ public interface ProductPricePlanMapper {
ProductPricePlan selectByPrimaryKey(Integer id);
ProductPricePlan selectBySkn(Integer skn);
int updateByPrimaryKeySelective(ProductPricePlan record);
int updateByPrimaryKey(ProductPricePlan record);
... ...
... ... @@ -166,4 +166,15 @@
#{item}
</foreach>
</select>
<select id="selectBySkns" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from product_price where product_skn in
<foreach item="item" index="index" collection="list" open="("
separator="," close=")">
#{item}
</foreach>
</select>
</mapper>
\ No newline at end of file
... ...
... ... @@ -27,6 +27,13 @@
from product_price_plan
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectBySkn" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from product_price_plan
where product_skn = #{skn,jdbcType=INTEGER}
limit 1
</select>
<select id="selectCount" resultType="java.lang.Integer">
select count(id)
from product_price_plan
... ...
... ... @@ -2,17 +2,24 @@ package com.yoho.search.consumer.index.fullbuild;
import com.yoho.search.consumer.index.common.IIndexBuilder;
import com.yoho.search.consumer.service.base.ProductPricePlanService;
import com.yoho.search.dal.ProductPriceMapper;
import com.yoho.search.dal.model.ProductPrice;
import com.yoho.search.dal.model.ProductPricePlan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Component
public class ProductPricePlanIndexBuilder extends IIndexBuilder {
@Autowired
private ProductPricePlanService productPricePlanService;
@Autowired
private ProductPriceMapper productPriceMapper;
@Override
public int getTotalCount() throws Exception {
... ... @@ -21,7 +28,14 @@ public class ProductPricePlanIndexBuilder extends IIndexBuilder {
@Override
public List<?> getPageLists(int offset, int limit) throws Exception {
return productPricePlanService.getPageLists(offset, limit);
List<ProductPricePlan> productPricePlanList = productPricePlanService.getPageLists(offset, limit);
List<Integer> sknList = productPricePlanList.stream().map(ProductPricePlan::getProductSkn).collect(Collectors.toList());
List<ProductPrice> productPriceList = productPriceMapper.selectBySkns(sknList);
Map<Integer,BigDecimal> productPriceMap = productPriceList.stream().collect(Collectors.toMap(ProductPrice::getProductSkn, ProductPrice::getVipDiscount));
productPricePlanList.stream().forEach(p->{
p.setVipDiscount(productPriceMap.get(p.getProductSkn()));
});
return productPricePlanList;
}
@Override
... ...
... ... @@ -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);
}
}
... ...
... ... @@ -48,6 +48,9 @@
},
"status": {
"type": "integer"
},
"vipDiscount": {
"type": "double"
}
}
}
... ...
... ... @@ -21,6 +21,7 @@ public class ProductPriceBO {
private Integer lastReducePriceTime;
private BigDecimal basicPrice;
private Integer productVipStatus;
private BigDecimal vipDiscount;
// others
private String isDiscount;
... ... @@ -48,9 +49,18 @@ public class ProductPriceBO {
this.lastReducePriceTime = productPrice.getLastReducePriceTime();
this.basicPrice = productPrice.getBasicPrice();
this.productVipStatus = productPrice.getProductVipStatus();
this.vipDiscount = productPrice.getVipDiscount();
}
public Integer getLastReducePriceTime() {
public BigDecimal getVipDiscount() {
return vipDiscount;
}
public void setVipDiscount(BigDecimal vipDiscount) {
this.vipDiscount = vipDiscount;
}
public Integer getLastReducePriceTime() {
return lastReducePriceTime;
}
... ...
... ... @@ -162,9 +162,18 @@ public class ProductPriceBuilder implements ViewBuilder {
vip3Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice) * 0.88));
break;
case 2:
vip1Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice) * 0.95));
vip2Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice) * 0.95));
vip3Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice) * 0.95));
boolean gt0 = productPriceBO.getVipDiscount().compareTo(BigDecimal.ZERO)==1;
boolean lt1 = productPriceBO.getVipDiscount().compareTo(BigDecimal.ONE)==-1;
if(productPriceBO.getVipDiscount()!=null&&gt0&&lt1){
BigDecimal price = productPriceBO.getVipDiscount().multiply(BigDecimal.valueOf(Math.ceil(salesPrice)));
vip1Price = Double.parseDouble(String.format("%.2f", price));
vip2Price = Double.parseDouble(String.format("%.2f", price));
vip3Price = Double.parseDouble(String.format("%.2f", price));
}else{
vip1Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice) * 0.95));
vip2Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice) * 0.95));
vip3Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice) * 0.95));
}
break;
case 3:
vip1Price = Double.parseDouble(String.format("%.2f", Math.ceil(salesPrice)));
... ...