package com.purui.prims;

import com.purui.prims.auth.api.authentication.pojo.User;
import com.purui.prims.common.pojo.enums.DataFormatTypeEnum;
import com.purui.prims.common.pojo.enums.TypeEnums;
import com.purui.prims.headless.api.pojo.FieldParam;
import com.purui.prims.headless.api.pojo.MetricDefineByFieldParams;
import com.purui.prims.headless.api.pojo.enums.MetricDefineType;
import com.purui.prims.headless.api.pojo.request.MetricReq;
import com.purui.prims.headless.server.persistence.dataobject.MetricDO;
import com.purui.prims.headless.server.persistence.repository.MetricRepository;
import com.purui.prims.headless.server.pojo.MetricFilter;
import com.purui.prims.headless.server.service.impl.MetricServiceImpl;
import org.junit.Assert;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 类说明
 *
 * @author charlie
 * @date 2024/7/8
 */
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class MetricTest extends BaseTest{

    @Resource
    private MetricServiceImpl metricService;

    @Resource
    private MetricRepository metricRepository;

    private List<Long> metricIds;

    @BeforeAll
    public void setUp() {
        MetricFilter metaFilter = new MetricFilter();
        metaFilter.setModelIds(Arrays.asList(14L));
        List<MetricDO> metrics = new ArrayList<>();
        metrics = (metricRepository.getMetric(metaFilter));
        Assert.assertNotNull(metrics);
        metricIds = metrics.stream().map(MetricDO::getId).collect(Collectors.toList());
    }

    @Test
    public void testBatchPublishWithValidMetrics() {
        Assert.assertNotNull(metricIds);
        metricService.batchPublish(metricIds, User.getFakeUser());
    }

    @Test
    public void testUnBatchPublishWithMetrics() {
        Assert.assertNotNull(metricIds);
        metricService.batchUnPublish(metricIds, User.getFakeUser());
    }


    @Test
    void createRankMetric() {
        List<FieldParam> fields = new ArrayList<>();
        fields.add(new FieldParam("ECName"));
        fields.add(new FieldParam("TotalAmt"));
        createCustomMetric("测试排名","test_rank", 17L,"DENSE_RANK() over(partition by ECName order by TotalAmt desc)", fields);
    }

    @Test
    void createFiledMetric() {
        List<FieldParam> fields = new ArrayList<>();
        fields.add(new FieldParam("MdtrtID"));
        createCustomMetric("测试字段","test_field", 17L,"IF(MdtrtID is null,0,1)", fields);
    }

    void createCustomMetric(String name,String bizName,Long modelId, String expr,List<FieldParam> fields) {
        MetricReq metricReq = new MetricReq();
        metricReq.setName(name);
        metricReq.setBizName(bizName);
        metricReq.setDescription(name);
        metricReq.setStatus(1);
        metricReq.setTypeEnum(TypeEnums.METRIC);
        metricReq.setModelId(modelId);
        metricReq.setDataFormatType(DataFormatTypeEnum.DECIMAL.name());
        metricReq.setMetricDefineType(MetricDefineType.CUSTOM);
        MetricDefineByFieldParams metricDefineByFieldParams = new MetricDefineByFieldParams();
        metricDefineByFieldParams.setFields(fields);
        metricDefineByFieldParams.setExpr(expr);
        metricReq.setMetricDefineByFieldParams(metricDefineByFieldParams);
        metricService.createMetric(metricReq, User.getFakeUser());
    }

}
