seed_processes.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/usr/bin/env python3
  2. """批量初始化工艺库测试数据(biz_process),已存在工序代码则跳过。
  3. 用法(在 backend 目录):
  4. source venv/bin/activate
  5. python scripts/seed_processes.py
  6. python scripts/seed_processes.py --tenant-no zkxw
  7. 依赖:biz_process_category 中已有对应分类编码(可先运行 reset_process_categories.py)。
  8. """
  9. from __future__ import annotations
  10. import argparse
  11. import sys
  12. from pathlib import Path
  13. from typing import Dict, Optional
  14. ROOT = Path(__file__).resolve().parents[1]
  15. if str(ROOT) not in sys.path:
  16. sys.path.insert(0, str(ROOT))
  17. from app.core.database import SessionLocal
  18. from app.crud import process as process_svc
  19. from app.models.models import BizProcessCategory
  20. ADMIN_ID = "d3000000000000000000000000000001"
  21. # (process_code, process_name, category_code, description, equipment_name, estimated_duration_h, order_num, remark)
  22. SAMPLE_PROCESSES = [
  23. (
  24. "PR-YBJS-001",
  25. "样本接收登记",
  26. "PC-YBJS",
  27. "核对样本信息、登记条码并分配接收批次",
  28. "样本接收台",
  29. "0.5",
  30. 1,
  31. "样本接收首道工序",
  32. ),
  33. (
  34. "PR-YBJS-002",
  35. "样本初检",
  36. "PC-YBJS",
  37. "外观检查、温度记录及初筛不合格样本隔离",
  38. "生物安全柜",
  39. "0.75",
  40. 2,
  41. None,
  42. ),
  43. (
  44. "PR-XBYP-001",
  45. "外周血分离",
  46. "PC-XBYP",
  47. "密度梯度离心分离单个核细胞",
  48. "台式离心机",
  49. "1",
  50. 3,
  51. None,
  52. ),
  53. (
  54. "PR-XBYP-002",
  55. "细胞培养扩增",
  56. "PC-XBYP",
  57. "接种至培养体系并进行扩增培养",
  58. "CO2培养箱",
  59. "8",
  60. 4,
  61. "长周期工序",
  62. ),
  63. (
  64. "PR-XBYP-003",
  65. "换液传代",
  66. "PC-XBYP",
  67. "更换培养基并按密度传代",
  68. "生物安全柜",
  69. "1.5",
  70. 5,
  71. None,
  72. ),
  73. (
  74. "PR-ZJJC-001",
  75. "细胞活性检测",
  76. "PC-ZJJC",
  77. "台盼蓝染色法检测细胞活率",
  78. "倒置显微镜",
  79. "0.5",
  80. 6,
  81. None,
  82. ),
  83. (
  84. "PR-ZJJC-002",
  85. "支原体检测",
  86. "PC-ZJJC",
  87. "PCR法检测支原体污染",
  88. "PCR仪",
  89. "3",
  90. 7,
  91. None,
  92. ),
  93. (
  94. "PR-DCGL-001",
  95. "程序降温冻存",
  96. "PC-DCGL",
  97. "按程序降温曲线完成细胞冻存",
  98. "程序降温仪",
  99. "2.5",
  100. 8,
  101. None,
  102. ),
  103. (
  104. "PR-DCGL-002",
  105. "冻存管入库",
  106. "PC-DCGL",
  107. "扫码登记并转入液氮罐指定位置",
  108. "扫码枪",
  109. "0.33",
  110. 9,
  111. None,
  112. ),
  113. (
  114. "PR-BZZF-001",
  115. "成品包装发运",
  116. "PC-BZZF",
  117. "成品核对、冷链包装及发运交接",
  118. "包装台",
  119. "0.75",
  120. 10,
  121. "末道工序",
  122. ),
  123. ]
  124. def _load_category_map(db, tenant_no: str) -> Dict[str, str]:
  125. rows = (
  126. db.query(BizProcessCategory)
  127. .filter(BizProcessCategory.tenant_no == tenant_no, BizProcessCategory.del_flag == 0)
  128. .all()
  129. )
  130. return {row.category_code: row.id for row in rows}
  131. def seed(tenant_no: str = "zkxw", user_id: str = ADMIN_ID) -> None:
  132. db = SessionLocal()
  133. created = 0
  134. skipped = 0
  135. missing_categories: set[str] = set()
  136. try:
  137. category_map = _load_category_map(db, tenant_no)
  138. for (
  139. code,
  140. name,
  141. category_code,
  142. description,
  143. equipment_name,
  144. estimated_duration,
  145. order_num,
  146. remark,
  147. ) in SAMPLE_PROCESSES:
  148. if process_svc.process_code_exists(db, code, tenant_no):
  149. skipped += 1
  150. continue
  151. category_id: Optional[str] = category_map.get(category_code)
  152. if category_code and not category_id:
  153. missing_categories.add(category_code)
  154. process_svc.create_process(
  155. db,
  156. process_code=code,
  157. process_name=name,
  158. category_id=category_id,
  159. description=description,
  160. equipment_ids=None,
  161. estimated_duration=estimated_duration,
  162. tenant_no=tenant_no,
  163. order_num=order_num,
  164. remark=remark,
  165. user_id=user_id,
  166. )
  167. created += 1
  168. print(
  169. f"工艺库测试数据初始化完成(租户 {tenant_no}):"
  170. f"新增 {created} 条,跳过 {skipped} 条(工序代码已存在)。"
  171. )
  172. if missing_categories:
  173. print(
  174. "以下工序分类编码未找到,对应工艺已写入但 category_id 为空:"
  175. f" {', '.join(sorted(missing_categories))}"
  176. )
  177. print("可先执行:python scripts/reset_process_categories.py")
  178. finally:
  179. db.close()
  180. def main() -> None:
  181. parser = argparse.ArgumentParser(description="初始化 biz_process 测试数据")
  182. parser.add_argument("--tenant-no", default="zkxw", help="租户编号,默认 zkxw")
  183. parser.add_argument("--user-id", default=ADMIN_ID, help="create_by / update_by 用户 id")
  184. args = parser.parse_args()
  185. seed(tenant_no=args.tenant_no, user_id=args.user_id)
  186. if __name__ == "__main__":
  187. main()