package com.gree.mall.manager.logic.es;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.gree.mall.manager.bean.es.CommonListES;
import com.gree.mall.manager.utils.CommonUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 操作es
 */

@Service
public class EsLogic {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    public boolean createIndexAndMapping(Class<?> classType) {
        if(elasticsearchRestTemplate.indexOps(classType).exists()){
            return true;
        }
        boolean b = elasticsearchRestTemplate.indexOps(classType).create();
        return b;
    }

    //@Test
    public void add(String comListId,String remark,List<Object> rows) throws NoSuchFieldException, IllegalAccessException {
        //0=workerNumber,从1开始
        Integer start = 1;
        Integer fieldSize = 50;
        //1.创建索引
        this.createIndexAndMapping(CommonListES.class);
        //2.新增内容
        List<CommonListES> list = new ArrayList<>();
        for(int i = 0;i< rows.size() ; i++) {
            CommonUtils.initList2(rows,rows.size());
            List<Object> row = (List<Object>) rows.get(i);
            String workerNumber = (String)row.get(0);
            if(StringUtils.isEmpty(workerNumber)){
                break;
            }
            CommonListES commonListES = new CommonListES();
            commonListES.setType(i==0?"1":"2");
            commonListES.setWorkerNumber(workerNumber);
            commonListES.setWebsitNumber(workerNumber);
            commonListES.setComListId(comListId);
            
            for(int x = start;x <= row.size();x++) {
                if(x > fieldSize){
                    continue;
                }
                String field = "field"+x;
                Field declaredField = commonListES.getClass().getDeclaredField(field);
                declaredField.setAccessible(true);
                declaredField.set(commonListES,row.get(x-1)==null?"-":row.get(x-1));
            }

            list.add(commonListES);
        }

        elasticsearchRestTemplate.save(list);
    }


    public  List<CommonListES>  list(List<String> websitNumbers,String workerNumber,String comId){
        //标题
        BoolQueryBuilder titleQueryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.termQuery("type", "1"));
        if(StringUtils.isNotEmpty(comId)){
            titleQueryBuilder.must(QueryBuilders.termQuery("comListId", comId));
        }
        //内容
        BoolQueryBuilder must = QueryBuilders.boolQuery().must(QueryBuilders.termQuery("type", "2"));
        if(StringUtils.isNotEmpty(comId)){
            must.must(QueryBuilders.termQuery("comListId", comId));
        }
        if(StringUtils.isNotEmpty(workerNumber)){
            must.must(QueryBuilders.matchPhraseQuery("workerNumber", workerNumber));
        }
        if(CollectionUtils.isNotEmpty(websitNumbers)){
            must.must(QueryBuilders.termsQuery("websitNumber.keyword",websitNumbers));
        }
        NativeSearchQuery titleBuild = new NativeSearchQueryBuilder().withQuery(titleQueryBuilder).build();
        NativeSearchQuery dataBuild = new NativeSearchQueryBuilder().withQuery(must).build();

        List<SearchHit<CommonListES>> searchHits1 = elasticsearchRestTemplate.search(titleBuild, CommonListES.class).getSearchHits();
        List<SearchHit<CommonListES>> searchHits2 = elasticsearchRestTemplate.search(dataBuild, CommonListES.class).getSearchHits();
        List<CommonListES> titles = searchHits1.stream().map(SearchHit<CommonListES>::getContent).collect(Collectors.toList());
        List<CommonListES> datas = searchHits2.stream().map(SearchHit<CommonListES>::getContent).collect(Collectors.toList());
        titles.addAll(datas);

        return titles;
    }



}
