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

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.ruoyi.bst.device.constants.DeviceCacheKey;
import com.ruoyi.bst.device.domain.Device;
import com.ruoyi.bst.device.domain.enums.DeviceStatus;
import com.ruoyi.bst.device.service.DeviceConverter;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;

@Service
public class DeviceConverterImpl implements DeviceConverter{

    @Override
    public Device toPoByCreate(Device data) {
        if (data == null) {
            return null;
        }
        Device po = new Device();
        // 设置设备基本信息
        po.setMac(data.getMac());
        po.setSn(data.getSn());
        po.setVehicleNum(data.getVehicleNum());
        po.setModelId(data.getModelId());
        po.setStatus(DeviceStatus.STORAGE.getCode());
        
        // 设置运营相关信息
        po.setMchId(data.getMchId());
        po.setAreaId(data.getAreaId());
        return po;
    }

    @Override
    public Device toPoByUpdate(Device data) {
        if (data == null) {
            return null;
        }
        Device po = new Device();

        // 只允许管理员更新的数据
        if (SecurityUtils.isSysAdmin()) {
            po.setSn(data.getSn());
        }

        // 设置设备基本信息
        po.setVehicleNum(data.getVehicleNum());
        po.setModelId(data.getModelId());
        
        // 设置运营相关信息
        po.setMchId(data.getMchId());
        po.setAreaId(data.getAreaId());
        return po;
        
    }

    @Override
    public Map<String, Object> toCacheInfo(Device device) {
        Map<String, Object> info = new HashMap<>();

        info.put(DeviceCacheKey.MAC, device.getMac());

        if (device.getVoltage() != null) {
            info.put(DeviceCacheKey.VOLTAGE, device.getVoltage());
        }
        if (device.getSignalStrength() != null) {
            info.put(DeviceCacheKey.SIGNAL_STRENGTH, device.getSignalStrength());
        }
        if (device.getQuality() != null) {
            info.put(DeviceCacheKey.QUALITY, device.getQuality());
        }
        if (device.getSatellites() != null) {
            info.put(DeviceCacheKey.SATELLITES, device.getSatellites());
        }
        if (device.getIotStatus() != null) {
            info.put(DeviceCacheKey.IOT_STATUS, device.getIotStatus());
        }
        if (device.getLongitude() != null) {
            info.put(DeviceCacheKey.LONGITUDE, device.getLongitude());
        }
        if (device.getLatitude() != null) {
            info.put(DeviceCacheKey.LATITUDE, device.getLatitude());
        }
        if (device.getLastTime() != null) {
            info.put(DeviceCacheKey.LAST_TIME, DateUtils.format(device.getLastTime(), DateUtils.YYYY_MM_DD_HH_MM_SS));
        }
        if (device.getLastLocationTime() != null) {
            info.put(DeviceCacheKey.LAST_LOCATION_TIME, DateUtils.format(device.getLastLocationTime(), DateUtils.YYYY_MM_DD_HH_MM_SS));
        }
        if (device.getSoftwareVersion() != null) {
            info.put(DeviceCacheKey.SOFTWARE_VERSION, device.getSoftwareVersion());
        }
        if (device.getLocationType() != null) {
            info.put(DeviceCacheKey.LOCATION_TYPE, device.getLocationType());
        }
        if (device.getOnlineStatus() != null) {
            info.put(DeviceCacheKey.ONLINE_STATUS, device.getOnlineStatus());
        }
        if (device.getLastOnlineTime() != null) {
            info.put(DeviceCacheKey.LAST_ONLINE_TIME, DateUtils.format(device.getLastOnlineTime(), DateUtils.YYYY_MM_DD_HH_MM_SS));
        }
        return info;
    }
    
}
