package com.cpp.web.controller.configManager;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cpp.common.core.domain.R;
import com.cpp.web.domain.datafactory.FtpChannel;
import com.cpp.web.domain.station.ElectricField;
import com.cpp.web.domain.station.enums.ProvinceEnum;
import com.cpp.web.service.datafactory.FtpChannelService;
import com.cpp.web.service.station.ElectricFieldService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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


/**
 * 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 {
    @Autowired
    ElectricFieldService electricFieldService;
    @Autowired
    FtpChannelService ftpChannelService;

    /**
     * 根据场站名称或者类型分页查询
     *
     * @param currentPage
     * @param pageSize
     * @param stationName
     * @param electricFieldType
     * @return
     */
    @ApiOperation(value = "根据场站名称或者类型分页查询", notes = "分页查询")
    @GetMapping("/getList")
    public R getList(Long currentPage,Long pageSize,String stationName,String electricFieldType) {
        Page page = new Page(currentPage, pageSize);
        page.setMaxLimit((long) -1);
        return R.ok(electricFieldService.page(page, electricFieldService.getList(stationName,electricFieldType)));
    }

    /**
     * 通过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.fail(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.fail(provinceType);
        }

    }

    @GetMapping("/getFtpChannelId")
    public R getFtpChannelId() {
        List<FtpChannel> ftpChannelList = ftpChannelService.list();
        List<Map<String, String>> list = new ArrayList<>();
        for (FtpChannel e : ftpChannelList) {
            Map<String, String> map = new HashMap<>();
            map.put("label", e.getName());
            map.put("value", e.getId()+"");
            list.add(map);
        }
        return R.ok(list);
    }

    /**
     * 新增idp_electric_field
     *
     * @param electricField idp_electric_field
     * @return R
     */
    @ApiOperation(value = "新增idp_electric_field", notes = "新增idp_electric_field")
    //    @SysLog("新增idp_electric_field")
    @PostMapping("/save")
    ////@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")
    @PostMapping("/updateById")
    ////@PreAuthorize("@pms.hasPermission('idp_electricfield_edit')")
    public R updateById(@RequestBody ElectricField electricField) {
        return R.ok(electricFieldService.updateById(electricField));
    }

    /**
     * 通过id删除idp_electric_field
     *
     * @param
     * @return R
     */
    @ApiOperation(value = "通过id删除idp_electric_field", notes = "通过id删除idp_electric_field")
    //    @SysLog("通过id删除idp_electric_field")
    @PostMapping("/remove")
    ////@PreAuthorize("@pms.hasPermission('idp_electricfield_del')")
    public R removeById(@RequestBody ElectricField electricField) {
        return R.ok(electricFieldService.removeById(electricField.getId()));
    }


    @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.fail(e.toString());
        }
    }

    @GetMapping("/allData")
    public R allData() {
        List<ElectricField> electricFieldList = electricFieldService.list();
        return R.ok(electricFieldList);
    }
}
