package com.purui.prims;

import com.purui.prims.auth.api.authentication.pojo.User;
import com.purui.prims.common.pojo.Aggregator;
import com.purui.prims.common.pojo.DateConf;
import com.purui.prims.common.pojo.Filter;
import com.purui.prims.common.pojo.enums.AggOperatorEnum;
import com.purui.prims.common.pojo.enums.FilterOperatorEnum;
import com.purui.prims.common.pojo.enums.QueryType;
import com.purui.prims.common.pojo.enums.TypeEnums;
import com.purui.prims.common.util.ContextUtils;
import com.purui.prims.headless.api.pojo.DataSetDetail;
import com.purui.prims.headless.api.pojo.DataSetModelConfig;
import com.purui.prims.headless.api.pojo.MetricTypeDefaultConfig;
import com.purui.prims.headless.api.pojo.QueryConfig;
import com.purui.prims.headless.api.pojo.request.DataSetReq;
import com.purui.prims.headless.api.pojo.request.QuerySqlReq;
import com.purui.prims.headless.api.pojo.request.QueryStructReq;
import com.purui.prims.headless.api.pojo.response.SemanticQueryResp;
import com.purui.prims.headless.server.service.DataSetService;
import com.purui.prims.headless.server.service.SemanticLayerService;
import org.apache.commons.collections.CollectionUtils;
import org.junit.Assert;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static java.time.LocalDate.now;

/**
 * 类说明
 *
 * @author charlie
 * @date 2024/8/8
 */
public class DatasetTest extends BaseTest {

    @Test
    void createTest() {
        DataSetService dataSetService = ContextUtils.getBean(DataSetService.class);

        DataSetReq dataSetReq = new DataSetReq();
        dataSetReq.setId(9L);
        dataSetReq.setName("test");
        dataSetReq.setAlias("test");
        dataSetReq.setBizName("test");
        dataSetReq.setDescription("test");
        dataSetReq.setDomainId(10L);
        dataSetReq.setStatus(1);
        dataSetReq.setSensitiveLevel(1);
        dataSetReq.setCreatedBy("test");
        dataSetReq.setUpdatedBy("test");
        DataSetDetail dataSetDetail = new DataSetDetail();
        List<DataSetModelConfig> configs = new ArrayList<>();

        DataSetModelConfig config = new DataSetModelConfig();
        config.setDimensions(Arrays.asList(41L, 42L));
        config.setMetrics(Arrays.asList(38L, 39L));
        config.setId(17L);  // 模型ID，必须
        configs.add(config);

        config = new DataSetModelConfig();
        config.setDimensions(Arrays.asList(45L));
        config.setId(18L);  // 模型ID，必须
        configs.add(config);


        config = new DataSetModelConfig();
        config.setDimensions(Arrays.asList(46L));
        config.setMetrics(Arrays.asList(36L, 37L));
        config.setId(19L);  // 模型ID，必须
        configs.add(config);

        dataSetDetail.setDataSetModelConfigs(configs);
        dataSetReq.setDataSetDetail(dataSetDetail);
        dataSetReq.setTypeEnum(TypeEnums.DATASET);
        QueryConfig queryConfig = new QueryConfig();
        queryConfig.setMetricTypeDefaultConfig(new MetricTypeDefaultConfig());
        dataSetReq.setQueryConfig(queryConfig);

        dataSetService.save(dataSetReq, User.getFakeUser());
    }

    @Test
    void querySqlTest() {
        SemanticQueryResp queryResp = null;
        try {
//            queryResp = queryBySql("select sum(就诊人次),sum(便民就诊人次),医院名称,就诊日期 from 门诊实际就诊 group by 医院名称,就诊日期");
            String sql = "select sum(就诊人次),sum(便民就诊人次),医院名称,就诊日期 from 门诊实际就诊 group by 医院名称,就诊日期";
//            SemanticQueryReq queryMultiStructReq = buildMultiStructReq(sql);
//            queryResp = queryByReq(queryMultiStructReq);

        } catch (Exception e) {
            e.printStackTrace();
        }
//        Assert.assertNotNull(queryResp);
    }


    QueryStructReq buildQueryStructReq(List<String> groups,Long datasetId,String metricName) {
        Aggregator aggregator = new Aggregator();
        aggregator.setFunc(AggOperatorEnum.SUM);
        aggregator.setColumn(metricName);
        QueryStructReq queryStructReq = new QueryStructReq();
        queryStructReq.setQueryType(QueryType.METRIC);

        if (aggregator != null) {
            queryStructReq.setAggregators(Arrays.asList(aggregator));
        }

        if (CollectionUtils.isNotEmpty(groups)) {
            queryStructReq.setGroups(groups);
        }
        queryStructReq.setDataSetId(datasetId);

        DateConf dateConf = new DateConf();
        dateConf.setDateMode(DateConf.DateMode.BETWEEN);
        dateConf.setEndDate(now().plusDays(0).toString());
        dateConf.setStartDate(now().plusDays(-365).toString());
        queryStructReq.setDateInfo(dateConf);

        return queryStructReq;
    }

    @Test
    void querySumTest() {
        SemanticLayerService semanticLayerService = ContextUtils.getBean(SemanticLayerService.class);
        Aggregator aggregator = new Aggregator();
        aggregator.setFunc(AggOperatorEnum.SUM);
        aggregator.setColumn("avg_bigAmt");
        QueryStructReq queryStructReq = buildQueryStructReq(Arrays.asList("ECName"), aggregator);
        queryStructReq.setDataSetId(9L);

        QuerySqlReq sqlReq = queryStructReq.convert();

        try {
            SemanticQueryResp queryResp = semanticLayerService.queryByReq(sqlReq, User.getFakeUser());
            Assert.assertNotNull(queryResp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    void queryFilterTest() {
        SemanticLayerService semanticLayerService = ContextUtils.getBean(SemanticLayerService.class);
        Aggregator aggregator = new Aggregator();
        aggregator.setFunc(AggOperatorEnum.SUM);
        aggregator.setColumn("平均超大金额");
        QueryStructReq queryStructReq = buildQueryStructReq(Arrays.asList("医保类型"), aggregator);
        queryStructReq.setDataSetName("社保明细");

        Filter filter = new Filter();
        filter.setName("医院名称");
        filter.setOperator(FilterOperatorEnum.IN);
        filter.setValue(Arrays.asList("成都普瑞眼科医院"));
        queryStructReq.setDimensionFilters(Arrays.asList(filter));

        QuerySqlReq sqlReq = queryStructReq.convert();

        try {
            SemanticQueryResp queryResp = semanticLayerService.queryByReq(sqlReq, User.getFakeUser());
            Assert.assertNotNull(queryResp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    void queryDetailTest() {
        SemanticLayerService semanticLayerService = ContextUtils.getBean(SemanticLayerService.class);
        List<String> groups = null;
        Aggregator aggregator = null;

        QueryStructReq queryStructReq = buildQueryStructReq(groups, aggregator, QueryType.DETAIL);
        queryStructReq.setDataSetId(14L);
        QuerySqlReq sqlReq = new QuerySqlReq();
        if (groups == null && aggregator == null) {
            sqlReq.setDataSetId(14L);
            sqlReq.setSql("select * from test_dataset4");
        } else {
            sqlReq = queryStructReq.convert();
        }

        try {
            SemanticQueryResp queryResp = semanticLayerService.queryByReq(sqlReq, User.getFakeUser());
            Assert.assertNotNull(queryResp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    void publishTest() {
        DataSetService dataSetService = ContextUtils.getBean(DataSetService.class);
        dataSetService.publish(9L, User.getFakeUser());
    }

    @Test
    void biTest() {
        DataSetService dataSetService = ContextUtils.getBean(DataSetService.class);
        String sql = dataSetService.buildAsSelectSql(9L);
        Assert.assertNotNull(sql);
        System.out.println(sql);
    }

}
