package com.jiayue.ipp.idp.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiayue.ipp.common.data.entity.WeatherStationStatusData;
import com.jiayue.ipp.idp.service.WeatherStationStatusDataService;
import com.jiayue.ipp.idp.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * idp_weather_station_status_data
 *
 * @author whc
 * @date 2022-03-18 15:49:50
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/weatherstationstatusdata")
@Api(value = "weatherstationstatusdata", tags = "idp_weather_station_status_data管理")
public class WeatherStationStatusDataController {

    private final WeatherStationStatusDataService weatherStationStatusDataService;

    /**
     * 分页查询
     *
     * @param page                     分页对象
     * @param weatherStationStatusData idp_weather_station_status_data
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getWeatherStationStatusDataPage(Page page, WeatherStationStatusData weatherStationStatusData) {
        return R.ok(weatherStationStatusDataService.page(page, Wrappers.query(weatherStationStatusData)).addOrder(OrderItem.asc("time")));
    }

    /**
     * 通过id查询idp_weather_station_status_data
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") String id) {
        return R.ok(weatherStationStatusDataService.getById(id));
    }

    /**
     * 新增idp_weather_station_status_data
     *
     * @param weatherStationStatusData idp_weather_station_status_data
     * @return R
     */
    @ApiOperation(value = "新增idp_weather_station_status_data", notes = "新增idp_weather_station_status_data")
    @PostMapping
    public R save(@RequestBody WeatherStationStatusData weatherStationStatusData) {
        return R.ok(weatherStationStatusDataService.save(weatherStationStatusData));
    }

    /**
     * 修改idp_weather_station_status_data
     *
     * @param weatherStationStatusData idp_weather_station_status_data
     * @return R
     */
    @ApiOperation(value = "修改idp_weather_station_status_data", notes = "修改idp_weather_station_status_data")
    @PutMapping
    public R updateById(@RequestBody WeatherStationStatusData weatherStationStatusData) {
        return R.ok(weatherStationStatusDataService.updateById(weatherStationStatusData));
    }

    /**
     * 通过id删除idp_weather_station_status_data
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除idp_weather_station_status_data", notes = "通过id删除idp_weather_station_status_data")
    @DeleteMapping("/{id}")
    public R removeById(@PathVariable String id) {
        return R.ok(weatherStationStatusDataService.removeById(id));
    }

    @ApiOperation(value = "根据条件查询", notes = "查询")
    @PostMapping("/queryCharts")
    public R queryCharts(String stationCode, Long startTime, Long endTime, String equipmentId) {
        return R.ok(weatherStationStatusDataService.findByStationCodeAndeTimeBetweenAndEquipmentId(stationCode, new Date(startTime), new Date(endTime), equipmentId));
    }

    @ApiOperation(value = "根据条件查询", notes = "分页查询")
    @PostMapping("/queryTableData")
    public R queryTableData(Long currentPage, Long pageSize, String stationCode, Long startTime, Long endTime, String equipmentId) {
        Page page = new Page(currentPage, pageSize);
        page.addOrder(OrderItem.asc("time"));
        page.setMaxLimit((long) -1);
        return R.ok(weatherStationStatusDataService.page(page, weatherStationStatusDataService.getPageByStationCodeAndeTimeBetweenAndEquipmentId(stationCode, new Date(startTime), new Date(endTime), equipmentId)));
    }
}
