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.WindTowerInfo;
import com.jiayue.ipfcst.console.service.WindTowerInfoService;
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("windTowerInfo")
@Slf4j public class WindTowerInfoController {

    private final WindTowerInfoService windTowerInfoService;

    @Autowired public WindTowerInfoController(WindTowerInfoService windTowerInfoService) {
        this.windTowerInfoService = windTowerInfoService;
    }

    /**
     * 新增测风塔
     *
     * @param windTowerInfo 参数
     * @return 执行结果
     */
    @SneakyThrows
    @SaveValidate
    @PostMapping()
    public ResponseVO add(@RequestBody WindTowerInfo windTowerInfo) {
        try {
            this.windTowerInfoService.add(windTowerInfo);
            return ResponseVO.success(1);
        } catch (Exception ex) {
            log.error("系统错误" + ex);
            return ResponseVO.error(ex);
        }

    }

    /**
     * 修改测风塔
     *
     * @param windTowerInfo 参数
     * @return 执行结果
     */
    @SneakyThrows
    @SaveValidate
    @PutMapping()
    public ResponseVO update(@RequestBody WindTowerInfo windTowerInfo) {
        this.windTowerInfoService.update(windTowerInfo);
        return ResponseVO.success(1);
    }

    /**
     * 删除测风塔
     *
     * @param id 参数
     * @return 执行结果
     */
    @SneakyThrows
    @SaveValidate
    @DeleteMapping("/{id}")
    public ResponseVO delete(@PathVariable Integer id) {
        this.windTowerInfoService.delete(id);
        return ResponseVO.success(1);
    }

    /**
     * 获取测风塔
     *
     * @param page 参数
     * @return 执行结果
     */
    @SneakyThrows
    @GetMapping(value = "/{page}/{size}")
    public ResponseVO getAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        WindTowerInfo windTowerInfo = new WindTowerInfo();
        Page<WindTowerInfo> windTowerPage = this.windTowerInfoService.get(windTowerInfo, page, size);
      System.out.println(windTowerPage.toString());
        return ResponseVO.success(windTowerPage);
    }

    /**
     * 获取测风塔
     *
     * @return 执行结果
     */
    @SneakyThrows
    @GetMapping(value = "")
    public ResponseVO getAll() {
      List<WindTowerInfo> list = this.windTowerInfoService.getAll();
      return ResponseVO.success(list);
    }
}
