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.enums.ProvinceEnum;
import com.jiayue.ipp.idp.service.ElectricFieldService;
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_electric_field
 *
 * @author whc
 * @date 2022-03-18 15:48:48
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/electricfield")
@Api(value = "electricfield", tags = "idp_electric_field管理")
public class ElectricFieldController {

    private final ElectricFieldService electricFieldService;

    /**
     * 分页查询
     *
     * @param page          分页对象
     * @param electricField idp_electric_field
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    ////@PreAuthorize("@pms.hasPermission('idp_electricfield_get')")
    public R getElectricFieldPage(Page page, ElectricField electricField) {
        return R.ok(electricFieldService.page(page, Wrappers.query(electricField)));
    }

    /**
     * 通过id查询idp_electric_field
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    ////@PreAuthorize("@pms.hasPermission('idp_electricfield_get')")
    public R getById(@PathVariable("id") String id) {
        return R.ok(electricFieldService.getById(id));
    }

    @GetMapping("/all")
    public R findAll() {
        List<ElectricField> electricFieldList = electricFieldService.list();
        List<Map<String, String>> list = new ArrayList<>();
        for (ElectricField e : electricFieldList) {
            Map<String, String> map = new HashMap<>();
            map.put("label", e.getName());
            map.put("value", e.getStationCode());
            list.add(map);
        }
        return R.ok(list);
    }

    @GetMapping("/getElectricFieldTypeEnum")
    public R getElectricFieldTypeEnum() {
        List<Map<String, Object>> type = new ArrayList<>();
        Map<String, Object> map;
        try {
            map = new HashMap<>();
            map.put("label", "光伏电站");
            map.put("value", "1");
            type.add(map);
            map = new HashMap<>();
            map.put("label", "风力电站");
            map.put("value", "2");
            type.add(map);
            return R.ok(type);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed(type);
        }

    }

    @GetMapping("/getProvinceEnum")
    public R getProvinceEnum() {
        List<Map<String, Object>> provinceType = new ArrayList<>();
        Map<String, Object> map;
        try {
            for (ProvinceEnum e : ProvinceEnum.values()) {
                map = new HashMap<>();
                map.put("label", e.getMessage());
                map.put("key", e.name());
                map.put("value", e.name());
                provinceType.add(map);
            }
            return R.ok(provinceType);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed(provinceType);
        }

    }

    /**
     * 新增idp_electric_field
     *
     * @param electricField idp_electric_field
     * @return R
     */
    @ApiOperation(value = "新增idp_electric_field", notes = "新增idp_electric_field")
    //    @SysLog("新增idp_electric_field")
    @PostMapping
    ////@PreAuthorize("@pms.hasPermission('idp_electricfield_add')")
    public R save(@RequestBody ElectricField electricField) {
        return R.ok(electricFieldService.save(electricField));
    }

    /**
     * 修改idp_electric_field
     *
     * @param electricField idp_electric_field
     * @return R
     */
    @ApiOperation(value = "修改idp_electric_field", notes = "修改idp_electric_field")
    //    @SysLog("修改idp_electric_field")
    @PutMapping
    ////@PreAuthorize("@pms.hasPermission('idp_electricfield_edit')")
    public R updateById(@RequestBody ElectricField electricField) {
        return R.ok(electricFieldService.updateById(electricField));
    }

    /**
     * 通过id删除idp_electric_field
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除idp_electric_field", notes = "通过id删除idp_electric_field")
    //    @SysLog("通过id删除idp_electric_field")
    @DeleteMapping("/{id}")
    ////@PreAuthorize("@pms.hasPermission('idp_electricfield_del')")
    public R removeById(@PathVariable String id) {
        return R.ok(electricFieldService.removeById(id));
    }


    @ApiOperation(value = "通过条件查询装机容量", notes = "通过条件查询装机容量")
    @PostMapping("/getByStationCode")
    ////@PreAuthorize("@pms.hasPermission('idp_electricfield_get')")
    public R getByStationCode(String stationCode) {
        return R.ok(electricFieldService.findByStationCode(stationCode));
    }

    @ApiOperation(value = "同步数据", notes = "同步数据")
    @GetMapping("/syncStation")
    //@PreAuthorize("@pms.hasPermission('idp_windtowerstatusdata_get')")
    public R syncStation() {
        return R.ok(electricFieldService.saveBatch(electricFieldService.syncStation()));
    }

    @GetMapping("/homgpageAllWindTurbineInfo")
    public R homgpageAllWindTurbineInfo() {
        List<ElectricField> electricFieldList = electricFieldService.list();
        List<Map<String, String>> list = new ArrayList<>();
        for(ElectricField electricField:electricFieldList){
            Map<String, String> map = new HashMap<>();
            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);
        }

        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());
        }
    }
}
