package com.jiayue.insu.incloud.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiayue.insu.common.core.util.R;
import com.jiayue.insu.incloud.entity.ForecastData;
import com.jiayue.insu.incloud.entity.Record;
import com.jiayue.insu.incloud.entity.Station;
import com.jiayue.insu.incloud.service.ForecastDataService;
import com.jiayue.insu.incloud.service.StationService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * ForecastDataController
 *
 * @author yh
 * @date 2022-03-18 15:48:48
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/forecastData")
public class ForecastDataController {

    private final ForecastDataService forecastDataService;

    /**
     * 分页查询
     *
     * @param page   分页对象
     * @param forecastData forecastData
     * @return
     */

    @GetMapping("/page")
    public R getPage(Page page, ForecastData forecastData) {
        LambdaQueryWrapper<ForecastData> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if(forecastData.getStationCode()!=null){
            lambdaQueryWrapper.eq(ForecastData::getStationCode,forecastData.getStationCode());
        }
        if(forecastData.getStartTime()!=null && forecastData.getEndTime()!=null){
            lambdaQueryWrapper.between(ForecastData::getForecastTime,forecastData.getStartTime(),forecastData.getEndTime());
        }

        page.addOrder(OrderItem.asc("forecast_time"));

        return R.ok(forecastDataService.page(page, lambdaQueryWrapper));
    }


    /**
     * 新增forecastData
     *
     * @param forecastData forecastData
     * @return R
     */

    @PostMapping
    public R save(@RequestBody ForecastData forecastData) {
        return R.ok(forecastDataService.save(forecastData));
    }

    /**
     * 修改forecastData
     *
     * @param forecastData forecastData
     * @return R
     */

    @PutMapping
    public R updateById(@RequestBody ForecastData forecastData) {
        return R.ok(forecastDataService.updateById(forecastData));
    }

    /**
     * 通过id删除
     *
     * @param id id
     * @return R
     */
    @DeleteMapping("/{id}")
    public R removeById(@PathVariable Long id) {
        return R.ok(forecastDataService.removeById(id));
    }


}
