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.PreventReplay;
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;

    /**
     * 获取短期分页信息
     *
     * @return 用户信息
     */
    @GetMapping(value = "/getAll")
    @PreAuthorize("@ss.hasPermi('biz:dq:getAll')")
    @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不能是空！");
            }

            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);
        }
    }
}
