Authored by hugufei

fix ProductCustomizeTagBuilder

... ... @@ -38,7 +38,7 @@ public class ActivityTagMqListener extends AbstractIndexMqListener {
@Override
protected void deleteData(String id) throws Exception {
activityTagService.delete(Integer.valueOf(id));
productCustomizeTagBuilder.buildActivityTagsMap();
productCustomizeTagBuilder.reloadActivityTagMap();
indexService.deleteIndexData(this.getIndexName(), id);
}
... ... @@ -49,7 +49,7 @@ public class ActivityTagMqListener extends AbstractIndexMqListener {
return;
}
activityTagService.saveOrUpdate(activityTag);
productCustomizeTagBuilder.buildActivityTagsMap();
productCustomizeTagBuilder.reloadActivityTagMap();
JSONObject jsonObject = new JSONObject();
jsonObject.put("id",activityTag.getId());
jsonObject.put("activityName",activityTag.getActivityName());
... ...
... ... @@ -92,19 +92,19 @@ public class ProductMqListener extends AbstractIndexMqListener {
logger.info("[step(3/5)=getProductIBO][productId={}][cost={}ms]", productId, costStatistics.getCost());
// 2、构建ProductPrice
ProductPriceBO productPriceBO = productPriceBOLogicService.buildProductPriceBOWithNew(productSkn, productIBO.getFirstShelveTime(),productIBO.getSellType());
ProductPriceBO productPriceBO = productPriceBOLogicService.buildProductPriceBOWithNew(productSkn, productIBO.getFirstShelveTime(),
productIBO.getSellType());
logger.info("[step(4/5)=getProductPriceBO][productId={}][cost={}ms]", productId, costStatistics.getCost());
// 3、构建更新对象
JSONObject jsonObject = (JSONObject) JSON.toJSON(productIBO);
if (!isUpdate) {
jsonObject.put("salesNum", 0);
}
if(productPriceBO!=null){
if (productPriceBO != null) {
jsonObject.putAll((JSONObject) JSON.toJSON(productPriceBO));
}
jsonObject.put("customizeTags", productCustomizeTagBuilder.buildCustomizeTagByProductId(productId));
jsonObject.put("customizeTags", productCustomizeTagBuilder.getCustomizeTags(productIBO.getIspromotion()));
// 4、更新到ES
try {
this.updateProductIndexWithDataMap(jsonObject, productId);
... ...
package com.yoho.search.consumer.service.logicService.productIndex;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.consumer.service.bo.ProductIndexBO;
... ... @@ -7,16 +21,6 @@ import com.yoho.search.dal.ActivityTagMapper;
import com.yoho.search.dal.ProductMapper;
import com.yoho.search.dal.model.ActivityTag;
import com.yoho.search.dal.model.Product;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by wangnan on 2017/9/11.
... ... @@ -24,76 +28,84 @@ import java.util.Map;
@Component
public class ProductCustomizeTagBuilder implements IndexFieldBuilder {
@Autowired
private ActivityTagMapper activityTagMapper;
@Autowired
private ProductMapper productMapper;
private static final Logger logger = LoggerFactory.getLogger(ProductCustomizeTagBuilder.class);
private static Map<Integer, ActivityTag> activityTagsMap = new HashMap<>();
@Autowired
private ActivityTagMapper activityTagMapper;
@Autowired
private ProductMapper productMapper;
@PostConstruct
public void buildActivityTagsMap() throws BeansException {
activityTagsMap.clear();
List<ActivityTag> activityTagList = activityTagMapper.selectLists();
if (CollectionUtils.isEmpty(activityTagList)) {
return;
}
for (ActivityTag activityTag : activityTagList) {
String[] isPromotionIdArray = activityTag.getIsPromotionIds().split(",");
if (isPromotionIdArray.length == 0) {
continue;
}
for (String promotionId : isPromotionIdArray) {
if (StringUtils.isNotBlank(promotionId)) {
activityTagsMap.put(Integer.valueOf(promotionId), activityTag);
}
}
}
}
private Map<Integer, List<ActivityTag>> activityTagsMap = new HashMap<Integer, List<ActivityTag>>();
@PostConstruct
private void buildActivityTagsMap(){
reloadActivityTagMap();
}
public void reloadActivityTagMap() {
this.activityTagsMap = getActivityTagMap();
}
@Override
public void build(List<ProductIndexBO> productIndexBOs, List<Integer> ids, List<Integer> sknList) {
List<Product> productList = productMapper.selectListByIds(ids);
if (CollectionUtils.isEmpty(productList)) {
return;
}
productIndexBOs.stream().forEach(productIndexBO -> {
ActivityTag activityTag = activityTagsMap.get(productIndexBO.getIspromotion());
if (activityTag != null) {
JSONObject customizeTag = new JSONObject();
customizeTag.put("id", activityTag.getId());
customizeTag.put("name", activityTag.getActivityName());
customizeTag.put("url", activityTag.getTagUrl());
JSONArray customizeTags = new JSONArray();
customizeTags.add(customizeTag);
productIndexBO.setCustomizeTags(customizeTags);
}
});
}
private Map<Integer, List<ActivityTag>> getActivityTagMap() {
Map<Integer, List<ActivityTag>> tempMap = new HashMap<Integer, List<ActivityTag>>();
List<ActivityTag> activityTagList = activityTagMapper.selectLists();
if (CollectionUtils.isEmpty(activityTagList)) {
return tempMap;
}
for (ActivityTag activityTag : activityTagList) {
String isPromotionIds = activityTag.getIsPromotionIds();
if (StringUtils.isBlank(isPromotionIds)) {
continue;
}
String[] isPromotionIdArray = isPromotionIds.split(",");
for (String promotionIdStr : isPromotionIdArray) {
try {
Integer promotionId = Integer.valueOf(promotionIdStr);
List<ActivityTag> activityTags = tempMap.get(promotionId);
if (activityTags == null) {
activityTags = new ArrayList<ActivityTag>();
tempMap.put(promotionId, activityTags);
}
activityTags.add(activityTag);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
}
return tempMap;
}
public JSONArray buildCustomizeTagByProductId(Integer productId) {
Product product = productMapper.selectByPrimaryKey(productId);
if (product == null) {
return new JSONArray(0);
}
Integer promotionId = product.getIsPromotion();
if (promotionId == null) {
return new JSONArray(0);
}
@Override
public void build(List<ProductIndexBO> productIndexBOs, List<Integer> ids, List<Integer> sknList) {
List<Product> productList = productMapper.selectListByIds(ids);
if (CollectionUtils.isEmpty(productList)) {
return;
}
productIndexBOs.stream().forEach(productIndexBO -> {
JSONArray customizeTags = getCustomizeTags(productIndexBO.getIspromotion());
productIndexBO.setCustomizeTags(customizeTags);
});
}
ActivityTag activityTag = activityTagsMap.get(promotionId);
if (activityTag != null) {
JSONObject customizeTag = new JSONObject();
customizeTag.put("id", activityTag.getId());
customizeTag.put("name", activityTag.getActivityName());
customizeTag.put("url", activityTag.getTagUrl());
JSONArray customizeTags = new JSONArray();
customizeTags.add(customizeTag);
public JSONArray getCustomizeTags(Integer promotionId) {
JSONArray customizeTags = new JSONArray();
List<ActivityTag> activityTags = activityTagsMap.get(promotionId);
if (activityTags == null) {
return customizeTags;
}
for (ActivityTag activityTag : activityTags) {
JSONObject customizeTag = new JSONObject();
customizeTag.put("id", activityTag.getId());
customizeTag.put("name", activityTag.getActivityName());
customizeTag.put("url", activityTag.getTagUrl());
customizeTags.add(customizeTag);
}
return customizeTags;
}
return customizeTags;
}
return new JSONArray(0);
}
public static void main(String[] args) {
String a = "";
System.out.println(a.split(",").length);
}
}
... ...