package com.purui.prims;

import com.alibaba.fastjson.JSON;
import com.purui.prims.common.pojo.PersistenceConf;
import com.purui.prims.common.util.ContextUtils;
import com.purui.prims.headless.api.pojo.request.MetricPersistenceConfigReq;
import com.purui.prims.headless.server.schedule.DatasetPersistenceTask;
import com.purui.prims.headless.server.schedule.MetricPersistenceTask;
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.setRunCycleCode("DAY");
//            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) {
            PersistenceConf conf = new PersistenceConf();
            conf.setPeriod("DAY");
            conf.setUnit(1);
            conf.setStartTime("08:00:00");
            conf.setWay("INC");
            conf.setType("MANUAL");
            MetricPersistenceConfigReq metricConfigReq = new MetricPersistenceConfigReq();
            metricConfigReq.setMetricId(103L);
            metricConfigReq.setTableName("index_metric_" + metricConfigReq.getMetricId());
            metricConfigReq.setRunCycleCode("DAY");
            metricConfigReq.setDefaultConfig(JSON.toJSONString(conf));
            metricConfigReq.setCreatedAt(new java.util.Date());
            metricConfigReq.setUpdatedAt(new java.util.Date());
            boolean 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();
        }
    }
}
