sample_reception_check_item.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. """样本接收检查项配置 CRUD"""
  2. from __future__ import annotations
  3. from datetime import datetime
  4. from typing import Any, Dict, List, Optional, Tuple
  5. from sqlalchemy import and_, func, or_
  6. from sqlalchemy.orm import Session
  7. from app.core.utils import generate_uuid
  8. from app.models.models import BizSampleReceptionCheckItem
  9. def _row_to_dict(r: BizSampleReceptionCheckItem) -> Dict[str, Any]:
  10. return {
  11. "id": r.id,
  12. "itemCode": r.item_code,
  13. "itemName": r.item_name,
  14. "status": r.status or "1",
  15. "tenantNo": r.tenant_no,
  16. "orderNum": r.order_num if r.order_num is not None else 0,
  17. "remark": r.remark or "",
  18. "createTime": r.create_time.strftime("%Y-%m-%d %H:%M:%S") if r.create_time else "",
  19. "updateTime": r.update_time.strftime("%Y-%m-%d %H:%M:%S") if r.update_time else "",
  20. }
  21. def item_code_exists(
  22. db: Session, code: str, tenant_no: str, exclude_id: Optional[str] = None
  23. ) -> bool:
  24. c = (code or "").strip()
  25. if not c:
  26. return False
  27. q = db.query(BizSampleReceptionCheckItem).filter(
  28. and_(
  29. BizSampleReceptionCheckItem.item_code == c,
  30. BizSampleReceptionCheckItem.tenant_no == tenant_no,
  31. BizSampleReceptionCheckItem.del_flag == 0,
  32. )
  33. )
  34. if exclude_id:
  35. q = q.filter(BizSampleReceptionCheckItem.id != exclude_id)
  36. return q.first() is not None
  37. def _order_query(q):
  38. return q.order_by(
  39. BizSampleReceptionCheckItem.order_num.asc(),
  40. BizSampleReceptionCheckItem.create_time.desc(),
  41. )
  42. def list_check_items(
  43. db: Session,
  44. *,
  45. page: int = 1,
  46. page_size: int = 10,
  47. keyword: Optional[str] = None,
  48. status: Optional[str] = None,
  49. tenant_no: Optional[str] = None,
  50. ) -> Tuple[List[Dict[str, Any]], int]:
  51. def _filtered():
  52. q0 = db.query(BizSampleReceptionCheckItem).filter(
  53. BizSampleReceptionCheckItem.del_flag == 0
  54. )
  55. if tenant_no:
  56. q0 = q0.filter(BizSampleReceptionCheckItem.tenant_no == tenant_no)
  57. kw = (keyword or "").strip()
  58. if kw:
  59. like = f"%{kw}%"
  60. q0 = q0.filter(
  61. or_(
  62. BizSampleReceptionCheckItem.item_code.like(like),
  63. BizSampleReceptionCheckItem.item_name.like(like),
  64. )
  65. )
  66. st = (status or "").strip()
  67. if st in ("0", "1"):
  68. q0 = q0.filter(BizSampleReceptionCheckItem.status == st)
  69. return q0
  70. total = _filtered().order_by(None).with_entities(
  71. func.count(BizSampleReceptionCheckItem.id)
  72. ).scalar() or 0
  73. page = max(1, page)
  74. page_size = min(max(1, page_size), 100)
  75. rows = _order_query(_filtered()).offset((page - 1) * page_size).limit(page_size).all()
  76. return [_row_to_dict(x) for x in rows], int(total)
  77. def list_active_check_items(db: Session, tenant_no: str) -> List[Dict[str, Any]]:
  78. rows = (
  79. _order_query(
  80. db.query(BizSampleReceptionCheckItem).filter(
  81. BizSampleReceptionCheckItem.tenant_no == tenant_no,
  82. BizSampleReceptionCheckItem.del_flag == 0,
  83. BizSampleReceptionCheckItem.status == "1",
  84. )
  85. )
  86. .all()
  87. )
  88. return [_row_to_dict(x) for x in rows]
  89. def get_check_item(
  90. db: Session, row_id: str, tenant_no: Optional[str] = None
  91. ) -> Optional[BizSampleReceptionCheckItem]:
  92. q = db.query(BizSampleReceptionCheckItem).filter(
  93. and_(BizSampleReceptionCheckItem.id == row_id, BizSampleReceptionCheckItem.del_flag == 0)
  94. )
  95. if tenant_no:
  96. q = q.filter(BizSampleReceptionCheckItem.tenant_no == tenant_no)
  97. return q.first()
  98. def create_check_item(
  99. db: Session,
  100. *,
  101. item_code: str,
  102. item_name: str,
  103. status: str = "1",
  104. tenant_no: str,
  105. order_num: Optional[int] = None,
  106. remark: Optional[str] = None,
  107. user_id: Optional[str] = None,
  108. create_dept_id: str = "",
  109. ) -> BizSampleReceptionCheckItem:
  110. code = (item_code or "").strip()
  111. if not code:
  112. raise ValueError("检查项编码不能为空")
  113. if item_code_exists(db, code, tenant_no):
  114. raise ValueError("检查项编码已存在")
  115. name = (item_name or "").strip()
  116. if not name:
  117. raise ValueError("检查项名称不能为空")
  118. st = (status or "1").strip()
  119. if st not in ("0", "1"):
  120. raise ValueError("状态无效")
  121. obj = BizSampleReceptionCheckItem(
  122. id=generate_uuid(),
  123. item_code=code,
  124. item_name=name,
  125. status=st,
  126. tenant_no=tenant_no,
  127. order_num=order_num if order_num is not None else 0,
  128. remark=(remark or "").strip() or None,
  129. create_by=user_id,
  130. create_dept_id=create_dept_id or "",
  131. create_time=datetime.now(),
  132. update_by=user_id,
  133. update_time=datetime.now(),
  134. del_flag=0,
  135. )
  136. db.add(obj)
  137. db.commit()
  138. db.refresh(obj)
  139. return obj
  140. def update_check_item(
  141. db: Session,
  142. row_id: str,
  143. *,
  144. item_code: Optional[str] = None,
  145. item_name: Optional[str] = None,
  146. status: Optional[str] = None,
  147. order_num: Optional[int] = None,
  148. remark: Optional[str] = None,
  149. user_id: Optional[str] = None,
  150. tenant_no: Optional[str] = None,
  151. ) -> BizSampleReceptionCheckItem:
  152. obj = get_check_item(db, row_id, tenant_no=tenant_no)
  153. if not obj:
  154. raise ValueError("检查项不存在")
  155. if item_code is not None:
  156. code = item_code.strip()
  157. if not code:
  158. raise ValueError("检查项编码不能为空")
  159. if code != obj.item_code and item_code_exists(db, code, obj.tenant_no, exclude_id=row_id):
  160. raise ValueError("检查项编码已存在")
  161. obj.item_code = code
  162. if item_name is not None:
  163. name = (item_name or "").strip()
  164. if not name:
  165. raise ValueError("检查项名称不能为空")
  166. obj.item_name = name
  167. if status is not None:
  168. st = status.strip()
  169. if st not in ("0", "1"):
  170. raise ValueError("状态无效")
  171. obj.status = st
  172. if order_num is not None:
  173. obj.order_num = order_num
  174. if remark is not None:
  175. obj.remark = remark.strip() or None
  176. obj.update_by = user_id
  177. obj.update_time = datetime.now()
  178. db.add(obj)
  179. db.commit()
  180. db.refresh(obj)
  181. return obj
  182. def soft_delete_check_item(
  183. db: Session, row_id: str, tenant_no: Optional[str] = None
  184. ) -> None:
  185. obj = get_check_item(db, row_id, tenant_no=tenant_no)
  186. if not obj:
  187. raise ValueError("检查项不存在")
  188. obj.del_flag = 1
  189. obj.update_time = datetime.now()
  190. db.add(obj)
  191. db.commit()