-- ============================================================
-- 数据库清理脚本：保留 users, products, product_categories, exchange_rates
-- 清除所有其他业务数据
-- ============================================================

BEGIN;

-- 临时禁用触发器（绕过外键约束）
SET session_replication_role = replica;

-- 清除所有业务表数据
TRUNCATE TABLE projects CASCADE;
TRUNCATE TABLE customers CASCADE;
TRUNCATE TABLE suppliers CASCADE;
TRUNCATE TABLE subcontractors CASCADE;
TRUNCATE TABLE logistics_companies CASCADE;
TRUNCATE TABLE purchase_requests CASCADE;
TRUNCATE TABLE purchase_orders CASCADE;
TRUNCATE TABLE purchase_order_items CASCADE;
TRUNCATE TABLE purchases CASCADE;
TRUNCATE TABLE purchase_items CASCADE;
TRUNCATE TABLE payment_plans CASCADE;
TRUNCATE TABLE payment_requests CASCADE;
TRUNCATE TABLE payment_records CASCADE;
TRUNCATE TABLE payment_nodes CASCADE;
TRUNCATE TABLE advances CASCADE;
TRUNCATE TABLE advance_settlements CASCADE;
TRUNCATE TABLE reimbursements CASCADE;
TRUNCATE TABLE verifications CASCADE;
TRUNCATE TABLE executions CASCADE;
TRUNCATE TABLE approval_records CASCADE;
TRUNCATE TABLE inventory_records CASCADE;
TRUNCATE TABLE budget_projects CASCADE;
TRUNCATE TABLE budget_attachments CASCADE;
TRUNCATE TABLE budget_quotations CASCADE;
TRUNCATE TABLE construction_logs CASCADE;
TRUNCATE TABLE construction_log_photos CASCADE;
TRUNCATE TABLE construction_milestones CASCADE;
TRUNCATE TABLE milestone_evidence CASCADE;
TRUNCATE TABLE project_work_items CASCADE;
TRUNCATE TABLE quotations CASCADE;
TRUNCATE TABLE quotation_items CASCADE;
TRUNCATE TABLE subcontract_contracts CASCADE;
TRUNCATE TABLE subcontract_payments CASCADE;
TRUNCATE TABLE contacts CASCADE;
TRUNCATE TABLE supplier_contacts CASCADE;
TRUNCATE TABLE supplier_payment_infos CASCADE;
TRUNCATE TABLE subcontractor_contacts CASCADE;
TRUNCATE TABLE subcontractor_payment_infos CASCADE;
TRUNCATE TABLE customer_contacts CASCADE;
TRUNCATE TABLE customer_payment_infos CASCADE;
TRUNCATE TABLE logistics_company_contacts CASCADE;
TRUNCATE TABLE logistics_company_bank_accounts CASCADE;
TRUNCATE TABLE bank_accounts CASCADE;
TRUNCATE TABLE negotiation_reminders CASCADE;
TRUNCATE TABLE warranty_reminders CASCADE;
TRUNCATE TABLE settings CASCADE;
TRUNCATE TABLE workflow_definitions CASCADE;
TRUNCATE TABLE workflow_nodes CASCADE;

-- 恢复触发器
SET session_replication_role = DEFAULT;

COMMIT;

-- 验证结果
SELECT 'users' as tbl, COUNT(*) as cnt FROM users
UNION ALL SELECT 'products', COUNT(*) FROM products
UNION ALL SELECT 'product_categories', COUNT(*) FROM product_categories
UNION ALL SELECT 'exchange_rates', COUNT(*) FROM exchange_rates
UNION ALL SELECT 'projects', COUNT(*) FROM projects
UNION ALL SELECT 'customers', COUNT(*) FROM customers
UNION ALL SELECT 'suppliers', COUNT(*) FROM suppliers
UNION ALL SELECT 'advances', COUNT(*) FROM advances
UNION ALL SELECT 'purchase_requests', COUNT(*) FROM purchase_requests
ORDER BY 1;
