package com.tofly.gpsboot.util;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.util.StringUtil;


import com.tofly.gpsboot.common.Constant;
import com.tofly.es.dto.EsCarGpsUploadQueryDto;
import com.tofly.es.dto.EsQueryDto;
import com.tofly.es.dto.UserGpsSearchDto;
import org.elasticsearch.index.query.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tuonbed
 * @date 2020/5/18  9:51
 */
public class EsQueryUtil {

    public static QueryBuilder[] putQUery(Page page, EsQueryDto esQuery) {
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("uploadTime");
        if (esQuery.getStartDate() != null) {
            rangeQueryBuilder.from(esQuery.getStartDate().getTime());
        }
        if (esQuery.getEndDate() != null) {
            rangeQueryBuilder.to(esQuery.getEndDate().getTime());
        }
        BoolQueryBuilder qb = QueryBuilders.boolQuery();
        if (esQuery.getUserId() != null) {
            MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("userId", esQuery.getUserId());
            queryBuilder.minimumShouldMatch("100%");
            qb.filter(queryBuilder);
        }
        if (esQuery.getGpsDeviceId() != null) {
            MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("gpsDeviceId", esQuery.getGpsDeviceId());
            queryBuilder.minimumShouldMatch("100%");
            qb.filter(queryBuilder);
        }
        if (esQuery.getCarId() != null) {
            MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("carId", esQuery.getCarId());
            queryBuilder.minimumShouldMatch("100%");
            qb.filter(queryBuilder);
        }
        String userIds = esQuery.getUserIds();
        if(StringUtil.isNotEmpty(userIds)){
            List<Long> longList = Arrays.stream(userIds.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                    .collect(Collectors.toList());
            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
            boolQuery.must(QueryBuilders.termsQuery("userId", longList));
            qb.filter(boolQuery);
        }
        // 增加 工作状态/GPS状态/网络状态查询条件
        String workState = esQuery.getWorkState();
        String network = esQuery.getNetwork();
        String networkState = esQuery.getNetworkState();
        if(StringUtil.isNotEmpty(workState)){
            List<String> workStateList = Arrays.stream(workState.split(Constant.CSV)).map(String::trim)
                    .collect(Collectors.toList());
            qb.filter(QueryBuilders.termsQuery("workState", workStateList));
        }
        if(StringUtil.isNotEmpty(network)){
            List<String> networkList = Arrays.stream(network.split(Constant.CSV)).map(String::trim)
                    .collect(Collectors.toList());
            qb.filter( QueryBuilders.termsQuery("network", networkList));
        }
        if(StringUtil.isNotEmpty(networkState)){
            List<String> networkStateList = Arrays.stream(networkState.split(Constant.CSV)).map(String::trim)
                    .collect(Collectors.toList());
            qb.filter(QueryBuilders.termsQuery("networkState", networkStateList));
        }
        QueryBuilder[] queryBuilders = new QueryBuilder[2];
        queryBuilders[0] = rangeQueryBuilder;
        queryBuilders[1] = qb;

        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("uploadTime");
        orderItem.setAsc(false);
        if (esQuery.getIsAsc() != null){
            orderItem.setAsc(true);
        }
        page.addOrder(orderItem);
        return queryBuilders;
    }


    public static QueryBuilder[] CarGpsUploadQuery(Page page, EsCarGpsUploadQueryDto esCarGpsUploadQuery) {
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("gpsTime");
        if (esCarGpsUploadQuery.getStartTime() != null) {
            rangeQueryBuilder.from(esCarGpsUploadQuery.getStartTime().getTime());
        }
        if (esCarGpsUploadQuery.getEndTime() != null) {
            rangeQueryBuilder.to(esCarGpsUploadQuery.getEndTime().getTime());
        }
        BoolQueryBuilder qb = QueryBuilders.boolQuery();
        if (esCarGpsUploadQuery.getCarId() != null) {
            MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("carId", esCarGpsUploadQuery.getCarId());
            queryBuilder.minimumShouldMatch("100%");
            qb.filter(queryBuilder);
        }
        if (esCarGpsUploadQuery.getGpsType() != null) {
            MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("gpsType", esCarGpsUploadQuery.getGpsType());
            queryBuilder.minimumShouldMatch("100%");
            qb.filter(queryBuilder);
        }
        QueryBuilder[] queryBuilders = new QueryBuilder[2];
        queryBuilders[0] = rangeQueryBuilder;
        queryBuilders[1] = qb;

        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("gpsTime");
        orderItem.setAsc(false);
        if (esCarGpsUploadQuery.getOrderAsc() != null && esCarGpsUploadQuery.getOrderAsc()){
            orderItem.setAsc(true);
        }
        page.addOrder(orderItem);
        return queryBuilders;
    }

    public static QueryBuilder[] getGpsLst(Page page, UserGpsSearchDto userGpsSearch) {
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("gpsTime");
        if (userGpsSearch.getStartDate() != null) {
            rangeQueryBuilder.from(userGpsSearch.getStartDate().getTime());
        }
        if (userGpsSearch.getEndDate() != null) {
            rangeQueryBuilder.to(userGpsSearch.getEndDate().getTime());
        }
        BoolQueryBuilder qb = QueryBuilders.boolQuery();
        if (userGpsSearch.getUserId() != null) {
            MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("userId", userGpsSearch.getUserId());
            queryBuilder.minimumShouldMatch("100%");
            qb.filter(queryBuilder);
        }
        if (StringUtil.isNotEmpty(userGpsSearch.getIsValid())) {
            MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("isValid", userGpsSearch.getIsValid());
            qb.must(queryBuilder);
        }
        if (userGpsSearch.getSection() != null) {
            MatchQueryBuilder qbGpsSection = QueryBuilders.matchQuery("gpsSection", userGpsSearch.getSection().toString());
            qb.must(qbGpsSection);
        }
        QueryBuilder[] queryBuilders = new QueryBuilder[2];
        queryBuilders[0] = rangeQueryBuilder;
        queryBuilders[1] = qb;

        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("gpsTime");
        orderItem.setAsc(true);
        page.addOrder(orderItem);
        return queryBuilders;
    }
}
