package com.ruoyi;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateRange;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.ucp.entity.*;
import com.ruoyi.ucp.feign.AdapterApi;
import com.ruoyi.ucp.service.*;
import com.ruoyi.ucp.util.CalcCache;
import com.ruoyi.web.controller.JavaFunctionJobHandler;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.net.URI;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;

@SpringBootTest//classes = RuoYiApp.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
@RunWith(SpringRunner.class)
public class NewTests {

    @Resource
    private JavaFunctionJobHandler task;
    @Resource
    private IStationInfoMinService stationInfoMinService;

    @Test
    public void test() {
//        task.calcTurbineRFDLSameDay();
//        task.calcTurbine5s2SameDay();
        /*Map<Double, Double> a1 = CalcCache.fitcoef.get("GJNY_SXGS_FSG_F_WT_0040_EQ");
        Map<Double, Double> a3 = CalcCache.fitcoef.get("GJNY_SXGS_FSG_F_WT_0047_EQ");
        TreeMap<Double, Double> b1 = new TreeMap<>();
        TreeMap<Double, Double> b3 = new TreeMap<>();
        b1.putAll(a1);
        b3.putAll(a3);
        StrBuilder sb = new StrBuilder();
        for (Double v : b1.keySet()) {
            sb.append(v).append(",");
        }
        System.out.println(sb);
        StrBuilder sb2 = new StrBuilder();
        for (Double v : b1.values()) {
            sb2.append(v).append(",");
        }
        System.out.println(sb2);
        StrBuilder sb3 = new StrBuilder();
        for (Double v : b3.values()) {
            sb3.append(v).append(",");
        }
        System.out.println(sb3);
        System.out.println();*/

        Date date = DateUtil.parse("2024-10-10");
//        task.calcTurbineSsfsQx(date);
//        task.ambientTemperature();

//        task.calcTurbine5s2(date);
//        task.calcLineRfdl(date);

//        task.calcStationZhcyRfDl(date,"GJNY_SXGS_XZ_FDC_STA");
//        task.writeReportPool(date);
//        task.writeReportPoolProject(date);

//        task.calcTurbine5s2(date);
//        task.calcStationSwGwCyRdl(date,"GJNY_SXGS_XZ_FDC_STA");
//        task.calcStationZhcyRfDl(date);
//        task.writeReportPool(date);
//        task.writeReportPoolPjfs2(date);
//        task.SwdlRepair(date);
//        task.calcLineRfdl(date);
//        task.calcTurbineStateMin(date);
//        calcStationZhcydl(date);
//        calcNhglZs(date);
        // 创建日期范围生成器
//        DateTime date = DateUtil.date();
//        task.calcTurbine5s2(date);


//        DateTime start = DateUtil.parse("2023-06-27");
//        DateTime end = DateUtil.parse("2023-09-27");
//        task.calcGlqxnhSurplus(start, end);
//        DateRange range = DateUtil.range(start, end, DateField.DAY_OF_YEAR);
//        for (DateTime date : range) {
//            System.out.println(DateUtil.date().toString() + "：" + date.toString());

            /*task.calcLineRfdl(date);
            //上网购网厂用电量
            task.calcStationSwGwCyRdl(date);
            //计算综合厂用电量
            task.calcStationZhcyRfDl(date);*/


//            task.calcTurbineRFDL(date);
//            task.writeReportPoolPjfs2(date);
        //安全天数
//            calcAQTS(time.toJdkDate());
            /*task.calcLineRfdl(date,"GJNY_SXGS_ZZ_FDC_STA");
            task.calcStationSwGwCyRdl(date);
            task.calcStationZhcyRfDl(date);
            task.calcLineSwGWCyDl(date);
            task.writeReportPool(date);
            task.writeReportPoolProject(date);*/
//            task.calcLineSwGWCyDl(date,"GJNY_SXGS_XZ_FDC_STA");
//            task.writeReportPoolProject(date);
//            //计算综合厂用电量
//            task.calcStationZhcydl(date);
//
            /*task.calcTurbineRFDL(date);
            task.calcStationRfdlFj(date);
            task.calcTurbine5s2(date);
            task.writeReportPool5SPjfsLlfdl(date);*/

//            task.writeReportPool(date);
//            int test = task.test(date, "zuozhuang.610825F02Y0AFA10GS009DQ0251DG02RAW00", 1320);
//            System.out.println(test);


            /*task.calcLineRfdlSameDay();
            task.calcStationSwGwCyDlSameDay();
            task.calcStationZhcyRfDlSameDay();
            task.calcLineSwGWCyDlSameDay();
            task.writeReportPoolSameDay();
            task.writeReportPoolProjectSameDay();*/
//    }
//        calcTurbineRFDL(date);
//        task.calcTurbine5s2(date);
//        writeReportPool(date);

//        task.calcStationSspjfs();
        /*while (true) {
            task.calcRealtimeTurbineZt();
            ThreadUtil.sleep(1000);
        }*/
//        task.calcRealtimeTurbineZt();
    }

    @Test
    public void test2() {
        // 创建日期范围生成器
        DateTime start = DateUtil.parse("2024-10-02");
        DateTime end = DateUtil.parse("2024-11-09");
        DateRange range = DateUtil.range(start, end, DateField.DAY_OF_YEAR);
        for (DateTime date : range) {
            System.out.println(DateUtil.date() + "：" + date);
//            task.calcAQTS(date);
//            task.calcLineRfdl(date,"GJNY_SXGS_JSL_FDC_STA");
//            task.calcStationSwGwCyRdl(date,"GJNY_SXGS_JSL_FDC_STA");
//            task.calcStationZhcyRfDl(date,"GJNY_SXGS_JSL_FDC_STA");
//            task.calcLineSwGWCyDl(date,"GJNY_SXGS_JSL_FDC_STA");
//            task.writeReportPool(date);
//            task.writeReportPoolProject(date);
//            task.calcStationHourPjglRfdl(DateUtil.date());
            task.calcTurbine5s2(date);
        }
//        task.calcStationHourPjglRfdl(DateUtil.date());
        DateTime date = DateUtil.date();
//        task.calcStationHourPJGL(date);
//        task.calcLineRfdl(date);
//        task.calcStationHourRFDL(date);
//        task.calcTurbinePjfsPjglKyglLlglMin(DateUtil.date(), 15);
//        task.calcStationLlglKyglSjglAgcMin(date, 15);
    }

    @Test
    public void test3() {
        // 创建日期范围生成器
        DateTime start = DateUtil.parse("2024-01-01");
        DateTime end = DateUtil.parse("2024-10-01");
        DateRange range = DateUtil.range(start, end, DateField.MONTH);
        for (DateTime date : range) {
            System.out.println(DateUtil.date() + "：" + date);
//            task.calcStationRjhfdl(date);

        }
    }

    @Test
    public void test4() {
//        while (true){
//            task.calcStationRealtimeLLgl();
//            ThreadUtil.sleep(60000);
//        }
        DateTime start = DateUtil.parse("2023-12-01");
        DateTime end = DateUtil.parse("2023-12-01");
        DateRange range = DateUtil.range(start, end, DateField.MONTH);
        List<String> stIds = new ArrayList<>();
        stIds.add("GJNY_SXGS_FSG_FDC_STA");
        stIds.add("GJNY_SXGS_JSL_FDC_STA");
        stIds.add("GJNY_SXGS_CSL_FDC_STA");
        stIds.add("GJNY_SXGS_ZZ_FDC_STA");
        stIds.add("GJNY_SXGS_XZ_FDC_STA");
        stIds.add("GJNY_SXGS_LJS_FDC_STA");
        for (DateTime date : range) {
//            task.calcTurbineStateMin(date);
            task.predictExaminBc(stIds,date);
        }
//        task.calcGlqxnhSurplus(start, end);

    }

    @Test
    public void test5() {
        DateTime date = DateUtil.date();
        //task.calcTurbineStateMin(date);
        task.calcStationRjhfdl(date);
    }

    public static void main(String[] args) {
        DateTime date = DateUtil.date();
        DateRange months = DateUtil.range(date, DateUtil.endOfYear(date), DateField.MONTH);
        for (DateTime month : months) {
            System.out.println(month);
        }
    }

    @Test
    public void test6() {
        DateTime begin = DateUtil.parse("2024-11-07 22:30:00");
        DateTime end = DateUtil.parse("2024-11-07 22:46:00");
//        DateTime date = DateUtil.date();
//        DateTime begin = DateUtil.beginOfDay(date);
//        begin = DateUtil.offsetMinute(begin, 15);
//        DateTime end = date;
        int i = 1;
        DateTime minBegin = begin, minEnd = DateUtil.offsetMinute(begin, 15);
        while (minEnd.isBefore(end)) {
            minEnd = DateUtil.offsetMinute(begin, i * 15);
            task.calcStationLlglKyglSjglAgcMin(minBegin, 15);
//            task.calcStationSjglAgcPjfsHjwdDlMin(minEnd);
            System.out.println(minBegin.toString() + "," + minEnd.toString());
            i++;
            minBegin = minEnd;
        }
    }
    @Test
    public void test7() {
        DateTime begin = DateUtil.parse("2024-01-01");
        DateTime end = DateUtil.parse("2024-11-05 14:30:00");
        int i = 1;
        DateTime minBegin = begin, minEnd = DateUtil.offsetMinute(begin, 15);
        List<String> stIds = new ArrayList<>();
        stIds.add("GJNY_SXGS_FSG_FDC_STA");
        stIds.add("GJNY_SXGS_JSL_FDC_STA");
        stIds.add("GJNY_SXGS_CSL_FDC_STA");
        stIds.add("GJNY_SXGS_ZZ_FDC_STA");
        stIds.add("GJNY_SXGS_XZ_FDC_STA");
        stIds.add("GJNY_SXGS_LJS_FDC_STA");
        QueryWrapper<StationInfoMin> wrapper=new QueryWrapper<>();
        wrapper.lambda().between(StationInfoMin::getRecordDate,begin,end);
        List<StationInfoMin> list = stationInfoMinService.list(wrapper);
        Map<String, List<StationInfoMin>> collect = list.stream().collect(Collectors.groupingBy(m -> DateUtil.date(m.getRecordDate()).toString()));
        while (minEnd.isBefore(end)) {
            minEnd = DateUtil.offsetMinute(begin, i * 15);
//            task.ycbs(stIds,minBegin,minEnd,collect.get(minEnd.toString()));
            System.out.println(minBegin.toString() + "," + minEnd.toString());
            i++;
            minBegin = minEnd;
        }
    }
    @Test
    public void test8() {
        DateTime begin = DateUtil.parse("2024-11-05 14:30:00");
        DateTime end = DateUtil.parse("2024-11-07 10:00:00");
        int i = 1;
        DateTime minBegin = begin, minEnd = DateUtil.offsetMinute(begin, 15);
        List<String> stIds = new ArrayList<>();
        stIds.add("GJNY_SXGS_FSG_FDC_STA");
        stIds.add("GJNY_SXGS_JSL_FDC_STA");
        stIds.add("GJNY_SXGS_CSL_FDC_STA");
        stIds.add("GJNY_SXGS_ZZ_FDC_STA");
        stIds.add("GJNY_SXGS_XZ_FDC_STA");
        stIds.add("GJNY_SXGS_LJS_FDC_STA");
        QueryWrapper<StationInfoMin> wrapper=new QueryWrapper<>();
        wrapper.lambda().between(StationInfoMin::getRecordDate,begin,end);
        List<StationInfoMin> list = stationInfoMinService.list(wrapper);
        Map<String, List<StationInfoMin>> collect = list.stream().collect(Collectors.groupingBy(m -> DateUtil.date(m.getRecordDate()).toString()));
        while (minEnd.isBefore(end)) {
            minEnd = DateUtil.offsetMinute(begin, i * 15);
//            task.ycbs2(stIds,minBegin,minEnd,collect.get(minEnd.toString()));
            System.out.println(minBegin.toString() + "," + minEnd.toString());
            i++;
            minBegin = minEnd;
        }
    }
}
