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.ElectricField;
import com.jiayue.ipp.common.data.entity.WindTurbineInfo;
import com.jiayue.ipp.idp.service.ElectricFieldService;
import com.jiayue.ipp.idp.service.WindTurbineInfoService;
import com.jiayue.ipp.idp.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;


/**
 * idp_wind_turbine_info
 *
 * @author whc
 * @date 2022-03-18 15:50:00
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/windturbineinfo")
@Slf4j
@Api(value = "windturbineinfo", tags = "idp_wind_turbine_info管理")
public class WindTurbineInfoController {

    private final WindTurbineInfoService windTurbineInfoService;
    private final ElectricFieldService electricFieldService;

    /**
     * 分页查询
     *
     * @param page            分页对象
     * @param windTurbineInfo idp_wind_turbine_info
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getWindTurbineInfoPage(Page page, WindTurbineInfo windTurbineInfo) {
        return R.ok(windTurbineInfoService.page(page, Wrappers.query(windTurbineInfo)));
    }

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

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

    /**
     * 新增idp_wind_turbine_info
     *
     * @param windTurbineInfo idp_wind_turbine_info
     * @return R
     */
    @ApiOperation(value = "新增idp_wind_turbine_info", notes = "新增idp_wind_turbine_info")
    @PostMapping
    public R save(@RequestBody WindTurbineInfo windTurbineInfo) {
        return R.ok(windTurbineInfoService.save(windTurbineInfo));
    }

    /**
     * 修改idp_wind_turbine_info
     *
     * @param windTurbineInfo idp_wind_turbine_info
     * @return R
     */
    @ApiOperation(value = "修改idp_wind_turbine_info", notes = "修改idp_wind_turbine_info")
    @PutMapping
    public R updateById(@RequestBody WindTurbineInfo windTurbineInfo) {
        return R.ok(windTurbineInfoService.updateById(windTurbineInfo));
    }

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

    /**
     * 根据场站编号查询 返回map格式
     *
     * @param stationCode
     * @return
     */
    @ApiOperation(value = "根据场站编号查询", notes = "分页查询")
    @PostMapping("/findByStationCode")
    public R findByStationCode(String stationCode) {
        List<WindTurbineInfo> windTurbineInfoList = windTurbineInfoService.findByStationCode(stationCode);
        List<Map<String, String>> list = new ArrayList<>();
        for (WindTurbineInfo 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<WindTurbineInfo> windTurbineInfoList = windTurbineInfoService.list();
        List<Map<String, String>> list = new ArrayList<>();
        for (WindTurbineInfo w : windTurbineInfoList) {
            Map<String, String> map = new HashMap<>();
            map.put("label", w.getName());
            map.put("value", w.getEquipmentNo());
            list.add(map);
        }
        return R.ok(list);
    }
    @GetMapping("/homgpageAllWindTurbineInfo")
    public R homgpageAllWindTurbineInfo() {
        List<ElectricField> electricFieldList = electricFieldService.list();
        List<WindTurbineInfo> windTurbineInfoList = windTurbineInfoService.list();
//        List<WindTurbineInfo> windTurbineInfoList = windTurbineInfoService.findByStationCode("T00007");
        List<Map<String, String>> list = new ArrayList<>();
        for(ElectricField electricField:electricFieldList){
            Map<String, String> map = new HashMap<>();
            map.put("stationCode",electricField.getStationCode());
            map.put("type","ElectricField");
            map.put("name", electricField.getName());
            map.put("sign", electricField.getSign());
            map.put("electricFieldTypeEnum", electricField.getElectricFieldTypeEnum().equals("1")?"光伏电站":"风力电站");
            map.put("capacity", electricField.getCapacity().setScale(2, RoundingMode.HALF_UP).toString());
            map.put("longitude", electricField.getLongitude().toString());
            map.put("latitude", electricField.getLatitude().toString());
            list.add(map);
        }
        for (WindTurbineInfo w : windTurbineInfoList) {
            List<ElectricField> electricFields = electricFieldList.stream().filter(s -> s.getSign().equals(s.getStationCode())).collect(Collectors.toList());
            Map<String, String> map = new HashMap<>();
            map.put("type","WindTurbineInfo");
            map.put("electricField", !electricFields.isEmpty()?electricFields.get(0).getName():w.getStationCode());
            map.put("name", w.getName());
            map.put("equipmentNo", w.getEquipmentNo());
            map.put("longitude", w.getLongitude().toString());
            map.put("latitude", w.getLatitude().toString());
            list.add(map);
        }
        return R.ok(list);
    }

    /**
     * 获取首页限电标识状态
     * @return 结果集
     */
    @GetMapping(value = "/getServerTime")
    public R getServerTime(){
        try{
            Date d = new Date();
            return R.ok(d);
        }catch(Exception e){
            log.error("系统时间获取失败",e);
            return R.failed(e.toString());
        }
    }
}
