package com.hw.tdengine.service.impl;

import com.hw.common.core.constant.TdEngineConstants;
import com.hw.tdengine.api.domain.DeviceStatus;
import com.hw.tdengine.mapper.DeviceStatusMapper;
import com.hw.tdengine.service.IDeviceStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description: 设备状态信息业务服务
 * @ClassName: DeviceStatusServiceImpl
 * @Author : xins
 * @Date :2023-09-05  13:27
 * @Version :1.0
 */
@Service
public class DeviceStatusServiceImpl implements IDeviceStatusService {

    @Autowired
    private DeviceStatusMapper deviceStatusMapper;

    /**
     * @return List<DeviceStatus>
     * @param: DeviceStatus
     * @description 获取时间范围内每天的在线设备
     * @author xins
     * @date 2023-09-05 13:23
     */
    @Override
    public List<DeviceStatus> getOnlineDevicesGroupByDay(DeviceStatus queryDeviceStatus) {

        Map<String, Object> params = queryDeviceStatus.getParams();
        params.put("databaseName", TdEngineConstants.PLATFORM_DB_NAME);
        params.put("superTableName", TdEngineConstants.DEFAULT_DEVICE_STATUS_SUPER_TABLE_NAME);
//        DeviceStatus queryDeviceStatus = new DeviceStatus();
//        params.put("beginTime", beginTime);
//        params.put("endTime", endTime);
//        queryDeviceStatus.setParams(params);
//        if (sceneId != null) {
//            queryDeviceStatus.setSceneId(sceneId);
//        }

        List<DeviceStatus> deviceStatuses = deviceStatusMapper.getOnlineDevicesGroupByDay(queryDeviceStatus);

        return deviceStatuses;
    }


    /**
     * @return List<Map<String, Object>>
     * @param: DeviceStatus
     * @description 获取某个事件前最新的在线设备
     * @author xins
     * @date 2023-09-05 13:23
     */
    @Override
    public List<Map<String, Object>> getLastOnlineDevices(DeviceStatus queryDeviceStatus) {

        Map<String, Object> params = queryDeviceStatus.getParams();
        params.put("databaseName", TdEngineConstants.PLATFORM_DB_NAME);
        params.put("superTableName", TdEngineConstants.DEFAULT_DEVICE_STATUS_SUPER_TABLE_NAME);
        List<Map<String, Object>> deviceStatusMap = deviceStatusMapper.getLastOnlineDevices(queryDeviceStatus);
        List<Map<String, Object>> filterMaps = new ArrayList<>();

        //tdengine，在select时row会带着括号，需要将括号过滤掉
        for (Map<String, Object> latestMap : deviceStatusMap) {
            Map<String, Object> filterMap = latestMap.entrySet()
                    .stream()
                    .filter(entry -> entry.getValue() != null)
                    .collect(HashMap::new, (m, v) ->
                            m.put(v.getKey().substring(v.getKey().indexOf("(") + 1,
                                    v.getKey().indexOf(")")), v.getValue()), HashMap::putAll);
            filterMaps.add(filterMap);
        }

        return filterMaps;
    }


    /**
     * @return List<Map<String, Object>>
     * @param: DeviceStatus
     * @description 获取设备状态信息列表
     * @author xins
     * @date 2023-09-05 13:23
     */
    @Override
    public List<Map<String, Object>> getDeviceStatusList(DeviceStatus queryDeviceStatus) {
        Map<String, Object> params = queryDeviceStatus.getParams();
        params.put("databaseName", TdEngineConstants.PLATFORM_DB_NAME);
        params.put("tableName", TdEngineConstants.getDeviceStatusTableName(queryDeviceStatus.getDeviceId()));
        List<Map<String, Object>> deviceStatusMap = deviceStatusMapper.getDeviceStatusList(queryDeviceStatus);
        return deviceStatusMap;
    }

}
