package com.jay.monitor.data.server.utils;

import cn.hutool.core.bean.BeanUtil;
import com.jay.monitor.data.server.anno.ESSearch;
import com.jay.monitor.data.server.enums.ESQuerySyntax;
import com.jay.monitor.data.server.models.entity.es.JayMonitorMQIndex;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class ESUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(ESUtils.class);
    private static List<Class> typeList =
        Arrays.asList(Integer.class, String.class, Date.class, Double.class, Long.class);

    public static QueryBuilder beanToQueryBuilder(Object t) throws Exception {
        if (t == null) {
            return null;
        }
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        Field[] declaredFields = FieldUtils.getAllFields(t.getClass());
        for (int i = 0; i < declaredFields.length; i++) {
            Field declaredField = declaredFields[i];
            String name = declaredField.getName();
            Object value = BeanUtil.getProperty(t, name);
            //&& typeList.contains(value.getClass())
            if (value != null && !ObjectUtils.isEmpty(value)) {
                ESSearch esSearch = AnnotationUtils.findAnnotation(declaredField, ESSearch.class);
                QueryBuilder queryBuilder = null;

                if (esSearch == null && AnnotationUtils.findAnnotation(declaredField, Ignore.class) != null) {
                    continue;
                }

                if (value instanceof Date) {
                    value = ((Date)value).getTime();
                }

                if (esSearch != null) {
                    name = DataUtils.getDefaultValue(esSearch.fieldName(), name);
                }

                if (esSearch == null || esSearch.value() == ESQuerySyntax.eq) {
                    if (value instanceof String) {
                        name = name + ".keyword";
                    }
                    queryBuilder = QueryBuilders.matchQuery(name, value);
                } else if (esSearch.value() == ESQuerySyntax.gt) {
                    queryBuilder = QueryBuilders.rangeQuery(name).gt(value);
                } else if (esSearch.value() == ESQuerySyntax.lt) {
                    queryBuilder = QueryBuilders.rangeQuery(name).lt(value);
                } else if (esSearch.value() == ESQuerySyntax.like) {
                    queryBuilder = QueryBuilders.matchPhraseQuery(name, value);
                }
                boolQueryBuilder.must(queryBuilder);
            }
        }
        return boolQueryBuilder;
    }

    public static void setId(Object target, Object idSource) {
        try {
            String id = BeanUtils.getProperty(idSource, "id");
            setId(target, id);
        } catch (Exception e) {
            LOGGER.warn("填充ID对象失败:{} -> {}", idSource.toString(), target.toString());
        }
    }

    public static void setId(Object obj, String id) {
        try {
            BeanUtils.setProperty(obj, "id", id);
        } catch (Exception e) {
            LOGGER.warn("没有对象:{}找到对应的id属性 : {}", obj.toString(), e.getMessage());
        }
    }

    public static void main(String[] args) throws Exception {
        JayMonitorMQIndex index = new JayMonitorMQIndex();
        index.setRequestTime(11111L);
        QueryBuilder queryBuilder = beanToQueryBuilder(index);
        System.out.println(queryBuilder);
    }
}
