|
|
package com.yoho.search.service.scene.pages;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.search.base.utils.*;
|
|
|
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.dal.model.ZeroHelpProduct;
|
|
|
import com.yoho.search.models.SearchApiResult;
|
|
|
import com.yoho.search.service.helper.SearchParamHelper;
|
|
|
import com.yoho.search.service.index.ProductPricePlanIndexBaseService;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
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.elasticsearch.search.sort.SortBuilder;
|
|
|
import org.elasticsearch.search.sort.SortBuilders;
|
|
|
import org.elasticsearch.search.sort.SortOrder;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author wangnan
|
|
|
* @version 2019/8/16
|
|
|
*/
|
|
|
@Service
|
|
|
public class ZeroHelpProductPageSceneService extends AbstractPageSceneService {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ZeroHelpProductPageSceneService.class);
|
|
|
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
@Autowired
|
|
|
private SearchParamHelper searchParamHelper;
|
|
|
@Autowired
|
|
|
private ProductPricePlanIndexBaseService productPricePlanIndexBaseService;
|
|
|
|
|
|
@Override
|
|
|
public String pageId() {
|
|
|
return SearchPageIdDefine.PAGE_ID_ZERO_HELP;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult productList(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
|
|
|
SearchApiResult searchApiResult = new SearchApiResult();
|
|
|
logger.info("[class=ZeroHelpProductPageSceneService][func=productList][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
|
|
|
|
|
|
// 构造查询参数
|
|
|
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("分页参数错误");
|
|
|
}
|
|
|
|
|
|
// 构造searchParam
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
searchParam.setOffset((page - 1) * pageSize);
|
|
|
searchParam.setSize(pageSize > 100 ? 100 : pageSize);
|
|
|
|
|
|
// Filter
|
|
|
BoolQueryBuilder filter = this.buildFilter(paramMap);
|
|
|
searchParam.setFiter(filter);
|
|
|
|
|
|
// Sort
|
|
|
List<SortBuilder<?>> sortBuilders = new ArrayList<>();
|
|
|
sortBuilders.add(SortBuilders.fieldSort("orderBy").order(SortOrder.DESC));
|
|
|
sortBuilders.add(SortBuilders.fieldSort("id").order(SortOrder.DESC));
|
|
|
searchParam.setSortBuilders(sortBuilders);
|
|
|
|
|
|
// 执行搜索
|
|
|
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_ZERO_HELP_PRODUCT, searchParam);
|
|
|
|
|
|
// 获取skn列表
|
|
|
List<Map<String, Object>> results = searchResult.getResultList();
|
|
|
if (CollectionUtils.isEmpty(results)) {
|
|
|
return searchApiResult.setCode(500);
|
|
|
}
|
|
|
Map<Integer, ZeroHelpProduct> zeroHelpProductMap = results.stream().collect(Collectors.toMap(a -> MapUtils.getIntValue(a, "productSkn"), a -> JSON.parseObject(JSON.toJSONString(a), ZeroHelpProduct.class)));
|
|
|
Collection<Integer> productSknList = zeroHelpProductMap.keySet();
|
|
|
|
|
|
// 查询ProductIndex
|
|
|
SearchResult searchResultProductIndex = this.queryProductInfoMap(productSknList);
|
|
|
if (searchResultProductIndex == null) {
|
|
|
return searchApiResult.setCode(500);
|
|
|
}
|
|
|
|
|
|
// 构造商品返回顺序
|
|
|
List<Map<String, Object>> productList = new ArrayList<>();
|
|
|
for (Map<String, Object> map : searchResultProductIndex.getResultList()) {
|
|
|
Map<String, Object> productMap = new HashMap<>();
|
|
|
Integer productSkn = MapUtils.getIntValue(map, ProductIndexEsField.productSkn, 0);
|
|
|
productMap.put("productSkn", productSkn);
|
|
|
productMap.put("productName", MapUtils.getString(map, ProductIndexEsField.productName, ""));
|
|
|
productMap.put("defaultImages", MapUtils.getString(map, ProductIndexEsField.defaultImages, ""));
|
|
|
productMap.put("sales_price", MapUtils.getString(map, ProductIndexEsField.salesPrice, ""));
|
|
|
ZeroHelpProduct zeroHelpProduct = zeroHelpProductMap.get(productSkn);
|
|
|
if (zeroHelpProduct == null) {
|
|
|
continue;
|
|
|
}
|
|
|
productMap.put("id", zeroHelpProduct.getId());
|
|
|
productMap.put("begin_time", zeroHelpProduct.getBeginTime());
|
|
|
productMap.put("end_time", zeroHelpProduct.getEndTime());
|
|
|
productMap.put("target_price", zeroHelpProduct.getTargetPrice());
|
|
|
productMap.put("helper_num", zeroHelpProduct.getHelperNum());
|
|
|
productList.add(productMap);
|
|
|
}
|
|
|
|
|
|
//加入变价计划
|
|
|
productPricePlanIndexBaseService.fillProductPricePlanForCutDown(productList);
|
|
|
JSONObject result = buildReturnDataMap(searchResult.getTotal(), searchResult.getPage(), pageSize, searchResult.getTotalPage(), productList);
|
|
|
return new SearchApiResult().setData(result);
|
|
|
} catch (Exception e) {
|
|
|
return SearchApiResultUtils.errorSearchApiResult(logger, paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private SearchResult queryProductInfoMap(Collection<Integer> sknList) {
|
|
|
try {
|
|
|
if (CollectionUtils.isEmpty(sknList)) {
|
|
|
return new SearchResult();
|
|
|
}
|
|
|
BoolQueryBuilder filter = QueryBuilders.boolQuery();
|
|
|
filter.must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, sknList));
|
|
|
Map<String, String> paramMap = new HashMap<>();
|
|
|
this.addParamsToParamMap(paramMap);
|
|
|
SearchParam searchParam = searchParamHelper.buildWithMustFilter(paramMap, filter);
|
|
|
searchParam.setSize(sknList.size());
|
|
|
return searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
|
return new SearchResult();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private JSONObject buildReturnDataMap(long total, int page, int page_size, long page_total, List<Map<String, Object>> list) {
|
|
|
JSONObject dataMap = new JSONObject();
|
|
|
dataMap.put("total", total);
|
|
|
dataMap.put("page", page);
|
|
|
dataMap.put("page_size", page_size);
|
|
|
dataMap.put("page_total", page_total);
|
|
|
dataMap.put("product_list", list);
|
|
|
return dataMap;
|
|
|
}
|
|
|
|
|
|
private BoolQueryBuilder buildFilter(Map<String, String> paramMap) {
|
|
|
BoolQueryBuilder filter = QueryBuilders.boolQuery();
|
|
|
String not_skn = MapUtils.getString(paramMap, "not_skn");
|
|
|
if (StringUtils.isNotBlank(not_skn)) {
|
|
|
filter.mustNot(QueryBuilders.termQuery("productSkn", not_skn));
|
|
|
}
|
|
|
filter.must(QueryBuilders.termQuery("status", "1"));
|
|
|
filter.must(QueryBuilders.termQuery("delStatus", "1"));
|
|
|
long current = DateUtil.getCurrentTimeSecond();
|
|
|
filter.must(QueryBuilders.rangeQuery("beginTime").lte(current));
|
|
|
filter.must(QueryBuilders.rangeQuery("endTime").gte(current));
|
|
|
return filter;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void addParamsToParamMap(Map<String, String> paramMap) {
|
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_STATUS, "1");// 上架
|
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_STOCKNUM, "1");// 有库存
|
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_CONTAIN_FOBBIDEN, "Y");// 可以显示屏蔽商品
|
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_STORESHOWSTATUS, "1,2,5,6");// 排除不展示的商品
|
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_ISLIMITEDBUY, "N");// 不支持限购
|
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_ISDESPOSITADVANCE, "N");// 不支持定金预售
|
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_ISLIMITTIMEADVANCE, "N");// 不支持定金促销
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult aggregations(Map<String, String> paramMap) {
|
|
|
return new SearchApiResult();
|
|
|
}
|
|
|
} |
...
|
...
|
|