-- ====================================
-- 模拟批量插入过程调试
-- ====================================

-- 1. 清空xq_client_log表
TRUNCATE TABLE xq_client_log;

-- 2. 检查项圈设备数据，模拟convertToCollarLog方法的处理逻辑
SELECT 
    device_id,
    latitude,
    longitude,
    voltage,
    temperature,
    steps,
    same_day_steps,
    CASE 
        WHEN latitude = '0' OR latitude IS NULL OR latitude = '' THEN 'INVALID_LAT'
        ELSE 'VALID_LAT'
    END as lat_status,
    CASE 
        WHEN longitude = '0' OR longitude IS NULL OR longitude = '' THEN 'INVALID_LNG'
        ELSE 'VALID_LNG'
    END as lng_status,
    CASE 
        WHEN (latitude = '0' OR latitude IS NULL OR latitude = '') 
         AND (longitude = '0' OR longitude IS NULL OR longitude = '') THEN 'SKIP'
        ELSE 'PROCESS'
    END as process_status
FROM iot_device_data 
WHERE device_type = 4
ORDER BY update_time DESC;

-- 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
)
SELECT 
    device_id,
    CAST(voltage AS CHAR) as device_voltage,
    CAST(temperature AS CHAR) as device_temp,
    NULL as server_device_id,
    latitude,
    longitude,
    steps as walk_steps,
    same_day_steps as y_walk_steps,
    NOW() as create_time,
    'BATCH_TEST' as create_by,
    NOW() as update_time,
    'BATCH_TEST' as update_by
FROM iot_device_data 
WHERE device_type = 4
AND latitude != '0' 
AND longitude != '0'
AND latitude IS NOT NULL 
AND longitude IS NOT NULL
AND latitude != '' 
AND longitude != '';

-- 4. 检查批量插入结果
SELECT COUNT(*) as '批量插入记录数' FROM xq_client_log WHERE create_by = 'BATCH_TEST';

-- 5. 查看插入的数据
SELECT 
    device_id,
    latitude,
    longitude,
    device_voltage,
    device_temp,
    walk_steps,
    y_walk_steps,
    create_time
FROM xq_client_log 
WHERE create_by = 'BATCH_TEST'
ORDER BY create_time DESC;

-- 6. 清理测试数据
DELETE FROM xq_client_log WHERE create_by = 'BATCH_TEST';
