-- 为xq_client_log表添加缺失的字段
-- 根据实体类XqClientLog的要求，添加所有必需的字段

-- 检查并添加device_voltage字段
SET @sql1 = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
     WHERE TABLE_SCHEMA = DATABASE() 
     AND TABLE_NAME = 'xq_client_log' 
     AND COLUMN_NAME = 'device_voltage') > 0,
    'SELECT "device_voltage字段已存在" as message',
    'ALTER TABLE xq_client_log ADD COLUMN device_voltage varchar(20) DEFAULT NULL COMMENT "设备电量" AFTER device_id'
));

PREPARE stmt1 FROM @sql1;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;

-- 检查并添加device_temp字段
SET @sql2 = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
     WHERE TABLE_SCHEMA = DATABASE() 
     AND TABLE_NAME = 'xq_client_log' 
     AND COLUMN_NAME = 'device_temp') > 0,
    'SELECT "device_temp字段已存在" as message',
    'ALTER TABLE xq_client_log ADD COLUMN device_temp varchar(20) DEFAULT NULL COMMENT "设备温度" AFTER device_voltage'
));

PREPARE stmt2 FROM @sql2;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;

-- 检查并添加server_device_id字段
SET @sql3 = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
     WHERE TABLE_SCHEMA = DATABASE() 
     AND TABLE_NAME = 'xq_client_log' 
     AND COLUMN_NAME = 'server_device_id') > 0,
    'SELECT "server_device_id字段已存在" as message',
    'ALTER TABLE xq_client_log ADD COLUMN server_device_id varchar(50) DEFAULT NULL COMMENT "主机编号" AFTER device_temp'
));

PREPARE stmt3 FROM @sql3;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;

-- 检查并添加walk_steps字段
SET @sql4 = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
     WHERE TABLE_SCHEMA = DATABASE() 
     AND TABLE_NAME = 'xq_client_log' 
     AND COLUMN_NAME = 'walk_steps') > 0,
    'SELECT "walk_steps字段已存在" as message',
    'ALTER TABLE xq_client_log ADD COLUMN walk_steps bigint(20) DEFAULT NULL COMMENT "总步数" AFTER longitude'
));

PREPARE stmt4 FROM @sql4;
EXECUTE stmt4;
DEALLOCATE PREPARE stmt4;

-- 检查并添加y_walk_steps字段
SET @sql5 = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
     WHERE TABLE_SCHEMA = DATABASE() 
     AND TABLE_NAME = 'xq_client_log' 
     AND COLUMN_NAME = 'y_walk_steps') > 0,
    'SELECT "y_walk_steps字段已存在" as message',
    'ALTER TABLE xq_client_log ADD COLUMN y_walk_steps bigint(20) DEFAULT NULL COMMENT "昨日总步数" AFTER walk_steps'
));

PREPARE stmt5 FROM @sql5;
EXECUTE stmt5;
DEALLOCATE PREPARE stmt5;

-- 检查并添加create_time字段
SET @sql6 = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
     WHERE TABLE_SCHEMA = DATABASE() 
     AND TABLE_NAME = 'xq_client_log' 
     AND COLUMN_NAME = 'create_time') > 0,
    'SELECT "create_time字段已存在" as message',
    'ALTER TABLE xq_client_log ADD COLUMN create_time datetime DEFAULT CURRENT_TIMESTAMP COMMENT "创建时间" AFTER y_walk_steps'
));

PREPARE stmt6 FROM @sql6;
EXECUTE stmt6;
DEALLOCATE PREPARE stmt6;

-- 检查并添加create_by字段
SET @sql7 = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
     WHERE TABLE_SCHEMA = DATABASE() 
     AND TABLE_NAME = 'xq_client_log' 
     AND COLUMN_NAME = 'create_by') > 0,
    'SELECT "create_by字段已存在" as message',
    'ALTER TABLE xq_client_log ADD COLUMN create_by varchar(50) DEFAULT NULL COMMENT "创建人" AFTER create_time'
));

PREPARE stmt7 FROM @sql7;
EXECUTE stmt7;
DEALLOCATE PREPARE stmt7;

-- 检查并添加update_time字段
SET @sql8 = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
     WHERE TABLE_SCHEMA = DATABASE() 
     AND TABLE_NAME = 'xq_client_log' 
     AND COLUMN_NAME = 'update_time') > 0,
    'SELECT "update_time字段已存在" as message',
    'ALTER TABLE xq_client_log ADD COLUMN update_time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT "更新时间" AFTER create_by'
));

PREPARE stmt8 FROM @sql8;
EXECUTE stmt8;
DEALLOCATE PREPARE stmt8;

-- 检查并添加update_by字段
SET @sql9 = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
     WHERE TABLE_SCHEMA = DATABASE() 
     AND TABLE_NAME = 'xq_client_log' 
     AND COLUMN_NAME = 'update_by') > 0,
    'SELECT "update_by字段已存在" as message',
    'ALTER TABLE xq_client_log ADD COLUMN update_by varchar(50) DEFAULT NULL COMMENT "更新人" AFTER update_time'
));

PREPARE stmt9 FROM @sql9;
EXECUTE stmt9;
DEALLOCATE PREPARE stmt9;

-- 显示最终的表结构
DESCRIBE xq_client_log;
