package com.jiayue.ipp.idp.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.jiayue.ipp.common.data.entity.WeatherStationInfo;
import com.jiayue.ipp.idp.service.WeatherStationInfoService;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * idp_weather_station_info
 *
 * @author whc
 * @date 2022-03-18 15:49:41
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/weatherstationinfo")
@Api(value = "weatherstationinfo", tags = "idp_weather_station_info管理")
public class WeatherStationInfoController {

    private final WeatherStationInfoService weatherStationInfoService;

    /**
     * 分页查询
     *
     * @param page               分页对象
     * @param weatherStationInfo idp_weather_station_info
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getWeatherStationInfoPage(Page page, WeatherStationInfo weatherStationInfo) {
        return R.ok(weatherStationInfoService.page(page, Wrappers.query(weatherStationInfo)));
    }

    /**
     * 根据场站编号分页查询
     *
     * @param currentPage
     * @param pageSize
     * @param stationCode
     * @return
     */
    @ApiOperation(value = "根据场站编号分页查询", notes = "分页查询")
    @PostMapping("/getByStationCode")
    public R getByStationCode(Long currentPage, Long pageSize, String stationCode) {
        Page page = new Page(currentPage, pageSize);
        page.setMaxLimit((long) -1);
        return R.ok(weatherStationInfoService.page(page, weatherStationInfoService.findByStationCode(stationCode)));
    }

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

    /**
     * 新增idp_weather_station_info
     *
     * @param weatherStationInfo idp_weather_station_info
     * @return R
     */
    @ApiOperation(value = "新增idp_weather_station_info", notes = "新增idp_weather_station_info")
    @PostMapping
    public R save(@RequestBody WeatherStationInfo weatherStationInfo) {
        return R.ok(weatherStationInfoService.save(weatherStationInfo));
    }

    /**
     * 修改idp_weather_station_info
     *
     * @param weatherStationInfo idp_weather_station_info
     * @return R
     */
    @ApiOperation(value = "修改idp_weather_station_info", notes = "修改idp_weather_station_info")
    @PutMapping
    public R updateById(@RequestBody WeatherStationInfo weatherStationInfo) {
        return R.ok(weatherStationInfoService.updateById(weatherStationInfo));
    }

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

    /**
     * 根据场站编号查询 返回map格式
     *
     * @param stationCode
     * @return
     */
    @ApiOperation(value = "根据场站编号查询", notes = "分页查询")
    @PostMapping("/getByStationCodeReturnMap")
    public R getByStationCode(String stationCode) {
        List<WeatherStationInfo> windTurbineInfoList = weatherStationInfoService.getByStationCode(stationCode);
        List<Map<String, String>> list = new ArrayList<>();
        for (WeatherStationInfo e : windTurbineInfoList) {
            Map<String, String> map = new HashMap<>();
            map.put("label", e.getName());
            map.put("value", e.getEquipmentNo());
            list.add(map);
        }
        return R.ok(list);
    }

    @GetMapping("/all")
    public R findAll() {
        List<WeatherStationInfo> weatherStationInfoList = weatherStationInfoService.list();
        List<Map<String, String>> list = new ArrayList<>();
        for (WeatherStationInfo w : weatherStationInfoList) {
            Map<String, String> map = new HashMap<>();
            map.put("label", w.getName());
            map.put("value", w.getEquipmentNo());
            list.add(map);
        }
        return R.ok(list);
    }
}
