package com.jiayue.ssi.controller;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiayue.ssi.annotation.OperateLog;
import com.jiayue.ssi.annotation.PreventReplay;
import com.jiayue.ssi.backenum.AuditType;
import com.jiayue.ssi.backenum.BusinessType;
import com.jiayue.ssi.constant.CustomException;
import com.jiayue.ssi.entity.*;
import com.jiayue.ssi.service.*;
import com.jiayue.ssi.util.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 短期接口
 *
 * @author xsl
 * @since 2023/03/13
 */
@RestController
@RequestMapping("/forecastPowerShortTermController")
@Slf4j
public class ForecastPowerShortTermController {
    @Autowired
    ForecastPowerShortTermService forecastPowerShortTermService;
    @Autowired
    SysParameterService sysParameterService;

    /**
     * 获取短期分页信息
     *
     * @return 用户信息
     */
    @GetMapping(value = "/getAll")
    @PreAuthorize("@ss.hasPermi('biz:dq:getAll')")
    @OperateLog(title = "短期预测", businessType = BusinessType.QUERY, auditType = AuditType.BIZ,operdesc = "查询短期预测数据")
    @PreventReplay
    public ResponseVO getAll(String currentPage, String pageSize, String startTime, String endTime) throws CustomException {
        try {
            Integer cp;
            if (StringUtils.isNotEmpty(currentPage)) {
                if (NumberUtil.isInteger(currentPage)){
                    cp = Integer.parseInt(currentPage);
                }
                else{
                    return ResponseVO.fail("currentPage不是整数！");
                }
            }
            else{
                return ResponseVO.fail("currentPage不能是空！");
            }

            Integer ps;
            if (StringUtils.isNotEmpty(pageSize)) {
                if (NumberUtil.isInteger(pageSize)){
                    ps = Integer.parseInt(pageSize);
                }
                else{
                    return ResponseVO.fail("pageSize不是整数！");
                }
            }
            else{
                return ResponseVO.fail("pageSize不能是空！");
            }

            if (NumberUtil.isLong(startTime) && NumberUtil.isLong(endTime)){
                if (Long.parseLong(endTime)-Long.parseLong(startTime)<0){
                    return ResponseVO.fail("开始时间大于截止时间！");
                }
            }
            else {
                return ResponseVO.fail("查询时间不正确！");
            }

            int day = 3;
            String ycMaxDay = sysParameterService.queryByKey("YC_MAX_DAY","3");
            if (NumberUtil.isInteger(ycMaxDay)){
                day = Integer.parseInt(ycMaxDay);
                if (day<1 || day>7){
                    return ResponseVO.fail("获取参数中预测最大天数应该设置1-7天之间");
                }
            }
            else{
                return ResponseVO.fail("预测查询最大天数不是整型数值！");
            }

            // 页面日期天数是否<=参数值
            if (Long.parseLong(endTime)-Long.parseLong(startTime)> 60 * 60 * 24 * 1000*day){
                return ResponseVO.fail("最多查询"+day+"天的数据哦！");
            }

            QueryWrapper<ForecastPowerShortTerm> wrapper = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(startTime)) {
                wrapper.ge("forecast_time", startTime);
            }
            if (StringUtils.isNotEmpty(endTime)) {
                wrapper.le("forecast_time", endTime);
            }
            wrapper.orderByAsc("forecast_time");
            Page<ForecastPowerShortTerm> result = forecastPowerShortTermService.page(new Page<>(cp, ps), wrapper);
            return ResponseVO.success(result);
        } catch (Exception e) {
            throw new CustomException("获取短期列表异常", e);
        }
    }
    /**
     * 按时间查询实时预测短期
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 结果集
     */
    @GetMapping(value = "/getDraw")
    @PreAuthorize("@ss.hasPermi('biz:dq:getAll')")
    @PreventReplay
    public ResponseVO getDraw(String startTime, String endTime) throws CustomException{
        Map<String,Object> map = new HashMap<>();
        try{
            map = forecastPowerShortTermService.findByForecastTimeBetween(Long.parseLong(startTime),Long.parseLong(endTime));
            return ResponseVO.success(map);
        } catch (Exception e) {
            throw new CustomException("获取短期图形数据异常", e);
        }
    }
}
