merge_work_order_tables.sql 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. -- ============================================================
  2. -- 工单表合并迁移:biz_temp_* → 主表 + work_order_type
  3. -- Date: 2026-05-24
  4. -- 执行顺序:先本脚本,再 work_order_unification_phase1.sql
  5. -- 适用:仍保留 biz_temp_qc_work_order / biz_temp_production_work_order 的旧库
  6. -- ============================================================
  7. -- 1. biz_qc_work_order: 加 work_order_type(若已存在则跳过,需 MySQL 8+ 或手动检查)
  8. -- ALTER TABLE biz_qc_work_order
  9. -- ADD COLUMN work_order_type VARCHAR(20) NOT NULL DEFAULT 'inbound'
  10. -- COMMENT '工单类型: inbound=入库, temporary=临时' AFTER work_order_no;
  11. -- 2. reception 关联字段改为可空(临时工单不需要)
  12. -- ALTER TABLE biz_qc_work_order
  13. -- MODIFY COLUMN reception_id VARCHAR(64) NULL,
  14. -- MODIFY COLUMN protocol_sample_id VARCHAR(64) NULL,
  15. -- MODIFY COLUMN collection_protocol_id VARCHAR(64) NULL,
  16. -- MODIFY COLUMN reception_source VARCHAR(20) NULL DEFAULT 'cell_storage';
  17. -- 3. 迁移临时质检工单(按 id 去重,避免重复执行)
  18. INSERT INTO biz_qc_work_order (
  19. id, work_order_no, work_order_type, work_order_status,
  20. has_lab, has_third_party, third_party_fee_total,
  21. configured_time, configured_by,
  22. tenant_no, order_num, remark,
  23. create_by, create_dept_id, create_time, update_by, update_time, del_flag
  24. )
  25. SELECT
  26. t.id, t.work_order_no, 'temporary', t.work_order_status,
  27. t.has_lab, t.has_third_party, t.third_party_fee_total,
  28. t.configured_time, t.configured_by,
  29. t.tenant_no, t.order_num, t.remark,
  30. t.create_by, t.create_dept_id, t.create_time, t.update_by, t.update_time, t.del_flag
  31. FROM biz_temp_qc_work_order t
  32. WHERE NOT EXISTS (SELECT 1 FROM biz_qc_work_order m WHERE m.id = t.id);
  33. -- 4. biz_production_work_order: 加 work_order_type / configured_time / configured_by(若未加请手动 ALTER)
  34. -- ALTER TABLE biz_production_work_order ADD COLUMN work_order_type ...
  35. -- ALTER TABLE biz_production_work_order ADD COLUMN configured_time DATETIME NULL ...
  36. -- ALTER TABLE biz_production_work_order ADD COLUMN configured_by VARCHAR(64) NULL ...
  37. -- 5. 迁移临时生产工单
  38. INSERT INTO biz_production_work_order (
  39. id, work_order_no, work_order_type, work_order_status,
  40. configured_time, configured_by,
  41. tenant_no, order_num, remark,
  42. create_by, create_dept_id, create_time, update_by, update_time, del_flag
  43. )
  44. SELECT
  45. t.id, t.work_order_no, 'temporary', t.work_order_status,
  46. t.configured_time, t.configured_by,
  47. t.tenant_no, t.order_num, t.remark,
  48. t.create_by, t.create_dept_id, t.create_time, t.update_by, t.update_time, t.del_flag
  49. FROM biz_temp_production_work_order t
  50. WHERE NOT EXISTS (SELECT 1 FROM biz_production_work_order m WHERE m.id = t.id);
  51. -- 6. 删除临时表(确认迁移成功后执行)
  52. DROP TABLE IF EXISTS biz_temp_qc_work_order;
  53. DROP TABLE IF EXISTS biz_temp_production_work_order;