-- ====================================
-- 深度调试：检查数据库表结构和数据
-- ====================================

-- 1. 检查xq_client_log表的latitude字段定义
SELECT 
    COLUMN_NAME,
    DATA_TYPE,
    CHARACTER_MAXIMUM_LENGTH,
    COLUMN_TYPE,
    IS_NULLABLE,
    COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_SCHEMA = DATABASE() 
AND TABLE_NAME = 'xq_client_log'
AND COLUMN_NAME = 'latitude';

-- 2. 检查iot_device_data表中项圈设备的latitude数据
SELECT 
    device_id,
    latitude,
    LENGTH(CAST(latitude AS CHAR)) as lat_length,
    CHAR_LENGTH(CAST(latitude AS CHAR)) as char_length,
    HEX(CAST(latitude AS CHAR)) as hex_value
FROM iot_device_data 
WHERE device_type = 4 
ORDER BY LENGTH(CAST(latitude AS CHAR)) DESC
LIMIT 10;

-- 3. 检查是否有超长的latitude数据
SELECT 
    device_id,
    latitude,
    LENGTH(CAST(latitude AS CHAR)) as lat_length
FROM iot_device_data 
WHERE device_type = 4 
AND LENGTH(CAST(latitude AS CHAR)) > 50
ORDER BY LENGTH(CAST(latitude AS CHAR)) DESC;

-- 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
) VALUES (
    'TEST_DEVICE_001',
    '3.300',
    '25.80',
    'TEST_SERVER_001',
    '30.481277875444164',
    '114.40076076679632',
    21,
    0,
    NOW(),
    'DEBUG_TEST',
    NOW(),
    'DEBUG_TEST'
);

-- 5. 检查插入结果
SELECT 
    device_id,
    latitude,
    longitude,
    device_voltage,
    device_temp,
    LENGTH(latitude) as lat_length,
    LENGTH(longitude) as lng_length,
    create_by,
    create_time
FROM xq_client_log 
WHERE create_by = 'DEBUG_TEST'
ORDER BY create_time DESC;

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