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.WindTowerInfo;
import com.jiayue.ipp.idp.service.WindTowerInfoService;
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_wind_tower_info
 *
 * @author whc
 * @date 2022-03-18 15:49:56
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/windtowerinfo")
@Api(value = "windtowerinfo", tags = "idp_wind_tower_info管理")
public class WindTowerInfoController {

    private final WindTowerInfoService windTowerInfoService;

    /**
     * 分页查询
     *
     * @param page          分页对象
     * @param windTowerInfo idp_wind_tower_info
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getWindTowerInfoPage(Page page, WindTowerInfo windTowerInfo) {
        return R.ok(windTowerInfoService.page(page, Wrappers.query(windTowerInfo)));
    }

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

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

    /**
     * 新增idp_wind_tower_info
     *
     * @param windTowerInfo idp_wind_tower_info
     * @return R
     */
    @ApiOperation(value = "新增idp_wind_tower_info", notes = "新增idp_wind_tower_info")
    @PostMapping
    public R save(@RequestBody WindTowerInfo windTowerInfo) {
        return R.ok(windTowerInfoService.save(windTowerInfo));
    }

    /**
     * 修改idp_wind_tower_info
     *
     * @param windTowerInfo idp_wind_tower_info
     * @return R
     */
    @ApiOperation(value = "修改idp_wind_tower_info", notes = "修改idp_wind_tower_info")
    @PutMapping
    public R updateById(@RequestBody WindTowerInfo windTowerInfo) {
        return R.ok(windTowerInfoService.updateById(windTowerInfo));
    }

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

    /**
     * 根据场站编号查询 返回map格式
     *
     * @param stationCode
     * @return
     */
    @ApiOperation(value = "根据场站编号查询", notes = "分页查询")
    @PostMapping("/getByStationCode")
    public R findByStationCode(String stationCode) {
        List<WindTowerInfo> windTurbineInfoList = windTowerInfoService.getByStationCode(stationCode);
        List<Map<String, String>> list = new ArrayList<>();
        for (WindTowerInfo 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<WindTowerInfo> windTowerInfoList = windTowerInfoService.list();
        List<Map<String, String>> list = new ArrayList<>();
        for (WindTowerInfo w : windTowerInfoList) {
            Map<String, String> map = new HashMap<>();
            map.put("label", w.getName());
            map.put("value", w.getEquipmentNo());
            list.add(map);
        }
        return R.ok(list);
    }
}
