SET FOREIGN_KEY_CHECKS = 0;

-- ==============================================
-- 清理流程管理相关表（Flowable流程引擎）
-- ==============================================

-- 流程运行时任务表
TRUNCATE TABLE `ACT_RU_TASK`;
TRUNCATE TABLE `ACT_RU_EXECUTION`;
TRUNCATE TABLE `ACT_RU_VARIABLE`;
TRUNCATE TABLE `ACT_RU_IDENTITYLINK`;
TRUNCATE TABLE `ACT_RU_EVENT_SUBSCR`;
TRUNCATE TABLE `ACT_RU_ENTITYLINK`;
TRUNCATE TABLE `ACT_RU_JOB`;
TRUNCATE TABLE `ACT_RU_TIMER_JOB`;
TRUNCATE TABLE `ACT_RU_SUSPENDED_JOB`;
TRUNCATE TABLE `ACT_RU_DEADLETTER_JOB`;
TRUNCATE TABLE `ACT_RU_EXTERNAL_JOB`;
TRUNCATE TABLE `ACT_RU_HISTORY_JOB`;
TRUNCATE TABLE `ACT_RU_ACTINST`;

-- 流程历史表
TRUNCATE TABLE `ACT_HI_TASKINST`;
TRUNCATE TABLE `ACT_HI_PROCINST`;
TRUNCATE TABLE `ACT_HI_ACTINST`;
TRUNCATE TABLE `ACT_HI_VARINST`;
TRUNCATE TABLE `ACT_HI_DETAIL`;
TRUNCATE TABLE `ACT_HI_COMMENT`;
TRUNCATE TABLE `ACT_HI_ATTACHMENT`;
TRUNCATE TABLE `ACT_HI_IDENTITYLINK`;
TRUNCATE TABLE `ACT_HI_ENTITYLINK`;
TRUNCATE TABLE `ACT_HI_TSK_LOG`;

-- 流程定义表
TRUNCATE TABLE `ACT_RE_PROCDEF`;
TRUNCATE TABLE `ACT_RE_DEPLOYMENT`;
TRUNCATE TABLE `ACT_RE_MODEL`;
TRUNCATE TABLE `ACT_PROCDEF_INFO`;
TRUNCATE TABLE `ACT_GE_BYTEARRAY`;
TRUNCATE TABLE `ACT_EVT_LOG`;

-- 事件注册表
TRUNCATE TABLE `FLW_EVENT_DEFINITION`;
TRUNCATE TABLE `FLW_CHANNEL_DEFINITION`;
TRUNCATE TABLE `FLW_EVENT_DEPLOYMENT`;
TRUNCATE TABLE `FLW_EVENT_RESOURCE`;
TRUNCATE TABLE `FLW_RU_BATCH`;
TRUNCATE TABLE `FLW_RU_BATCH_PART`;

-- ==============================================
-- 清理工作流相关表（自定义工作流）
-- ==============================================

TRUNCATE TABLE `wf_form`;
TRUNCATE TABLE `wf_deploy_form`;
TRUNCATE TABLE `wf_category`;
TRUNCATE TABLE `wf_copy`;

-- ==============================================
-- 清理设备管理相关表
-- ==============================================

TRUNCATE TABLE `dv_repair`;
TRUNCATE TABLE `dv_repair_line`;
TRUNCATE TABLE `dv_check_record`;
TRUNCATE TABLE `dv_check_record_line`;
TRUNCATE TABLE `dv_check_machinery`;
TRUNCATE TABLE `dv_check_plan`;
TRUNCATE TABLE `dv_check_subject`;
TRUNCATE TABLE `dv_mainten_record`;
TRUNCATE TABLE `dv_mainten_record_line`;
TRUNCATE TABLE `dv_machinery`;
TRUNCATE TABLE `dv_subject`;

-- 保留设备类型表（基础数据）
-- TRUNCATE TABLE `dv_machinery_type`;

-- ==============================================
-- 清理WMS业务表（非库存相关）
-- ==============================================

-- 订单相关
TRUNCATE TABLE `wms_order`;
TRUNCATE TABLE `wms_order_detail`;
TRUNCATE TABLE `wms_order_profit`;

-- 采购相关
TRUNCATE TABLE `wms_purchase_plan`;
TRUNCATE TABLE `wms_purchase_plan_detail`;

-- 排产相关
TRUNCATE TABLE `wms_schedule_plan`;
TRUNCATE TABLE `wms_schedule_plan_detail`;

-- 生产相关
TRUNCATE TABLE `wms_production_line`;
TRUNCATE TABLE `wms_process_task`;
TRUNCATE TABLE `wms_processe`;
TRUNCATE TABLE `wms_product_processe`;
TRUNCATE TABLE `wms_product_bom`;
TRUNCATE TABLE `wms_bom`;
TRUNCATE TABLE `wms_bom_item`;
TRUNCATE TABLE `wms_production_task`;

-- 财务相关
TRUNCATE TABLE `wms_account`;
TRUNCATE TABLE `wms_financial_document`;
TRUNCATE TABLE `wms_journal_entry`;
TRUNCATE TABLE `wms_journal`;
TRUNCATE TABLE `wms_payable`;
TRUNCATE TABLE `wms_receivable`;

-- 合同与供应商
TRUNCATE TABLE `wms_contract`;
TRUNCATE TABLE `wms_supplier`;

-- 客户管理
TRUNCATE TABLE `wms_customer`;

-- 质量检验
TRUNCATE TABLE `wms_check_item`;
TRUNCATE TABLE `wms_check_task`;
TRUNCATE TABLE `wms_check_task_item`;
TRUNCATE TABLE `wms_common_defect`;

-- 其他
TRUNCATE TABLE `wms_camera_management`;
TRUNCATE TABLE `wms_express`;
TRUNCATE TABLE `wms_express_question`;
TRUNCATE TABLE `wms_product_sales_script`;
TRUNCATE TABLE `wms_batch`;
TRUNCATE TABLE `wms_report_detail`;
TRUNCATE TABLE `wms_report_summary`;
TRUNCATE TABLE `wms_product_spec`;
TRUNCATE TABLE `wms_product_spec_group`;

-- ==============================================
-- 清理质量检验相关表
-- ==============================================

TRUNCATE TABLE `is_inspection_commission`;
TRUNCATE TABLE `is_inspection_task`;
TRUNCATE TABLE `is_sample_inventory`;

-- ==============================================
-- 清理代码生成相关表
-- ==============================================

TRUNCATE TABLE `gen_table`;
TRUNCATE TABLE `gen_table_column`;

-- ==============================================
-- 清理系统日志表
-- ==============================================

TRUNCATE TABLE `sys_oper_log`;
TRUNCATE TABLE `sys_logininfor`;
TRUNCATE TABLE `sys_oss`;

-- ==============================================
-- 重置自增ID（可选）
-- ==============================================

ALTER TABLE `dv_machinery_type` AUTO_INCREMENT = 1;
ALTER TABLE `wms_category` AUTO_INCREMENT = 1;
ALTER TABLE `wms_raw_material` AUTO_INCREMENT = 1;
ALTER TABLE `wms_product` AUTO_INCREMENT = 1;
ALTER TABLE `wms_warehouse` AUTO_INCREMENT = 1;
ALTER TABLE `wms_stock` AUTO_INCREMENT = 1;
ALTER TABLE `wms_stock_io` AUTO_INCREMENT = 1;
ALTER TABLE `wms_stock_io_detail` AUTO_INCREMENT = 1;
ALTER TABLE `wms_stock_log` AUTO_INCREMENT = 1;

SET FOREIGN_KEY_CHECKS = 1;

SELECT '数据清理完成！' AS result;