-- ====================================
-- 简化测试：逐条插入项圈数据
-- ====================================

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

-- 2. 检查表是否已清空
SELECT COUNT(*) as '记录数量' FROM xq_client_log;

-- 3. 获取第一条项圈设备数据
SELECT 
    device_id,
    voltage,
    temperature,
    latitude,
    longitude,
    steps,
    same_day_steps,
    server_device_id
FROM iot_device_data 
WHERE device_type = 4 
ORDER BY update_time DESC
LIMIT 1;

-- 4. 尝试插入第一条数据
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,
    'SINGLE_TEST' as create_by,
    NOW() as update_time,
    'SINGLE_TEST' as update_by
FROM iot_device_data 
WHERE device_type = 4 
ORDER BY update_time DESC
LIMIT 1;

-- 5. 检查插入结果
SELECT 
    device_id,
    device_voltage,
    device_temp,
    latitude,
    longitude,
    walk_steps,
    y_walk_steps,
    create_by,
    create_time
FROM xq_client_log 
WHERE create_by = 'SINGLE_TEST';

-- 6. 尝试插入所有项圈数据（逐条）
-- 注意：这个查询会尝试插入所有项圈数据，如果失败会显示具体哪条数据有问题
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,
    'BATCH_TEST' as create_by,
    NOW() as update_time,
    'BATCH_TEST' as update_by
FROM iot_device_data 
WHERE device_type = 4 
ORDER BY update_time DESC;

-- 7. 检查批量插入结果
SELECT 
    COUNT(*) as '插入的记录数',
    MIN(create_time) as '最早创建时间',
    MAX(create_time) as '最晚创建时间'
FROM xq_client_log 
WHERE create_by = 'BATCH_TEST';
