package com.jiayue.ipfcst.console.controller;

import com.jiayue.ipfcst.aop.SaveValidate;
import com.jiayue.ipfcst.common.core.web.vo.RequestVO;
import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.common.data.entity.WindTurbineInfo;
import com.jiayue.ipfcst.console.service.WindTurbineInfoService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 风机restful接口
 *
 * @author tl
 * @version 3.0
 * @since 2020/6/7 11:29
 */
@RestController
@RequestMapping("windTurbineInfo")
@Slf4j
public class WindTurbineInfoController {

    private final WindTurbineInfoService windTurbineInfoService;

    @Autowired
    public WindTurbineInfoController(WindTurbineInfoService windTurbineInfoService) {
        this.windTurbineInfoService = windTurbineInfoService;
    }

    /**
     * 新增风机
     *
     * @param windTurbineInfo 参数
     * @return 执行结果
     */
    @SneakyThrows
    @SaveValidate
    @PostMapping()
    public ResponseVO add(@RequestBody WindTurbineInfo windTurbineInfo) {
        this.windTurbineInfoService.add(windTurbineInfo);
        return ResponseVO.success(1);
    }

    /**
     * 修改风机
     *
     * @param windTurbineInfo 参数
     * @return 执行结果
     */
    @SneakyThrows
    @SaveValidate
    @PutMapping()
    public ResponseVO update(@RequestBody WindTurbineInfo windTurbineInfo) {
        this.windTurbineInfoService.update(windTurbineInfo);
        return ResponseVO.success(1);
    }

    /**
     * 获取风机
     *
     * @param requestVO 参数
     * @return 执行结果
     */
    @SneakyThrows
    @GetMapping("ById")
    public ResponseVO get(@RequestBody RequestVO<WindTurbineInfo> requestVO) {
      WindTurbineInfo windTurbineInfo = requestVO.getData();
      windTurbineInfo = this.windTurbineInfoService.get(windTurbineInfo.getId());
        return ResponseVO.success(windTurbineInfo);
    }

    /**
     * 删除风机
     *
     * @return 执行结果
     */
    @SneakyThrows
    @SaveValidate
    @DeleteMapping("/{id}")
    public ResponseVO delete(@PathVariable Integer id) {
        this.windTurbineInfoService.delete(id);
        return ResponseVO.success(1);
    }

    /**
     * 获取风机
     *
     * @param page 参数
     * @param size 参数
     * @return 执行结果
     */
    @SneakyThrows
    @GetMapping(value = "/{page}/{size}")
    public ResponseVO getAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
      WindTurbineInfo windTurbineInfo = new WindTurbineInfo();
      windTurbineInfo.setCorrectState(null);
        Page<WindTurbineInfo> windTurbinePage = this.windTurbineInfoService.get(windTurbineInfo, page, size);
        return ResponseVO.success(windTurbinePage);
    }


    /**
     * 获取风机
     *
     * @return 执行结果
     */
    @SneakyThrows
    @GetMapping()
    public ResponseVO getAll() {
      List<WindTurbineInfo> list = this.windTurbineInfoService.getAll();
      return ResponseVO.success(list);
    }

  /**
   * 批量保存风机信息
   *
   * @param windTurbineInfo 风机实体
   * @return 执行结果
   */
  @SaveValidate
  @PostMapping(value = "/saveAll/{startValue}/{endValue}")
  public ResponseVO allSaveWindTurbineInfo(@RequestBody WindTurbineInfo windTurbineInfo, @PathVariable("startValue") Integer startValue, @PathVariable("endValue") Integer endValue) {
    try {
      windTurbineInfoService.addALL(windTurbineInfo,startValue,endValue);
      return ResponseVO.success();
    } catch (Exception e) {
      e.printStackTrace();
      log.error("批量保存风机信息异常");
      return ResponseVO.fail();
    }
  }

  /**
   * 删除风机信息
   *
   * @param ids 删除id
   * @return 执行结果
   */
  @SaveValidate
  @DeleteMapping(value = "/deleteIds/{ids}")
  public ResponseVO deleteWindTurbineInfo(@PathVariable("ids") String ids) {
    try {
      windTurbineInfoService.delete(ids);
      return ResponseVO.success();
    } catch (Exception e) {
      e.printStackTrace();
      log.error(" 删除逆变器异常");
      return ResponseVO.fail();
    }

  }

}
