package com.liangjian.dataplatform.member.service.impl;

import com.liangjian.dataplatform.member.dao.elasticsearch.UserPointLogRepository;
import com.liangjian.dataplatform.member.service.IUserPointStasticService;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.sum.Sum;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.GetQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 黄树文
 * @Date: 2019/12/5 17:27
 */
@Service
public class UserPointStasticService implements IUserPointStasticService {

    @Autowired
    private UserPointLogRepository userPointLogRepository;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchTemplate;

    public Long getPointsSumWith(String field) {
        RestHighLevelClient client = elasticsearchTemplate.getClient();
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(field).gt(0);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder()
                .query(rangeQueryBuilder)
                .aggregation(AggregationBuilders.sum("sumPoints").field(field));
        SearchRequest searchRequest = new SearchRequest("mem_user_point_log").source(sourceBuilder);
        try {
            SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
            Sum valueCount = response.getAggregations().get("sumPoints");
            Double value = valueCount.getValue();
            return value.longValue();
        } catch (IOException ex) {
            ex.printStackTrace();
            return 0L;
        }
    }

    @Override
    public Map<String, Object> getPoints() {
        Long pointsSendOut = getPointsSumWith("value");
        Long pointsConsumed = getPointsSumWith("value");
        Long pointsToBeConsumed = getPointsSumWith("point");
        Long pointsExpired = getPointsSumWith("point");
        HashMap<String, Object> pointsMap = new HashMap<>();
        pointsMap.put("pointsConsumed",pointsConsumed);
        pointsMap.put("pointsToBeConsumed",pointsToBeConsumed);
        pointsMap.put("pointsSendOut",pointsSendOut);
        pointsMap.put("pointsExpired",pointsExpired);
        return pointsMap;
    }
}
