2026-10-22-fix-builtin-route-missing-steps.sql 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. -- 修复内置工艺路线缺步骤(2026-10-22)
  2. -- 对齐 docs/superpowers/specs/2026-10-22-cc-builtin-route-steps-fix-design.md
  3. --
  4. -- 现象:CC样本处理-BUILTIN 等内置路线仅有路线头、无 biz_process_route_step,
  5. -- 导致 B* 入库编码自动派发后任务显示「样本处理 0/0」。
  6. -- 策略:对「内置 + 无有效步骤 + 源路线有步骤」的 *-BUILTIN 路线,
  7. -- 从源路线(route_code 去掉 -BUILTIN)深拷贝步骤及产出/数据采集等子表。
  8. -- 幂等:仅当目标内置路线当前 0 条有效步骤时执行;已有步骤则跳过。
  9. -- MySQL 5.7
  10. -- 1) 需要修复的内置路线 ↔ 源路线
  11. DROP TEMPORARY TABLE IF EXISTS tmp_builtin_route_fix;
  12. CREATE TEMPORARY TABLE tmp_builtin_route_fix (
  13. dst_route_id VARCHAR(64) NOT NULL,
  14. src_route_id VARCHAR(64) NOT NULL,
  15. tenant_no VARCHAR(64) NOT NULL,
  16. PRIMARY KEY (dst_route_id)
  17. ) ENGINE=MEMORY;
  18. INSERT INTO tmp_builtin_route_fix (dst_route_id, src_route_id, tenant_no)
  19. SELECT
  20. b.id,
  21. (
  22. SELECT s.id
  23. FROM biz_process_route s
  24. WHERE s.tenant_no = b.tenant_no
  25. AND s.route_code = REPLACE(b.route_code, '-BUILTIN', '')
  26. AND s.del_flag = 0
  27. AND IFNULL(s.is_builtin, '0') = '0'
  28. ORDER BY
  29. CASE WHEN IFNULL(s.is_current, '0') = '1' THEN 0 ELSE 1 END,
  30. s.update_time DESC,
  31. s.create_time DESC
  32. LIMIT 1
  33. ) AS src_route_id,
  34. b.tenant_no
  35. FROM biz_process_route b
  36. WHERE IFNULL(b.is_builtin, '0') = '1'
  37. AND b.del_flag = 0
  38. AND b.route_code LIKE '%-BUILTIN'
  39. AND NOT EXISTS (
  40. SELECT 1
  41. FROM biz_process_route_step st
  42. WHERE st.route_id = b.id
  43. AND st.del_flag = 0
  44. );
  45. -- 去掉找不到源、或源也无步骤的行
  46. DELETE FROM tmp_builtin_route_fix
  47. WHERE src_route_id IS NULL
  48. OR NOT EXISTS (
  49. SELECT 1
  50. FROM biz_process_route_step st
  51. WHERE st.route_id = tmp_builtin_route_fix.src_route_id
  52. AND st.del_flag = 0
  53. );
  54. -- 2) 源步骤 → 新步骤 ID 映射
  55. DROP TEMPORARY TABLE IF EXISTS tmp_step_id_map;
  56. CREATE TEMPORARY TABLE tmp_step_id_map (
  57. src_step_id VARCHAR(64) NOT NULL,
  58. dst_step_id VARCHAR(64) NOT NULL,
  59. dst_route_id VARCHAR(64) NOT NULL,
  60. tenant_no VARCHAR(64) NOT NULL,
  61. PRIMARY KEY (src_step_id)
  62. ) ENGINE=MEMORY;
  63. INSERT INTO tmp_step_id_map (src_step_id, dst_step_id, dst_route_id, tenant_no)
  64. SELECT
  65. ss.id,
  66. -- MD5+源步骤保证同语句多行不撞主键;前缀避免与历史 UUID 冲突
  67. CONCAT('BF', UPPER(MD5(CONCAT('fix-builtin-step:', f.dst_route_id, ':', ss.id)))),
  68. f.dst_route_id,
  69. f.tenant_no
  70. FROM tmp_builtin_route_fix f
  71. INNER JOIN biz_process_route_step ss
  72. ON ss.route_id = f.src_route_id
  73. AND ss.del_flag = 0;
  74. -- 3) 拷贝步骤
  75. INSERT INTO biz_process_route_step (
  76. id, route_id, process_id, step_order, process_code, category_id,
  77. process_name, description, equipment_ids, equipment_name, estimated_duration,
  78. sop_ids, sop_content, tenant_no, order_num, remark,
  79. create_by, create_dept_id, create_time, update_by, update_time, del_flag
  80. )
  81. SELECT
  82. m.dst_step_id,
  83. m.dst_route_id,
  84. ss.process_id,
  85. ss.step_order,
  86. ss.process_code,
  87. ss.category_id,
  88. ss.process_name,
  89. ss.description,
  90. ss.equipment_ids,
  91. ss.equipment_name,
  92. ss.estimated_duration,
  93. ss.sop_ids,
  94. ss.sop_content,
  95. ss.tenant_no,
  96. ss.order_num,
  97. ss.remark,
  98. ss.create_by,
  99. ss.create_dept_id,
  100. NOW(),
  101. ss.update_by,
  102. NOW(),
  103. 0
  104. FROM tmp_step_id_map m
  105. INNER JOIN biz_process_route_step ss ON ss.id = m.src_step_id;
  106. -- 4) 试剂
  107. INSERT INTO biz_process_route_step_reagent (
  108. id, step_id, reagent_code, reagent_name, spec, standard_usage, unit,
  109. is_key_material, tenant_no, order_num, remark,
  110. create_by, create_dept_id, create_time, update_by, update_time, del_flag
  111. )
  112. SELECT
  113. CONCAT('BF', UPPER(MD5(CONCAT('fix-builtin-reag:', m.dst_step_id, ':', r.id)))),
  114. m.dst_step_id,
  115. r.reagent_code,
  116. r.reagent_name,
  117. r.spec,
  118. r.standard_usage,
  119. r.unit,
  120. r.is_key_material,
  121. r.tenant_no,
  122. r.order_num,
  123. r.remark,
  124. r.create_by,
  125. r.create_dept_id,
  126. NOW(),
  127. r.update_by,
  128. NOW(),
  129. 0
  130. FROM tmp_step_id_map m
  131. INNER JOIN biz_process_route_step_reagent r
  132. ON r.step_id = m.src_step_id
  133. AND r.del_flag = 0;
  134. -- 5) 耗材
  135. INSERT INTO biz_process_route_step_consumable (
  136. id, step_id, consumable_code, consumable_name, spec_model, standard_usage,
  137. unit, is_required, remark, tenant_no, order_num,
  138. create_by, create_dept_id, create_time, update_by, update_time, del_flag
  139. )
  140. SELECT
  141. CONCAT('BF', UPPER(MD5(CONCAT('fix-builtin-cons:', m.dst_step_id, ':', c.id)))),
  142. m.dst_step_id,
  143. c.consumable_code,
  144. c.consumable_name,
  145. c.spec_model,
  146. c.standard_usage,
  147. c.unit,
  148. c.is_required,
  149. c.remark,
  150. c.tenant_no,
  151. c.order_num,
  152. c.create_by,
  153. c.create_dept_id,
  154. NOW(),
  155. c.update_by,
  156. NOW(),
  157. 0
  158. FROM tmp_step_id_map m
  159. INNER JOIN biz_process_route_step_consumable c
  160. ON c.step_id = m.src_step_id
  161. AND c.del_flag = 0;
  162. -- 6) 产出
  163. INSERT INTO biz_process_route_step_output (
  164. id, step_id, output_type_id, output_name, output_barcode, tenant_no,
  165. order_num, remark, create_by, create_dept_id, create_time, update_by,
  166. update_time, del_flag
  167. )
  168. SELECT
  169. CONCAT('BF', UPPER(MD5(CONCAT('fix-builtin-out:', m.dst_step_id, ':', o.id)))),
  170. m.dst_step_id,
  171. o.output_type_id,
  172. o.output_name,
  173. o.output_barcode,
  174. o.tenant_no,
  175. o.order_num,
  176. o.remark,
  177. o.create_by,
  178. o.create_dept_id,
  179. NOW(),
  180. o.update_by,
  181. NOW(),
  182. 0
  183. FROM tmp_step_id_map m
  184. INNER JOIN biz_process_route_step_output o
  185. ON o.step_id = m.src_step_id
  186. AND o.del_flag = 0;
  187. -- 7) 数据采集
  188. INSERT INTO biz_process_route_step_data_collection (
  189. id, step_id, collection_name, unit, tenant_no, order_num, remark,
  190. create_by, create_dept_id, create_time, update_by, update_time, del_flag
  191. )
  192. SELECT
  193. CONCAT('BF', UPPER(MD5(CONCAT('fix-builtin-dc:', m.dst_step_id, ':', d.id)))),
  194. m.dst_step_id,
  195. d.collection_name,
  196. d.unit,
  197. d.tenant_no,
  198. d.order_num,
  199. d.remark,
  200. d.create_by,
  201. d.create_dept_id,
  202. NOW(),
  203. d.update_by,
  204. NOW(),
  205. 0
  206. FROM tmp_step_id_map m
  207. INNER JOIN biz_process_route_step_data_collection d
  208. ON d.step_id = m.src_step_id
  209. AND d.del_flag = 0;
  210. -- 8) 校验(人工看结果)
  211. SELECT
  212. b.route_code,
  213. b.route_name,
  214. (SELECT COUNT(*) FROM biz_process_route_step st
  215. WHERE st.route_id = b.id AND st.del_flag = 0) AS step_count
  216. FROM biz_process_route b
  217. WHERE IFNULL(b.is_builtin, '0') = '1'
  218. AND b.del_flag = 0
  219. AND b.route_code LIKE '%-BUILTIN'
  220. ORDER BY b.route_code;
  221. DROP TEMPORARY TABLE IF EXISTS tmp_step_id_map;
  222. DROP TEMPORARY TABLE IF EXISTS tmp_builtin_route_fix;