package com.jiayue.ipfcst.console.controller;

import com.jiayue.ipfcst.aop.SaveValidate;
import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.common.data.entity.WeatherStationInfo;
import com.jiayue.ipfcst.console.service.WeatherStationInfoService;
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("/weatherStationInfo")
@Slf4j
public class WeatherStationInfoController {

    private final WeatherStationInfoService weatherStationInfoService;

    @Autowired
    public WeatherStationInfoController(WeatherStationInfoService weatherStationInfoService) {
        this.weatherStationInfoService = weatherStationInfoService;
    }

    /**
     * 新增环境监测仪
     *
     * @param weatherStationInfo 参数
     * @return 执行结果
     */
    @SneakyThrows
    @SaveValidate
    @PostMapping()
    public ResponseVO add(@RequestBody WeatherStationInfo weatherStationInfo) {
      try {
        this.weatherStationInfoService.add(weatherStationInfo);
        return ResponseVO.success(1);
      } catch (Exception ex) {
        log.error("系统错误" + ex);
        return ResponseVO.error(ex);
      }
    }

    /**
     * 修改环境监测仪
     *
     * @param weatherStationInfo 参数
     * @return 执行结果
     */
    @SneakyThrows
    @SaveValidate
    @PutMapping()
    public ResponseVO update(@RequestBody WeatherStationInfo weatherStationInfo) {
      try {
        this.weatherStationInfoService.update(weatherStationInfo);
        return ResponseVO.success(1);
      } catch (Exception ex) {
        log.error("系统错误" + ex);
        return ResponseVO.error(ex);
      }

    }

    /**
     * 获取环境监测仪
     *
     * @param weatherStationInfo 参数
     * @return 执行结果
     */
    @SneakyThrows
    @GetMapping("/ById")
    public ResponseVO get(@RequestBody WeatherStationInfo weatherStationInfo) {
        try {
          weatherStationInfo = this.weatherStationInfoService.get(weatherStationInfo.getId());
          return ResponseVO.success(weatherStationInfo);
        } catch (Exception ex) {
          log.error("系统错误" + ex);
          return ResponseVO.error(ex);
        }

    }

    /**
     * 删除环境监测仪
     *
     * @param id 参数
     * @return 执行结果
     */
    @SneakyThrows
    @SaveValidate
    @DeleteMapping("/{id}")
    public ResponseVO delete(@PathVariable Integer id) {
      try {
        this.weatherStationInfoService.delete(id);
        return ResponseVO.success(1);
      } catch (Exception ex) {
        log.error("系统错误" + ex);
        return ResponseVO.error(ex);
      }

    }

    /**
     * 获取环境监测仪
     *
     * @param page 参数
     * @return 执行结果
     */
    @GetMapping(value = "/{page}/{size}")
    public ResponseVO getAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        try {
          WeatherStationInfo weatherStationInfo = new WeatherStationInfo();
          Page<WeatherStationInfo> weatherStationInfoPage = this.weatherStationInfoService.get(weatherStationInfo, page, size);
          return ResponseVO.success(weatherStationInfoPage);
        } catch (Exception ex) {
          log.error("系统错误" + ex);
          return ResponseVO.error(ex);
        }

    }

  /**
   * 获取环境监测仪
   *
   * @return 执行结果
   */
  @GetMapping()
  public ResponseVO getAll() {
    try {
      List<WeatherStationInfo> list = this.weatherStationInfoService.getAll();
      return ResponseVO.success(list);
    } catch (Exception ex) {
      log.error("系统错误" + ex);
      return ResponseVO.error(ex);
    }

  }
}
