seed_qc_materials.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/usr/bin/env python3
  2. """质检物料库测试数据(biz_qc_material + 入库批次 + 变动记录),已存在编码/入库编号则跳过。"""
  3. from __future__ import annotations
  4. import sys
  5. from decimal import Decimal
  6. from pathlib import Path
  7. ROOT = Path(__file__).resolve().parents[1]
  8. if str(ROOT) not in sys.path:
  9. sys.path.insert(0, str(ROOT))
  10. from app.core.database import SessionLocal
  11. from app.crud import qc_material as material_svc
  12. from app.crud import qc_material_stock_batch as batch_svc
  13. # (material_code, material_name, spec_model, unit, warning_qty, order_num, remark,
  14. # stock_in_no, stock_in_date, expire_date, catalog_no, batch_no, manufacturer,
  15. # unit_price, qty)
  16. QC_MATERIAL_SEEDS = [
  17. (
  18. "QC-MAT-001",
  19. "DMEM培养基",
  20. "500ml/瓶",
  21. "瓶",
  22. Decimal("5"),
  23. 1,
  24. "细胞培养基础培养基",
  25. "RK-20260601-001",
  26. "2026-06-01",
  27. "2027-06-01",
  28. "CAT-DMEM-500",
  29. "BN20260501",
  30. "Gibco",
  31. Decimal("128.00"),
  32. Decimal("20"),
  33. ),
  34. (
  35. "QC-MAT-002",
  36. "胎牛血清",
  37. "500ml/瓶",
  38. "瓶",
  39. Decimal("3"),
  40. 2,
  41. "细胞培养添加物",
  42. "RK-20260601-002",
  43. "2026-06-01",
  44. "2027-03-01",
  45. "CAT-FBS-500",
  46. "BN20260415",
  47. "Gibco",
  48. Decimal("2680.00"),
  49. Decimal("6"),
  50. ),
  51. (
  52. "QC-MAT-003",
  53. "青霉素-链霉素双抗",
  54. "100ml/瓶",
  55. "瓶",
  56. Decimal("5"),
  57. 3,
  58. "细胞培养抗生素",
  59. "RK-20260601-003",
  60. "2026-06-02",
  61. "2026-12-02",
  62. "CAT-PS-100",
  63. "BN20260520",
  64. "HyClone",
  65. Decimal("186.00"),
  66. Decimal("10"),
  67. ),
  68. (
  69. "QC-MAT-004",
  70. "胰酶消化液",
  71. "100ml/瓶",
  72. "瓶",
  73. Decimal("4"),
  74. 4,
  75. "贴壁细胞消化",
  76. "RK-20260601-004",
  77. "2026-06-02",
  78. "2026-11-02",
  79. "CAT-TRY-100",
  80. "BN20260518",
  81. "Sigma",
  82. Decimal("95.00"),
  83. Decimal("8"),
  84. ),
  85. (
  86. "QC-MAT-005",
  87. "台盼蓝染色液",
  88. "50ml/瓶",
  89. "瓶",
  90. Decimal("2"),
  91. 5,
  92. "细胞活力检测",
  93. "RK-20260601-005",
  94. "2026-06-03",
  95. "2027-06-03",
  96. "CAT-TB-50",
  97. "BN20260525",
  98. "Bio-Rad",
  99. Decimal("72.00"),
  100. Decimal("5"),
  101. ),
  102. ]
  103. def seed(tenant_no: str = "zkxw", user_id: str = "d3000000000000000000000000000001") -> None:
  104. db = SessionLocal()
  105. created_material = 0
  106. skipped_material = 0
  107. created_batch = 0
  108. skipped_batch = 0
  109. try:
  110. for row in QC_MATERIAL_SEEDS:
  111. (
  112. code,
  113. name,
  114. spec,
  115. unit,
  116. warning_qty,
  117. order_num,
  118. remark,
  119. stock_in_no,
  120. stock_in_date,
  121. expire_date,
  122. catalog_no,
  123. batch_no,
  124. manufacturer,
  125. unit_price,
  126. qty,
  127. ) = row
  128. amount = (unit_price * qty).quantize(Decimal("0.01"))
  129. material = None
  130. if material_svc.code_exists(db, code, tenant_no):
  131. skipped_material += 1
  132. from app.models.models import BizQcMaterial
  133. material = (
  134. db.query(BizQcMaterial)
  135. .filter(
  136. BizQcMaterial.material_code == code,
  137. BizQcMaterial.tenant_no == tenant_no,
  138. BizQcMaterial.del_flag == 0,
  139. )
  140. .first()
  141. )
  142. else:
  143. material = material_svc.create_material(
  144. db,
  145. material_code=code,
  146. material_name=name,
  147. spec_model=spec,
  148. unit=unit,
  149. stock_qty=Decimal("0"),
  150. warning_qty=warning_qty,
  151. status="1",
  152. tenant_no=tenant_no,
  153. order_num=order_num,
  154. remark=remark,
  155. user_id=user_id,
  156. )
  157. created_material += 1
  158. if not material:
  159. print(f"跳过批次 {stock_in_no}:物料 {code} 不存在")
  160. continue
  161. if batch_svc.stock_in_no_exists(db, stock_in_no, tenant_no):
  162. skipped_batch += 1
  163. continue
  164. batch_svc.stock_in_one(
  165. db,
  166. material.id,
  167. stock_in_no=stock_in_no,
  168. stock_in_date=stock_in_date,
  169. expire_date=expire_date,
  170. catalog_no=catalog_no,
  171. batch_no=batch_no,
  172. manufacturer=manufacturer,
  173. unit_price=unit_price,
  174. amount=amount,
  175. qty=qty,
  176. remark="测试数据初始化",
  177. tenant_no=tenant_no,
  178. user_id=user_id,
  179. )
  180. created_batch += 1
  181. print(
  182. f"质检物料库测试数据:物料新增 {created_material} 条、跳过 {skipped_material} 条;"
  183. f"入库批次新增 {created_batch} 条、跳过 {skipped_batch} 条。"
  184. )
  185. finally:
  186. db.close()
  187. if __name__ == "__main__":
  188. seed()