CollageActivityIndexBaseService.java 6.42 KB
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<>();
        }
    }
}