-- ============================================================
-- 清空运行时数据脚本（保留基础配置数据）
-- 适用于：生产环境部署前清空测试/历史运行数据
-- 执行方式：mysql -u root -proot < clean-runtime-data.sql
-- ============================================================

-- ========== 业务库 (cnc_business) ==========

USE cnc_business;

-- 1. 告警记录
DELETE FROM cnc_alert WHERE 1=1;
ALTER TABLE cnc_alert AUTO_INCREMENT = 1;

-- 2. 采集记录
DELETE FROM cnc_collect_record WHERE 1=1;
ALTER TABLE cnc_collect_record AUTO_INCREMENT = 1;

-- 3. 日产量汇总
DELETE FROM cnc_daily_production WHERE 1=1;
ALTER TABLE cnc_daily_production AUTO_INCREMENT = 1;

-- 4. 机床日状态
DELETE FROM cnc_machine_daily_status WHERE 1=1;
ALTER TABLE cnc_machine_daily_status AUTO_INCREMENT = 1;

-- 5. 产量调整记录
DELETE FROM cnc_production_adjustment WHERE 1=1;
ALTER TABLE cnc_production_adjustment AUTO_INCREMENT = 1;

-- 6. 产量分段记录
DELETE FROM cnc_production_segment WHERE 1=1;
ALTER TABLE cnc_production_segment AUTO_INCREMENT = 1;

-- 7. 员工日汇总
DELETE FROM cnc_worker_daily_summary WHERE 1=1;
ALTER TABLE cnc_worker_daily_summary AUTO_INCREMENT = 1;

-- 8. 重置机床实时状态字段
UPDATE cnc_machine SET 
  last_collect_time = NULL,
  last_device_status = NULL,
  last_run_status = NULL,
  last_program_name = NULL,
  last_part_count = NULL,
  last_operate_mode = NULL,
  last_machining_status = NULL,
  last_ping_time = NULL,
  last_ping_latency = NULL;

-- 9. 重置采集地址状态
UPDATE cnc_collect_address SET 
  last_collect_time = NULL,
  last_collect_status = NULL,
  fail_count = 0;

-- ========== 日志库 (cnc_log) ==========

USE cnc_log;

-- 10. 采集器心跳
DELETE FROM log_collector_heartbeat WHERE 1=1;
ALTER TABLE log_collector_heartbeat AUTO_INCREMENT = 1;

-- 11. 采集分析记录
DELETE FROM log_collect_analysis WHERE 1=1;
ALTER TABLE log_collect_analysis AUTO_INCREMENT = 1;

-- 12. 采集周期
DELETE FROM log_collect_cycle WHERE 1=1;
ALTER TABLE log_collect_cycle AUTO_INCREMENT = 1;

-- 13. 原始JSON日志
DELETE FROM log_collect_raw WHERE 1=1;
ALTER TABLE log_collect_raw AUTO_INCREMENT = 1;

-- 14. 分区追踪
DELETE FROM log_partition_tracker WHERE 1=1;
ALTER TABLE log_partition_tracker AUTO_INCREMENT = 1;

-- 15. 系统日志
DELETE FROM log_system WHERE 1=1;
ALTER TABLE log_system AUTO_INCREMENT = 1;
