2026-08-07 d65c665b483f9c7b57358d4ad2c4bf3c6e1f9608
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
-- ============================================================
-- 系统业务数据清理存储过程
--
-- 用途:清空所有业务单据数据,保留系统配置和基础数据
-- 注意:此操作不可逆,执行前请确认已备份数据库
--
-- 使用方式: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();
-- ============================================================