package com.nantian.cloud.dbcapp.unitTest;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.nantian.cloud.dbcapp.service.*;
import com.nantian.cloud.dbcapp.task.CalculateTask;
import com.nantian.cloud.dbcapp.unit.DateUtil;
import com.nantian.cloud.dbcapp.vo.*;
import com.nantian.sc.cloud.common.core.util.R;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class UnitTest {
    @Autowired
    EnterpriseService enterpriseService;
    @Autowired
    EnergyService energyService;
    @Autowired
    ElectricityService electricityService;
    @Autowired
    BigScreenService bigScreenService;
    @Autowired
    CalculateTask calculateTask;
    @Autowired
    DataHomeService dataHomeService;


    @Test  //查询各能源使用情况
    public void energyValue() {
        energyService.getEnergyWarning("普洱", "全部", "2021-01-01&2022-01-31");
        //enterpriseService.getListValue("915304256908928184", "2021-10", "00");
        // 数据采集情况
        // energyService.getUpCollectByDayNew("云南", "全部", "2021-09");
    }

    @Test //获取综合能耗
    public void zhnh() throws ParseException {
        //enterpriseService.getTotalZhnh("91533321MA6KAMNM7A", "2021-01-01&2021-10-31");
        //Map<String, Object> energyByDate = energyService.getEnergyByDate("云南", "全部", "2018-01-01&2018-12-31");
        //Map<String, Object> energyByArea = energyService.getEnergyByArea("云南", "全部", "2018-01-01&2018-12-31");
        Map map = enterpriseService.getAccessEnergysTructure("91530124772672325G");
        Map map5 = enterpriseService.getAccessEnergysTructure("91530300695670763K");
        Map map6 = enterpriseService.getAccessEnergysTructure("91530113772668027B");
        System.out.println("查询完成");
    }

    @Test
    public void zhnhByMonth() throws ParseException {
        //按月统计综合能耗列表
        //enterpriseService.getZhnhByMonthList("91530381MACDRHPK7J","2023-01-01&2023-05-31","00");
        //查询企业实物量
        //enterpriseService.getMaterialValue("91530381MACDRHPK7J","3300","2023-01-01&2023-05-31","00");
        //按月统计企业产品产量
        //enterpriseService.getProductByMonthList("91530381MACDRHPK7J","2023-01-01&2023-05-31");
        //对标管理-产品名称
        //List<String> epc = enterpriseService.getProductByEpc("91530381MACDRHPK7J", "2023-01-01&2023-05-31");
        //大屏-超限企业
        List<Map<String, String>> limit = dataHomeService.getOverLimit("2023-01", "2023-04");
        System.out.println("查询完成");
    }

    @Test
    public void zhnhNew() throws ParseException {
        Map<String, Object> map = energyService.getCoalConsumption("云南", "全部", "2022-03-01&2022-03-31");
        System.out.println("已完成");
    }

    @Test //查询企业单耗信息
    public void unitConsumption() throws ParseException {
        List<String> monthBetween = DateUtil.getMonthBetween("2016-01", "2021-02");
        List<JSONArray> list = new ArrayList<>();
        for (String month : monthBetween) {
            JSONArray jsonArray = enterpriseService.getUnitConsumption("91530000218880856K", "可比熟料综合能耗(历史)", month);
            list.add(jsonArray);
        }
        System.out.println(list);
        //企业单耗信息
        //enterpriseService.getUnitConsumption("91530000218880856K","可比熟料综合能耗(历史)","2016-01&2018-08");
        //指标排序
//        enterpriseService.getOrderByQuota("530000","91530328775526884A","铝锭综合交流电耗","2021-01&2021-11");
    }

    @Test
    public void testGetDetailMonthByDataCodeName(){
        enterpriseService.getDetailMonthByDataCodeName("91532932665546120J","2021-01","0000000008000072");
    }

    @Test
    public void getIndexName() throws ParseException {
        //获取工序信息
        //enterpriseService.getIndexName("91530425217792969W","2021-01-01&2021-05-31");
        //enterpriseService.getIndexName("91530425217792969W","2021-01&2021-05");
        //enterpriseService.getIndexName("91530425217792969W","2021-01");
        enterpriseService.getDetailHaveOne("915301297571665710","2022","0000000004000130");
        //首页仪表盘数据获取
        energyService.getCollectAndElectricityList("云南", "全部", "2022-01-01&2022-06-31", "");
    }

    @Test
    public void electricityValue() throws ParseException {
        //电力数据
        ElectricityIndex data = electricityService.getEnterpriseInfoData("91530328775526884A", "2015-01&2020-08");
        //ElectricityIndex dataDetail = electricityService.getEnterpriseInfoDataDetail("91530100MA6KKEQ68F", "2015-10");
        System.out.println("11111111111111111");
    }

    @Test
    public void getIndex() throws ParseException {
        enterpriseService.getValueByProcedure("91530325MA6N9Y8P6F","", "2022-11",null, null, null,null);
//        List<String> dataCodeList = energyService.parseFormulToSimpleAndParamsMap("云南", "全部", "2020-01&" + "2023-04", "2020-01", "2023-04", null);
//        energyService.renewEnergyConsumptionByForuml(dataCodeList, "2020-01", "2023-04");
    }

    @Test
    public void getExceptionInfo(){
        //企业首页查询异常（波动异常、传输异常）
        //Map<String, Object> map = enterpriseService.getExceptionInfo("91530425217792969W", "2022-01-01&2022-08-01");
        //异常数据明细（含企业二级界面）
        //ExceptionIndexInfo exceptionInfo = energyService.getExceptionInfo("云南", "", "2022-01-01&2022-08-01", "91530425217792969W", "2", 1, 100, "天然气", "");
        //按月展示异常数据趋势图
        //List<CoalVo> montList = energyService.getExceptionInfoList("云南", "", "2022-01-01&2022-08-01", "91530425217792969W", "1");
        //异常类型排名
        //R typeOrder = energyService.getExceptionOrder("云南", "", "2022-01-01&2022-08-01", "91530425217792969W", "1");
        //异常地区排名
        R areaOrder = energyService.getExceptionByAreaOrder("云南", "", "2022-01-01&2022-08-01", "91530425217792969W", "1");
        //异常企业排名
        //R enterpriseOrder = energyService.getExceptionEnterpriseOrder("云南", "", "2022-01-01&2022-08-01", "91530425217792969W", "1");
        ////在线数据异常占比
        energyService.getCollectByArea("云南", "全部", "2022-01-01&2022-07-31");
        energyService.getCollectByIndustry("云南", "全部", "2022-01-01&2022-07-31");
        energyService.getCollectByMonth("云南", "全部", "2022-01-01&2022-07-31");
        energyService.getCollectByDay("云南", "全部", "2022-01-01&2022-07-31");
        System.out.println("------------------------------");
    }

    @Test
    public void gteMonthReport() throws ParseException {
        //Map<String, Object> map = enterpriseService.getMaterialValue("915301247604411758", "3300", "2022-01-01&2022-10-31", "00");
        //System.out.println(map);
        //在线监测月报告
        //R tableInfos = energyService.getEnergyTableInfos("2022-09", null);
        //行业在线采集数据显示
        //R collect = energyService.getCollectByIndustry("云南", "全部", "2022-01-01&2022-07-31");
        //大屏在线监测企业数据显示
        //bigScreenService.getAllEnergyZxjcYear();
        System.out.println("完成");
        //能源消费结构表
        //energyService.getConsumption("云南", "全部", "2022-01-01&2022-10-31");
        JSONArray consumption1 = enterpriseService.getConsumption("91530402217670662B", "2015-10-01&2020-09-31");
        //JSONArray consumption2 = enterpriseService.getConsumption("91530300217221381D", "2015-10-01&2021-08-31");
        //JSONArray consumption3 = enterpriseService.getConsumption("91530100216585124P", "2015-01-01&2022-12-31");
        System.out.println("完成");
    }

    //计算大屏支撑数据
    @Test
    public void getBigScreenData() throws ParseException {
        //企业信息
//        bigScreenService.getAllEnergyZxjcYear();
//        bigScreenService.getAllEnergyDlgkYear();
//        bigScreenService.getAllEnergyDjgjYear();
//        bigScreenService.getAllEnergyNameYear();
        //年度能耗数据
//        bigScreenService.getAllTce();
//        bigScreenService.getAllAreaByYear();
//        bigScreenService.getAllHyByYear();
//        bigScreenService.getAllWaterYear();
//        energyService.getWaterConsumption("云南", "全部", "2022-01-01&2022-12-31");
//        bigScreenService.getAllElectricityYear();
//        bigScreenService.getAllXfjgYear();
//        bigScreenService.getAllMeiYear();
        //近6月能耗数据
//        bigScreenService.getAllTce6();
//        bigScreenService.getAllAreaByYear6();
//        bigScreenService.getAllHyByYear6();
//        bigScreenService.getAllWaterYear6();
//        bigScreenService.getAllElectricityYear6();
//        bigScreenService.getAllMeiYear6();
        //能报数据推送
//        calculateTask.reportPush();
//        List<CompanyEnergyInfoVo> energyInfo = energyService.getCompanyListEnergyInfo("云南", "全部", "2022-08-01&2023-01-31", "中石油云南石化有限公司");
//        System.out.println("----完成----");
        //结算日志查询
//        QualityCalculateLogVo calculateLogsVo = new QualityCalculateLogVo();
//        calculateLogsVo.setOperateType("2");
//        calculateLogsVo.setUploadTime("2023-02-17");
//        Page page = new Page();
//        energyService.getPageInfoList(page, calculateLogsVo);
        //停产状态判断提示
        Map<String, Object> map = enterpriseService.getEnterpriseStatusChange("91530325MA6N9Y8P6F");
        System.out.println("完成");
    }

    //中间表 t_energy_consumption 获取能源消耗
    @Test
    public void energyConsumption() throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
        String date = df.format(new Date());
        //获取公式中计算点
        List<String> dataCodeList = energyService.parseFormulToSimpleAndParamsMap("云南", "全部", "2020-01&" + date, "2020-01", date, null);
        energyService.renewEnergyConsumptionByForuml(dataCodeList, "2020-01", date);
    }

    //公式解析
    @Test
    public void getDataCode(){
        String formul = "#f0000000001010211*#r0000000001010211" +
                "-#f0000000002090011*#r0000000002090011*0.56/100" +
                "-#f0000000002100011*#r0000000002100011/1000" +
                "+#f0000000002210011*#r0000000002210011" +
                "+#f0000000002330011*#r0000000002330011*1.229/10000";
        Pattern shouldConvertCompile = compile("#f[0-9A-Z]{16}[*]#r[0-9A-Z]{16}");
        Matcher shouldConvertMatcher = shouldConvertCompile.matcher(formul);
        List<String> codeSegment = new ArrayList();
        while (shouldConvertMatcher.find()) {
            codeSegment.add(shouldConvertMatcher.group());
        }

        for (int i=0; i< codeSegment.size(); i++) {
            //当前字符串
            String currentStr = codeSegment.get(i);
            //下一个字符串
            String nextStr = (i == codeSegment.size()-1) ? "" : codeSegment.get(i+1);
            System.out.println("-----------------------------------------------");
            //取出需要折标的采集点
            System.out.println("tmp："+currentStr);
            System.out.println("code："+currentStr.substring(2,18));

            int currentIndex = formul.indexOf(currentStr);
            int symbol = 1; //默认正号
            if(i != 0){ //不是第一段，需要判断符号
                String frontStr = formul.substring(currentIndex-1);
                System.out.println("frontStr: "+frontStr);
                if(frontStr.startsWith("-")){ //负号
                    symbol = -1;
                }
            }
            System.out.println("symbol: "+symbol);

            String ratio = "1"; //默认为1，不用折算
            String behindStr = formul.substring(currentIndex + 37);
            System.out.println("behindStr: "+behindStr);
            if(behindStr.startsWith("*") || behindStr.startsWith("/")){ //需要折算
                if(StringUtils.isNotEmpty(nextStr)){
                    int nextIndex = formul.indexOf(nextStr);
                    ratio = "1"+formul.substring(currentIndex+37, nextIndex-1);
                }else if(StringUtils.isNotEmpty(behindStr)){
                    ratio = "1"+behindStr;
                }
            }
            System.out.println("ratio: "+ratio);
            System.out.println("-----------------------------------------------");

        }
    }
}
