package com.search.supersearch;
import cn.hutool.json.JSONUtil;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.IndexResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.search.supersearch.domain.DwdNocFilesEs;
import com.search.supersearch.domain.IamDataCategory;
import com.search.supersearch.mapper.pgsql.CustomMapper;
import com.search.supersearch.model.pojo.DocsIndex;
import com.search.supersearch.service.mysql.IamDataCategoryService;
import com.search.supersearch.service.pgsql.DwdNocFilesEsService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 测试 elasticSearch client 索引相关的操作
 *
 * @author WT
 * @date 2021/12/28 16:27:45
 */
@Slf4j
@SpringBootTest
@ActiveProfiles("wr") //这里
public class JdbcTest {

    @Resource
    IamDataCategoryService iamDataCategoryService;
    @Resource
    DwdNocFilesEsService dwdNocFilesEsService;
    @Resource
    CustomMapper customMapper;

    @Autowired
    private ElasticsearchClient elasticsearchClient;

    @Test
    @SneakyThrows
    public void run() {
        Page<IamDataCategory> page = iamDataCategoryService.lambdaQuery().page(new Page<>(2, 3));
        List<IamDataCategory> records = page.getRecords();
        System.out.println(records);
    }

    @Test
    @SneakyThrows
    public void run2() {
        Page<DwdNocFilesEs> objectPage = new Page<>();
        objectPage.setCurrent(3);
        objectPage.setSize(3);
        Page<DwdNocFilesEs> page = dwdNocFilesEsService.lambdaQuery().page(new Page<>(2, 3));
        List<DwdNocFilesEs> records = page.getRecords();
        System.out.println(records);
        System.out.println(records.get(0).getSeq());
    }

    @Test
    @SneakyThrows
    public void run3() {
        List<Map> maps = customMapper.selectUserByNameAndAge("dwd_noc_files_es", new Page(0, 2));

        DocsIndex docsIndex = new DocsIndex();
        docsIndex.setEs_fileId("1");
        docsIndex.setEs_index_creat_date(new Date());
        docsIndex.setEs_file_path("1");
        docsIndex.setEs_ord_id("1");
        docsIndex.setEs_index_name("1");
        docsIndex.setData(maps.get(0));


        IndexResponse indexResponse = elasticsearchClient.index(indexRequest -> {
//                indexRequest.index(INDEX_NAME).document(new AhaIndex().setName("wangWu").setAge(21))
            indexRequest.index("dwd_noc_files_es").document(docsIndex);
            return indexRequest;
        });
        log.info("== response: {}, responseStatus: {}", indexResponse, indexResponse.result());
        System.out.println(JSONUtil.toJsonStr(maps));

    }

    @Test
    @SneakyThrows
    public void run4() {
        List<Map> maps = customMapper.selectUserByNameAndAge("dwd_noc_files_es", new Page(0, 2));
        System.out.println(JSONUtil.toJsonStr(maps));

        System.out.println("============");
        String str = customMapper.setschema_name("dws_7tzly_test");
        System.out.println(JSONUtil.toJsonStr(str));
        System.out.println("============");

        List<Map> maps2 = customMapper.selectUserByNameAndAge("tz_pojjxmlxgdtmst_i", new Page(0, 2));
        System.out.println(JSONUtil.toJsonStr(maps2));
    }
}
