package com.jjt.data;

import com.jjt.JjtApplication;
import com.jjt.calc.service.ITwinCalcHourEnergyService;
import com.jjt.common.utils.DateUtils;
import com.jjt.utils.IotService;
import com.jjt.utils.Tools;
import com.jjt.ws.service.ITwinWorkshopCalcService;
import com.jjt.ws.service.ITwinWorkshopEnergyService;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * 印花机
 */
@SpringBootTest(classes = JjtApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("devp")
public class EnergyTest {
    @Resource
    private IotService iotService;
    @Resource
    private SqlSessionFactory factory;
    @Resource
    private ITwinWorkshopEnergyService energyService;
    @Resource
    private ITwinCalcHourEnergyService calcHourEnergyService;
    @Resource
    private ITwinWorkshopCalcService calcService;

    public static void main(String[] args) {
        String st = "2025-03-04";
        LocalDate localDate = LocalDate.parse(st);
        LocalDateTime start = LocalDateTime.of(localDate, LocalTime.MIN);
        LocalDateTime end = start.plusHours(1);
        LocalDateTime stop = Tools.currWholeTime();
        while (!end.isAfter(stop)) {
            int i = start.getHour();
            System.err.println(DateUtils.parseTimeToStr(start) + "\t" + i + DateUtils.parseTimeToStr(end));
//            calcHourEnergyService.calcEnergy(start, end);
//            if (i == 6) {
//                calcService.calc(start.toLocalDate().minusDays(1));
//            }
            start = end;
            end = end.plusHours(1);
        }
    }

    @Test
    void test() {
        iotService.setToken();
        String st = "2025-06-12";
        LocalDate localDate = LocalDate.parse(st);
        LocalDateTime start = LocalDateTime.of(localDate, LocalTime.MIN).plusHours(7);
        LocalDateTime end = start.plusHours(1);
        LocalDateTime stop = Tools.currWholeTime();
        while (!end.isAfter(stop)) {
            int i = start.getHour();
            System.err.println(DateUtils.parseTimeToStr(start) + "\t" + i + DateUtils.parseTimeToStr(end));
            calcHourEnergyService.calcEnergy(start, end);
            if (i == 6) {
                calcService.calc(start.toLocalDate().minusDays(1));
            }
            start = end;
            end = end.plusHours(1);
        }
    }

    @Test
    void day() {
        for (int i = 10; i < 23; i++) {
            String d = "2025-04-" + (i < 10 ? "0" + i : i);
            LocalDate localDate = LocalDate.parse(d);
            calcService.calc(localDate);
        }

//        String d = "2025-04-08";
//        LocalDate localDate = LocalDate.parse(d);
//        calcService.calc(localDate);
    }
}
