-- 详细调试xq_client_log表插入问题
-- 找出Data truncation的真正原因

-- 1. 检查xq_client_log表的所有字段长度
SELECT 
    COLUMN_NAME,
    DATA_TYPE,
    CHARACTER_MAXIMUM_LENGTH,
    NUMERIC_PRECISION,
    NUMERIC_SCALE,
    IS_NULLABLE,
    COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_SCHEMA = DATABASE() 
AND TABLE_NAME = 'xq_client_log'
ORDER BY ORDINAL_POSITION;

-- 2. 检查iot_device_data表中的项圈设备数据
SELECT 
    device_id,
    device_type,
    voltage,
    temperature,
    steps,
    same_day_steps,
    latitude,
    longitude,
    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_type = 4 
ORDER BY update_time DESC
LIMIT 5;

-- 3. 尝试手动插入一条测试记录
INSERT INTO xq_client_log (
    device_id, 
    device_voltage, 
    device_temp, 
    server_device_id,
    latitude, 
    longitude, 
    walk_steps, 
    y_walk_steps,
    create_time, 
    create_by, 
    update_time, 
    update_by
) VALUES (
    '24075000139',
    '3.300',
    '25.80',
    'test_server_id',
    '30.481277875444164',
    '114.40076076679632',
    21,
    0,
    NOW(),
    'TEST',
    NOW(),
    'TEST'
);

-- 4. 检查插入结果
SELECT 
    device_id,
    device_voltage,
    device_temp,
    walk_steps,
    y_walk_steps,
    latitude,
    longitude,
    create_time
FROM xq_client_log 
WHERE device_id = '24075000139'
ORDER BY create_time DESC;

-- 5. 检查是否有其他可能超长的字段
SELECT 
    COLUMN_NAME,
    CHARACTER_MAXIMUM_LENGTH,
    DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_SCHEMA = DATABASE() 
AND TABLE_NAME = 'xq_client_log'
AND CHARACTER_MAXIMUM_LENGTH IS NOT NULL
AND CHARACTER_MAXIMUM_LENGTH < 1000;
