package com.njcn.gather.icd.mapping.component;

import com.njcn.gather.icd.mapping.pojo.param.IcdConsistencyCheckRequest;
import com.njcn.gather.icd.mapping.pojo.vo.IcdConsistencyCheckResponse;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.nio.file.Files;
import java.nio.file.Path;

class IcdConsistencyCheckServiceTest {

    private final FileStorageService fileStorageService = new FileStorageService();
    private final IcdConsistencyCheckService service = new IcdConsistencyCheckService(fileStorageService);

    @TempDir
    Path tempDir;

    @Test
    void checkShouldReturnMismatchIssuesWithoutAutoCorrectingConsistencyDiffs() throws Exception {
        IcdConsistencyCheckRequest request = new IcdConsistencyCheckRequest();
        request.setStandardJson(buildStandardJson());
        request.setCheckedJson(buildCheckedJson());
        request.setSaveToDisk(true);
        request.setOutputDir(tempDir.toString());

        IcdConsistencyCheckResponse response = service.check(request);

        Assertions.assertEquals(0, response.getResult());
        Assertions.assertEquals("不符合", response.getMessage());
        Assertions.assertFalse(response.getIssues().isEmpty());
        Assertions.assertTrue(response.getIssuesJson().contains("ReportMap"));
        Assertions.assertNull(response.getCorrectedJson());
        Assertions.assertTrue(Files.list(tempDir).findAny().isPresent());
    }

    @Test
    void checkShouldReturnPassAndCorrectedJsonForRtFreSelfMappingRule() {
        IcdConsistencyCheckRequest request = new IcdConsistencyCheckRequest();
        request.setStandardJson(buildRtFreStandardJson());
        request.setCheckedJson(buildRtFreCheckedJson());

        IcdConsistencyCheckResponse response = service.check(request);

        Assertions.assertEquals(1, response.getResult());
        Assertions.assertTrue(response.getIssues().isEmpty());
        Assertions.assertTrue(response.getCorrectedJson().contains("\"rptID\" : \"demoRtFre\""));
        Assertions.assertTrue(response.getCorrectedJson().contains("\"FlickerFlag\" : \"1\""));
        Assertions.assertTrue(response.getCorrectedJson().contains("\"reportCount\" : 0"));
        Assertions.assertTrue(response.getCorrectedJson().contains("\"reportCount\" : 1"));
    }

    @Test
    void checkShouldNotCorrectRtFreWhenReportMapAlreadyMatchesStandard() {
        IcdConsistencyCheckRequest request = new IcdConsistencyCheckRequest();
        request.setStandardJson(buildRtFreStandardJson());
        request.setCheckedJson(buildRtFreStandardJson());

        IcdConsistencyCheckResponse response = service.check(request);

        Assertions.assertEquals(1, response.getResult());
        Assertions.assertNull(response.getCorrectedJson());
    }

    @Test
    void checkShouldKeepStandardDoiWhenSameDoiListContainsDuplicateDesc() {
        IcdConsistencyCheckRequest request = new IcdConsistencyCheckRequest();
        request.setStandardJson(buildStandardJson());
        request.setCheckedJson(buildDuplicateDoiDescJson());

        IcdConsistencyCheckResponse response = service.check(request);

        Assertions.assertEquals(0, response.getResult());
        Assertions.assertTrue(response.getIssuesJson().contains("同一个 doiList 下存在 desc 相同的指标"));
        Assertions.assertTrue(response.getIssuesJson().contains("频率+Hz 在标准映射中存在"));
        Assertions.assertTrue(response.getIssuesJson().contains("频率+Hz2 不在标准映射中"));
        Assertions.assertNotNull(response.getCorrectedJson());
        Assertions.assertTrue(response.getCorrectedJson().contains("\"name\" : \"Hz\""));
        Assertions.assertFalse(response.getCorrectedJson().contains("\"name\" : \"Hz2\""));
    }

    @Test
    void checkShouldIgnoreVoltageStartMetricsDuringDoiConsistency() {
        IcdConsistencyCheckRequest request = new IcdConsistencyCheckRequest();
        request.setStandardJson(buildVoltageStartMetricsStandardJson());
        request.setCheckedJson(buildStandardJson());

        IcdConsistencyCheckResponse response = service.check(request);

        Assertions.assertEquals(1, response.getResult());
        Assertions.assertTrue(response.getIssues().isEmpty());
    }

    @Test
    void checkShouldReportEmptySdiListAsTypeListProblem() {
        IcdConsistencyCheckRequest request = new IcdConsistencyCheckRequest();
        request.setStandardJson(buildStandardJson());
        request.setCheckedJson(buildEmptySdiListJson());

        IcdConsistencyCheckResponse response = service.check(request);

        Assertions.assertEquals(0, response.getResult());
        Assertions.assertTrue(response.getIssuesJson().contains("typeList 不能为空：统计数据 / A相 / 频率"));
        Assertions.assertFalse(response.getIssuesJson().contains("sdiList 不能为空"));
    }

    @Test
    void checkShouldIgnoreVoltageStartMetricsDuringSelfFormatTypeListValidation() {
        IcdConsistencyCheckRequest request = new IcdConsistencyCheckRequest();
        request.setStandardJson(buildVoltageStartMetricsWithoutSdiJson());
        request.setCheckedJson(buildVoltageStartMetricsWithoutSdiJson());

        IcdConsistencyCheckResponse response = service.check(request);

        Assertions.assertEquals(1, response.getResult());
        Assertions.assertTrue(response.getIssues().isEmpty());
    }

    @Test
    void checkShouldReturnPassWhenCheckedJsonMatchesStandardJson() {
        IcdConsistencyCheckRequest request = new IcdConsistencyCheckRequest();
        request.setStandardJson(buildStandardJson());
        request.setCheckedJson(buildStandardJson());

        IcdConsistencyCheckResponse response = service.check(request);

        Assertions.assertEquals(1, response.getResult());
        Assertions.assertEquals("符合", response.getMessage());
        Assertions.assertTrue(response.getIssues().isEmpty());
        Assertions.assertNull(response.getCorrectedJson());
    }

    private String buildStandardJson() {
        return "{\n" +
                "  \"IED\":\"IED1\",\n" +
                "  \"LD\":\"LD0\",\n" +
                "  \"DataType\":\"1\",\n" +
                "  \"unit\":\"s\",\n" +
                "  \"ReportMap\":[\n" +
                "    {\"desc\":\"统计数据\",\"reportCount\":2,\"rptID\":\"rpt-stat\",\"name\":\"brcbStat\",\"buffered\":\"BR\",\"inst\":\"01\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"波动闪变\",\"reportCount\":1,\"rptID\":\"rpt-flk\",\"name\":\"brcbFlk\",\"buffered\":\"BR\",\"inst\":\"02\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"实时数据\",\"reportCount\":1,\"rptID\":\"rpt-rt\",\"name\":\"brcbRt\",\"buffered\":\"RP\",\"inst\":\"03\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"暂态事件\",\"reportCount\":1,\"rptID\":\"rpt-tran\",\"name\":\"brcbTran\",\"buffered\":\"BR\",\"inst\":\"04\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"}\n" +
                "  ],\n" +
                "  \"DataSetList\":[\n" +
                buildDataSet("MMXU", "统计数据", "1", "A相", "Hz", "频率", 1, 4, "Hz") + ",\n" +
                buildDataSet("MSQI", "实时数据", "1", "A相", "A", "电流", 1, 2, "A") + ",\n" +
                buildDataSet("MHAI", "谐波数据", "1", "A相", "Har", "谐波", 1, 2, "%") + ",\n" +
                buildDataSet("MFLK", "波动闪变", "1", "A相", "Flk", "闪变", 1, 2, "pu") + "\n" +
                "  ]\n" +
                "}";
    }

    private String buildCheckedJson() {
        return "{\n" +
                "  \"IED\":\"IED1\",\n" +
                "  \"LD\":\"LD0\",\n" +
                "  \"DataType\":\"1\",\n" +
                "  \"unit\":\"ms\",\n" +
                "  \"ReportMap\":[\n" +
                "    {\"desc\":\"统计数据\",\"reportCount\":1,\"rptID\":\"rpt-stat\",\"name\":\"brcbStat\",\"buffered\":\"BR\",\"inst\":\"01\",\"FlickerFlag\":\"0\",\"Select\":\"part\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"波动闪变\",\"reportCount\":1,\"rptID\":\"rpt-flk\",\"name\":\"brcbFlk\",\"buffered\":\"BR\",\"inst\":\"02\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"实时数据\",\"reportCount\":1,\"rptID\":\"rpt-rt\",\"name\":\"brcbRt\",\"buffered\":\"RP\",\"inst\":\"03\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"暂态事件\",\"reportCount\":1,\"rptID\":\"rpt-tran\",\"name\":\"brcbTran\",\"buffered\":\"BR\",\"inst\":\"04\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"}\n" +
                "  ],\n" +
                "  \"DataSetList\":[\n" +
                buildDataSet("MMXU", "统计数据", "1", "A相", "Hz", "频率", 3, 6, "Hz") + ",\n" +
                buildDataSet("MSQI", "实时数据", "1", "A相", "A", "电流", 1, 2, "A") + ",\n" +
                buildDataSet("MHAI", "谐波数据", "1", "A相", "Har", "谐波", 1, 2, "%") + ",\n" +
                buildDataSet("MFLK", "波动闪变", "1", "A相", "Flk", "闪变", 1, 2, "pu") + "\n" +
                "  ]\n" +
                "}";
    }

    private String buildRtFreStandardJson() {
        return "{\n" +
                "  \"IED\":\"IED1\",\n" +
                "  \"LD\":\"LD0\",\n" +
                "  \"DataType\":\"1\",\n" +
                "  \"unit\":\"s\",\n" +
                "  \"ReportMap\":[\n" +
                "    {\"desc\":\"统计数据\",\"reportCount\":1,\"rptID\":\"rpt-stat\",\"name\":\"brcbStat\",\"buffered\":\"BR\",\"inst\":\"01\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"波动闪变\",\"reportCount\":1,\"rptID\":\"rpt-flk\",\"name\":\"brcbFlk\",\"buffered\":\"BR\",\"inst\":\"02\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"实时数据\",\"reportCount\":0,\"rptID\":\"demoRtFre\",\"name\":\"brcbRt\",\"buffered\":\"RP\",\"inst\":\"03\",\"FlickerFlag\":\"1\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"暂态事件\",\"reportCount\":1,\"rptID\":\"rpt-tran\",\"name\":\"brcbTran\",\"buffered\":\"BR\",\"inst\":\"04\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"}\n" +
                "  ],\n" +
                "  \"DataSetList\":[\n" +
                buildDataSet("MMXU", "统计数据", "1", "A相", "Hz", "频率", 1, 4, "Hz") + ",\n" +
                buildDataSet("MSQI", "实时数据", "1", "A相", "A", "电流", 1, 2, "A") + ",\n" +
                buildDataSet("MHAI", "谐波数据", "1", "A相", "Har", "谐波", 1, 2, "%") + ",\n" +
                buildDataSet("MFLK", "波动闪变", "1", "A相", "Flk", "闪变", 1, 2, "pu") + "\n" +
                "  ]\n" +
                "}";
    }

    private String buildRtFreCheckedJson() {
        return "{\n" +
                "  \"IED\":\"IED1\",\n" +
                "  \"LD\":\"LD0\",\n" +
                "  \"DataType\":\"1\",\n" +
                "  \"unit\":\"s\",\n" +
                "  \"ReportMap\":[\n" +
                "    {\"desc\":\"统计数据\",\"reportCount\":2,\"rptID\":\"rpt-stat\",\"name\":\"brcbStat\",\"buffered\":\"BR\",\"inst\":\"01\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"波动闪变\",\"reportCount\":1,\"rptID\":\"rpt-flk\",\"name\":\"brcbFlk\",\"buffered\":\"BR\",\"inst\":\"02\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"实时数据\",\"reportCount\":1,\"rptID\":\"demoRtFre\",\"name\":\"brcbRt\",\"buffered\":\"RP\",\"inst\":\"03\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"暂态事件\",\"reportCount\":1,\"rptID\":\"rpt-tran\",\"name\":\"brcbTran\",\"buffered\":\"BR\",\"inst\":\"04\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"}\n" +
                "  ],\n" +
                "  \"DataSetList\":[\n" +
                buildDataSet("MMXU", "统计数据", "1", "A相", "Hz", "频率", 1, 4, "Hz") + ",\n" +
                buildDataSet("MSQI", "实时数据", "1", "A相", "A", "电流", 1, 2, "A") + ",\n" +
                buildDataSet("MHAI", "谐波数据", "1", "A相", "Har", "谐波", 1, 2, "%") + ",\n" +
                buildDataSet("MFLK", "波动闪变", "1", "A相", "Flk", "闪变", 1, 2, "pu") + "\n" +
                "  ]\n" +
                "}";
    }

    private String buildDuplicateDoiDescJson() {
        return "{\n" +
                "  \"IED\":\"IED1\",\n" +
                "  \"LD\":\"LD0\",\n" +
                "  \"DataType\":\"1\",\n" +
                "  \"unit\":\"s\",\n" +
                "  \"ReportMap\":[\n" +
                "    {\"desc\":\"统计数据\",\"reportCount\":2,\"rptID\":\"rpt-stat\",\"name\":\"brcbStat\",\"buffered\":\"BR\",\"inst\":\"01\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"波动闪变\",\"reportCount\":1,\"rptID\":\"rpt-flk\",\"name\":\"brcbFlk\",\"buffered\":\"BR\",\"inst\":\"02\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"实时数据\",\"reportCount\":1,\"rptID\":\"rpt-rt\",\"name\":\"brcbRt\",\"buffered\":\"RP\",\"inst\":\"03\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"暂态事件\",\"reportCount\":1,\"rptID\":\"rpt-tran\",\"name\":\"brcbTran\",\"buffered\":\"BR\",\"inst\":\"04\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"}\n" +
                "  ],\n" +
                "  \"DataSetList\":[\n" +
                buildDataSetWithDuplicateDoiDesc("MMXU", "统计数据", "1", "A相") + ",\n" +
                buildDataSet("MSQI", "实时数据", "1", "A相", "A", "电流", 1, 2, "A") + ",\n" +
                buildDataSet("MHAI", "谐波数据", "1", "A相", "Har", "谐波", 1, 2, "%") + ",\n" +
                buildDataSet("MFLK", "波动闪变", "1", "A相", "Flk", "闪变", 1, 2, "pu") + "\n" +
                "  ]\n" +
                "}";
    }

    private String buildVoltageStartMetricsStandardJson() {
        return "{\n" +
                "  \"IED\":\"IED1\",\n" +
                "  \"LD\":\"LD0\",\n" +
                "  \"DataType\":\"1\",\n" +
                "  \"unit\":\"s\",\n" +
                "  \"ReportMap\":[\n" +
                "    {\"desc\":\"统计数据\",\"reportCount\":2,\"rptID\":\"rpt-stat\",\"name\":\"brcbStat\",\"buffered\":\"BR\",\"inst\":\"01\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"波动闪变\",\"reportCount\":1,\"rptID\":\"rpt-flk\",\"name\":\"brcbFlk\",\"buffered\":\"BR\",\"inst\":\"02\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"实时数据\",\"reportCount\":1,\"rptID\":\"rpt-rt\",\"name\":\"brcbRt\",\"buffered\":\"RP\",\"inst\":\"03\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"暂态事件\",\"reportCount\":1,\"rptID\":\"rpt-tran\",\"name\":\"brcbTran\",\"buffered\":\"BR\",\"inst\":\"04\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"}\n" +
                "  ],\n" +
                "  \"DataSetList\":[\n" +
                buildDataSetWithVoltageStartMetrics("MMXU", "统计数据", "1", "A相") + ",\n" +
                buildDataSet("MSQI", "实时数据", "1", "A相", "A", "电流", 1, 2, "A") + ",\n" +
                buildDataSet("MHAI", "谐波数据", "1", "A相", "Har", "谐波", 1, 2, "%") + ",\n" +
                buildDataSet("MFLK", "波动闪变", "1", "A相", "Flk", "闪变", 1, 2, "pu") + "\n" +
                "  ]\n" +
                "}";
    }

    private String buildVoltageStartMetricsWithoutSdiJson() {
        return "{\n" +
                "  \"IED\":\"IED1\",\n" +
                "  \"LD\":\"LD0\",\n" +
                "  \"DataType\":\"1\",\n" +
                "  \"unit\":\"s\",\n" +
                "  \"ReportMap\":[\n" +
                "    {\"desc\":\"统计数据\",\"reportCount\":2,\"rptID\":\"rpt-stat\",\"name\":\"brcbStat\",\"buffered\":\"BR\",\"inst\":\"01\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"波动闪变\",\"reportCount\":1,\"rptID\":\"rpt-flk\",\"name\":\"brcbFlk\",\"buffered\":\"BR\",\"inst\":\"02\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"实时数据\",\"reportCount\":1,\"rptID\":\"rpt-rt\",\"name\":\"brcbRt\",\"buffered\":\"RP\",\"inst\":\"03\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"暂态事件\",\"reportCount\":1,\"rptID\":\"rpt-tran\",\"name\":\"brcbTran\",\"buffered\":\"BR\",\"inst\":\"04\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"}\n" +
                "  ],\n" +
                "  \"DataSetList\":[\n" +
                buildDataSetWithVoltageStartMetricsWithoutSdi("MMXU", "统计数据", "1", "A相") + ",\n" +
                buildDataSet("MSQI", "实时数据", "1", "A相", "A", "电流", 1, 2, "A") + ",\n" +
                buildDataSet("MHAI", "谐波数据", "1", "A相", "Har", "谐波", 1, 2, "%") + ",\n" +
                buildDataSet("MFLK", "波动闪变", "1", "A相", "Flk", "闪变", 1, 2, "pu") + "\n" +
                "  ]\n" +
                "}";
    }

    private String buildEmptySdiListJson() {
        return "{\n" +
                "  \"IED\":\"IED1\",\n" +
                "  \"LD\":\"LD0\",\n" +
                "  \"DataType\":\"1\",\n" +
                "  \"unit\":\"s\",\n" +
                "  \"ReportMap\":[\n" +
                "    {\"desc\":\"统计数据\",\"reportCount\":2,\"rptID\":\"rpt-stat\",\"name\":\"brcbStat\",\"buffered\":\"BR\",\"inst\":\"01\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"波动闪变\",\"reportCount\":1,\"rptID\":\"rpt-flk\",\"name\":\"brcbFlk\",\"buffered\":\"BR\",\"inst\":\"02\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"实时数据\",\"reportCount\":1,\"rptID\":\"rpt-rt\",\"name\":\"brcbRt\",\"buffered\":\"RP\",\"inst\":\"03\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"},\n" +
                "    {\"desc\":\"暂态事件\",\"reportCount\":1,\"rptID\":\"rpt-tran\",\"name\":\"brcbTran\",\"buffered\":\"BR\",\"inst\":\"04\",\"FlickerFlag\":\"0\",\"Select\":\"all\",\"TrgOps\":\"dchg\"}\n" +
                "  ],\n" +
                "  \"DataSetList\":[\n" +
                buildDataSetWithoutSdiList("MMXU", "统计数据", "1", "A相", "Hz", "频率") + ",\n" +
                buildDataSet("MSQI", "实时数据", "1", "A相", "A", "电流", 1, 2, "A") + ",\n" +
                buildDataSet("MHAI", "谐波数据", "1", "A相", "Har", "谐波", 1, 2, "%") + ",\n" +
                buildDataSet("MFLK", "波动闪变", "1", "A相", "Flk", "闪变", 1, 2, "pu") + "\n" +
                "  ]\n" +
                "}";
    }

    private String buildDataSet(String lnClass, String groupDesc, String inst, String instDesc,
                                String doiName, String doiDesc, int start, int end, String unit) {
        return "    {\"desc\":\"" + groupDesc + "\",\"lnClass\":\"" + lnClass + "\",\"instList\":[{\"inst\":\"" + inst +
                "\",\"desc\":\"" + instDesc + "\",\"doiList\":[{\"name\":\"" + doiName + "\",\"desc\":\"" + doiDesc +
                "\",\"start\":" + start + ",\"end\":" + end + ",\"unit\":\"" + unit +
                "\",\"coefficient\":1.0,\"baseflag\":1,\"basecount\":1,\"icdcout\":10,\"sdiList\":[{\"name\":\"mag\",\"desc\":\"幅值\",\"typeList\":[{\"name\":\"f\",\"desc\":\"浮点\"}]}]}]}]}";
    }

    private String buildDataSetWithoutSdiList(String lnClass, String groupDesc, String inst, String instDesc,
                                              String doiName, String doiDesc) {
        return "    {\"desc\":\"" + groupDesc + "\",\"lnClass\":\"" + lnClass + "\",\"instList\":[{\"inst\":\"" + inst +
                "\",\"desc\":\"" + instDesc + "\",\"doiList\":[{\"name\":\"" + doiName + "\",\"desc\":\"" + doiDesc +
                "\",\"start\":1,\"end\":4,\"unit\":\"Hz\",\"coefficient\":1.0,\"baseflag\":1,\"basecount\":1,\"icdcout\":10,\"sdiList\":[]}]}]}";
    }

    private String buildDataSetWithDuplicateDoiDesc(String lnClass, String groupDesc, String inst, String instDesc) {
        return "    {\"desc\":\"" + groupDesc + "\",\"lnClass\":\"" + lnClass + "\",\"instList\":[{\"inst\":\"" + inst +
                "\",\"desc\":\"" + instDesc + "\",\"doiList\":[" +
                buildDoi("Hz2", "频率", 5, 8, "Hz") + "," +
                buildDoi("Hz", "频率", 1, 4, "Hz") +
                "]}]}";
    }

    private String buildDataSetWithVoltageStartMetrics(String lnClass, String groupDesc, String inst, String instDesc) {
        return "    {\"desc\":\"" + groupDesc + "\",\"lnClass\":\"" + lnClass + "\",\"instList\":[{\"inst\":\"" + inst +
                "\",\"desc\":\"" + instDesc + "\",\"doiList\":[" +
                buildDoi("Hz", "频率", 1, 4, "Hz") + "," +
                buildDoi("VolDistStr", "电压扰动事件启动", 5, 6, "") + "," +
                buildDoi("VolDipStr", "电压暂降事件启动", 7, 8, "") + "," +
                buildDoi("VolSwellStr", "电压暂升事件启动", 9, 10, "") + "," +
                buildDoi("VolInterStr", "电压中断事件启动", 11, 12, "") + "," +
                buildDoi("VolDipSet", "电压暂降启动定值", 13, 14, "V") + "," +
                buildDoi("VolSwellSet", "电压暂升启动定值", 15, 16, "V") + "," +
                buildDoi("VolInterSet", "电压中断启动定值", 17, 18, "V") +
                "]}]}";
    }

    private String buildDataSetWithVoltageStartMetricsWithoutSdi(String lnClass, String groupDesc, String inst, String instDesc) {
        return "    {\"desc\":\"" + groupDesc + "\",\"lnClass\":\"" + lnClass + "\",\"instList\":[{\"inst\":\"" + inst +
                "\",\"desc\":\"" + instDesc + "\",\"doiList\":[" +
                buildDoiWithoutSdi("VolDistStr", "电压扰动事件启动", 5, 6, "") + "," +
                buildDoiWithoutSdi("VolDipStr", "电压暂降事件启动", 7, 8, "") + "," +
                buildDoiWithoutSdi("VolSwellStr", "电压暂升事件启动", 9, 10, "") + "," +
                buildDoiWithoutSdi("VolInterStr", "电压中断事件启动", 11, 12, "") + "," +
                buildDoiWithoutSdi("VolDipSet", "电压暂降启动定值", 13, 14, "V") + "," +
                buildDoiWithoutSdi("VolSwellSet", "电压暂升启动定值", 15, 16, "V") + "," +
                buildDoiWithoutSdi("VolInterSet", "电压中断启动定值", 17, 18, "V") +
                "]}]}";
    }

    private String buildDoi(String doiName, String doiDesc, int start, int end, String unit) {
        return "{\"name\":\"" + doiName + "\",\"desc\":\"" + doiDesc +
                "\",\"start\":" + start + ",\"end\":" + end + ",\"unit\":\"" + unit +
                "\",\"coefficient\":1.0,\"baseflag\":1,\"basecount\":1,\"icdcout\":10,\"sdiList\":[{\"name\":\"mag\",\"desc\":\"幅值\",\"typeList\":[{\"name\":\"f\",\"desc\":\"浮点\"}]}]}";
    }

    private String buildDoiWithoutSdi(String doiName, String doiDesc, int start, int end, String unit) {
        return "{\"name\":\"" + doiName + "\",\"desc\":\"" + doiDesc +
                "\",\"start\":" + start + ",\"end\":" + end + ",\"unit\":\"" + unit +
                "\",\"coefficient\":1.0,\"baseflag\":1,\"basecount\":1,\"icdcout\":10,\"sdiList\":[]}";
    }
}
