CollageActivityIndexBaseService.java
6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.yoho.search.service.index;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.yoho.search.base.utils.DateUtil;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.common.BaseService;
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.common.SearchRequestCollageParams;
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 java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Service
public class CollageActivityIndexBaseService extends BaseService {
private static final Logger logger = LoggerFactory.getLogger(CollageActivityIndexBaseService.class);
private static final List<String> collageActivityParamsList = Arrays.asList(
SearchRequestCollageParams.COLLAGE_PARAM_ACTIVITYID,
SearchRequestCollageParams.COLLAGE_PARAM_OPENLIMIT,
SearchRequestCollageParams.COLLAGE_PARAM_JOINLIMIT,
SearchRequestCollageParams.COLLAGE_PARAM_ISAUTO,
SearchRequestCollageParams.COLLAGE_PARAM_PAYLIMIT,
SearchRequestCollageParams.COLLAGE_PARAM_PRODUCTLIMIT,
SearchRequestCollageParams.COLLAGE_PARAM_PEOPLENUMLIMIT);
@Autowired
private SearchCommonService searchCommonService;
private Map<String, Object> getCollageActivityInfo(Map<String, Object> esMap) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("activity_id", MapUtils.getIntValue(esMap, "activityId", 0));
map.put("begin_time", MapUtils.getIntValue(esMap, "beginTime", 0));
map.put("end_time", MapUtils.getIntValue(esMap, "endTime", 0));
map.put("people_num", MapUtils.getIntValue(esMap, "peopleNum", 0));
map.put("join_limit", MapUtils.getIntValue(esMap, "joinLimit", 0));
map.put("open_limit", MapUtils.getIntValue(esMap, "openLimit", 0));
map.put("product_limit", MapUtils.getIntValue(esMap, "productLimit", 0));
map.put("pay_limit", MapUtils.getIntValue(esMap, "payLimit", 0));
return map;
}
//Guava Cache
LoadingCache<String, Map<Integer, Map<String, Object>>> allValidActivitysCache = CacheBuilder.newBuilder()
.maximumSize(10).expireAfterWrite(1, TimeUnit.MINUTES).build(new CacheLoader<String, Map<Integer, Map<String, Object>>>() {
public Map<Integer, Map<String, Object>> load(String key) {
//默认查询全部
Map<String, String> paramMap = new HashMap<>();
paramMap.put(SearchRequestCollageParams.COLLAGE_PARAM_SHOW_TYPE, "3");
return queryValidActivitys(paramMap);
}
});
public Map<Integer, Map<String, Object>> queryAllValidActivitys() {
try {
return allValidActivitysCache.get("AllValidActivitys");
} catch (Exception e) {
return new HashMap<>();
}
}
/**
* 生成时间过滤参数
*
* @param paramMap
* @return
*/
private BoolQueryBuilder genBeginTimeFilter(Map<String, String> paramMap) {
long current = DateUtil.getCurrentTimeSecond();
//1、生成正在开始的过滤条件
BoolQueryBuilder hasBeginFilter = QueryBuilders.boolQuery();
hasBeginFilter.must(QueryBuilders.rangeQuery("beginTime").lte(current));//过滤活动开始时间
hasBeginFilter.must(QueryBuilders.rangeQuery("endTime").gte(current));//过滤活动结束时间
//2、即将开始的过滤条件
BoolQueryBuilder notBeginFilter = QueryBuilders.boolQuery();
notBeginFilter.must(QueryBuilders.rangeQuery("beginTime").gte(current));//过滤活动开始时间
notBeginFilter.must(QueryBuilders.rangeQuery("endTime").gte(current));//过滤活动结束时间
//3、执行参数判断
String showType = MapUtils.getString(paramMap, SearchRequestCollageParams.COLLAGE_PARAM_SHOW_TYPE, "1");
if ("1".equalsIgnoreCase(showType)) {
return hasBeginFilter;
}
if ("2".equalsIgnoreCase(showType)) {
return notBeginFilter;
}
return QueryBuilders.boolQuery().should(hasBeginFilter).should(notBeginFilter);
}
/**
* 根据参数查询【正在生效或已经生效】的活动信息
*
* @param paramMap
* @return
*/
public Map<Integer, Map<String, Object>> queryValidActivitys(Map<String, String> paramMap) {
try {
SearchParam searchParam = new SearchParam();
//1、生成filter
BoolQueryBuilder filter = QueryBuilders.boolQuery();
searchParam.setFiter(filter);
//1.1、按参数过滤
addTermQueryBatch(paramMap, filter, collageActivityParamsList);
//1.2、默认状态为1
filter.must(QueryBuilders.termQuery("status", 1));//过滤状态为1
//1.3、生成时间参数
filter.must(this.genBeginTimeFilter(paramMap));
//2、设置分页-默认认为正在进行的拼团活动不超过100个
searchParam.setOffset(0);
searchParam.setSize(100);
//3、执行搜索
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_COLLAGE_ACTIVITY, searchParam);
List<Map<String, Object>> collageActivitys = searchResult.getResultList();
//4、生成结果
Map<Integer, Map<String, Object>> result = new HashMap<>();
for (Map<String, Object> collageActivity : collageActivitys) {
Map<String, Object> bo = this.getCollageActivityInfo(collageActivity);
result.put(MapUtils.getIntValue(bo, "activity_id"), bo);
}
return result;
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new HashMap<>();
}
}
}