package com.jiayue.ipp.idp.api;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jiayue.ipp.common.data.entity.ForecastPowerShortTerm;
import com.jiayue.ipp.common.data.entity.ForecastPowerShortTermHis;
import com.jiayue.ipp.common.data.entity.ForecastPowerUltraShortTerm;
import com.jiayue.ipp.common.data.entity.ForecastPowerUltraShortTermHis;
import com.jiayue.ipp.idp.dto.ForecastData;
import com.jiayue.ipp.idp.dto.ForecastDataApiDto;
import com.jiayue.ipp.idp.service.ForecastPowerShortTermHisService;
import com.jiayue.ipp.idp.service.ForecastPowerShortTermService;
import com.jiayue.ipp.idp.service.ForecastPowerUltraShortTermHisService;
import com.jiayue.ipp.idp.service.ForecastPowerUltraShortTermService;
import com.jiayue.ipp.idp.util.DateTimeUtil;
import com.jiayue.ipp.idp.util.DateTypeConversionUtils;
import com.jiayue.ipp.idp.util.R;
import javafx.scene.input.DataFormat;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * 预测接口
 *
 * @Author: tl
 * @Date: 2023/7/11 9:48
 */
@RequestMapping("forecastDataApi")
@RestController
public class ForecastDataApi {

    @Autowired
    private ForecastPowerShortTermHisService forecastPowerShortTermHisService;

    @Autowired
    private ForecastPowerShortTermService forecastPowerShortTermService;

    @Autowired
    private ForecastPowerUltraShortTermService forecastPowerUltraShortTermService;

    @Autowired
    private ForecastPowerUltraShortTermHisService forecastPowerUltraShortTermHisService;

    @GetMapping("/realTimeShortTerm240/{forecastManufactor}/{stationCode}")
    public R realTime240(@PathVariable String forecastManufactor, @PathVariable String stationCode) {


        try {

            List<ForecastPowerShortTerm> list = new ArrayList<>();
            long now = System.currentTimeMillis();
            LambdaUpdateWrapper<ForecastPowerShortTerm> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.between(ForecastPowerShortTerm::getForecastTime, new Date(DateTimeUtil.getDayStartTime(now + 86400000).getTime()+900000), new Date(DateTimeUtil.getDayLastTime(now + 864000000).getTime()+900000));
            lambdaUpdateWrapper.eq(ForecastPowerShortTerm::getForecastManufactor, forecastManufactor);
            lambdaUpdateWrapper.eq(ForecastPowerShortTerm::getStationCode, stationCode);
            forecastPowerShortTermService.list(lambdaUpdateWrapper).forEach(f -> list.add(f));
            list.stream().sorted(Comparator.comparing(ForecastPowerShortTerm::getForecastTime));
            ForecastDataApiDto<ForecastPowerShortTerm> forecastDataApiDto = new ForecastDataApiDto();
            forecastDataApiDto.setDate(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            forecastDataApiDto.setManufacturer("SYJY");
            forecastDataApiDto.setStationCode(stationCode);
            forecastDataApiDto.setForecastData(list);

            return R.ok(forecastDataApiDto, 200, "短期实时获取成功！");
        } catch (Exception e) {
            return R.failed(e, 500, "请求异常o(╥﹏╥)o");
        }


    }


    @GetMapping("/realTimeUltraShortTerm4/{forecastManufactor}/{stationCode}")
    public R realTime4(@PathVariable String forecastManufactor, @PathVariable String stationCode) {
        try {

            List<ForecastPowerUltraShortTerm> list = new ArrayList<>();
            long now = System.currentTimeMillis();
            DateTimeUtil.getMomentFor15Minute(now);

            LambdaUpdateWrapper<ForecastPowerUltraShortTerm> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.between(ForecastPowerUltraShortTerm::getForecastTime, new Date(now), new Date(now + 4 * 60 * 60 * 1000));
            lambdaUpdateWrapper.eq(ForecastPowerUltraShortTerm::getForecastManufactor, forecastManufactor);
            lambdaUpdateWrapper.eq(ForecastPowerUltraShortTerm::getStationCode, stationCode);
            forecastPowerUltraShortTermService.list(lambdaUpdateWrapper).forEach(f -> list.add(f));
            list.stream().sorted(Comparator.comparing(ForecastPowerUltraShortTerm::getForecastTime));
            ForecastDataApiDto<ForecastPowerUltraShortTerm> forecastDataApiDto = new ForecastDataApiDto();
            forecastDataApiDto.setDate(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            forecastDataApiDto.setManufacturer("SYJY");
            forecastDataApiDto.setStationCode(stationCode);
            forecastDataApiDto.setForecastData(list);

            return R.ok(forecastDataApiDto, 200, "超短期实时获取成功！");
        } catch (Exception e) {
            return R.failed(e, 500, "请求异常o(╥﹏╥)o");
        }


    }

    @GetMapping("/historicalShortTerm/{forecastManufactor}/{stationCode}/{howLongAgo}/{startTime}/{endTime}")
    public R historicalShortTerm(@PathVariable String forecastManufactor, @PathVariable String stationCode, @PathVariable String howLongAgo, @PathVariable Long startTime, @PathVariable Long endTime) {

        try {
            List<ForecastPowerShortTermHis> list = new ArrayList<>();

            LambdaUpdateWrapper<ForecastPowerShortTermHis> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.between(ForecastPowerShortTermHis::getForecastTime, new Date(startTime), new Date(endTime));
            lambdaUpdateWrapper.eq(ForecastPowerShortTermHis::getForecastManufactor, forecastManufactor);
            lambdaUpdateWrapper.eq(ForecastPowerShortTermHis::getStationCode, stationCode);
            lambdaUpdateWrapper.eq(ForecastPowerShortTermHis::getForecastHowLongAgo, howLongAgo);
            forecastPowerShortTermHisService.list(lambdaUpdateWrapper).forEach(f -> list.add(f));
            list.stream().sorted(Comparator.comparing(ForecastPowerShortTermHis::getForecastTime));
            ForecastDataApiDto<ForecastPowerShortTermHis> forecastDataApiDto = new ForecastDataApiDto();
            forecastDataApiDto.setDate(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            forecastDataApiDto.setManufacturer("SYJY");
            forecastDataApiDto.setStationCode(stationCode);
            forecastDataApiDto.setHowLongAgo(howLongAgo);
            forecastDataApiDto.setForecastData(list);

            return R.ok(forecastDataApiDto, 200, "短期历史获取成功！");
        } catch (Exception e) {
            return R.failed(e, 500, "请求异常o(╥﹏╥)o");
        }

    }

    @GetMapping("/historicalUltraShortTerm/{forecastManufactor}/{stationCode}/{howLongAgo}/{startTime}/{endTime}")
    public R historicalUltraShortTerm(@PathVariable String forecastManufactor, @PathVariable String stationCode, @PathVariable String howLongAgo, @PathVariable Long startTime, @PathVariable Long endTime) {

        try {

            List<ForecastPowerUltraShortTermHis> list = new ArrayList<>();
            LambdaUpdateWrapper<ForecastPowerUltraShortTermHis> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.between(ForecastPowerUltraShortTermHis::getForecastTime, new Date(startTime), new Date(endTime));
            lambdaUpdateWrapper.eq(ForecastPowerUltraShortTermHis::getForecastManufactor, forecastManufactor);
            lambdaUpdateWrapper.eq(ForecastPowerUltraShortTermHis::getStationCode, stationCode);
            lambdaUpdateWrapper.eq(ForecastPowerUltraShortTermHis::getForecastHowLongAgo, howLongAgo);
            forecastPowerUltraShortTermHisService.list(lambdaUpdateWrapper).forEach(f -> list.add(f));
            list.stream().sorted(Comparator.comparing(ForecastPowerUltraShortTermHis::getForecastTime));
            ForecastDataApiDto<ForecastPowerUltraShortTermHis> forecastDataApiDto = new ForecastDataApiDto();
            forecastDataApiDto.setDate(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            forecastDataApiDto.setManufacturer("SYJY");
            forecastDataApiDto.setStationCode(stationCode);
            forecastDataApiDto.setHowLongAgo(howLongAgo);
            forecastDataApiDto.setForecastData(list);

            return R.ok(forecastDataApiDto, 200, "超短期历史获取成功！");
        } catch (Exception e) {
            return R.failed(e, 500, "请求异常o(╥﹏╥)o");
        }

    }


    @GetMapping("/electricEnergy/{forecastManufactor}/{stationCode}")
    public R electricEnergy(@PathVariable String forecastManufactor, @PathVariable String stationCode) {

        DecimalFormat df = new DecimalFormat("0.00");
        try {

            List<ForecastPowerShortTerm> list = new ArrayList<>();
            long now = System.currentTimeMillis();
            LambdaUpdateWrapper<ForecastPowerShortTerm> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.between(ForecastPowerShortTerm::getForecastTime, new Date(DateTimeUtil.getDayStartTime(now + 86400000).getTime()+900000), new Date(DateTimeUtil.getDayLastTime(now + 864000000).getTime()+900000));
            lambdaUpdateWrapper.eq(ForecastPowerShortTerm::getForecastManufactor, forecastManufactor);
            lambdaUpdateWrapper.eq(ForecastPowerShortTerm::getStationCode, stationCode);
            forecastPowerShortTermService.list(lambdaUpdateWrapper).forEach(f -> list.add(f));
            list.stream().sorted(Comparator.comparing(ForecastPowerShortTerm::getForecastTime));
            ForecastDataApiDto<ForecastData> forecastDataApiDto = new ForecastDataApiDto();
            forecastDataApiDto.setDate(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            forecastDataApiDto.setManufacturer("SYJY");
            forecastDataApiDto.setStationCode(stationCode);

            List<ForecastData> forecastDatas = new ArrayList<>();
            int count = 0;
            double sum = 0;

            for (int i = 0; i < list.size(); i++) {
                ForecastPowerShortTerm forecastPowerShortTerm = list.get(i);
                sum += forecastPowerShortTerm.getFpValue().doubleValue();
                count++;

                if (count == 4) {
                    double average = sum / count;
                    ForecastData forecastData = new ForecastData();
                    forecastData.setTime(DateUtil.format(forecastPowerShortTerm.getForecastTime(), "yyyy-MM-dd HH"));
                    forecastData.setPower(df.format(average));
                    forecastDatas.add(forecastData);
                    count = 0;
                    sum = 0;
                }
            }
            forecastDataApiDto.setForecastData(forecastDatas);

            return R.ok(forecastDataApiDto, 200, "发电量获取成功！");
        } catch (Exception e) {
            return R.failed(e, 500, "请求异常o(╥﹏╥)o");
        }

    }

}
