|
|
package com.yoho.search.service.index;
|
|
|
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.common.SearchCommonService;
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* Created by wangnan on 2017/9/12.
|
|
|
*/
|
|
|
@Service
|
|
|
public class CustomizeTagBaseService {
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
private static final String ACTIVITY_TAG_INDEX_NAME = ISearchConstants.INDEX_NAME_ACTIVITY_TAG;
|
|
|
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
|
|
|
|
|
|
public List<Map<String, Object>> getTagListByIds(final Collection<?> tagIds) {
|
|
|
List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
|
|
|
try {
|
|
|
List<Map<String, Object>> sizeEsMapList = searchCommonService.doMultiGetCommon(ACTIVITY_TAG_INDEX_NAME, tagIds);
|
|
|
for (Map<String, Object> sizeEsMap : sizeEsMapList) {
|
|
|
resultList.add(this.getTagMap(sizeEsMap));
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
|
}
|
|
|
return resultList;
|
|
|
}
|
|
|
|
|
|
private Map<String, Object> getTagMap(Map<String, Object> esMap) {
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
map.put("id", MapUtils.getIntValue(esMap, "id", 0));
|
|
|
map.put("name", MapUtils.getString(esMap, "activityName", ""));
|
|
|
return map;
|
|
|
}
|
|
|
} |
|
|
package com.yoho.search.service.index;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.google.common.cache.CacheBuilder;
|
|
|
import com.google.common.cache.CacheLoader;
|
|
|
import com.google.common.cache.LoadingCache;
|
|
|
import com.yoho.search.base.utils.ConvertUtils;
|
|
|
import com.yoho.search.base.utils.DateUtil;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.common.SearchCommonService;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.core.es.model.SearchResult;
|
|
|
import com.yoho.search.dal.model.CustomizeTag;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.Assert;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* Created by wangnan on 2017/9/12.
|
|
|
*/
|
|
|
@Service
|
|
|
public class CustomizeTagBaseService {
|
|
|
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
private static final String CACHE_KEY = "CacheKey";
|
|
|
private static final String POOLID_CUSTOMIZETAG_MAP_CACHE_KEY = "poolIdCustomizeTagMapCacheKey";
|
|
|
private static final String PRODUCTINDEX_FIELD_POOLIDS = "pool_ids";
|
|
|
|
|
|
//Guava Cache
|
|
|
LoadingCache<String, JSONObject> customizeTagCache = CacheBuilder.newBuilder()
|
|
|
.maximumSize(30000)
|
|
|
.expireAfterWrite(5, TimeUnit.MINUTES)
|
|
|
.build(new CacheLoader<String, JSONObject>() {
|
|
|
public JSONObject load(String key) {
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
jsonObject.put(POOLID_CUSTOMIZETAG_MAP_CACHE_KEY, buildCustomizeTagMap());
|
|
|
return jsonObject;
|
|
|
}
|
|
|
});
|
|
|
|
|
|
/**
|
|
|
* 给列表中的每个skn加入自定义标签节点
|
|
|
*/
|
|
|
public void fillCustomizeTag(List<Map<String, Object>> productList) {
|
|
|
try {
|
|
|
//1、判断
|
|
|
if (CollectionUtils.isEmpty(productList)) {
|
|
|
return;
|
|
|
}
|
|
|
//2、获取poolId和customizeTag的map,缓存5分钟
|
|
|
Map<String, List<CustomizeTag>> customizeTagMap = this.getCustomizeTagMapFromCache();
|
|
|
if (customizeTagMap.isEmpty()) {
|
|
|
return;
|
|
|
}
|
|
|
//3、遍历product列表,插入customizeTag
|
|
|
for (Map<String, Object> productMap : productList) {
|
|
|
String poolIds = MapUtils.getString(productMap, PRODUCTINDEX_FIELD_POOLIDS, "");
|
|
|
if (StringUtils.isEmpty(poolIds)) {
|
|
|
continue;
|
|
|
}
|
|
|
List<JSONObject> customizeTags = this.buildCustomizeTag(poolIds, customizeTagMap);
|
|
|
productMap.put("customize_tag_new", customizeTags);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 从缓存中获取poolId->CustomizeTags映射,用于构建列表customizeTag
|
|
|
*/
|
|
|
private Map<String, List<CustomizeTag>> getCustomizeTagMapFromCache() throws Exception {
|
|
|
JSONObject jsonObject = customizeTagCache.get(CACHE_KEY);
|
|
|
Assert.isTrue(jsonObject != null, "[cache jsonObject cannot be null][fun=getCustomizeTagMapFromCache]");
|
|
|
return (Map<String, List<CustomizeTag>>) jsonObject.get(POOLID_CUSTOMIZETAG_MAP_CACHE_KEY);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 根据poolIds生成自定义标签节点
|
|
|
*/
|
|
|
private List<JSONObject> buildCustomizeTag(String poolIds, Map<String, List<CustomizeTag>> customizeTagBOMap) throws Exception {
|
|
|
String[] poolIdArray = poolIds.split(",");
|
|
|
if (poolIdArray.length == 0) {
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
List<String> poolIdList = Arrays.asList(poolIdArray);
|
|
|
List<JSONObject> customizeTagList = new ArrayList<>();
|
|
|
for (String poolId : poolIdList) {
|
|
|
List<CustomizeTag> customizeTagBOS = customizeTagBOMap.get(poolId);
|
|
|
if (CollectionUtils.isEmpty(customizeTagBOS)) {
|
|
|
continue;
|
|
|
}
|
|
|
//按照开始时间排序
|
|
|
List<CustomizeTag> sortedCustomizeTagList = customizeTagBOS.stream().sorted(Comparator.comparingInt(CustomizeTag::getBeginTime).reversed()).collect(Collectors.toList());
|
|
|
if (CollectionUtils.isEmpty(sortedCustomizeTagList)) {
|
|
|
continue;
|
|
|
}
|
|
|
//取第一个
|
|
|
CustomizeTag customizeTag = sortedCustomizeTagList.get(0);
|
|
|
JSONObject tag = new JSONObject();
|
|
|
tag.put("id", customizeTag.getId());
|
|
|
tag.put("name", customizeTag.getActivityName());
|
|
|
tag.put("url", customizeTag.getTagUrl());
|
|
|
customizeTagList.add(tag);
|
|
|
}
|
|
|
return customizeTagList;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 构建poolId->ActivityTagBO Map
|
|
|
*/
|
|
|
private Map<String, List<CustomizeTag>> buildCustomizeTagMap() {
|
|
|
try {
|
|
|
Map<String, List<CustomizeTag>> customizeTagBOMap = new HashMap();
|
|
|
List<CustomizeTag> customizeTagList = getCustomizeTags(false, null);
|
|
|
if (CollectionUtils.isEmpty(customizeTagList)) {
|
|
|
return new HashMap<>();
|
|
|
}
|
|
|
for (CustomizeTag customizeTag : customizeTagList) {
|
|
|
String poolIds = customizeTag.getPoolId();
|
|
|
String[] poolIdsArray = poolIds.split(",");
|
|
|
if (poolIdsArray == null) {
|
|
|
continue;
|
|
|
}
|
|
|
for (int i = 0; i < poolIdsArray.length; i++) {
|
|
|
if (customizeTagBOMap.containsKey(poolIdsArray[i])) {
|
|
|
List<CustomizeTag> customizeTags = customizeTagBOMap.get(poolIdsArray[i]);
|
|
|
customizeTags.add(customizeTag);
|
|
|
} else {
|
|
|
List<CustomizeTag> customizeTags = new ArrayList<>();
|
|
|
customizeTags.add(customizeTag);
|
|
|
customizeTagBOMap.put(poolIdsArray[i], customizeTags);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return customizeTagBOMap;
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
|
return new HashMap<>();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查CustomizeTag索引,可以查指定的poolId的
|
|
|
*/
|
|
|
private List<CustomizeTag> getCustomizeTags(boolean filterPoolId, String poolIdsString) throws Exception {
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
|
|
if (filterPoolId) {
|
|
|
if (StringUtils.isEmpty(poolIdsString)) {
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
int[] poolIds = ConvertUtils.stringToIntArray(poolIdsString, ",");
|
|
|
boolQueryBuilder.must(QueryBuilders.termsQuery("poolId", poolIds));
|
|
|
}
|
|
|
boolQueryBuilder.must(QueryBuilders.termQuery("status", 1));
|
|
|
long currentTime = DateUtil.getCurrentTimeSecond();
|
|
|
boolQueryBuilder.must(QueryBuilders.rangeQuery("beginTime").lt(currentTime));
|
|
|
boolQueryBuilder.must(QueryBuilders.rangeQuery("endTime").gt(currentTime));
|
|
|
searchParam.setFiter(boolQueryBuilder);
|
|
|
searchParam.setSize(10000);
|
|
|
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_CUSTOMIZE_TAG, searchParam);
|
|
|
if (searchResult == null || CollectionUtils.isEmpty(searchResult.getResultList())) {
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
List<CustomizeTag> resultList = new ArrayList<>();
|
|
|
for (Map<String, Object> esMap : searchResult.getResultList()) {
|
|
|
if (!esMap.isEmpty()) {
|
|
|
resultList.add(parseCustomizeTagInfo(esMap));
|
|
|
}
|
|
|
}
|
|
|
return resultList;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* CustomizeTag索引数据映射到BO
|
|
|
*/
|
|
|
private CustomizeTag parseCustomizeTagInfo(Map<String, Object> esMap) {
|
|
|
CustomizeTag customizeTagBO = new CustomizeTag();
|
|
|
customizeTagBO.setId(MapUtils.getIntValue(esMap, "id", 0));
|
|
|
customizeTagBO.setActivityName(MapUtils.getString(esMap, "activityName", ""));
|
|
|
customizeTagBO.setTagUrl(MapUtils.getString(esMap, "tagUrl", ""));
|
|
|
customizeTagBO.setPoolId(MapUtils.getString(esMap, "poolId", ""));
|
|
|
customizeTagBO.setBeforeImgUrl(MapUtils.getString(esMap, "beforeImgUrl", ""));
|
|
|
customizeTagBO.setAfterImgUrl(MapUtils.getString(esMap, "afterImgUrl", ""));
|
|
|
customizeTagBO.setBeginTime(MapUtils.getIntValue(esMap, "beginTime", 0));
|
|
|
customizeTagBO.setEndTime(MapUtils.getIntValue(esMap, "endTime", 0));
|
|
|
return customizeTagBO;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 用于聚合里返回自定义标签
|
|
|
*/
|
|
|
public List<Map<String, Object>> getTagListByIds(List<String> poolIdList) {
|
|
|
try {
|
|
|
String poolIdsString = poolIdList.stream().collect(Collectors.joining(","));
|
|
|
List<CustomizeTag> customizeTagBOList = this.getCustomizeTags(true, poolIdsString);
|
|
|
if (CollectionUtils.isEmpty(customizeTagBOList)) {
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
List<CustomizeTag> sortedCustomizeTags = customizeTagBOList.stream().sorted(Comparator.comparingInt(CustomizeTag::getBeginTime).reversed()).collect(Collectors.toList());
|
|
|
if (CollectionUtils.isEmpty(sortedCustomizeTags)) {
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
List<Map<String, Object>> resultList = new ArrayList<>();
|
|
|
sortedCustomizeTags.stream().forEach(p -> resultList.add(this.getTagMap(p)));
|
|
|
return resultList;
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
|
}
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
|
|
|
private Map<String, Object> getTagMap(CustomizeTag customizeTagBO) {
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
map.put("id", customizeTagBO.getPoolId());
|
|
|
map.put("name", customizeTagBO.getActivityName());
|
|
|
map.put("beforeImgUrl", customizeTagBO.getBeforeImgUrl());
|
|
|
map.put("afterImgUrl", customizeTagBO.getAfterImgUrl());
|
|
|
return map;
|
|
|
}
|
|
|
} |
...
|
...
|
|