package com.ho.business.service.impl;

import com.ho.business.entity.ModelDeviceColComp;
import com.ho.business.feignclient.TdFeignClient;
import com.ho.business.service.ModelDeviceService;
import com.ho.business.service.PcsService;
import com.ho.business.vo.resp.DeviceRespVO;
import com.ho.common.tools.exception.DataResult;
import com.ho.datacollect.api.constant.DataCollectConstant;
import com.ho.td.api.entity.elecMeter.ElecPowerGeneration;
import com.ho.td.api.entity.query.TdBaseTimeQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description pcs数据操作实现
 * Author yule
 * Date 2023/5/6 16:09
 */
@Service
@Slf4j
public class PcsServiceImpl implements PcsService {

    @Autowired
    ModelDeviceService modelDeviceService;

    @Autowired
    TdFeignClient tdFeignClient;

    @Override
    public ElecPowerGeneration dealPowerBetween(DeviceRespVO deviceRespVO, Date beginTime, Date endTime) {
        //创建远程调用的查询参数
        TdBaseTimeQuery tdBaseTimeQuery = new TdBaseTimeQuery();
        //充电量来自BMS日充,日放
        List<ModelDeviceColComp> totalChargeList = modelDeviceService.getCompListByType("device", deviceRespVO.getDeviceType(), DataCollectConstant.Elec_Meter.TOTAL_CHARGE);
        List<ModelDeviceColComp> totalDisChargeList = modelDeviceService.getCompListByType("device", deviceRespVO.getDeviceType(), DataCollectConstant.Elec_Meter.TOTAL_DISCHARGE);
        Map<String, String> colNameMap = new HashMap<>();
        if (!totalChargeList.isEmpty()) {
            colNameMap.put(DataCollectConstant.Elec_Meter.TOTAL_CHARGE, totalChargeList.get(0).getDeviceCol());
        }
        if (!totalDisChargeList.isEmpty()) {
            colNameMap.put(DataCollectConstant.Elec_Meter.TOTAL_DISCHARGE, totalDisChargeList.get(0).getDeviceCol());
        }
        tdBaseTimeQuery.setSrcId(deviceRespVO.getSrcId());
        tdBaseTimeQuery.setColNameMap(colNameMap);
        tdBaseTimeQuery.setGroupId(deviceRespVO.getGroupId());
        tdBaseTimeQuery.setStationId(deviceRespVO.getStationId());

        //当日的零点时间
        tdBaseTimeQuery.setBegin(beginTime);
        tdBaseTimeQuery.setEnd(endTime);
        //远程调用查询
        DataResult<ElecPowerGeneration> result = tdFeignClient.getTodayPowerGeneration(tdBaseTimeQuery);
        ElecPowerGeneration data = null;
        if (result != null && result.isSuccess()) {
            data = result.getData();
        }
        return data;
    }
}
