|
|
package com.yoho.search.service.scene.general;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
|
|
|
import com.yoho.error.event.SearchEvent;
|
|
|
import com.yoho.search.aop.cache.SearchCacheAble;
|
|
|
import com.yoho.search.base.utils.EventReportEnum;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.cache.CacheTimeConstants;
|
|
|
import com.yoho.search.cache.beans.AbstractPageComponent;
|
|
|
import com.yoho.search.common.SearchCommonService;
|
|
|
import com.yoho.search.common.utils.SearchApiResultUtils;
|
|
|
import com.yoho.search.core.es.agg.IAggregation;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.core.es.model.SearchResult;
|
...
|
...
|
@@ -14,6 +15,7 @@ import com.yoho.search.core.es.utils.IgnoreSomeException; |
|
|
import com.yoho.search.models.SearchApiResult;
|
|
|
import com.yoho.search.service.aggregations.impls.AggregationFactory;
|
|
|
import com.yoho.search.service.helper.SearchParamHelper;
|
|
|
import com.yoho.search.service.recall.models.common.ParamQueryFilter;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
...
|
...
|
@@ -26,7 +28,7 @@ import java.util.Arrays; |
|
|
import java.util.Map;
|
|
|
|
|
|
@Service
|
|
|
public class DiscountService implements ApplicationEventPublisherAware {
|
|
|
public class DiscountService extends AbstractPageComponent<JSONObject> implements ApplicationEventPublisherAware {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(DiscountService.class);
|
|
|
|
...
|
...
|
@@ -44,46 +46,38 @@ public class DiscountService implements ApplicationEventPublisherAware { |
|
|
this.publisher = applicationEventPublisher;
|
|
|
}
|
|
|
|
|
|
@SearchCacheAble(cacheInMinute = 30, cacheName = "DISCOUNT", excludeParams = {"client_type", "yh_channel", "order", "uid", "udid"})
|
|
|
public SearchApiResult discount(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
// 构造查询参数
|
|
|
SearchParam searchParam = searchParamHelper.buildDefault(paramMap);
|
|
|
|
|
|
// 按1-3,4-6,7-10折的规则进行聚合[按区间聚合]
|
|
|
IAggregation discountAggregation = aggregationFactory.getDiscountAggregation();
|
|
|
searchParam.setAggregationBuilders(Arrays.asList(discountAggregation.getBuilder()));
|
|
|
searchParam.setSize(0);
|
|
|
|
|
|
// 构造返回结果
|
|
|
SearchApiResult searchApiResult = new SearchApiResult().setMessage("discount List.");
|
|
|
|
|
|
// es检索
|
|
|
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
|
|
|
if (searchResult == null || searchResult.getAggMaps() == null) {
|
|
|
return searchApiResult.setData("");
|
|
|
JSONObject discountObject = discount(paramMap, null);
|
|
|
return new SearchApiResult().setMessage("discount List.").setData(discountObject);
|
|
|
}
|
|
|
Object discountAggResult = discountAggregation.getAggregationResponseMap(searchResult.getAggMaps());
|
|
|
if (discountAggResult == null) {
|
|
|
return searchApiResult.setData("");
|
|
|
}
|
|
|
JSONObject discountObject = new JSONObject();
|
|
|
discountObject.put("discount", discountAggResult);
|
|
|
return searchApiResult.setData(discountObject);
|
|
|
|
|
|
public JSONObject discount(Map<String, String> paramMap, BoolQueryBuilder mustFilter) {
|
|
|
try {
|
|
|
ParamQueryFilter paramQueryFilter = searchParamHelper.buildParamQueryFilter(paramMap, mustFilter);
|
|
|
Object value = super.queryWithCache(paramQueryFilter, JSONObject.class);
|
|
|
return value == null ? null : (JSONObject) value;
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
|
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getEventName(), EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getFunctionName(),
|
|
|
EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
|
|
|
return SearchApiResultUtils.errorSearchApiResult("discount", paramMap, e);
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public JSONObject discount(Map<String, String> paramMap, BoolQueryBuilder mustFilter) {
|
|
|
try {
|
|
|
logger.info("[func=discount][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
|
|
|
@Override
|
|
|
protected RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter) {
|
|
|
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("DISCOUNT:").appendVar(paramQueryFilter.getParamMd5Key());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected int cacheTimeInMinute() {
|
|
|
return CacheTimeConstants.CACHE_30_MINUTE;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected JSONObject doRealQuery(ParamQueryFilter paramQueryFilter) {
|
|
|
// 构造查询参数
|
|
|
SearchParam searchParam = searchParamHelper.buildWithMustFilter(paramMap, mustFilter);
|
|
|
SearchParam searchParam = searchParamHelper.buildSearchParam(paramQueryFilter);
|
|
|
|
|
|
// 按1-3,4-6,7-10折的规则进行聚合[按区间聚合]
|
|
|
IAggregation discountAggregation = aggregationFactory.getDiscountAggregation();
|
...
|
...
|
@@ -103,11 +97,5 @@ public class DiscountService implements ApplicationEventPublisherAware { |
|
|
JSONObject discountObject = new JSONObject();
|
|
|
discountObject.put("discount", discountAggResult);
|
|
|
return discountObject.getJSONObject("discount");
|
|
|
} catch (Exception e) {
|
|
|
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getEventName(), EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getFunctionName(),
|
|
|
EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|