package com.ho.business.service;

import com.ho.business.constant.DeviceTypeConstant;
import com.ho.business.entity.Station;
import com.ho.business.vo.DeviceTransfer;
import com.ho.business.vo.req.DeviceReqVO;
import com.ho.business.vo.resp.DeviceRespVO;
import com.ho.business.vo.resp.openStationHome.StatusMonitorVo;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author fancl
 * @desc: 状态监视实现类
 * @date 2023/4/17
 */
@Service
@Slf4j
public class StatusMonitorServiceImpl implements StatusMonitorService {

    @Autowired
    DeviceService deviceService;

    @Autowired
    StationService stationService;

    @Autowired
    RedisService redisService;

    @Override
    public StatusMonitorVo getStatusMonitor(Integer stationId) {
        StatusMonitorVo statusMonitorVo = new StatusMonitorVo();
        List<String> deviceTypes = new ArrayList<>();
        //取pcs对应的rediskey
        deviceTypes.add(DeviceTypeConstant.PCS);
        deviceTypes.add(DeviceTypeConstant.PCS + "_" + stationId);
        DeviceReqVO deviceReqVO = new DeviceReqVO();
        deviceReqVO.setStationId(stationId);
        deviceReqVO.setDeviceTypeList(deviceTypes);
        deviceReqVO.setNeedFilter(CommonConstant.ONE);
        List<DeviceRespVO> list = deviceService.getList(deviceReqVO);
        if (!list.isEmpty()) {
            DeviceRespVO deviceRespVO = list.get(0);
            String key = deviceRespVO.getDeviceType() + ":" + stationId + ":" + deviceRespVO.getSrcId();
            if (redisService.hasKey(key)) {
                DeviceTransfer deviceTransfer = (DeviceTransfer) redisService.hget(key, RedisKeyConstant.PCS_DEVICE.GRID);
                if (deviceTransfer != null) {
                    statusMonitorVo.setGrid(deviceTransfer.getValue());
                }
            }
            //以下值从redis中获取
            //运行模式/总故障状态/远程就地状态/电网频率
            //其中运行状态来源于3个值
            //停机状态(shutdownStatus) 待机状态(readinessStatus) 运行状态(runningStatus)


        }

        //运行状态由3个值组成,任意一个值等于1就将它赋值给 runMode
            /*Integer shutdownStatus = null;
            Integer readinessStatus = null;
            Integer runningStatus = null;
            HashMap shutdownStatusMap = (HashMap) redisService.hget(pcsDeviceRedisKey, RedisKeyConstant.PCS_DEVICE.SHUTDOWN_STATUS);
            if (shutdownStatusMap != null && shutdownStatusMap.containsKey(RedisKeyConstant.PCS_DEVICE.SHUTDOWN_STATUS)) {
                shutdownStatus = (Integer) shutdownStatusMap.get(RedisKeyConstant.PCS_DEVICE.SHUTDOWN_STATUS);
            }
            HashMap readinessStatusMap = (HashMap) redisService.hget(pcsDeviceRedisKey, RedisKeyConstant.PCS_DEVICE.READINESS_STATUS);
            if (readinessStatusMap != null && readinessStatusMap.containsKey(RedisKeyConstant.PCS_DEVICE.READINESS_STATUS)) {
                readinessStatus = (Integer) readinessStatusMap.get(RedisKeyConstant.PCS_DEVICE.READINESS_STATUS);
            }
            HashMap runningStatusMap = (HashMap) redisService.hget(pcsDeviceRedisKey, RedisKeyConstant.PCS_DEVICE.RUNNING_STATUS);
            if (runningStatusMap != null && runningStatusMap.containsKey(RedisKeyConstant.PCS_DEVICE.RUNNING_STATUS)) {
                runningStatus = (Integer) runningStatusMap.get(RedisKeyConstant.PCS_DEVICE.RUNNING_STATUS);
            }
            //哪个是1就赋值为哪个状态
            if (CommonConstant.ONE.equals(shutdownStatus)) {
                statusMonitorVo.setRunMode(OpenStationConstant.SHUTDOWN);
            } else if (CommonConstant.ONE.equals(readinessStatus)) {
                statusMonitorVo.setRunMode(OpenStationConstant.READINESS);
            } else if (CommonConstant.ONE.equals(runningStatus)) {
                statusMonitorVo.setRunMode(OpenStationConstant.RUNNING);
            }
            HashMap totalFaultStatusMap = (HashMap) redisService.hget(pcsDeviceRedisKey, RedisKeyConstant.PCS_DEVICE.TOTAL_FAULT_STATUS);
            if (totalFaultStatusMap != null && totalFaultStatusMap.containsKey(RedisKeyConstant.PCS_DEVICE.TOTAL_FAULT_STATUS)) {
                Integer totalFaultStatus = (Integer) totalFaultStatusMap.get(RedisKeyConstant.PCS_DEVICE.TOTAL_FAULT_STATUS);
                statusMonitorVo.setTotalFaultStatus(totalFaultStatus);
            }
            HashMap remoteLocalStatusMap = (HashMap) redisService.hget(pcsDeviceRedisKey, RedisKeyConstant.PCS_DEVICE.REMOTE_LOCAL_STATUS);
            if (remoteLocalStatusMap != null && remoteLocalStatusMap.containsKey(RedisKeyConstant.PCS_DEVICE.REMOTE_LOCAL_STATUS)) {
                Integer remoteLocalStatus = (Integer) remoteLocalStatusMap.get(RedisKeyConstant.PCS_DEVICE.REMOTE_LOCAL_STATUS);
                statusMonitorVo.setRemoteLocalStatus(remoteLocalStatus);
            }*/

        //取bms对应的rediskey
        deviceTypes = new ArrayList<>();
        //取pcs对应的rediskey
        deviceTypes.add(DeviceTypeConstant.BMS);
        deviceTypes.add(DeviceTypeConstant.BMS + "_" + stationId);
        List<String> bmsDeviceRedisKeys = getDeviceRedisKey(stationId, deviceTypes);
        BigDecimal rechargeCapacity = BigDecimal.ZERO;
        BigDecimal dischargeCapacity = BigDecimal.ZERO;
        //可充可放来自于累计值
        if (!bmsDeviceRedisKeys.isEmpty()) {
            //循环累加
            for (String bmsDeviceRedisKey : bmsDeviceRedisKeys) {
                log.info("bmsDeviceRedisKey:" + bmsDeviceRedisKey);
                //可充电电量/可放电电量
                DeviceTransfer rechargeCapacityDT = (DeviceTransfer) redisService.hget(bmsDeviceRedisKey, RedisKeyConstant.BMS_DEVICE.RECHARGE_CAPACITY);
                if (rechargeCapacityDT != null) {
                    BigDecimal rechargeCap = rechargeCapacityDT.getValue();
                    if (rechargeCap == null) {
                        rechargeCap = BigDecimal.ZERO;
                    }
                    rechargeCapacity = rechargeCapacity.add(rechargeCap);
                }
                DeviceTransfer dischargeCapacityDT = (DeviceTransfer) redisService.hget(bmsDeviceRedisKey, RedisKeyConstant.BMS_DEVICE.DISCHARGE_CAPACITY);
                if (dischargeCapacityDT != null) {
                    BigDecimal dischargeCap = dischargeCapacityDT.getValue();
                    if (dischargeCap == null) {
                        dischargeCap = BigDecimal.ZERO;
                    }
                    dischargeCapacity = dischargeCapacity.add(dischargeCap);
                }
            }
            statusMonitorVo.setRechargeCapacity(rechargeCapacity);
            statusMonitorVo.setDischargeCapacity(dischargeCapacity);
        }
        return statusMonitorVo;
    }

    //抽取一个根据设备类型查询到该设备缓存Key的方法
    @Override
    public List<String> getDeviceRedisKey(Integer stationId, List<String> deviceTypes) {
        List<String> keyList = new ArrayList<>();
        //查站信息
        Station station = stationService.selectById(stationId);
        if (station == null) {
            return keyList;
        }
        //查pcs设备
        DeviceReqVO deviceReqVO = new DeviceReqVO();
        deviceReqVO.setStationId(stationId);
        //先入一个
        deviceReqVO.setDeviceTypeList(deviceTypes);
        List<DeviceRespVO> pcsDeviceList = deviceService.getList(deviceReqVO);
        //只取第一个设备
        if (pcsDeviceList.isEmpty()) {
            return keyList;
        }
        for (DeviceRespVO deviceRespVO : pcsDeviceList) {
            //拿到设备类型,然后拼接key从缓存中取值
            String deviceType = deviceRespVO.getDeviceType();
            StringBuilder keyBuilder = new StringBuilder();
            String deviceRedisKey = keyBuilder.append(deviceType).append(":")
                    .append(stationId).append(":").append(deviceRespVO.getSrcId()).toString();
            keyList.add(deviceRedisKey);

        }
        return keyList;

    }


}
