package com.ruoyi.bst.device.service.impl;

import com.ruoyi.bst.device.domain.DeviceQuery;
import com.ruoyi.bst.device.domain.vo.DeviceStatVO;
import com.ruoyi.bst.device.mapper.DeviceMapper;
import com.ruoyi.bst.device.service.DeviceDashboard;
import com.ruoyi.common.domain.vo.StringIntegerVO;
import com.ruoyi.common.utils.MathUtils;
import com.ruoyi.dashboard.constants.StatKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DeviceDashboardImpl implements DeviceDashboard {

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public DeviceStatVO selectStat(DeviceQuery query, List<String> keys) {
        DeviceStatVO vo = new DeviceStatVO();
        if (keys.contains(StatKeys.DEVICE_COUNT)) {
            vo.setCount(this.selectCount(query));
        }
        if (keys.contains(StatKeys.DEVICE_STATUS_COUNT)) {
            vo.setStatusCount(this.selectStatusCount(query));
        }
        if (keys.contains(StatKeys.DEVICE_ONLINE_STATUS_COUNT)) {
            vo.setOnlineStatusCount(this.selectOnlineStatusCount(query));
        }
        return vo;
    }

    // 获取设备状态数量
    @Override
    public Map<String, Integer> selectStatusCount(DeviceQuery query) {
        List<StringIntegerVO> list = deviceMapper.selectStatusCount(query);
        return list.stream().collect(Collectors.toMap(StringIntegerVO::getKey, StringIntegerVO::getValue));
    }

    // 获取设备在线状态数量
    @Override
    public Map<String, Integer> selectOnlineStatusCount(DeviceQuery query) {
        List<StringIntegerVO> list = deviceMapper.selectOnlineStatusCount(query);
        return list.stream().collect(Collectors.toMap(StringIntegerVO::getKey, StringIntegerVO::getValue));
    }

    @Override
    public Integer selectCount(DeviceQuery query) {
        return MathUtils.dv(deviceMapper.selectCount(query));
    }

}
