package com.jiayue.ipfcst.console.controller;


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

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

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

    public ManualInterventionRatioController(ManualInterventionRatioService manualInterventionRatioService) {
      this.manualInterventionRatioService = manualInterventionRatioService;
    }

    @GetMapping(value = "queryManualInterventionRatioData/{startTime}/{endTime}/{fileType}")
    public ResponseVO queryManualInterventionData(@PathVariable Long startTime,
                                                                    @PathVariable Long endTime,
                                                                    @PathVariable String fileType){
      List<ManualInterventionRatioData> list = new ArrayList<>();
      try {
            list= manualInterventionRatioService.queryManualInterventionRatio(startTime,endTime,fileType);
            return ResponseVO.success(list);
        }catch (Exception e) {
            log.error("系统错误：", e);
        } finally {
            list = manualInterventionRatioService.queryManualInterventionRatio(startTime,endTime,fileType);
            return ResponseVO.success(list);
        }
    }

  /**
   * 保存人工干预数据(POST请求)
   *
   * @param
   * @return 保存结果
   */
  @PostMapping(value = "saveManualInterventionRatio")
  public ResponseVO saveManualInterventionRatio(@RequestBody ManualInterventionRatioData manualInterventionRatioData ) {
    try {
      if(manualInterventionRatioService.saveManualInterventionRatio(manualInterventionRatioData)){
        return ResponseVO.success("保存成功");
      }else{
        return ResponseVO.fail("保存失败，请检查是否设置过点位");
      }

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

  @DeleteMapping("/delete/{id}")
  public ResponseVO deleteById(@PathVariable("id") Integer id) {
    try {
      manualInterventionRatioService.delete(id);
      return ResponseVO.success();
    }catch (Exception e){
      log.error("删除人工干预系数配置异常" + e);
      return ResponseVO.error(e);
    }
  }
}
