-- ============================================================
-- 系统业务数据清理存储过程
--
-- 用途：清空所有业务单据数据，保留系统配置和基础数据
-- 注意：此操作不可逆，执行前请确认已备份数据库
--
-- 使用方式：CALL sp_clean_business_data();
-- ============================================================

DELIMITER $$

DROP PROCEDURE IF EXISTS sp_clean_business_data$$

CREATE PROCEDURE sp_clean_business_data()
BEGIN
    -- 关闭外键检查，避免因表间引用导致删除失败
    SET FOREIGN_KEY_CHECKS = 0;

    -- ============================================================
    -- 1. 系统日志与Token（可安全清理）
    -- ============================================================
    TRUNCATE TABLE system_login_log;
    TRUNCATE TABLE system_operate_log;
    TRUNCATE TABLE system_notify_message;
    TRUNCATE TABLE system_oauth2_access_token;
    TRUNCATE TABLE system_oauth2_refresh_token;
    TRUNCATE TABLE system_oauth2_code;
    TRUNCATE TABLE system_oauth2_approve;
    TRUNCATE TABLE infra_api_access_log;
    TRUNCATE TABLE infra_api_error_log;
    TRUNCATE TABLE infra_job_log;

    -- ============================================================
    -- 2. 文件与附件（业务文件，清理后不影响系统运行）
    -- ============================================================
    TRUNCATE TABLE system_storage_attachment;
    TRUNCATE TABLE system_storage_blob;
    TRUNCATE TABLE storage_attachment;
    TRUNCATE TABLE storage_blob;
    TRUNCATE TABLE infra_file;
    TRUNCATE TABLE infra_file_content;

    -- ============================================================
    -- 3. MES 生产模块 — 业务单据
    -- ============================================================
    TRUNCATE TABLE mes_pro_work_order_param_record_detail;
    TRUNCATE TABLE mes_pro_work_order_param_record;
    TRUNCATE TABLE mes_pro_work_order_process;
    TRUNCATE TABLE mes_pro_work_order_bom;
    TRUNCATE TABLE mes_pro_work_order;
    TRUNCATE TABLE mes_pro_task_issue;
    TRUNCATE TABLE mes_pro_task;
    TRUNCATE TABLE mes_pro_feedback;
    TRUNCATE TABLE mes_pro_mps_merge_item;
    TRUNCATE TABLE mes_pro_mps;
    TRUNCATE TABLE mes_pro_card_process;
    TRUNCATE TABLE mes_pro_card;
    TRUNCATE TABLE mes_pro_work_record_log;
    TRUNCATE TABLE mes_pro_work_record;
    TRUNCATE TABLE mes_pro_andon_record;

    -- ============================================================
    -- 4. MES 仓库模块 — 业务单据
    -- ============================================================
    -- 库存交易流水（底层，先清）
    TRUNCATE TABLE mes_wm_transaction;
    TRUNCATE TABLE mes_wm_stock_reserve;
    TRUNCATE TABLE mes_wm_material_stock;

    -- 序列号 / 批次 / 条码
    TRUNCATE TABLE mes_wm_sn;
    TRUNCATE TABLE mes_wm_batch;
    TRUNCATE TABLE mes_wm_barcode;

    -- 盘点
    TRUNCATE TABLE mes_wm_stock_taking_task_result;
    TRUNCATE TABLE mes_wm_stock_taking_task_line;
    TRUNCATE TABLE mes_wm_stock_taking_task;
    TRUNCATE TABLE mes_wm_stock_taking_plan_param;
    TRUNCATE TABLE mes_wm_stock_taking_plan;

    -- 包装
    TRUNCATE TABLE mes_wm_package_line;
    TRUNCATE TABLE mes_wm_package;

    -- 出入库明细/行（先清 detail/line，再清主表）
    TRUNCATE TABLE mes_wm_item_receipt_detail;
    TRUNCATE TABLE mes_wm_item_receipt_line;
    TRUNCATE TABLE mes_wm_item_receipt;
    TRUNCATE TABLE mes_wm_item_consume_detail;
    TRUNCATE TABLE mes_wm_item_consume_line;
    TRUNCATE TABLE mes_wm_item_consume;
    TRUNCATE TABLE mes_wm_product_produce_detail;
    TRUNCATE TABLE mes_wm_product_produce_line;
    TRUNCATE TABLE mes_wm_product_produce;
    TRUNCATE TABLE mes_wm_product_issue_detail;
    TRUNCATE TABLE mes_wm_product_issue_line;
    TRUNCATE TABLE mes_wm_product_issue;
    TRUNCATE TABLE mes_wm_product_receipt_detail;
    TRUNCATE TABLE mes_wm_product_receipt_line;
    TRUNCATE TABLE mes_wm_product_receipt;
    TRUNCATE TABLE mes_wm_product_sales_detail;
    TRUNCATE TABLE mes_wm_product_sales_line;
    TRUNCATE TABLE mes_wm_product_sales;
    TRUNCATE TABLE mes_wm_misc_receipt_detail;
    TRUNCATE TABLE mes_wm_misc_receipt_line;
    TRUNCATE TABLE mes_wm_misc_receipt;
    TRUNCATE TABLE mes_wm_misc_issue_detail;
    TRUNCATE TABLE mes_wm_misc_issue_line;
    TRUNCATE TABLE mes_wm_misc_issue;
    TRUNCATE TABLE mes_wm_outsource_receipt_detail;
    TRUNCATE TABLE mes_wm_outsource_receipt_line;
    TRUNCATE TABLE mes_wm_outsource_receipt;
    TRUNCATE TABLE mes_wm_outsource_issue_detail;
    TRUNCATE TABLE mes_wm_outsource_issue_line;
    TRUNCATE TABLE mes_wm_outsource_issue;
    TRUNCATE TABLE mes_wm_return_vendor_detail;
    TRUNCATE TABLE mes_wm_return_vendor_line;
    TRUNCATE TABLE mes_wm_return_vendor;
    TRUNCATE TABLE mes_wm_return_issue_detail;
    TRUNCATE TABLE mes_wm_return_issue_line;
    TRUNCATE TABLE mes_wm_return_issue;
    TRUNCATE TABLE mes_wm_return_sales_detail;
    TRUNCATE TABLE mes_wm_return_sales_line;
    TRUNCATE TABLE mes_wm_return_sales;
    TRUNCATE TABLE mes_wm_sales_notice_line;
    TRUNCATE TABLE mes_wm_sales_notice;
    TRUNCATE TABLE mes_wm_transfer_detail;
    TRUNCATE TABLE mes_wm_transfer_line;
    TRUNCATE TABLE mes_wm_transfer;
    TRUNCATE TABLE mes_wm_arrival_notice_line;
    TRUNCATE TABLE mes_wm_arrival_notice;

    -- ============================================================
    -- 5. MES 质检模块 — 业务数据
    -- ============================================================
    TRUNCATE TABLE mes_qc_indicator_result_detail;
    TRUNCATE TABLE mes_qc_indicator_result;
    TRUNCATE TABLE mes_qc_defect_record;
    TRUNCATE TABLE mes_qc_ncr;
    TRUNCATE TABLE mes_qc_iqc_line;
    TRUNCATE TABLE mes_qc_iqc;
    TRUNCATE TABLE mes_qc_ipqc_line;
    TRUNCATE TABLE mes_qc_ipqc;
    TRUNCATE TABLE mes_qc_oqc_line;
    TRUNCATE TABLE mes_qc_oqc;
    TRUNCATE TABLE mes_qc_rqc_line;
    TRUNCATE TABLE mes_qc_rqc;

    -- ============================================================
    -- 6. MES 设备模块 — 业务单据
    -- ============================================================
    TRUNCATE TABLE mes_dv_check_record_line;
    TRUNCATE TABLE mes_dv_check_record;
    TRUNCATE TABLE mes_dv_mainten_record_line;
    TRUNCATE TABLE mes_dv_mainten_record;
    TRUNCATE TABLE mes_dv_repair_line;
    TRUNCATE TABLE mes_dv_repair;

    -- ============================================================
    -- 7. MES 工艺/设计模块 — 业务单据
    -- ============================================================
    TRUNCATE TABLE mes_pd_archive;
    TRUNCATE TABLE mes_pd_document_audit;
    TRUNCATE TABLE mes_pd_document;
    TRUNCATE TABLE mes_pd_project;

    -- 编码生成记录（规则本身保留）
    TRUNCATE TABLE mes_md_auto_code_record;

    -- ============================================================
    -- 8. CRM 模块 — 业务单据
    -- ============================================================
    TRUNCATE TABLE after_sale_return_item;
    TRUNCATE TABLE after_sale_return;
    TRUNCATE TABLE after_sale_ticket_item;
    TRUNCATE TABLE after_sale_ticket;
    TRUNCATE TABLE after_sale_repair;
    TRUNCATE TABLE after_sale_permission;
    TRUNCATE TABLE crm_receivable_plan;
    TRUNCATE TABLE crm_receivable;
    TRUNCATE TABLE crm_contract_product;
    TRUNCATE TABLE crm_contract;
    TRUNCATE TABLE crm_sale_quotation_product;
    TRUNCATE TABLE crm_sale_quotation;
    TRUNCATE TABLE crm_business_product;
    TRUNCATE TABLE crm_contact_business;
    TRUNCATE TABLE crm_business;
    TRUNCATE TABLE crm_clue;
    TRUNCATE TABLE crm_contact;
    TRUNCATE TABLE crm_follow_up_record;
    TRUNCATE TABLE crm_customer;
    TRUNCATE TABLE crm_permission;

    -- ============================================================
    -- 9. ERP 模块 — 业务单据
    -- ============================================================
    TRUNCATE TABLE erp_finance_payment_item;
    TRUNCATE TABLE erp_finance_payment;
    TRUNCATE TABLE erp_finance_receipt_item;
    TRUNCATE TABLE erp_finance_receipt;
    TRUNCATE TABLE erp_purchase_order_items;
    TRUNCATE TABLE erp_purchase_order;
    TRUNCATE TABLE erp_purchase_request_items;
    TRUNCATE TABLE erp_purchase_request;
    TRUNCATE TABLE erp_sale_order_items;
    TRUNCATE TABLE erp_sale_order;

    -- ============================================================
    -- 10. HRM 模块 — 业务单据
    -- ============================================================
    TRUNCATE TABLE hrm_salary_payment_detail;
    TRUNCATE TABLE hrm_salary_payment;
    TRUNCATE TABLE hrm_salary_calculation;
    TRUNCATE TABLE hrm_attendance_summary;
    TRUNCATE TABLE hrm_attendance_record;
    TRUNCATE TABLE hrm_attendance_exception;
    TRUNCATE TABLE hrm_leave_application;
    TRUNCATE TABLE hrm_resignation_application;
    TRUNCATE TABLE hrm_transfer_application;
    TRUNCATE TABLE hrm_employee_work_history;
    TRUNCATE TABLE hrm_employee_education;
    TRUNCATE TABLE hrm_employee_emergency_contact;
    TRUNCATE TABLE hrm_employee_social_security;
    TRUNCATE TABLE hrm_employee_salary;
    TRUNCATE TABLE hrm_employee;

    -- ============================================================
    -- 11. SRM 模块 — 业务单据
    -- ============================================================
    TRUNCATE TABLE srm_bid_award;
    TRUNCATE TABLE srm_bid_evaluation;
    TRUNCATE TABLE srm_bid_open;
    TRUNCATE TABLE srm_tender_bid;
    TRUNCATE TABLE srm_tender_material;
    TRUNCATE TABLE srm_tender_project;
    TRUNCATE TABLE srm_delivery_plan;
    TRUNCATE TABLE srm_purchase_collaboration;
    TRUNCATE TABLE srm_supplier_quote;
    TRUNCATE TABLE srm_supplier_evaluation;
    TRUNCATE TABLE srm_supplier_certificate;
    TRUNCATE TABLE srm_supplier_contact;
    TRUNCATE TABLE srm_supplier_apply;

    -- ============================================================
    -- 12. BPM 模块 — 业务单据
    -- ============================================================
    TRUNCATE TABLE bpm_oa_leave;
    TRUNCATE TABLE bpm_process_instance_copy;

    -- ============================================================
    -- 13. AI 模块 — 会话/内容数据
    -- ============================================================
    TRUNCATE TABLE ai_chat_message;
    TRUNCATE TABLE ai_chat_conversation;
    TRUNCATE TABLE ai_knowledge_segment;
    TRUNCATE TABLE ai_knowledge_document;
    TRUNCATE TABLE ai_knowledge;
    TRUNCATE TABLE ai_image;
    TRUNCATE TABLE ai_mind_map;
    TRUNCATE TABLE ai_music;
    TRUNCATE TABLE ai_write;
    TRUNCATE TABLE ai_workflow;

    -- ============================================================
    -- 14. IM 模块 — 会话/消息数据
    -- ============================================================
    TRUNCATE TABLE im_group_message_receipt;
    TRUNCATE TABLE im_group_message;
    TRUNCATE TABLE im_private_message;
    TRUNCATE TABLE im_message_file;
    TRUNCATE TABLE im_group_request;
    TRUNCATE TABLE im_group_member;
    TRUNCATE TABLE im_group;
    TRUNCATE TABLE im_friend_request;
    TRUNCATE TABLE im_friend;
    TRUNCATE TABLE im_conversation_read;
    TRUNCATE TABLE im_conversation;
    TRUNCATE TABLE im_channel_message;
    TRUNCATE TABLE im_channel_material;
    TRUNCATE TABLE im_channel;
    TRUNCATE TABLE im_rtc_participant;
    TRUNCATE TABLE im_rtc_call;
    TRUNCATE TABLE im_user_online;

    -- ============================================================
    -- 15. OA 通知公告 — 业务数据
    -- ============================================================
    TRUNCATE TABLE oa_notice_read;
    TRUNCATE TABLE oa_notice_user;
    TRUNCATE TABLE oa_notice;
    TRUNCATE TABLE system_notice;

    -- ============================================================
    -- 恢复外键检查
    -- ============================================================
    SET FOREIGN_KEY_CHECKS = 1;

    -- 返回清理结果
    SELECT '业务数据清理完成' AS result;
END$$

DELIMITER ;

-- ============================================================
-- 执行示例：
--   CALL sp_clean_business_data();
-- ============================================================
