-- 检查iot_device_data表中的数据长度，找出导致截断的原因

-- 1. 检查设备24075000139的数据
SELECT 
    device_id,
    device_type,
    voltage,
    temperature,
    latitude,
    longitude,
    steps,
    same_day_steps,
    LENGTH(CAST(voltage AS CHAR)) as voltage_length,
    LENGTH(CAST(temperature AS CHAR)) as temp_length,
    LENGTH(CAST(latitude AS CHAR)) as lat_length,
    LENGTH(CAST(longitude AS CHAR)) as lng_length,
    LENGTH(CAST(steps AS CHAR)) as steps_length
FROM iot_device_data 
WHERE device_id = '24075000139' 
AND device_type = 4;

-- 2. 检查所有项圈设备的数据长度分布
SELECT 
    'voltage' as field_name,
    MIN(LENGTH(CAST(voltage AS CHAR))) as min_length,
    MAX(LENGTH(CAST(voltage AS CHAR))) as max_length,
    AVG(LENGTH(CAST(voltage AS CHAR))) as avg_length
FROM iot_device_data 
WHERE device_type = 4 AND voltage IS NOT NULL

UNION ALL

SELECT 
    'temperature' as field_name,
    MIN(LENGTH(CAST(temperature AS CHAR))) as min_length,
    MAX(LENGTH(CAST(temperature AS CHAR))) as max_length,
    AVG(LENGTH(CAST(temperature AS CHAR))) as avg_length
FROM iot_device_data 
WHERE device_type = 4 AND temperature IS NOT NULL

UNION ALL

SELECT 
    'latitude' as field_name,
    MIN(LENGTH(CAST(latitude AS CHAR))) as min_length,
    MAX(LENGTH(CAST(latitude AS CHAR))) as max_length,
    AVG(LENGTH(CAST(latitude AS CHAR))) as avg_length
FROM iot_device_data 
WHERE device_type = 4 AND latitude IS NOT NULL

UNION ALL

SELECT 
    'longitude' as field_name,
    MIN(LENGTH(CAST(longitude AS CHAR))) as min_length,
    MAX(LENGTH(CAST(longitude AS CHAR))) as max_length,
    AVG(LENGTH(CAST(longitude AS CHAR))) as avg_length
FROM iot_device_data 
WHERE device_type = 4 AND longitude IS NOT NULL;

-- 3. 检查xq_client_log表的当前字段长度
SELECT 
    COLUMN_NAME,
    DATA_TYPE,
    CHARACTER_MAXIMUM_LENGTH,
    NUMERIC_PRECISION,
    NUMERIC_SCALE
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_SCHEMA = DATABASE() 
AND TABLE_NAME = 'xq_client_log'
AND COLUMN_NAME IN ('latitude', 'longitude', 'device_voltage', 'device_temp', 'server_device_id', 'walk_steps', 'y_walk_steps');
