-- ====================================
-- 新的调试方案：检查项圈设备数据
-- ====================================

-- 1. 检查项圈设备的总数量
SELECT 
    COUNT(*) as '项圈设备总数'
FROM iot_device_data 
WHERE device_type = 4;

-- 2. 检查项圈设备的数据
SELECT 
    device_id,
    device_type,
    voltage,
    temperature,
    latitude,
    longitude,
    steps,
    same_day_steps,
    server_device_id,
    update_time,
    LENGTH(latitude) as lat_len,
    LENGTH(longitude) as lng_len,
    LENGTH(CAST(voltage AS CHAR)) as voltage_len,
    LENGTH(CAST(temperature AS CHAR)) as temp_len,
    LENGTH(server_device_id) as server_id_len
FROM iot_device_data 
WHERE device_type = 4 
ORDER BY update_time DESC;

-- 3. 检查是否有任何字段长度超过50
SELECT 
    device_id,
    CASE 
        WHEN LENGTH(CAST(voltage AS CHAR)) > 50 THEN CONCAT('voltage:', LENGTH(CAST(voltage AS CHAR)))
        WHEN LENGTH(CAST(temperature AS CHAR)) > 50 THEN CONCAT('temperature:', LENGTH(CAST(temperature AS CHAR)))
        WHEN LENGTH(latitude) > 50 THEN CONCAT('latitude:', LENGTH(latitude))
        WHEN LENGTH(longitude) > 50 THEN CONCAT('longitude:', LENGTH(longitude))
        WHEN LENGTH(server_device_id) > 50 THEN CONCAT('server_device_id:', LENGTH(server_device_id))
        ELSE 'OK'
    END as field_issue,
    voltage,
    temperature,
    latitude,
    longitude,
    server_device_id
FROM iot_device_data 
WHERE device_type = 4 
AND (
    LENGTH(CAST(voltage AS CHAR)) > 50 OR
    LENGTH(CAST(temperature AS CHAR)) > 50 OR
    LENGTH(latitude) > 50 OR
    LENGTH(longitude) > 50 OR
    LENGTH(server_device_id) > 50
);

-- 4. 检查是否有NULL或空值
SELECT 
    device_id,
    voltage,
    temperature,
    latitude,
    longitude,
    server_device_id,
    CASE 
        WHEN voltage IS NULL THEN 'voltage is NULL'
        WHEN temperature IS NULL THEN 'temperature is NULL'
        WHEN latitude IS NULL THEN 'latitude is NULL'
        WHEN longitude IS NULL THEN 'longitude is NULL'
        WHEN server_device_id IS NULL THEN 'server_device_id is NULL'
        WHEN latitude = '' THEN 'latitude is empty'
        WHEN longitude = '' THEN 'longitude is empty'
        ELSE 'OK'
    END as null_check
FROM iot_device_data 
WHERE device_type = 4 
AND (
    voltage IS NULL OR
    temperature IS NULL OR
    latitude IS NULL OR
    longitude IS NULL OR
    server_device_id IS NULL OR
    latitude = '' OR
    longitude = ''
);
