package com.ruoyi.business.service.impl;

import com.ruoyi.business.domain.VO.DeviceDataColumnVo;
import com.ruoyi.business.domain.VO.DeviceHistoryDataVo;
import com.ruoyi.business.domain.VO.DeviceLatestDataVo;
import com.ruoyi.business.service.IHwMonitorPlatformService;
import com.ruoyi.common.core.constant.HwDictConstants;
import com.ruoyi.common.core.constant.TdEngineConstants;
import com.ruoyi.tdengine.api.RemoteTdEngineService;
import com.ruoyi.tdengine.api.domain.TdHistorySelectDto;
import com.ruoyi.tdengine.api.domain.TdSuperTableSelectVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 监控平台数据处理接口
 * @ClassName: HwMonitorPlatformServiceImpl
 * @Author : xins
 * @Date :2023-09-16  14:50
 * @Version :1.0
 */
@Service
public class HwMonitorPlatformServiceImpl implements IHwMonitorPlatformService {
    @Resource
    private RemoteTdEngineService remoteTdEngineService;

    /**
     * @return List<Map < Object>>
     * @param: deviceLatestDataVo
     * @description 根据设备模型获取最新数据
     * @author xins
     * @date 2023-09-16 14:49
     */
    @Override
    public List<Map<String, Object>> selectLatestDataByTags(DeviceLatestDataVo deviceLatestDataVo) {
        TdSuperTableSelectVo tdSuperTableSelectVo = new TdSuperTableSelectVo();
        String databaseName = TdEngineConstants.getDatabaseName(deviceLatestDataVo.getSceneId());
        String superTableName = TdEngineConstants.getSuperTableName(deviceLatestDataVo.getDeviceModeId());
        tdSuperTableSelectVo.setDatabaseName(databaseName);
        tdSuperTableSelectVo.setSuperTableName(superTableName);
        tdSuperTableSelectVo.setGroupByTagsName(TdEngineConstants.ST_TAG_DEVICECODE);//todo,换成deviceid
        if (deviceLatestDataVo.getStartTime() != 0 || deviceLatestDataVo.getEndTime() != 0) {
            tdSuperTableSelectVo.setFirstFieldName(TdEngineConstants.DEFAULT_FIRST_FIELD_NAME);
        }
        tdSuperTableSelectVo.setDeviceCode(deviceLatestDataVo.getDeviceCode());
        tdSuperTableSelectVo.setDeviceName(deviceLatestDataVo.getDeviceName());
        tdSuperTableSelectVo.setStartTime(deviceLatestDataVo.getStartTime());
        tdSuperTableSelectVo.setEndTime(deviceLatestDataVo.getEndTime());
        tdSuperTableSelectVo.setOffset(deviceLatestDataVo.getOffset());
        tdSuperTableSelectVo.setLimit(deviceLatestDataVo.getLimit());
        return this.remoteTdEngineService.getLatestDataByTags(tdSuperTableSelectVo).getData();

    }


    /**
     * @param: deviceHistoryDataVo
     * @description 获取设备历史数据
     * @author xins
     * @date 2023-09-16 15:14
     * @return List<Map<Object>>
     */
    @Override
    public List<Map<String, Object>> getHistoryData(DeviceHistoryDataVo deviceHistoryDataVo) {
        TdHistorySelectDto tdHistorySelectDto = new TdHistorySelectDto();
        tdHistorySelectDto.setDatabaseName(TdEngineConstants.getDatabaseName(deviceHistoryDataVo.getSceneId()));
        tdHistorySelectDto.setTableName(TdEngineConstants.getDeviceDataTableName(deviceHistoryDataVo.getDeviceId()));
        tdHistorySelectDto.setOrderByFieldName(TdEngineConstants.DEFAULT_FIRST_FIELD_NAME);
        tdHistorySelectDto.setSort(TdEngineConstants.DEFAULT_ORDER_BY_MODE);
        if (deviceHistoryDataVo.getStartTime() != 0 || deviceHistoryDataVo.getEndTime() != 0) {
            tdHistorySelectDto.setFirstFieldName(TdEngineConstants.DEFAULT_FIRST_FIELD_NAME);
        }
        tdHistorySelectDto.setStartTime(deviceHistoryDataVo.getStartTime());
        tdHistorySelectDto.setEndTime(deviceHistoryDataVo.getEndTime());
        tdHistorySelectDto.setOffset(deviceHistoryDataVo.getOffset());
        tdHistorySelectDto.setLimit(deviceHistoryDataVo.getLimit());

        return this.remoteTdEngineService.getHistoryData(tdHistorySelectDto).getData();

    }

}
