-- 为车辆里程统计表添加分段数量字段
-- 用途：记录当日关联的GPS分段数量，用于数据完整性校验和业务监控

USE `ry-vue`;

-- 检查字段是否存在，如果不存在则添加
SET @dbname = DATABASE();
SET @tablename = 'tb_vehicle_mileage_stats';
SET @columnname = 'segment_count';
SET @preparedStatement = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE
      (TABLE_SCHEMA = @dbname)
      AND (TABLE_NAME = @tablename)
      AND (COLUMN_NAME = @columnname)
  ) > 0,
  'SELECT 1',
  CONCAT('ALTER TABLE ', @tablename, ' ADD COLUMN `segment_count` int(11) DEFAULT 0 COMMENT ''关联的分段数量'' AFTER `task_count`;')
));
PREPARE alterIfNotExists FROM @preparedStatement;
EXECUTE alterIfNotExists;
DEALLOCATE PREPARE alterIfNotExists;

-- 验证字段是否添加成功
DESC tb_vehicle_mileage_stats;
