package com.ho.business.service.impl;

import com.ho.business.constant.DeviceTypeConstant;
import com.ho.business.entity.Station;
import com.ho.business.service.ColCountService;
import com.ho.business.service.FreezeService;
import com.ho.business.service.StationHomeService;
import com.ho.business.util.CountOffsetUtils;
import com.ho.business.vo.req.StationReq;
import com.ho.business.vo.req.colCount.ColCountReq;
import com.ho.business.vo.resp.CountEleData;
import com.ho.business.vo.resp.colCount.ColCountResp;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.service.RedisService;
import com.ho.datacollect.api.constant.DataCollectConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author gyan
 * @desc: TODO
 * @DateTime: 2024/6/6 15:48
 */
@Service
@Slf4j
public class FreezeServiceImpl implements FreezeService {
    @Autowired
    StationHomeService stationHomeService;

    @Autowired
    ColCountService colCountService;

    @Autowired
    RedisService redisService;

    @Override
    public void getDayDate(List<Station> stationList) {
        ColCountReq vo = new ColCountReq();
        List<String> cols = new ArrayList<>();
        cols.add(DataCollectConstant.Elec_Meter.TOTAL_CHARGE);
        cols.add(DataCollectConstant.Elec_Meter.TOTAL_DISCHARGE);
        vo.setCols(cols);
        List<ColCountResp> colCountRespList = colCountService.selectByParam(vo);
        Map<Integer, String> typeMap = colCountRespList.stream().filter(i -> DataCollectConstant.Elec_Meter.TOTAL_CHARGE.equals(i.getCol())).collect(Collectors.toMap(ColCountResp::getStationId, ColCountResp::getDeviceType, (s1, s2) -> s1));
        List<ColCountResp> twoCCList = colCountRespList.stream().filter(i -> CommonConstant.TWO.equals(i.getCountFlag())).collect(Collectors.toList());
        Map<Integer, ColCountResp> chargeMap = new HashMap<>();
        Map<Integer, ColCountResp> dischargeMap = new HashMap<>();
        if(!twoCCList.isEmpty()){
            for (ColCountResp s:twoCCList) {
                if(DataCollectConstant.Elec_Meter.TOTAL_CHARGE.equals(s.getCol())){
                    chargeMap.put(s.getStationId(),s);
                }else if(DataCollectConstant.Elec_Meter.TOTAL_DISCHARGE.equals(s.getCol())){
                    dischargeMap.put(s.getStationId(),s);
                }
            }
        }
        for (Station station : stationList) {
            String key = "screenGroupData:" + station.getId();
            StationReq req = new StationReq();
//            String deviceType = colCountService.getDeviceType(station.getId());
            String deviceType = typeMap.get(station.getId());
            if (null == deviceType) {
                deviceType = DeviceTypeConstant.PCS;
            }
            req.setDeviceType(deviceType);
            req.setStationId(station.getId());
            CountEleData countEleData = stationHomeService.getCountEleData(req);
            if(chargeMap.get(station.getId()) != null){
                countEleData.setTotalChargeElec(CountOffsetUtils.countOffset(countEleData.getTotalChargeElec(), chargeMap.get(station.getId())));
            }
            if(dischargeMap.get(station.getId()) != null){
                countEleData.setTotalDischargeElec(CountOffsetUtils.countOffset(countEleData.getTotalDischargeElec(), dischargeMap.get(station.getId())));
            }
            countEleData.setDate(new Date());
            redisService.set(key, countEleData);
        }

    }
}
