package com.xhpc.pp.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.xhpc.common.api.dto.ChargingStationDto;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.data.redis.CacheRateModel;
import com.xhpc.evcs.cdjgpc.dto.*;
import com.xhpc.pp.mapper.XhpcDeviceMessageMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.*;
import java.util.concurrent.TimeUnit;

import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;

@RestController
public class StationController {

    @Resource
    XhpcDeviceMessageMapper deviceMessageMapper;

    @PostMapping("station/rateModel/{stationId}/{rateModelId}")
    public Object setStationRateModel(@PathVariable Long stationId, @PathVariable Long rateModelId, @RequestBody CacheRateModel rateModel) {

        String skey = "station:".concat(stationId.toString());
        ChargingStationDto cacheStation = REDIS.getCacheObject(skey);
        if (cacheStation==null) cacheStation = new ChargingStationDto();
        cacheStation.setRateModelId(rateModelId);
        REDIS.setCacheObject(skey, cacheStation);
        String rkey = "rateModel:".concat(rateModelId.toString());
        REDIS.setCacheObject(rkey, rateModel);
        return R.ok();
    }

    //生成场站推送数据信息
    @GetMapping("station/addNotificationStationInfo")
    public void addNotificationStationInfo(@RequestParam("stationId")Long stationId){
        CDStationInfo xhpcChargingStation = deviceMessageMapper.getXhpcChargingStation(stationId);
        if(xhpcChargingStation !=null){
            //场站信息入缓存
            String skey = "cdjgpc:station:".concat(stationId.toString())+".notification";
            REDIS.deleteObject(skey);

            if(xhpcChargingStation.getSwapMatchCarsName() !=null){
                String[] split = xhpcChargingStation.getSwapMatchCarsName().split(",");
                xhpcChargingStation.setSwapMatchCars(split);
            }
            xhpcChargingStation.setEquipmentInfos(new ArrayList<>());

            REDIS.setCacheObject(skey, xhpcChargingStation);
        }
    }

    //组合场站数据
    @GetMapping("station/getNotificationStationInfo")
    public R getNotificationStationInfo(@RequestParam("stationId") Long stationId){
        String key = "cdjgpc:station:".concat(stationId.toString())+".notification";
        CDStationInfo cdStationInfo = REDIS.getCacheObject(key);
        cdStationInfo.setEquipmentInfos(new ArrayList<>());
        //查询场站是桩是否对应上
        ChargingStationDto stationDto = REDIS.getCacheObject("station:"+stationId);
        Set<String> pks = stationDto.getPiles();
        if (pks != null) {
            List<CDEquipmentInfo> cdEquipmentInfos = new ArrayList<>();
            for (String pileNo : pks) {
                String pk = "pile:".concat(pileNo);
                String skey = "cdjgpc:".concat(pk)+".notification";
                CDEquipmentInfo cdEquipmentInfo = REDIS.getCacheObject(skey);
                if(cdEquipmentInfo==null || "".equals(cdEquipmentInfo.getEquipmentID())){
                    return R.fail("该场站下面："+pileNo+"桩未录入");
                }
                cdEquipmentInfos.add(cdEquipmentInfo);
            }
            cdStationInfo.setEquipmentInfos(cdEquipmentInfos);
            REDIS.setCacheObject(key, cdStationInfo);
            return R.ok();
        }
        return R.fail("场站未查询到充电桩信息");
    }

    //查询站点统计信息
    @GetMapping("station/queryStationStats")
    public R queryStationStats(@RequestParam("stationId") Long stationId,@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime){
        //获取场站
        CDStationStatslnfo stationStatsInfo = deviceMessageMapper.getStationEquipmentElectricity(stationId, startTime, endTime);
        stationStatsInfo.setStartTime(startTime);
        stationStatsInfo.setEndTime(endTime);
        //获取桩
        List<Long> chargingPileIds = deviceMessageMapper.getchargingPileId(stationId);
        List<CDSuqEquipmentStatslnfo> equipmentStatsInfos =new ArrayList<>();
        for (int i = 0; i < chargingPileIds.size(); i++) {
            Long chargingPileId = chargingPileIds.get(i);
            CDSuqEquipmentStatslnfo equipmentStatsInfo = deviceMessageMapper.getSupEquipmentStatsInfo(stationId, chargingPileId, startTime, endTime);
            List<CDSupConnectorStatslnfo> supConnectorStatslnfo = deviceMessageMapper.getSupConnectorStatslnfo(stationId, null, startTime, endTime);
            equipmentStatsInfo.setConnectorStatsInfos(supConnectorStatslnfo);
            equipmentStatsInfos.add(equipmentStatsInfo);
        }
        stationStatsInfo.setEquipmentStatsInfo(equipmentStatsInfos);

        String key ="cdjgpc:station:"+stationId+".queryStationStats";
        REDIS.setCacheObject(key, stationStatsInfo,30L, TimeUnit.MINUTES);
        return R.ok(stationStatsInfo);
    }


    @GetMapping("station/getNotificationStationFee")
    public R getNotificationStationFee(@RequestParam("stationId") Long stationId){
        List<CDChargeFeeDetail> notificationStationFee = deviceMessageMapper.getNotificationStationFee(stationId);
        String skey = "cdjgpc:stationFee:".concat(stationId.toString());
        CDStationFee cdStationFeeRequest =new CDStationFee();
        cdStationFeeRequest.setOperatorID("MA6DFCTD5");
        cdStationFeeRequest.setStationID(stationId.toString());
        cdStationFeeRequest.setChargeFeeDetails(notificationStationFee);
        REDIS.setCacheObject(skey, cdStationFeeRequest);
        String ske = "cdjgpc:stationFeeRequest:".concat(stationId.toString());
        REDIS.deleteObject(ske);
        REDIS.setCacheObject(ske, cdStationFeeRequest,30L, TimeUnit.MINUTES);
        return R.ok();
    }


    //查询充电设备运行统计信息
    @GetMapping("station/querySupEquipmentStatsInfo")
    public R querySupEquipmentStatsInfo(@RequestParam("stationId") Long stationId,@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime){
        //获取场站
        List<CDSuqEquipmentStatslnfo> cdSuqEquipmentStatslnfos =new ArrayList<>();
        List<Long> chargingPileIds = deviceMessageMapper.getchargingPileId(stationId);
        for (int i = 0; i < chargingPileIds.size(); i++) {
            CDSuqEquipmentStatslnfo supEquipmentStatsInfo = deviceMessageMapper.getSupEquipmentStatsInfo(stationId,chargingPileIds.get(i), startTime, endTime);
            //获取枪信息
            List<CDSupConnectorStatslnfo> supConnectorStatslnfo = deviceMessageMapper.getSupConnectorStatslnfo(stationId, chargingPileIds.get(i), startTime, endTime);
            supEquipmentStatsInfo.setConnectorStatsInfos(supConnectorStatslnfo);

            cdSuqEquipmentStatslnfos.add(supEquipmentStatsInfo);
        }
        String key ="cdjgpc:station:"+stationId+".querySupEquipmentStatsInfo";
        REDIS.setCacheObject(key, cdSuqEquipmentStatslnfos,30L, TimeUnit.MINUTES);
        return R.ok(cdSuqEquipmentStatslnfos);
    }

    //查询充电设备运行统计信息
    @GetMapping("station/queryConnectorStatsInfo")
    public R queryConnectorStatsInfo(@RequestParam("stationId") Long stationId,@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime){
        //获取枪信息
        List<CDSupConnectorStatslnfo> supConnectorStatslnfo = deviceMessageMapper.getSupConnectorStatslnfo(stationId, null, startTime, endTime);
        String key ="cdjgpc:station:"+stationId+".queryConnectorStatsInfo";
        REDIS.setCacheObject(key, supConnectorStatslnfo,30L, TimeUnit.MINUTES);
        return R.ok(supConnectorStatslnfo);
    }

    //查询充电站功率统信息
    @GetMapping("station/getSuperviseQueryStationPowerInfo")
    public R getSuperviseQueryStationPowerInfo(@RequestParam("stationIDs") String [] stationIDs,@RequestParam("dataTime") String dataTime,@RequestParam("replace")String replace){
        System.out.println("stationIDs===="+stationIDs.toString());
        List<String> list = Arrays.asList(stationIDs);
        List<Long> longList =new ArrayList<>();
        for (int i = 0; i <list.size() ; i++) {
            longList.add(Long.valueOf(list.get(i)));
        }
        //获取时间
        String now = DateUtil.now();
        int hh = Integer.parseInt(now.substring(11,13));
        int mm = Integer.parseInt(now.substring(14,16));
        int dd = Integer.parseInt(now.substring(17,19));
        int numbrt =0;
        if(mm<15){
            numbrt = hh*4;
        }else if(mm<30){
            numbrt = hh*4+1;
        }else if(mm<45){
            numbrt = hh*4+2;
        }else{
            numbrt = hh*4+3;
        }
        List<CDSupStationPowerInfo> superviseQueryStationPowerInfoList = deviceMessageMapper.getSuperviseQueryStationPowerInfo(longList, dataTime, numbrt);

        for (int i = 0; i < superviseQueryStationPowerInfoList.size(); i++) {
            //获取充电桩实时功率
            CDSupStationPowerInfo cdSupStationPowerInfo = superviseQueryStationPowerInfoList.get(i);

            List<CDSupEquipmentPowerInfo> pileSuperviseQueryStationPowerInfoList = deviceMessageMapper.getPileSuperviseQueryStationPowerInfo(Long.valueOf(cdSupStationPowerInfo.getStationID()), dataTime, numbrt);
            for (int j = 0; j <pileSuperviseQueryStationPowerInfoList.size() ; j++) {
                CDSupEquipmentPowerInfo cdSupEquipmentPowerlnfo = pileSuperviseQueryStationPowerInfoList.get(j);
                //获取枪实时功率
                List<CDSupConnectorPowerInfo> terminalSuperviseQueryStationPowerInfo = deviceMessageMapper.getTerminalSuperviseQueryStationPowerInfo(Long.valueOf(cdSupEquipmentPowerlnfo.getPileID()), dataTime, numbrt);
                cdSupEquipmentPowerlnfo.setConnectorPowerInfos(terminalSuperviseQueryStationPowerInfo);
            }
            cdSupStationPowerInfo.setEquipmentPowerInfos(pileSuperviseQueryStationPowerInfoList);
        }
        String key ="cdjgpc:station:"+replace+".getSuperviseQueryStationPowerInfo";
        REDIS.setCacheObject(key, superviseQueryStationPowerInfoList,30L, TimeUnit.MINUTES);

        return R.ok(superviseQueryStationPowerInfoList);
    }

    //查询充电站功率统信息
    @GetMapping("station/getSuperviseQueryEquipmentPowerInfo")
    public R getSuperviseQueryEquipmentPowerInfo(@RequestParam("stationID") String stationID,@RequestParam("dataTime") String dataTime,@RequestParam("replace")String replace){
        //获取时间
        String now = DateUtil.now();
        int hh = Integer.parseInt(now.substring(11,13));
        int mm = Integer.parseInt(now.substring(14,16));
        int dd = Integer.parseInt(now.substring(17,19));
        int numbrt =0;
        if(mm<15){
            numbrt = hh*4;
        }else if(mm<30){
            numbrt = hh*4+1;
        }else if(mm<45){
            numbrt = hh*4+2;
        }else{
            numbrt = hh*4+3;
        }

        List<CDSupEquipmentPowerInfo> pileSuperviseQueryStationPowerInfoList = deviceMessageMapper.getPileSuperviseQueryStationPowerInfo(Long.valueOf(stationID), dataTime, numbrt);
        for (int j = 0; j <pileSuperviseQueryStationPowerInfoList.size() ; j++) {
            CDSupEquipmentPowerInfo cdSupEquipmentPowerlnfo = pileSuperviseQueryStationPowerInfoList.get(j);
            //获取枪实时功率
            List<CDSupConnectorPowerInfo> terminalSuperviseQueryStationPowerInfo = deviceMessageMapper.getTerminalSuperviseQueryStationPowerInfo(Long.valueOf(cdSupEquipmentPowerlnfo.getPileID()), dataTime, numbrt);
            cdSupEquipmentPowerlnfo.setConnectorPowerInfos(terminalSuperviseQueryStationPowerInfo);
        }
        String key ="cdjgpc:station:"+replace+".getSuperviseQueryEquipmentPowerInfo";
        REDIS.setCacheObject(key, pileSuperviseQueryStationPowerInfoList,30L, TimeUnit.MINUTES);
        return R.ok(pileSuperviseQueryStationPowerInfoList);
    }


    //查充电事故
    @GetMapping("station/getSuperviseQueryAccidentInfo")
    public R getSuperviseQueryAccidentInfo(@RequestParam("lastQueryTime") String lastQueryTime,@RequestParam("replace") String replace){
        List<CDSupEquipmentAccidentInfo> list = deviceMessageMapper.getSuperviseQueryAccidentInfo();

        for (int i = 0; i <list.size() ; i++) {
            CDSupEquipmentAccidentInfo cdSupEquipmentAccidentInfo = list.get(i);
            if(!"".equals(cdSupEquipmentAccidentInfo.getPicturesName())){
                String[] split = cdSupEquipmentAccidentInfo.getPicturesName().split(",");
                String[] pictures =new String[split.length];
                for (int j = 0; j <split.length ; j++) {
                    String s = split[j];
                    pictures[j] = "https://xhpc-bucket1.oss-cn-hangzhou.aliyuncs.com/"+s;
                }
            }
            if(!"".equals(cdSupEquipmentAccidentInfo.getAnalysisReportName())){
                String[] split = cdSupEquipmentAccidentInfo.getAnalysisReportName().split(",");
                String[] analysisReport =new String[split.length];
                for (int j = 0; j <split.length ; j++) {
                    String s = split[j];
                    analysisReport[j] = "https://xhpc-bucket1.oss-cn-hangzhou.aliyuncs.com/"+s;
                }
            }
        }
        String key ="cdjgpc:station:"+replace+".getSuperviseQueryAccidentInfo";
        REDIS.setCacheObject(key, list,30L, TimeUnit.MINUTES);
        return R.ok(list);
    }


    //推送充电站用能统计信息
    @GetMapping("station/getSuperviseNotificationOperationStatusInfo")
    public R getSuperviseNotificationOperationStatusInfo(@RequestParam("replace")String replace){

        //获取昨天日期
        DateTime yesterday = DateUtil.yesterday();
        String format = DateUtil.format(yesterday, "yyyy-MM-dd");
        String startTime =format+" 00:00:00";
        String endTime =format+" 23:59:59";

        List<CDStationStatslnfo> stationStatsInfoList = deviceMessageMapper.getCDStationStatslnfoList(startTime, endTime);

        for (int k = 0; k <stationStatsInfoList.size() ; k++) {
            CDStationStatslnfo cdStationStatslnfo = stationStatsInfoList.get(k);
            Long stationId = Long.valueOf(cdStationStatslnfo.getStationID());
            //获取桩
            List<Long> chargingPileIds = deviceMessageMapper.getchargingPileId(stationId);
            List<CDSuqEquipmentStatslnfo> equipmentStatsInfos =new ArrayList<>();
            for (int i = 0; i < chargingPileIds.size(); i++) {
                Long chargingPileId = chargingPileIds.get(i);
                CDSuqEquipmentStatslnfo equipmentStatsInfo = deviceMessageMapper.getSupEquipmentStatsInfo(stationId, chargingPileId, startTime, endTime);
                List<CDSupConnectorStatslnfo> supConnectorStatslnfo = deviceMessageMapper.getSupConnectorStatslnfo(stationId, null, startTime, endTime);
                equipmentStatsInfo.setConnectorStatsInfos(supConnectorStatslnfo);
                equipmentStatsInfos.add(equipmentStatsInfo);
            }
            cdStationStatslnfo.setEquipmentStatsInfo(equipmentStatsInfos);
        }

        String key ="cdjgpc:station:"+replace+".getSuperviseNotificationOperationStatusInfo";
        REDIS.setCacheObject(key, stationStatsInfoList,30L, TimeUnit.MINUTES);
        return R.ok(stationStatsInfoList);
    }


    //推送充电站实时功率
    @GetMapping("station/getSuperviseNotificationRealtimePowerInfo")
    public R getSuperviseNotificationRealtimePowerInfo(@RequestParam("operatorIdEvcs")String operatorIdEvcs,@RequestParam("replace")String replace){

        List<Long> stationId = deviceMessageMapper.getStationId();
        //获取时间
        String now = DateUtil.now();
        String dataTime = now.substring(0,10);
        int hh = Integer.parseInt(now.substring(11,13));
        int mm = Integer.parseInt(now.substring(14,16));
        int numbrt =0;
        if(mm<15){
            numbrt = hh*4;
        }else if(mm<30){
            numbrt = hh*4+1;
        }else if(mm<45){
            numbrt = hh*4+2;
        }else{
            numbrt = hh*4+3;
        }
        List<CDSupStationPowerInfo> superviseQueryStationPowerInfoList = deviceMessageMapper.getSuperviseQueryStationPowerInfo(stationId, dataTime, numbrt);
        for (int i = 0; i < superviseQueryStationPowerInfoList.size(); i++) {
            //获取充电桩实时功率
            CDSupStationPowerInfo cdSupStationPowerInfo = superviseQueryStationPowerInfoList.get(i);

            List<CDSupEquipmentPowerInfo> pileSuperviseQueryStationPowerInfoList = deviceMessageMapper.getPileSuperviseQueryStationPowerInfo(Long.valueOf(cdSupStationPowerInfo.getStationID()), dataTime, numbrt);
            for (int j = 0; j <pileSuperviseQueryStationPowerInfoList.size() ; j++) {
                CDSupEquipmentPowerInfo cdSupEquipmentPowerlnfo = pileSuperviseQueryStationPowerInfoList.get(j);
                //获取枪实时功率
                List<CDSupConnectorPowerInfo> terminalSuperviseQueryStationPowerInfo = deviceMessageMapper.getTerminalSuperviseQueryStationPowerInfo(Long.valueOf(cdSupEquipmentPowerlnfo.getPileID()), dataTime, numbrt);
                cdSupEquipmentPowerlnfo.setConnectorPowerInfos(terminalSuperviseQueryStationPowerInfo);
            }
            cdSupStationPowerInfo.setEquipmentPowerInfos(pileSuperviseQueryStationPowerInfoList);
        }
        String key ="cdjgpc:station:"+replace+".getSuperviseNotificationRealtimePowerInfo";
        REDIS.setCacheObject(key, superviseQueryStationPowerInfoList,30L, TimeUnit.MINUTES);

        return R.ok(superviseQueryStationPowerInfoList);

    }


}
