package com.ho.business.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONObject;
import com.ho.business.constant.DeviceTypeConstant;
import com.ho.business.service.DeviceService;
import com.ho.business.service.ZhouZiHuanBaoService;
import com.ho.business.vo.DeviceTransfer;
import com.ho.business.vo.req.StationReq;
import com.ho.business.vo.resp.DeviceRespVO;
import com.ho.business.vo.resp.zhongzi.ZhouZiHuanBaoPV;
import com.ho.business.vo.resp.zhongzi.ZhouZiHuanBaoStatus;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class ZhouZiHuanBaoServiceImpl implements ZhouZiHuanBaoService {

    @Autowired
    RedisService redisService;

    @Autowired
    DeviceService deviceService;

    @Override
    public ZhouZiHuanBaoStatus getMiddlePart(StationReq req) {
        ZhouZiHuanBaoStatus status = new ZhouZiHuanBaoStatus();
        Integer stationId = req.getStationId();
        //获取电表表功率
        List<DeviceRespVO> list = deviceService.getListByDeviceType(stationId, DeviceTypeConstant.ELE_METER);
        DeviceTransfer deviceTransfer = null;
        if (!list.isEmpty()) {
            BigDecimal activePower = new BigDecimal(CommonConstant.ZERO);
            DeviceRespVO deviceRespVO = list.get(0);
            //在缓存里拿映射字段
            String key = deviceRespVO.getDeviceType() + ":" + stationId + ":" + deviceRespVO.getSrcId();
            if (redisService.hasKey(key)) {
                Map<Object, Object> infos = redisService.hgetall(key);
                deviceTransfer = (DeviceTransfer) infos.get(DeviceTypeConstant.ACTIVE_POWER);
                activePower = (deviceTransfer == null ? BigDecimal.ZERO : deviceTransfer.getValue());
            }
            status.setActivePower(NumberUtil.round(activePower, 2));
        }

        list = deviceService.getListByDeviceType(stationId, DeviceTypeConstant.PCS);
        if (!list.isEmpty()) {
            BigDecimal activePowerPCS = new BigDecimal(CommonConstant.ZERO);
            BigDecimal reactivePowerPCS = new BigDecimal(CommonConstant.ZERO);
            BigDecimal offGrid = new BigDecimal(CommonConstant.ZERO);
            BigDecimal onGrid = new BigDecimal(CommonConstant.ZERO);
            DeviceRespVO deviceRespVO = list.get(0);
            //在缓存里拿映射字段
            String key = deviceRespVO.getDeviceType() + ":" + stationId + ":" + deviceRespVO.getSrcId();
            if (redisService.hasKey(key)) {
                Map<Object, Object> infos = redisService.hgetall(key);
                deviceTransfer = (DeviceTransfer) infos.get(DeviceTypeConstant.ACTIVE_POWER);
                activePowerPCS = (deviceTransfer == null ? BigDecimal.ZERO : deviceTransfer.getValue());
                deviceTransfer = (DeviceTransfer) infos.get(DeviceTypeConstant.REACTIVE_POWER_PCS);
                reactivePowerPCS = (deviceTransfer == null ? BigDecimal.ZERO : deviceTransfer.getValue());
                deviceTransfer = (DeviceTransfer) infos.get(DeviceTypeConstant.ON_GRID);
                onGrid = (deviceTransfer == null ? BigDecimal.ZERO : deviceTransfer.getValue());
                deviceTransfer = (DeviceTransfer) infos.get(DeviceTypeConstant.OFF_GRID);
                offGrid = (deviceTransfer == null ? BigDecimal.ZERO : deviceTransfer.getValue());
            }
            status.setAndOffGrid(offGrid.intValue());
            status.setFlowDirection(deviceRespVO.getFlowDirection());
            status.setActivePowerPCS(NumberUtil.round(activePowerPCS, 2));
            status.setReactivePowerPCS(NumberUtil.round(reactivePowerPCS, 2));
        }

        list = deviceService.getListByDeviceType(stationId, DeviceTypeConstant.BMS);
        if (!list.isEmpty()) {
            BigDecimal soc = new BigDecimal(CommonConstant.ZERO);
            BigDecimal soh = new BigDecimal(CommonConstant.ZERO);
            DeviceRespVO deviceRespVO = list.get(0);
            //在缓存里拿映射字段
            String key = deviceRespVO.getDeviceType() + ":" + stationId + ":" + deviceRespVO.getSrcId();
            if (redisService.hasKey(key)) {
                Map<Object, Object> infos = redisService.hgetall(key);
                deviceTransfer = (DeviceTransfer) infos.get(DeviceTypeConstant.SOC);
                soc = (deviceTransfer == null ? BigDecimal.ZERO : deviceTransfer.getValue());
                deviceTransfer = (DeviceTransfer) infos.get(DeviceTypeConstant.SOH);
                soh = (deviceTransfer == null ? BigDecimal.ZERO : deviceTransfer.getValue());
            }
            status.setSoc(NumberUtil.round(soc, 2));
            status.setSoh(NumberUtil.round(soh, 2));
        }

        list = deviceService.getListByDeviceType(stationId, DeviceTypeConstant.DCDC);
        if (!list.isEmpty()) {
            BigDecimal acActivePower = new BigDecimal(CommonConstant.ZERO);
            DeviceRespVO deviceRespVO = list.get(0);
            //在缓存里拿映射字段
            String key = deviceRespVO.getDeviceType() + ":" + stationId + ":" + deviceRespVO.getSrcId();
            if (redisService.hasKey(key)) {
                Map<Object, Object> infos = redisService.hgetall(key);
                deviceTransfer = (DeviceTransfer) infos.get(DeviceTypeConstant.AC_ACTIVE_POWER);
                acActivePower = (deviceTransfer == null ? BigDecimal.ZERO : deviceTransfer.getValue());
            }
            status.setAcActivePower(NumberUtil.round(acActivePower, 2));
        }
        return status;
    }

    @Override
    public ZhouZiHuanBaoPV getStationData(StationReq req) {
        ZhouZiHuanBaoPV status = new ZhouZiHuanBaoPV();
        Integer stationId = req.getStationId();
        //获取电表表功率
        List<DeviceRespVO> list = deviceService.getListByDeviceType(stationId, DeviceTypeConstant.DCDC);
        DeviceTransfer deviceTransfer = null;
        if (!list.isEmpty()) {
            BigDecimal activePower = new BigDecimal(CommonConstant.ZERO);
            DeviceRespVO deviceRespVO = list.get(0);
            //在缓存里拿映射字段
            String key = deviceRespVO.getDeviceType() + ":" + stationId + ":" + deviceRespVO.getSrcId();
            if (redisService.hasKey(key)) {
                Map<Object, Object> infos = redisService.hgetall(key);
                deviceTransfer = (DeviceTransfer) infos.get(DeviceTypeConstant.CUMULATIVE_POWER_GENERATION);
                activePower = (deviceTransfer == null ? BigDecimal.ZERO : deviceTransfer.getValue());
            }
            status.setCumulativePowerGeneration(NumberUtil.round(activePower, 2));
        }
        return status;
    }
}
