package com.purui.prims;

import com.alibaba.fastjson.JSON;
import com.purui.prims.common.pojo.enums.TimeMode;
import com.purui.prims.common.util.ContextUtils;
import com.purui.prims.common.util.DatePeriodEnum;
import com.purui.prims.headless.api.pojo.MetricTypeDefaultConfig;
import com.purui.prims.headless.api.pojo.QueryConfig;
import com.purui.prims.headless.api.pojo.TimeDefaultConfig;
import com.purui.prims.headless.api.pojo.request.DatasetPersistenceConfigReq;
import com.purui.prims.headless.api.pojo.request.MetricPersistenceConfigReq;
import com.purui.prims.headless.api.pojo.response.DataSetResp;
import com.purui.prims.headless.server.schedule.DatasetPersistenceTask;
import com.purui.prims.headless.server.schedule.MetricPersistenceTask;
import com.purui.prims.headless.server.service.DataSetService;
import com.purui.prims.headless.server.service.DatasetPersistenceConfigService;
import com.purui.prims.headless.server.service.MetricPersistenceConfigService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;

/**
 * 持久化测试
 *
 * @author charlie
 * @date 2024/7/27
 */
public class PersistenceTest extends BaseTest {
    @Test
    void  createPersistenceTest() {
        long dataSetId = 9L;
        long databaseId = 3L;
        DataSetService dataSetService = ContextUtils.getBean(DataSetService.class);
        DatasetPersistenceConfigService datasetPersistenceConfigService = ContextUtils.getBean(DatasetPersistenceConfigService.class);
        boolean result = false;
        if (dataSetService !=null && datasetPersistenceConfigService !=null) {
            DataSetResp dataSet = dataSetService.getDataSet(dataSetId);
            DatasetPersistenceConfigReq configReq = new DatasetPersistenceConfigReq();
            configReq.setTableName("index_dataset_" + dataSetId);
            configReq.setDatasetId(dataSetId);
            configReq.setDatabaseId(databaseId);
            configReq.createdBy("admin");
            configReq.updatedBy("admin");
            configReq.setCreatedAt(new java.util.Date());
            configReq.setUpdatedAt(new java.util.Date());
            configReq.setRunCycleId(1);
            configReq.setStartDate("2024-07-17");
            configReq.setDefaultConfig(JSON.toJSONString(dataSet.getQueryConfig()));
            result = datasetPersistenceConfigService.save(configReq);
        }
        Assert.assertTrue(result);

        MetricPersistenceConfigService metricPersistenceConfigService = ContextUtils.getBean(MetricPersistenceConfigService.class);
        if (metricPersistenceConfigService != null) {
            QueryConfig queryConfig = new QueryConfig();
            TimeDefaultConfig timeDefaultConfig = new TimeDefaultConfig();
            timeDefaultConfig.setTimeMode(TimeMode.LAST);
            timeDefaultConfig.setPeriod(DatePeriodEnum.MONTH.name());
            timeDefaultConfig.setUnit(1);
            MetricTypeDefaultConfig metricTypeDefaultConfig = new MetricTypeDefaultConfig();
            metricTypeDefaultConfig.setTimeDefaultConfig(timeDefaultConfig);
            queryConfig.setMetricTypeDefaultConfig(metricTypeDefaultConfig);
            MetricPersistenceConfigReq metricConfigReq = new MetricPersistenceConfigReq();
            metricConfigReq.setTableName("index_metric_" + dataSetId);
            metricConfigReq.setMetricId(38L);
            metricConfigReq.setRunCycleId(1);
            metricConfigReq.setDefaultConfig(JSON.toJSONString(queryConfig));
            metricConfigReq.setCreatedAt(new java.util.Date());
            metricConfigReq.setUpdatedAt(new java.util.Date());
            result = metricPersistenceConfigService.save(metricConfigReq);
            Assert.assertTrue(result);

            /**
            metricConfigReq = new MetricPersistenceConfigReq();
            metricConfigReq.setTableName("index_metric_" + dataSetId);
            metricConfigReq.setMetricId(25L);
            metricConfigReq.setRunCycleId(1);
            metricConfigReq.setDefaultConfig(JSON.toJSONString(queryConfig));
            metricConfigReq.setCreatedAt(new java.util.Date());
            metricConfigReq.setUpdatedAt(new java.util.Date());
            result = metricPersistenceConfigService.save(metricConfigReq);
            Assert.assertTrue(result);

            metricConfigReq = new MetricPersistenceConfigReq();
            metricConfigReq.setTableName("index_metric_" + dataSetId);
            metricConfigReq.setMetricId(26L);
            metricConfigReq.setRunCycleId(1);
            metricConfigReq.setDefaultConfig(JSON.toJSONString(queryConfig));
            metricConfigReq.setCreatedAt(new java.util.Date());
            metricConfigReq.setUpdatedAt(new java.util.Date());
            result = metricPersistenceConfigService.save(metricConfigReq);
            Assert.assertTrue(result);
             */

            metricConfigReq = new MetricPersistenceConfigReq();
            metricConfigReq.setTableName("index_metric_" + dataSetId);
            metricConfigReq.setMetricId(39L);
            metricConfigReq.setRunCycleId(1);
            metricConfigReq.setDefaultConfig(JSON.toJSONString(queryConfig));
            metricConfigReq.setCreatedAt(new java.util.Date());
            metricConfigReq.setUpdatedAt(new java.util.Date());
            result = metricPersistenceConfigService.save(metricConfigReq);
            Assert.assertTrue(result);
        }

    }

    @Test
    void datasetPersistenceTest() {
        DatasetPersistenceTask persistenceTask = ContextUtils.getBean(DatasetPersistenceTask.class);
        if (persistenceTask != null) {
            persistenceTask.executeTask();
        }
    }

    @Test
    void metricPersistenceTest() {
        MetricPersistenceTask persistenceTask = ContextUtils.getBean(MetricPersistenceTask.class);
        if (persistenceTask != null) {
            persistenceTask.executeTask();
        }
    }
}
