process_category.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. """工艺工序分类 CRUD(biz_process_category)"""
  2. from __future__ import annotations
  3. import math
  4. from datetime import datetime
  5. from typing import Any, Dict, List, Optional, Tuple
  6. from sqlalchemy import and_, func, or_
  7. from sqlalchemy.orm import Session
  8. from app.core.utils import generate_uuid
  9. from app.models.models import BizProcessCategory
  10. def _row_to_dict(c: BizProcessCategory) -> Dict[str, Any]:
  11. return {
  12. "id": c.id,
  13. "categoryCode": c.category_code,
  14. "categoryName": c.category_name,
  15. "tenantNo": c.tenant_no,
  16. "orderNum": c.order_num if c.order_num is not None else 0,
  17. "remark": c.remark or "",
  18. "createTime": c.create_time.strftime("%Y-%m-%d %H:%M:%S") if c.create_time else "",
  19. "updateTime": c.update_time.strftime("%Y-%m-%d %H:%M:%S") if c.update_time else "",
  20. }
  21. def list_process_categories(
  22. db: Session,
  23. *,
  24. page: int = 1,
  25. page_size: int = 10,
  26. keyword: Optional[str] = None,
  27. tenant_no: Optional[str] = None,
  28. user_info=None,
  29. ) -> Tuple[List[Dict[str, Any]], int]:
  30. def _filtered():
  31. q0 = db.query(BizProcessCategory).filter(BizProcessCategory.del_flag == 0)
  32. if tenant_no:
  33. q0 = q0.filter(BizProcessCategory.tenant_no == tenant_no)
  34. kw = (keyword or "").strip()
  35. if kw:
  36. like = f"%{kw}%"
  37. q0 = q0.filter(
  38. or_(
  39. BizProcessCategory.category_code.like(like),
  40. BizProcessCategory.category_name.like(like),
  41. )
  42. )
  43. if user_info:
  44. from app.core.data_scope import apply_data_scope
  45. q0 = apply_data_scope(q0, BizProcessCategory, user_info, db, tenant_no)
  46. return q0
  47. total = _filtered().order_by(None).with_entities(func.count(BizProcessCategory.id)).scalar() or 0
  48. page = max(1, page)
  49. page_size = min(max(1, page_size), 100)
  50. rows = (
  51. _filtered()
  52. .order_by(BizProcessCategory.create_time.desc())
  53. .offset((page - 1) * page_size)
  54. .limit(page_size)
  55. .all()
  56. )
  57. return [_row_to_dict(x) for x in rows], int(total)
  58. def get_process_category(db: Session, category_id: str) -> Optional[BizProcessCategory]:
  59. return (
  60. db.query(BizProcessCategory)
  61. .filter(and_(BizProcessCategory.id == category_id, BizProcessCategory.del_flag == 0))
  62. .first()
  63. )
  64. def category_code_exists(db: Session, code: str, tenant_no: str, exclude_id: Optional[str] = None) -> bool:
  65. q = db.query(BizProcessCategory).filter(
  66. and_(BizProcessCategory.category_code == code.strip(), BizProcessCategory.tenant_no == tenant_no, BizProcessCategory.del_flag == 0)
  67. )
  68. if exclude_id:
  69. q = q.filter(BizProcessCategory.id != exclude_id)
  70. return q.first() is not None
  71. def create_process_category(
  72. db: Session,
  73. *,
  74. category_code: str,
  75. category_name: str,
  76. tenant_no: str = "zkxw",
  77. order_num: Optional[int] = None,
  78. remark: Optional[str] = None,
  79. user_id: Optional[str] = None,
  80. ) -> BizProcessCategory:
  81. cc = (category_code or "").strip()
  82. if not cc:
  83. raise ValueError("分类编码不能为空")
  84. if category_code_exists(db, cc, tenant_no):
  85. raise ValueError("分类编码已存在")
  86. obj = BizProcessCategory(
  87. id=generate_uuid(),
  88. category_code=cc,
  89. category_name=(category_name or "").strip() or cc,
  90. tenant_no=tenant_no,
  91. order_num=order_num if order_num is not None else 0,
  92. remark=(remark or "").strip() or None,
  93. create_by=user_id,
  94. create_dept_id="",
  95. create_time=datetime.now(),
  96. update_by=user_id,
  97. update_time=datetime.now(),
  98. del_flag=0,
  99. )
  100. db.add(obj)
  101. db.commit()
  102. db.refresh(obj)
  103. return obj
  104. def update_process_category(
  105. db: Session,
  106. category_id: str,
  107. *,
  108. category_code: Optional[str] = None,
  109. category_name: Optional[str] = None,
  110. order_num: Optional[int] = None,
  111. remark: Optional[str] = None,
  112. user_id: Optional[str] = None,
  113. ) -> BizProcessCategory:
  114. obj = get_process_category(db, category_id)
  115. if not obj:
  116. raise ValueError("工艺工序分类不存在")
  117. if category_code is not None:
  118. new_code = category_code.strip()
  119. if not new_code:
  120. raise ValueError("分类编码不能为空")
  121. if new_code != obj.category_code and category_code_exists(db, new_code, obj.tenant_no, exclude_id=category_id):
  122. raise ValueError("分类编码已存在")
  123. obj.category_code = new_code
  124. if category_name is not None:
  125. obj.category_name = (category_name or "").strip() or obj.category_name
  126. if order_num is not None:
  127. obj.order_num = order_num
  128. if remark is not None:
  129. obj.remark = (remark or "").strip() or None
  130. obj.update_by = user_id
  131. obj.update_time = datetime.now()
  132. db.add(obj)
  133. db.commit()
  134. db.refresh(obj)
  135. return obj
  136. def soft_delete_process_category(db: Session, category_id: str) -> None:
  137. obj = get_process_category(db, category_id)
  138. if not obj:
  139. raise ValueError("工艺工序分类不存在")
  140. obj.del_flag = 1
  141. obj.update_time = datetime.now()
  142. db.add(obj)
  143. db.commit()
  144. def total_pages(total: int, page_size: int) -> int:
  145. if total <= 0:
  146. return 0
  147. return int(math.ceil(total / float(page_size)))