package com.cf.imes.module.report.service.template;

import com.cf.imes.framework.organ.core.context.OrganContextHolder;
import com.cf.imes.framework.test.core.util.RandomUtils;
import com.cf.imes.module.report.controller.admin.dataset.vo.ReportDatasetFieldVO;
import com.cf.imes.module.report.controller.admin.dataset.vo.ReportDatasetParameterVO;
import com.cf.imes.module.report.controller.admin.dataset.vo.ReportDatasetSaveReqVO;
import com.cf.imes.module.report.controller.admin.datasource.vo.ReportDatasourceSaveReqVO;
import com.cf.imes.module.report.controller.admin.template.vo.ReportTemplateSaveReqVO;
import com.cf.imes.module.report.enums.datasource.ReportDatasourceTypeEnum;

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

/**
 * @author Gqr
 * @since 2024/7/5 17:05
 */
public abstract class ReportCommonServiceImplTest {

    protected static final String TEMPLATE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ureport><cell expand=\"None\" name=\"A1\" row=\"1\" col=\"1\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"B1\" row=\"1\" col=\"2\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"C1\" row=\"1\" col=\"3\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"D1\" row=\"1\" col=\"4\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"A2\" row=\"2\" col=\"1\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"B2\" row=\"2\" col=\"2\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"C2\" row=\"2\" col=\"3\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"D2\" row=\"2\" col=\"4\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"A3\" row=\"3\" col=\"1\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"B3\" row=\"3\" col=\"2\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"C3\" row=\"3\" col=\"3\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"D3\" row=\"3\" col=\"4\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><row row-number=\"1\" height=\"18\"/><row row-number=\"2\" height=\"18\"/><row row-number=\"3\" height=\"18\"/><column col-number=\"1\" width=\"80\"/><column col-number=\"2\" width=\"80\"/><column col-number=\"3\" width=\"80\"/><column col-number=\"4\" width=\"80\"/><paper type=\"A4\" left-margin=\"90\" right-margin=\"90\" top-margin=\"72\" bottom-margin=\"72\" paging-mode=\"fitpage\" fixrows=\"0\" width=\"595\" height=\"842\" orientation=\"portrait\" html-report-align=\"left\" bg-image=\"\" html-interval-refresh-value=\"0\" column-enabled=\"false\"></paper></ureport>";

    protected void setOrganId(Long... organId) {
        OrganContextHolder.setOrganId(organId.length > 0 ? organId[0] : 1L);
    }

    protected ReportTemplateSaveReqVO prepareTemplateReqVO(Long id) {
        return ReportTemplateSaveReqVO.builder()
                .id(id)
                .name("单元测试模板")
                .content(JsonUtil.zipString(TEMPLATE))
                .remark(RandomUtils.randomString())
                .build();

    }

    protected ReportDatasourceSaveReqVO prepareDatasourceReqVO(Long id, Long reportId) {
        return ReportDatasourceSaveReqVO.builder()
                .id(id)
                .name("单元测试数据源")
                .type(ReportDatasourceTypeEnum.SPRING.getDesc())
                .driver("com.mysql.cj.jdbc.Driver")
                .url("jdbc:mysql://192.168.1.205:3307/imes_base?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&nullCatalogMeansCurrent=true")
                .username("root")
                .password("root")
                .remark(RandomUtils.randomString())
                .build();
    }

    protected ReportDatasetSaveReqVO prepareDatasetReqVO(Long id, Long datasourceId) {
        ReportDatasetParameterVO idvo = new ReportDatasetParameterVO("id", "Integer", "1");
        ReportDatasetParameterVO appidvo = new ReportDatasetParameterVO("app_id", "String", "CF7526AD51");
        List<ReportDatasetParameterVO> params = new ArrayList<>() {{
            add(idvo);
            add(appidvo);
        }};

        ReportDatasetFieldVO idfield = new ReportDatasetFieldVO("id");
        List<ReportDatasetFieldVO> fields = new ArrayList<>() {{
            add(idfield);
        }};
        return ReportDatasetSaveReqVO.builder()
                .id(id)
                .name("单元测试数据集")
                .sql("select * from application where id = \"${param(\"id\")}\" and app_id = \"${param(\"app_id\")}\"")
                .datasourceId(datasourceId)
                .parameters(params)
                .fields(fields)
                .build();
    }
}
