-- ====================================
-- 深度调试：检查批量插入的具体问题
-- ====================================

-- 1. 检查项圈设备的详细数据
SELECT 
    device_id,
    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,
    HEX(latitude) as lat_hex,
    HEX(longitude) as lng_hex
FROM iot_device_data 
WHERE device_type = 4 
ORDER BY update_time DESC;

-- 2. 检查是否有重复的device_id
SELECT 
    device_id,
    COUNT(*) as count
FROM iot_device_data 
WHERE device_type = 4 
GROUP BY device_id
HAVING COUNT(*) > 1;

-- 3. 检查是否有NULL值
SELECT 
    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,
    voltage,
    temperature,
    latitude,
    longitude,
    server_device_id
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 = ''
);

-- 4. 检查是否有特殊字符
SELECT 
    device_id,
    latitude,
    longitude,
    HEX(latitude) as lat_hex,
    HEX(longitude) as lng_hex,
    LENGTH(latitude) as lat_len,
    LENGTH(longitude) as lng_len
FROM iot_device_data 
WHERE device_type = 4 
AND (
    latitude LIKE '%null%' OR
    longitude LIKE '%null%' OR
    latitude LIKE '%NULL%' OR
    longitude LIKE '%NULL%' OR
    latitude LIKE '%undefined%' OR
    longitude LIKE '%undefined%'
);

-- 5. 尝试手动插入所有8条数据（逐条）
-- 先清空表
TRUNCATE TABLE xq_client_log;

-- 插入第1条
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,
    latitude,
    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 
ORDER BY update_time DESC
LIMIT 1;

-- 检查插入结果
SELECT 
    device_id,
    device_voltage,
    device_temp,
    latitude,
    longitude,
    create_by,
    create_time
FROM xq_client_log 
WHERE create_by = 'MANUAL_TEST';
