|
|
package com.yoho.search.service.scene.others;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.error.event.SearchEvent;
|
|
|
import com.yoho.search.base.utils.DateUtil;
|
|
|
import com.yoho.search.base.utils.EventReportEnum;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.common.BaseService;
|
|
|
import com.yoho.search.common.SearchCommonService;
|
|
|
import com.yoho.search.common.SearchRequestParams;
|
|
|
import com.yoho.search.common.utils.SearchApiResultUtils;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.core.es.model.SearchResult;
|
|
|
import com.yoho.search.core.es.utils.IgnoreSomeException;
|
|
|
import com.yoho.search.models.SearchApiResult;
|
|
|
import com.yoho.search.service.recall.beans.cache.SknReturnInfoCacheBean;
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
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.context.ApplicationEventPublisher;
|
|
|
import org.springframework.context.ApplicationEventPublisherAware;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 拼团购
|
|
|
*
|
|
|
* @author wangnan
|
|
|
* @version 2018/9/4
|
|
|
*/
|
|
|
@Service
|
|
|
public class CollageService extends BaseService implements ApplicationEventPublisherAware {
|
|
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
|
|
|
private ApplicationEventPublisher publisher;
|
|
|
|
|
|
private static final List<String> paramsList = Arrays.asList(
|
|
|
SearchRequestParams.COLLAGE_PARAM_ACTIVITYID,
|
|
|
SearchRequestParams.COLLAGE_PARAM_PRODUCTSKN,
|
|
|
SearchRequestParams.COLLAGE_PARAM_ORDERBY,
|
|
|
SearchRequestParams.COLLAGE_PARAM_OPENLIMIT,
|
|
|
SearchRequestParams.COLLAGE_PARAM_JOINLIMIT,
|
|
|
SearchRequestParams.COLLAGE_PARAM_JOINLIMIT,
|
|
|
SearchRequestParams.COLLAGE_PARAM_ISAUTO,
|
|
|
SearchRequestParams.COLLAGE_PARAM_PAYLIMIT,
|
|
|
SearchRequestParams.COLLAGE_PARAM_PRODUCTLIMIT);
|
|
|
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
@Autowired
|
|
|
private SknReturnInfoCacheBean sknReturnInfoCacheBean;
|
|
|
|
|
|
@Override
|
|
|
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
|
|
this.publisher = applicationEventPublisher;
|
|
|
}
|
|
|
|
|
|
public SearchApiResult searchList(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
logger.info("[class=CollageService][func=searchList][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
|
|
|
|
|
|
// 1.构造查询参数
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
|
|
|
int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
|
|
|
if (page < 1 || pageSize < 0) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("分页参数错误");
|
|
|
}
|
|
|
if (pageSize > 100) {
|
|
|
pageSize = 100;
|
|
|
}
|
|
|
searchParam.setOffset((page - 1) * pageSize);
|
|
|
searchParam.setSize(pageSize);
|
|
|
|
|
|
//2.配置Filter
|
|
|
BoolQueryBuilder boolFilter = constructHelperFilterBuilder(paramMap);
|
|
|
searchParam.setFiter(boolFilter);
|
|
|
|
|
|
final String indexName = ISearchConstants.INDEX_NAME_COLLAGE;
|
|
|
|
|
|
//3.查询ES
|
|
|
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
|
|
|
SearchApiResult searchApiResult = new SearchApiResult();
|
|
|
if (searchResult == null) {
|
|
|
return searchApiResult.setCode(500);
|
|
|
}
|
|
|
|
|
|
//4.关联ProductIndex信息
|
|
|
List<Integer> productSknList = new ArrayList<>(searchResult.getResultList().size());
|
|
|
List<Map<String, Object>> tempList = getTempMap(searchResult.getResultList(), productSknList);
|
|
|
List<Map<String, Object>> sknInfoList = sknReturnInfoCacheBean.queryProductListBySkn(productSknList, productSknList.size());
|
|
|
Map<Integer, Map<String, Object>> sknInfoMap = new HashMap<>();
|
|
|
sknInfoList.stream().forEach(p -> sknInfoMap.put(MapUtils.getInteger(p, "product_skn"), p));
|
|
|
tempList.stream().forEach(p -> {
|
|
|
Integer productSkn = MapUtils.getInteger(p, "product_skn");
|
|
|
Map<String, Object> sknMap = sknInfoMap.get(productSkn);
|
|
|
p.put("product_name", MapUtils.getString(sknMap, "product_name", ""));
|
|
|
p.put("sales_price", MapUtils.getDoubleValue(sknMap, "sales_price"));
|
|
|
p.put("market_price", MapUtils.getDoubleValue(sknMap, "market_price"));
|
|
|
p.put("sales_num", MapUtils.getIntValue(sknMap, "sales_num"));
|
|
|
});
|
|
|
Map<Map<String, Object>, Integer> salesNumSknInfoMap = tempList.stream().collect(Collectors.toMap(p -> p, p -> MapUtils.getInteger(p, "sales_num")));
|
|
|
Map<Map<String, Object>,Integer> finalMap = new LinkedHashMap<>();
|
|
|
salesNumSknInfoMap.entrySet().stream().sorted(Map.Entry.comparingByValue()).forEachOrdered(e -> finalMap.put(e.getKey(), e.getValue()));
|
|
|
List<Map<String, Object>> finalList = new ArrayList<>(finalMap.size());
|
|
|
Iterator iterator = finalMap.entrySet().iterator();
|
|
|
while (iterator.hasNext()) {
|
|
|
Map.Entry entry = (Map.Entry) iterator.next();
|
|
|
finalList.add((Map<String, Object>) entry.getKey());
|
|
|
}
|
|
|
Collections.reverse(finalList);
|
|
|
// 4.构造返回结果并加入缓存
|
|
|
JSONObject dataMap = new JSONObject();
|
|
|
dataMap.put("total", searchResult.getTotal());
|
|
|
dataMap.put("page_size", searchParam.getSize());
|
|
|
dataMap.put("page_total", searchResult.getTotalPage());
|
|
|
dataMap.put("list", finalList);
|
|
|
return new SearchApiResult().setData(dataMap);
|
|
|
} catch (Exception e) {
|
|
|
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_IMAGEREPERTORY.getEventName(), EventReportEnum.SEARCHCONTROLLER_IMAGEREPERTORY.getFunctionName(),
|
|
|
EventReportEnum.SEARCHCONTROLLER_IMAGEREPERTORY.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
|
|
|
return SearchApiResultUtils.errorSearchApiResult(logger, paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private BoolQueryBuilder constructHelperFilterBuilder(Map<String, String> paramMap) throws Exception {
|
|
|
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
|
|
|
boolFilter.must(QueryBuilders.termQuery(SearchRequestParams.COLLAGE_PARAM_STATUS, 1));
|
|
|
boolFilter.must(QueryBuilders.termQuery(SearchRequestParams.COLLAGE_PARAM_ACTIVITYSTATUS, 1));
|
|
|
boolFilter.must(QueryBuilders.rangeQuery(SearchRequestParams.COLLAGE_PARAM_BEGINTIME).lte(DateUtil.getCurrentTimeSecond()));
|
|
|
boolFilter.must(QueryBuilders.rangeQuery(SearchRequestParams.COLLAGE_PARAM_ENDTIME).gte(DateUtil.getCurrentTimeSecond()));
|
|
|
addTermQueryBatch(paramMap, boolFilter, paramsList);
|
|
|
boolFilter = generalHandler(paramMap, boolFilter);
|
|
|
return boolFilter;
|
|
|
}
|
|
|
|
|
|
private List<Map<String, Object>> getTempMap(List<Map<String, Object>> resultList, List<Integer> productSknList) {
|
|
|
List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
|
|
|
for (Map<String, Object> map : resultList) {
|
|
|
Map<String, Object> productMap = getTempMap(map, productSknList);
|
|
|
returnList.add(productMap);
|
|
|
}
|
|
|
return returnList;
|
|
|
}
|
|
|
|
|
|
private Map<String, Object> getTempMap(Map<String, Object> map, List<Integer> productSknList) {
|
|
|
Map<String, Object> resultMap = new LinkedHashMap<>();
|
|
|
Integer productSkn = MapUtils.getIntValue(map, "productSkn");
|
|
|
productSknList.add(productSkn);
|
|
|
resultMap.put("product_skn", productSkn);
|
|
|
resultMap.put("people_num", MapUtils.getIntValue(map, "peopleNum"));
|
|
|
return resultMap;
|
|
|
}
|
|
|
} |
...
|
...
|
|