package com.yeejoin.amos.boot.module.jxiop.biz.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yeejoin.amos.boot.biz.common.controller.BaseController;
import com.yeejoin.amos.boot.biz.common.utils.RedisUtils;
import com.yeejoin.amos.boot.module.jxiop.api.entity.MapRegion;
import com.yeejoin.amos.boot.module.jxiop.api.entity.Region;
import com.yeejoin.amos.boot.module.jxiop.api.entity.StationBasic;
import com.yeejoin.amos.boot.module.jxiop.api.mapper.MapRegionMapper;
import com.yeejoin.amos.boot.module.jxiop.api.mapper.RegionMapper;
import com.yeejoin.amos.boot.module.jxiop.api.mapper.StationBasicMapper;
import com.yeejoin.amos.boot.module.jxiop.api.service.IMapRegionService;
import com.yeejoin.amos.boot.module.jxiop.biz.constants.CommonConstans;
import com.yeejoin.amos.boot.module.jxiop.biz.dto.*;
import com.yeejoin.amos.boot.module.jxiop.biz.entity.Test;
import com.yeejoin.amos.boot.module.jxiop.biz.service.impl.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javafx.scene.control.Tab;
import org.aspectj.apache.bcel.classfile.Module;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.influxdb.dto.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.typroject.tyboot.core.foundation.enumeration.UserType;
import org.typroject.tyboot.core.restful.doc.TycloudOperation;
import org.typroject.tyboot.core.restful.utils.ResponseHelper;
import org.typroject.tyboot.core.restful.utils.ResponseModel;

import java.util.*;
import java.util.logging.Handler;
import java.util.stream.Collectors;

@RestController
@Api(tags = "监盘地图接口")
@RequestMapping(value = "/monitormap")
public class MonitoringMapController extends BaseController {

    @Autowired
    MonitoringServiceImpl monitoringServiceImpl;
    @Autowired
    LargeScreenImpl largeScreenImpl;

    @Autowired
    RegionMapper regionMapper;
    @Autowired
    MapRegionMapper mapRegionMapper;
    @Autowired
    StationBasicMapper stationBasicMapper;
//    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
//    @ApiOperation(value = "获取监盘全国地图数据")
//    @GetMapping("/nationwide")
//    public ResponseModel<List<RegionNationWideDto>> getNationWideInfo(@RequestParam (value = "provinceName", required = false)String provinceName,@RequestParam (value = "type", required = false)String type) {
//      return ResponseHelper.buildResponse(monitoringServiceImpl.getNationWideInfo(provinceName,type));
//    }

//    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
//    @ApiOperation(value = "获取监盘全国地图-发电量数据")
//    @GetMapping("/getNationalPowerGenerationData")
//    public ResponseModel<List<HashMap<String,Object>>> getNationalPowerGenerationData() {
//        return ResponseHelper.buildResponse(monitoringServiceImpl.getNationalPowerGenerationData());
//    }

    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
    @ApiOperation(value = "获取监盘全国地图-获取功率曲线下拉框数据")
    @GetMapping("/getSelectDataForNational")
    public ResponseModel<List<HashMap<String,Object>>> getSelectDataForNational() {
        return ResponseHelper.buildResponse(monitoringServiceImpl.getSelectDataForNational());
    }

//    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
//    @ApiOperation(value = "获取区域电量指标完成情况")
//    @GetMapping("/getCompletionOfPowerIndicatorsByProvinceName")
//    public ResponseModel<ResultsData> getCompletionOfPowerIndicatorsByProvinceName(@RequestParam(required = true) int current, @RequestParam(required = true) int size, @RequestParam(required = true) String provinceName) {
//        return ResponseHelper.buildResponse(monitoringServiceImpl.getCompletionOfPowerIndicatorsByProvinceName(current,size,provinceName));
//    }

    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
    @ApiOperation(value = "根据场站id动态获取tab页签")
    @GetMapping("/getTabsByStationBasicId")
    public ResponseModel<List<TabDto>> getTabsByStationBasicId(@RequestParam(required = true) String stationBasicId) {
        return ResponseHelper.buildResponse(monitoringServiceImpl.getTabsByStationBasicId(stationBasicId));
    }
    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
    @ApiOperation(value = "监盘首页地图-全国")
    @GetMapping("/getAreaInfo")
    public ResponseModel<List<MapAreaInfoDto>> getAreaInfo() {
        return ResponseHelper.buildResponse(monitoringServiceImpl.getAreaInfo());
    }
//    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
//    @ApiOperation(value = "监盘首页地图-片区")
//    @GetMapping("/getStaitonListInfoByAreaName")
//    public ResponseModel<List<HomeMapStationInfoDto>> getStaitonListInfoByAreaName(@RequestParam(required = true)String areaName) {
//        return ResponseHelper.buildResponse(monitoringServiceImpl.getStaitonListInfoByAreaName(areaName));
//    }

    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
    @ApiOperation(value = "监盘首页-场站分类信息")
    @GetMapping("/getStaionCategoryInfo")
    public ResponseModel<List<Map<String,String>>> getStaionCategoryInfo(@RequestParam(required = false)String areaName) {
    return  ResponseHelper.buildResponse(monitoringServiceImpl.getStaionCategoryInfo(areaName));
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
    @ApiOperation(value = "监盘首页-装机容量")
    @GetMapping("/getInstallCapityByAreaName")
    public ResponseModel<HashMap<String,String>> getInstallCapityByAreaName(@RequestParam(required = false)String areaName) {
        return  ResponseHelper.buildResponse(monitoringServiceImpl.getInstallCapityByAreaName(areaName));
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
    @ApiOperation(value = "监盘首页-有功功率")
    @GetMapping("/getActivePowerByAreaName")
    public ResponseModel<HashMap<String,String>> getActivePowerByAreaName(@RequestParam(required = false)String areaName) {
        return  ResponseHelper.buildResponse(monitoringServiceImpl.getActivePowerByAreaName(areaName));
    }

//    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
//    @ApiOperation(value = "监盘首页-社会贡献")
//    @GetMapping("/getSocialContributionDtoList")
//    public ResponseModel<Page<SocialContributionDto>> getSocialContributionDtoList(@RequestParam(required = false)String areaName, @RequestParam(required = false)String station) {
//        return  ResponseHelper.buildResponse(monitoringServiceImpl.getSocialContributionDtoList(areaName,station));
//    }
//    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
//    @ApiOperation(value = "监盘首页-发电量趋势")
//    @GetMapping("/getPowerGenerationTrendsOfMonth")
//    public ResponseModel<HashMap<String, List<String>>> getPowerGenerationTrendsOfMonth(@RequestParam(required = false)String areaName) {
//        return  ResponseHelper.buildResponse(monitoringServiceImpl.getPowerGenerationTrendsOfMonth(areaName));
//    }
//    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
//    @ApiOperation(value = "监盘首页-发电量详情")
//    @GetMapping("/getDetailsOnPowergeneration")
//    public ResponseModel<Page<HashMap<String,String>>> getDetailsOnPowergeneration(@RequestParam(required = false)String areaName) {
//        return  ResponseHelper.buildResponse(monitoringServiceImpl.getDetailsOnPowergeneration(areaName));
//    }

//    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
//    @ApiOperation(value = "监盘首页-发电量趋势完成率")
//    @GetMapping("/getPowerGenerationTrendsOfCompletionRate")
//    public ResponseModel<HashMap<String, List<String>>> getPowerGenerationTrendsOfCompletionRate(@RequestParam(required = false)String areaName) {
//        return  ResponseHelper.buildResponse(monitoringServiceImpl.getPowerGenerationTrendsOfCompletionRate(areaName));
//    }

//    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
//    @ApiOperation(value = "监盘首页-发电top5")
//    @GetMapping("/getPowerGenerationTrendsOfCompletionTopFive")
//    public ResponseModel<Page<HashMap<String,String>>> getPowerGenerationTrendsOfCompletionTopFive(@RequestParam(required = false)String areaName) {
//        return  ResponseHelper.buildResponse(monitoringServiceImpl.getPowerGenerationTrendsOfCompletionTopFive(areaName));
//    }

//    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
//    @ApiOperation(value = "监盘首页-发电小时排名top3")
//    @GetMapping("/getPowerGenerationTrendsOfCompletionTopThree")
//    public ResponseModel<Page<HashMap<String, String>>> getPowerGenerationTrendsOfCompletionTopThree(@RequestParam(required = false)String areaName,@RequestParam(required = false,defaultValue = "0")String tabValue) {
//        return  ResponseHelper.buildResponse(monitoringServiceImpl.getPowerGenerationTrendsOfCompletionTopThree(tabValue,areaName));
//    }

    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
    @ApiOperation(value = "运行监盘-获取全国数据")
    @GetMapping("/getTotalData")
    public void getTotalData() {
          monitoringServiceImpl.getTotalData();
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
    @ApiOperation(value = "运行监盘-功率曲线")
    @GetMapping("/getTheStationPowerCurve")
    public ResponseModel<HashMap<String, Object>> getTheStationPowerCurve(@RequestParam(required = false)String stationId,@RequestParam(required = false)String date) {
        return ResponseHelper.buildResponse(monitoringServiceImpl.getTheStationPowerCurve(stationId,date));
    }





    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
    @ApiOperation(value = "场站-功率曲线")
    @GetMapping("/getDetailsWindSpeed")
    public ResponseModel<Map<String, Object>> getDetailsWindSpeed(@RequestParam(required = false)String stationId) {

        StationBasic stationBasic = stationBasicMapper.selectById(stationId);
        monitoringServiceImpl.getDetailsWindSpeedAlldata(stationBasic);
        return ResponseHelper.buildResponse(null);
    }
    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
    @ApiOperation(value = "场站-功率曲线")
    @GetMapping("/getDetailsWindSpeedAlldataqg")
    public ResponseModel<Map<String, Object>> getDetailsWindSpeedAlldataqg(@RequestParam(required = false)String stationId) {


        Map<String, Object> map=  largeScreenImpl.getDetailsWindSpeedAlldataqg();
        return ResponseHelper.buildResponse(map);
    }


    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
    @ApiOperation(value = "监盘首页-  全国发电量趋势完成率")
    @GetMapping("/gettimedateqyyfdqs")
    public ResponseModel<Map<String,Object>> sendQYFDLMqttyfdqs(String areaName) {
        List<StationBasic>   stationBasicListAll = getListOfStationBasicByAreaName(areaName);

        List<String> ids = stationBasicListAll.stream().map(StationBasic::getFanGatewayId).collect(Collectors.toList());
        List<Long> idsStationId = stationBasicListAll.stream().map(StationBasic::getSequenceNbr).collect(Collectors.toList());
        List<String> idsStationIdmnew =new ArrayList<>();
        for (Long aLong : idsStationId) {
            idsStationIdmnew.add(String.valueOf(aLong));
        }
        Map<String,Object> map=   largeScreenImpl.gettimedateqyyfdqs(new Date(),ids,areaName,idsStationIdmnew);



        return  ResponseHelper.buildResponse(map);
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
    @ApiOperation(value = "监盘首页-  区域功率曲线率")
    @GetMapping("/getDetailsWindqy")
    public ResponseModel<Map<String,Object>> getDetailsWindqy(String areaName) {

        List<StationBasic>   stationBasicListAll = getListOfStationBasicByAreaName(areaName);

        Map<String,Object> map=   largeScreenImpl.getDetailsWindqy( areaName,stationBasicListAll);

        return  ResponseHelper.buildResponse(map);
    }


    @TycloudOperation(needAuth = false, ApiLevel = UserType.AGENCY)
    @ApiOperation(value = "风机详情-风速功率曲线")
    @GetMapping("/detailsWindSpeed")
    public ResponseModel<Map<String, Object>> getDetailsWindSpeed(@RequestParam(value = "stationId") String stationId, String equipmentNumber) {
        StationBasic stationBasic = stationBasicMapper.selectById(stationId);
        String gatewayId = stationBasic.getFanGatewayId();
        Map<String, Object> detailsWindSpeed = largeScreenImpl.getDetailsWindSpeed(gatewayId, equipmentNumber);

        return ResponseHelper.buildResponse(detailsWindSpeed);
    }




    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
    @ApiOperation(value = "监盘首页-  全国发电量趋势完成率")
    @GetMapping("/gettimedateqyyfd")
    public ResponseModel<Map<String,Object>> sendQYFDLMqttyfd(String areaName) {

        List<StationBasic>   stationBasicListAll = getListOfStationBasicByAreaName(areaName);
        List<String> ids = stationBasicListAll.stream().map(StationBasic::getFanGatewayId).collect(Collectors.toList());
        Map<String,Object> map=   largeScreenImpl.gettimedateqyyfd( new Date(),ids, areaName);

        return  ResponseHelper.buildResponse(map);
    }



    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
    @ApiOperation(value = "监盘首页-  全国发电量趋势完成率")
    @GetMapping("/gettimedateyfdqs")
    public ResponseModel<Map<String,Object>> gettimedateyfdqs() {

        Map<String,Object> map=  largeScreenImpl.gettimedateyfdqs();


        return  ResponseHelper.buildResponse(map);
    }
    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
    @ApiOperation(value = "监盘首页-  全国发电量趋势完成率")
    @GetMapping("/gettimedateyfd")
    public ResponseModel< Map<String,Object>> gettimedateyfd() {


        Map<String,Object> map=   largeScreenImpl.gettimedateyfd();

        return  ResponseHelper.buildResponse(map);
    }
    public List<StationBasic> getListOfStationBasicByAreaName(String areaName) {
        List<StationBasic> stationBasicListAll = new ArrayList<>();
        MapRegion mapRegion = mapRegionMapper.selectOne(new QueryWrapper<MapRegion>().eq("name", areaName));
        //获取片区下的省份名称
        ArrayList<String> stringArrayList = mapRegion.getProvince();
        //变量获取所有的场站信息
        for (int i = 0; i < stringArrayList.size(); i++) {
            Region region = regionMapper.selectOne(new QueryWrapper<Region>().eq("LEVEL", 1).like("REGION_NAME", stringArrayList.get(i)));
            List<StationBasic> stationBasicList = stationBasicMapper.selectList(new QueryWrapper<StationBasic>().like("belong_area", region.getRegionCode()));
            stationBasicListAll.addAll(stationBasicList);
        }
        return stationBasicListAll;
    }





    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
    @ApiOperation(value = "大屏首页-  全国发电量趋势完成率")
    @GetMapping("/getSeriesDataqg")
    public ResponseModel<SeriesData> getSeriesDataqg() {

        SeriesData map=  largeScreenImpl.getSeriesDataqg();

        return  ResponseHelper.buildResponse(map);
    }
    @TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
    @ApiOperation(value = "大屏首页-  区域发电量趋势完成率")
    @GetMapping("/getSeriesDataqy")
    public ResponseModel<SeriesData> getSeriesDataqy(String  areaCode) {

        LambdaQueryWrapper<StationBasic> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(areaCode!=null,StationBasic::getAreaCode,areaCode);

        List<StationBasic> stationBasics = stationBasicMapper.selectList(wrapper);
       // 2023年11月8日 12点51分 库中存储的历史数据存在问题逻辑由原来的通过网关id查询数据修改为场站id
        List<String> ids = stationBasics.stream().map(stationBasic -> String.valueOf(stationBasic.getSequenceNbr())).collect(Collectors.toList());

        SeriesData map=   largeScreenImpl.getSeriesDataqy( new Date(),ids, areaCode);

        return  ResponseHelper.buildResponse(map);
    }










}
