-- ============================================
-- HoneyBox 业务数据库清理脚本
-- 用途：上线前清空用户产生的数据，保留系统配置
-- 执行前请备份数据库！
-- ============================================

USE honey_box;
GO

PRINT '开始清理用户数据...';
PRINT '执行时间: ' + CONVERT(VARCHAR, GETDATE(), 120);
PRINT '========================================';

-- ==================== 1. 订单相关 ====================
PRINT '清理订单相关表...';

DELETE FROM order_items_send;
DELETE FROM order_items_recovery;
DELETE FROM order_items;
DELETE FROM order_notifies;
DELETE FROM orders;

DBCC CHECKIDENT ('order_items_send', RESEED, 0);
DBCC CHECKIDENT ('order_items_recovery', RESEED, 0);
DBCC CHECKIDENT ('order_items', RESEED, 0);
DBCC CHECKIDENT ('order_notifies', RESEED, 0);
DBCC CHECKIDENT ('orders', RESEED, 0);

PRINT '  - 订单相关表 ✓';

-- ==================== 2. 财务流水 ====================
PRINT '清理财务流水表...';

DELETE FROM profit_money;
DELETE FROM profit_money2;
DELETE FROM profit_integral;
DELETE FROM profit_score;
DELETE FROM profit_ou_qi;
DELETE FROM profit_pay;

DBCC CHECKIDENT ('profit_money', RESEED, 0);
DBCC CHECKIDENT ('profit_money2', RESEED, 0);
DBCC CHECKIDENT ('profit_integral', RESEED, 0);
DBCC CHECKIDENT ('profit_score', RESEED, 0);
DBCC CHECKIDENT ('profit_ou_qi', RESEED, 0);
DBCC CHECKIDENT ('profit_pay', RESEED, 0);

PRINT '  - 财务流水表 ✓';

-- ==================== 3. 用户相关（子表先删） ====================
PRINT '清理用户相关表...';

DELETE FROM user_signs;
DELETE FROM user_tasks;
DELETE FROM user_coupons;
DELETE FROM user_equity_rewards;
DELETE FROM user_item_card;
DELETE FROM user_addresses;
DELETE FROM user_accounts;
DELETE FROM user_login_logs;
DELETE FROM user_poster_cache;
DELETE FROM user_recharges;
DELETE FROM user_refresh_tokens;

DBCC CHECKIDENT ('user_signs', RESEED, 0);
DBCC CHECKIDENT ('user_tasks', RESEED, 0);
DBCC CHECKIDENT ('user_coupons', RESEED, 0);
DBCC CHECKIDENT ('user_equity_rewards', RESEED, 0);
DBCC CHECKIDENT ('user_item_card', RESEED, 0);
DBCC CHECKIDENT ('user_addresses', RESEED, 0);
DBCC CHECKIDENT ('user_accounts', RESEED, 0);
DBCC CHECKIDENT ('user_login_logs', RESEED, 0);
DBCC CHECKIDENT ('user_poster_cache', RESEED, 0);
DBCC CHECKIDENT ('user_recharges', RESEED, 0);
DBCC CHECKIDENT ('user_refresh_tokens', RESEED, 0);

-- 优惠券/充值记录
DELETE FROM coupon_receives;
DELETE FROM diamond_orders;

DBCC CHECKIDENT ('coupon_receives', RESEED, 0);
DBCC CHECKIDENT ('diamond_orders', RESEED, 0);

-- 排行榜/收藏/锁定
DELETE FROM goods_collections;
DELETE FROM goods_locks;
DELETE FROM goods_king_ranks;
DELETE FROM rank_week;
DELETE FROM rank_month;
DELETE FROM refresh_tokens;

DBCC CHECKIDENT ('goods_collections', RESEED, 0);
DBCC CHECKIDENT ('goods_locks', RESEED, 0);
DBCC CHECKIDENT ('goods_king_ranks', RESEED, 0);
DBCC CHECKIDENT ('rank_week', RESEED, 0);
DBCC CHECKIDENT ('rank_month', RESEED, 0);
DBCC CHECKIDENT ('refresh_tokens', RESEED, 0);

-- 最后删除用户主表
DELETE FROM users;
DBCC CHECKIDENT ('users', RESEED, 0);

PRINT '  - 用户相关表 ✓';

-- ==================== 4. 商品数据 ====================
PRINT '清理商品数据...';

DELETE FROM goods_designated_prizes;
DELETE FROM goods_extensions;
DELETE FROM goods_items;
DELETE FROM goods;

DBCC CHECKIDENT ('goods_designated_prizes', RESEED, 0);
DBCC CHECKIDENT ('goods_extensions', RESEED, 0);
DBCC CHECKIDENT ('goods_items', RESEED, 0);
DBCC CHECKIDENT ('goods', RESEED, 0);

PRINT '  - 商品数据表 ✓';

-- ==================== 5. 管理员日志 ====================
PRINT '清理管理员日志...';

DELETE FROM admin_login_logs;
DELETE FROM admin_operation_logs;
DELETE FROM operation_logs;

DBCC CHECKIDENT ('admin_login_logs', RESEED, 0);
DBCC CHECKIDENT ('admin_operation_logs', RESEED, 0);
DBCC CHECKIDENT ('operation_logs', RESEED, 0);

PRINT '  - 管理员日志表 ✓';

PRINT '========================================';
PRINT '清理完成！';
PRINT '完成时间: ' + CONVERT(VARCHAR, GETDATE(), 120);
GO
