package com.jiayue.ipfcst.console.controller;


import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.common.data.entity.ManualInterventionData;
import com.jiayue.ipfcst.console.service.ManualInterventionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 人工干预数据控制类
 *
 * @author whc
 * @version 2.0
 * @since 2020/08/24 16:43
 */
@RestController
@RequestMapping("/manualInterventionController/")
@Slf4j
public class ManualInterventionController {
    private final ManualInterventionService manualInterventionService;

    public ManualInterventionController(ManualInterventionService manualInterventionService) {
        this.manualInterventionService = manualInterventionService;
    }

    @GetMapping(value = "queryManualInterventionData/{startTime}/{endTime}/{fileType}")
    public ResponseVO queryManualInterventionData(@PathVariable Long startTime,
                                                                    @PathVariable Long endTime,
                                                                    @PathVariable String fileType){
      List<ManualInterventionData> list = new ArrayList<>();
      try {
            list= manualInterventionService.queryManualIntervention(startTime,endTime,fileType);
            return ResponseVO.success(list);
        }catch (Exception e) {
            log.error("系统错误：", e);
        } finally {
            list = manualInterventionService.queryManualIntervention(startTime,endTime,fileType);
            return ResponseVO.success(list);
        }
    }
    /**
     * 保存人工干预数据(POST请求)
     *
     * @param
     * @return 保存结果
     */
    @PutMapping(value = "saveManualIntervention")
    public ResponseVO saveManualIntervention(@RequestBody List<Map<String,String>> list, Long startTime, String fileType) {
        try {

            if(manualInterventionService.saveManualIntervention(list)){
                return ResponseVO.success("保存成功");
            }else{
                return ResponseVO.success("保存失败");
            }

        } catch (RuntimeException e) {
            log.error("系统错误：", e);
            return ResponseVO.fail("保存失败,运行时异常");
        }
    }

  /**
   * 更新首页短期数据
   *
   * @param
   * @return 把今天实时短期数据插入到历史短期里
   */
  @GetMapping(value = "refreshSt")
  public ResponseVO refreshSt() {
    try {

      if(manualInterventionService.refreshSt()){
        return ResponseVO.success("更新成功");
      }else{
        return ResponseVO.success("更新失败");
      }

    } catch (RuntimeException e) {
      log.error("系统错误：", e);
      return ResponseVO.fail("更新失败,运行时异常");
    }
  }

  /**
   * 人工干预功能升级（tl）
   *
   * @param startTime
   * @param endTime
   * @param fileType
   * @return
   */
  @GetMapping(value = "getForecastPowerShortTermForDrawing/{startTime}/{endTime}/{fileType}")
  public ResponseVO getForecastPowerShortTermForDrawing(@PathVariable Long startTime,
                                                        @PathVariable Long endTime,
                                                        @PathVariable String fileType){
    List<List<Object>> lists;
    try {
      lists= manualInterventionService.getForecastPowerShortTermForDrawing(startTime,endTime,fileType);
      return ResponseVO.success(lists);
    }catch (Exception e) {
      log.error("系统错误：", e);
      return ResponseVO.fail();
    }
  }

}
