package com.ruoyi.business.service.impl;

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

import com.ruoyi.business.domain.VO.DeviceModeVo;
import com.ruoyi.common.core.constant.TdEngineConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.tdengine.api.RemoteTdEngineService;
import com.ruoyi.tdengine.api.domain.TdHistorySelectDto;
import com.ruoyi.tdengine.api.domain.TdSelectDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.HwDeviceMapper;
import com.ruoyi.business.domain.HwDevice;
import com.ruoyi.business.service.IHwDeviceService;

/**
 * 设备信息Service业务层处理
 *
 * @author xins
 * @date 2023-08-24
 */
@Service
public class HwDeviceServiceImpl implements IHwDeviceService
{
    @Autowired
    private HwDeviceMapper hwDeviceMapper;
    @Autowired
    private RemoteTdEngineService remoteTdEgineService;

    /**
     * 查询设备信息
     *
     * @param deviceId 设备信息主键
     * @return 设备信息
     */
    @Override
    public HwDevice selectHwDeviceByDeviceId(Long deviceId)
    {
        return hwDeviceMapper.selectHwDeviceByDeviceId(deviceId);
    }

    /**
     * 查询设备信息列表
     *
     * @param hwDevice 设备信息
     * @return 设备信息
     */
    @Override
    public List<HwDevice> selectHwDeviceList(HwDevice hwDevice)
    {
        return hwDeviceMapper.selectHwDeviceList(hwDevice);
    }

    /**
     * 新增设备信息
     *
     * @param hwDevice 设备信息
     * @return 结果
     */
    @Override
    public int insertHwDevice(HwDevice hwDevice)
    {
        hwDevice.setCreateTime(DateUtils.getNowDate());
        return hwDeviceMapper.insertHwDevice(hwDevice);
    }
    /**
     * 查询3.	设备各类型占所有设备数量百分比饼状图（左下角）
     * */
    @Override
    public List<DeviceModeVo> selectDeviceModeNameVo() {
        List<DeviceModeVo> list = hwDeviceMapper.selectDeviceModeNameVo();
        Integer value = 0;
        if (list.size() > 10) {
            for (int i = 9; i < list.size(); i++) {
                if (list.get(i).sum==null){
                    list.get(i).sum = 0;
                }
                value = value + list.get(i).sum;
            }
        }
        DeviceModeVo hwMonitorUnitTypeVo = new DeviceModeVo();
        hwMonitorUnitTypeVo.setDeviceModeName("其他");
        hwMonitorUnitTypeVo.setSum(value);
        Integer sums = 0;
        for (int i = 0; i < list.size();i++){
            if (list.get(i).sum==null){
                list.get(i).sum = 0;
            }
            sums = sums+list.get(i).sum;
        }
        for (int i=0;i<list.size();i++){
            list.get(i).setPercentage((double) (list.get(i).getSum()/sums));
        }
        if (list.size() > 10) {
            list.add(10, hwMonitorUnitTypeVo);
            return list.subList(0, 10);
        } else {
            return list;
        }
    }
    //获取有开关属性的设备
    @Override
    public Map<String, Map<String, Object>> selectDeviceByDeviceModeByMonitorUnitId(Long monitorUnitId){
        List<HwDevice> hwDevices = hwDeviceMapper.selectDeviceByDeviceModeByMonitorUnitId(monitorUnitId);//todo 根据监控单元(已解决)
        TdSelectDto tdSelectDto = new TdSelectDto();
        Map<String, Map<String, Object>> latestDataByTags = new HashMap<String, Map<String,Object>>();
        for (int i = 0; i < hwDevices.size(); i++){
            String s = hwDeviceMapper.selectDeviceByDeviceModeByDeviceModeFunction(hwDevices.get(i).getDeviceId());
            if (s!=null&&s!=""){
                tdSelectDto.setDatabaseName(TdEngineConstants.PLATFORM_DB_NAME);
                tdSelectDto.setSuperTableName(TdEngineConstants.DEFAULT_DEVICE_STATUS_SUPER_TABLE_NAME);
                tdSelectDto.setTagsName(hwDevices.get(i).getDeviceCode());
                latestDataByTags = (Map<String, Map<String, Object>>) remoteTdEgineService.getLatestDataByTags(tdSelectDto);
            }
        //todo 根据获取的device，通过devicemodeid获取devicemode，再根据devicemodeid获取devicemodefunction，（已解决）
            // todo 然后根据devicemodefunction中的datatype为8的（是bool的）(以解决)

        }
        return latestDataByTags;
    }
    //查询直采变量属性的设备
    @Override
    public Map<String, Map<String, Object>> selectDeviceModeFunctionByFunctionType(Long monitorUnitId) {
        List<HwDevice> hwDevices = hwDeviceMapper.selectDeviceByDeviceModeByMonitorUnitId(monitorUnitId);//todo 根据监控单元(已解决)
        TdSelectDto tdSelectDto = new TdSelectDto();
        Map<String, Map<String, Object>> latestDataByTags = new HashMap<String, Map<String,Object>>();
        for (int i = 0; i < hwDevices.size(); i++){
            String s = hwDeviceMapper.selectDeviceModeFunctionByFunctionType(hwDevices.get(i).getDeviceId());
            if (s!=null&&s!=""){
                tdSelectDto.setDatabaseName(TdEngineConstants.PLATFORM_DB_NAME);
                tdSelectDto.setSuperTableName(TdEngineConstants.DEFAULT_DEVICE_STATUS_SUPER_TABLE_NAME);
                tdSelectDto.setTagsName(hwDevices.get(i).getDeviceCode());
                latestDataByTags = (Map<String, Map<String, Object>>) remoteTdEgineService.getLatestDataByTags(tdSelectDto);
            }}
        return latestDataByTags;
    }

    @Override
    public List<Map<String, Object>> getHistoryData(Long deviceId) {
        TdHistorySelectDto tdHistorySelectDto = new TdHistorySelectDto();
        tdHistorySelectDto.setDatabaseName(TdEngineConstants.PLATFORM_DB_NAME);
        tdHistorySelectDto.setTableName(TdEngineConstants.getDeviceDataTableName(deviceId));
        tdHistorySelectDto.setOrderByFieldName(tdHistorySelectDto.getFirstFieldName());
        tdHistorySelectDto.setSort(TdEngineConstants.DEFAULT_ORDER_BY_MODE);
        List<Map<String, Object>> historyData = (List<Map<String, Object>>) remoteTdEgineService.getHistoryData(tdHistorySelectDto);
        return historyData;
    }

    /**
     * 修改设备信息
     *
     * @param hwDevice 设备信息
     * @return 结果
     */
    @Override
    public int updateHwDevice(HwDevice hwDevice)
    {
        hwDevice.setUpdateTime(DateUtils.getNowDate());
        return hwDeviceMapper.updateHwDevice(hwDevice);
    }

    /**
     * 批量删除设备信息
     *
     * @param deviceIds 需要删除的设备信息主键
     * @return 结果
     */
    @Override
    public int deleteHwDeviceByDeviceIds(Long[] deviceIds)
    {
        return hwDeviceMapper.deleteHwDeviceByDeviceIds(deviceIds);
    }

    /**
     * 删除设备信息信息
     *
     * @param deviceId 设备信息主键
     * @return 结果
     */
    @Override
    public int deleteHwDeviceByDeviceId(Long deviceId)
    {
        return hwDeviceMapper.deleteHwDeviceByDeviceId(deviceId);
    }
}
