-- ====================================
-- 步骤3：分析xq_client_log表的插入失败原因
-- ====================================

-- 3.1 检查当前xq_client_log表中的数据
SELECT 
    id,
    device_id as '设备ID',
    device_voltage as '设备电压',
    device_temp as '设备温度',
    server_device_id as '主机设备ID',
    latitude as '纬度',
    longitude as '经度',
    walk_steps as '步数',
    y_walk_steps as '当日步数',
    LENGTH(latitude) as '纬度长度',
    LENGTH(longitude) as '经度长度',
    LENGTH(device_voltage) as '电压长度',
    LENGTH(device_temp) as '温度长度',
    create_time as '创建时间'
FROM xq_client_log 
ORDER BY create_time DESC
LIMIT 10;

-- 3.2 尝试手动插入测试数据（使用iot_device_data中的第一条项圈数据）
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
)
SELECT 
    device_id,
    CAST(voltage AS CHAR) as device_voltage,
    CAST(temperature AS CHAR) as device_temp,
    server_device_id,
    CAST(latitude AS CHAR) as latitude,
    CAST(longitude AS CHAR) as longitude,
    steps as walk_steps,
    same_day_steps as y_walk_steps,
    NOW() as create_time,
    'MANUAL_TEST' as create_by,
    NOW() as update_time,
    'MANUAL_TEST' as update_by
FROM iot_device_data 
WHERE device_type = 4 
AND device_id NOT IN (SELECT device_id FROM xq_client_log WHERE create_by = 'MANUAL_TEST')
ORDER BY update_time DESC
LIMIT 1;

-- 3.3 检查插入结果
SELECT 
    device_id as '设备ID',
    device_voltage as '设备电压',
    device_temp as '设备温度',
    latitude as '纬度',
    longitude as '经度',
    LENGTH(latitude) as '纬度长度',
    LENGTH(longitude) as '经度长度',
    create_by as '创建者',
    create_time as '创建时间'
FROM xq_client_log 
WHERE create_by = 'MANUAL_TEST'
ORDER BY create_time DESC;

-- 3.4 检查是否有NULL值
SELECT 
    COUNT(*) as '总记录数',
    COUNT(device_id) as '有设备ID的记录数',
    COUNT(latitude) as '有纬度的记录数',
    COUNT(longitude) as '有经度的记录数',
    COUNT(device_voltage) as '有电压的记录数',
    COUNT(device_temp) as '有温度的记录数'
FROM xq_client_log;

-- 3.5 清理测试数据
DELETE FROM xq_client_log WHERE create_by = 'MANUAL_TEST';

