seed_market_materials.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #!/usr/bin/env python3
  2. """市场物料库测试数据(biz_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 material as material_svc
  12. from app.crud import material_stock_batch as batch_svc
  13. from app.models.material import BizMaterial
  14. # (material_code, material_name, category, spec_model, unit, warning_qty, order_num, remark,
  15. # stock_in_no, stock_in_date, expire_date, catalog_no, batch_no, manufacturer,
  16. # unit_price, qty)
  17. MARKET_MATERIAL_SEEDS = [
  18. (
  19. "MKT-MAT-001",
  20. "品牌宣传册",
  21. "xccyw",
  22. "A4 彩色 20页",
  23. "册",
  24. Decimal("50"),
  25. 1,
  26. "市场宣传用物料",
  27. "MKT-RK-20260615-001",
  28. "2026-06-10",
  29. "2028-06-10",
  30. "CAT-BRO-A4-20",
  31. "BN20260601",
  32. "安徽印务有限公司",
  33. Decimal("12.50"),
  34. Decimal("200"),
  35. ),
  36. (
  37. "MKT-MAT-002",
  38. "细胞存储服务手册",
  39. "xccyw",
  40. "A5 精装",
  41. "本",
  42. Decimal("30"),
  43. 2,
  44. "客户咨询配套资料",
  45. "MKT-RK-20260615-002",
  46. "2026-06-10",
  47. "2028-06-10",
  48. "CAT-MAN-A5",
  49. "BN20260602",
  50. "安徽印务有限公司",
  51. Decimal("18.00"),
  52. Decimal("100"),
  53. ),
  54. (
  55. "MKT-MAT-003",
  56. "采集专用管套装",
  57. "ccyw",
  58. "50ml×6支/套",
  59. "套",
  60. Decimal("20"),
  61. 3,
  62. "采集业务现场使用",
  63. "MKT-RK-20260615-003",
  64. "2026-06-11",
  65. "2027-06-11",
  66. "CAT-TUBE-50-6",
  67. "BN20260603",
  68. "康宁生物耗材",
  69. Decimal("85.00"),
  70. Decimal("50"),
  71. ),
  72. (
  73. "MKT-MAT-004",
  74. "知情同意书范本",
  75. "ccyw",
  76. "A4 单页",
  77. "份",
  78. Decimal("100"),
  79. 4,
  80. "采集业务合规文件",
  81. "MKT-RK-20260615-004",
  82. "2026-06-11",
  83. "2027-12-31",
  84. "CAT-ICF-A4",
  85. "BN20260604",
  86. "法务文印中心",
  87. Decimal("0.80"),
  88. Decimal("500"),
  89. ),
  90. (
  91. "MKT-MAT-005",
  92. "会议展架易拉宝",
  93. "xccyw",
  94. "80×200cm 含支架",
  95. "个",
  96. Decimal("5"),
  97. 5,
  98. "展会及会议布置",
  99. "MKT-RK-20260615-005",
  100. "2026-06-12",
  101. "2029-06-12",
  102. "CAT-ROLL-80200",
  103. "BN20260605",
  104. "合肥广告制作",
  105. Decimal("120.00"),
  106. Decimal("10"),
  107. ),
  108. ]
  109. def _get_material_by_code(db, code: str, tenant_no: str) -> BizMaterial | None:
  110. return (
  111. db.query(BizMaterial)
  112. .filter(
  113. BizMaterial.material_code == code,
  114. BizMaterial.tenant_no == tenant_no,
  115. BizMaterial.del_flag == 0,
  116. )
  117. .first()
  118. )
  119. def seed(tenant_no: str = "zkxw", user_id: str = "d3000000000000000000000000000001") -> None:
  120. db = SessionLocal()
  121. created_material = 0
  122. skipped_material = 0
  123. created_batch = 0
  124. skipped_batch = 0
  125. try:
  126. for row in MARKET_MATERIAL_SEEDS:
  127. (
  128. code,
  129. name,
  130. category,
  131. spec,
  132. unit,
  133. warning_qty,
  134. order_num,
  135. remark,
  136. stock_in_no,
  137. stock_in_date,
  138. expire_date,
  139. catalog_no,
  140. batch_no,
  141. manufacturer,
  142. unit_price,
  143. qty,
  144. ) = row
  145. amount = (unit_price * qty).quantize(Decimal("0.01"))
  146. material = _get_material_by_code(db, code, tenant_no)
  147. if material:
  148. skipped_material += 1
  149. else:
  150. material_svc.create_material(
  151. db,
  152. {
  153. "materialCode": code,
  154. "materialName": name,
  155. "category": category,
  156. "specModel": spec,
  157. "unit": unit,
  158. "warningQty": warning_qty,
  159. "orderNum": order_num,
  160. "remark": remark,
  161. "status": "1",
  162. },
  163. tenant_no=tenant_no,
  164. user_id=user_id,
  165. )
  166. created_material += 1
  167. material = _get_material_by_code(db, code, tenant_no)
  168. if not material:
  169. print(f"跳过批次 {stock_in_no}:物料 {code} 不存在")
  170. continue
  171. if batch_svc.stock_in_no_exists(db, stock_in_no, tenant_no):
  172. skipped_batch += 1
  173. continue
  174. batch_svc.stock_in_one(
  175. db,
  176. material.id,
  177. stock_in_no=stock_in_no,
  178. stock_in_date=stock_in_date,
  179. expire_date=expire_date,
  180. catalog_no=catalog_no,
  181. batch_no=batch_no,
  182. manufacturer=manufacturer,
  183. unit_price=unit_price,
  184. amount=amount,
  185. qty=qty,
  186. remark="测试数据初始化",
  187. tenant_no=tenant_no,
  188. user_id=user_id,
  189. )
  190. created_batch += 1
  191. print(
  192. f"市场物料库测试数据:物料新增 {created_material} 条、跳过 {skipped_material} 条;"
  193. f"入库批次新增 {created_batch} 条、跳过 {skipped_batch} 条。"
  194. )
  195. finally:
  196. db.close()
  197. if __name__ == "__main__":
  198. seed()