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

import com.liangjian.dataplatform.member.dao.elasticsearch.UserPointLogRepository;
import com.liangjian.dataplatform.member.dao.secondary.WeixinUserInfoRepository;
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.MatchPhraseQueryBuilder;
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.stereotype.Service;

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

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery;

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

    @Autowired
    private UserPointLogRepository userPointLogRepository;

    @Autowired
    private WeixinUserInfoRepository weixinUserInfoRepository;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchTemplate;

    public Long getPointsSumWith() {
        RestHighLevelClient client = elasticsearchTemplate.getClient();
        BoolQueryBuilder queryBuilder = boolQuery().mustNot(matchPhraseQuery("action", "订单扣减"))
                .mustNot(boolQuery().must(boolQuery().must(matchPhraseQuery("action", "人为增加积分"))
                        .must(matchPhraseQuery("increase", "false"))
                ));
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder()
                .query(queryBuilder)
                .aggregation(AggregationBuilders.sum("sumPoints").field("value"));
        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;
        }
    }


    private Long getPointsExpired() {
        return 0L;
    }

    private Long getPointsToBeConsumed() {
        try {
            return weixinUserInfoRepository.getPointsToBeConsumed();
        } catch (Exception e) {
            return 0L;
        }
    }

    private Long getPointsConsumed() {
        RestHighLevelClient client = elasticsearchTemplate.getClient();
        MatchPhraseQueryBuilder matchPhraseQueryBuilder = matchPhraseQuery("action", "订单扣减");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder()
                .query(matchPhraseQueryBuilder)
                .aggregation(AggregationBuilders.sum("sumPoints").field("value"));
        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();
        Long pointsConsumed = getPointsConsumed();
        Long pointsToBeConsumed = getPointsToBeConsumed();
        Long pointsExpired = getPointsExpired();
        HashMap<String, Object> pointsMap = new HashMap<>();
        pointsMap.put("pointsConsumed", pointsConsumed);
        pointsMap.put("pointsToBeConsumed", pointsToBeConsumed);
        pointsMap.put("pointsSendOut", pointsSendOut);
        pointsMap.put("pointsExpired", pointsExpired);
        return pointsMap;
    }


}
