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.InverterInfo;
import com.jiayue.ipp.idp.service.InverterInfoService;
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_inverter_info
 *
 * @author whc
 * @date 2022-03-18 15:49:11
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/inverterinfo")
@Api(value = "inverterinfo", tags = "idp_inverter_info管理")
public class InverterInfoController {

    private final InverterInfoService inverterInfoService;

    /**
     * 分页查询
     *
     * @param page         分页对象
     * @param inverterInfo idp_inverter_info
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getInverterInfoPage(Page page, InverterInfo inverterInfo) {
        return R.ok(inverterInfoService.page(page, Wrappers.query(inverterInfo)));
    }

    /**
     * 根据场站编号分页查询
     *
     * @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(inverterInfoService.page(page, inverterInfoService.getByStationCode(stationCode)));
    }

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

    /**
     * 新增idp_inverter_info
     *
     * @param inverterInfo idp_inverter_info
     * @return R
     */
    @ApiOperation(value = "新增idp_inverter_info", notes = "新增idp_inverter_info")
    @PostMapping
    public R save(@RequestBody InverterInfo inverterInfo) {
        return R.ok(inverterInfoService.save(inverterInfo));
    }

    /**
     * 修改idp_inverter_info
     *
     * @param inverterInfo idp_inverter_info
     * @return R
     */
    @ApiOperation(value = "修改idp_inverter_info", notes = "修改idp_inverter_info")
    @PutMapping
    public R updateById(@RequestBody InverterInfo inverterInfo) {
        return R.ok(inverterInfoService.updateById(inverterInfo));
    }

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

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