| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- """样本接收检查项配置 CRUD"""
- from __future__ import annotations
- from datetime import datetime
- from typing import Any, Dict, List, Optional, Tuple
- from sqlalchemy import and_, func, or_
- from sqlalchemy.orm import Session
- from app.core.utils import generate_uuid
- from app.models.models import BizSampleReceptionCheckItem
- def _row_to_dict(r: BizSampleReceptionCheckItem) -> Dict[str, Any]:
- return {
- "id": r.id,
- "itemCode": r.item_code,
- "itemName": r.item_name,
- "status": r.status or "1",
- "tenantNo": r.tenant_no,
- "orderNum": r.order_num if r.order_num is not None else 0,
- "remark": r.remark or "",
- "createTime": r.create_time.strftime("%Y-%m-%d %H:%M:%S") if r.create_time else "",
- "updateTime": r.update_time.strftime("%Y-%m-%d %H:%M:%S") if r.update_time else "",
- }
- def item_code_exists(
- db: Session, code: str, tenant_no: str, exclude_id: Optional[str] = None
- ) -> bool:
- c = (code or "").strip()
- if not c:
- return False
- q = db.query(BizSampleReceptionCheckItem).filter(
- and_(
- BizSampleReceptionCheckItem.item_code == c,
- BizSampleReceptionCheckItem.tenant_no == tenant_no,
- BizSampleReceptionCheckItem.del_flag == 0,
- )
- )
- if exclude_id:
- q = q.filter(BizSampleReceptionCheckItem.id != exclude_id)
- return q.first() is not None
- def _order_query(q):
- return q.order_by(
- BizSampleReceptionCheckItem.order_num.asc(),
- BizSampleReceptionCheckItem.create_time.desc(),
- )
- def list_check_items(
- db: Session,
- *,
- page: int = 1,
- page_size: int = 10,
- keyword: Optional[str] = None,
- status: Optional[str] = None,
- tenant_no: Optional[str] = None,
- ) -> Tuple[List[Dict[str, Any]], int]:
- def _filtered():
- q0 = db.query(BizSampleReceptionCheckItem).filter(
- BizSampleReceptionCheckItem.del_flag == 0
- )
- if tenant_no:
- q0 = q0.filter(BizSampleReceptionCheckItem.tenant_no == tenant_no)
- kw = (keyword or "").strip()
- if kw:
- like = f"%{kw}%"
- q0 = q0.filter(
- or_(
- BizSampleReceptionCheckItem.item_code.like(like),
- BizSampleReceptionCheckItem.item_name.like(like),
- )
- )
- st = (status or "").strip()
- if st in ("0", "1"):
- q0 = q0.filter(BizSampleReceptionCheckItem.status == st)
- return q0
- total = _filtered().order_by(None).with_entities(
- func.count(BizSampleReceptionCheckItem.id)
- ).scalar() or 0
- page = max(1, page)
- page_size = min(max(1, page_size), 100)
- rows = _order_query(_filtered()).offset((page - 1) * page_size).limit(page_size).all()
- return [_row_to_dict(x) for x in rows], int(total)
- def list_active_check_items(db: Session, tenant_no: str) -> List[Dict[str, Any]]:
- rows = (
- _order_query(
- db.query(BizSampleReceptionCheckItem).filter(
- BizSampleReceptionCheckItem.tenant_no == tenant_no,
- BizSampleReceptionCheckItem.del_flag == 0,
- BizSampleReceptionCheckItem.status == "1",
- )
- )
- .all()
- )
- return [_row_to_dict(x) for x in rows]
- def get_check_item(
- db: Session, row_id: str, tenant_no: Optional[str] = None
- ) -> Optional[BizSampleReceptionCheckItem]:
- q = db.query(BizSampleReceptionCheckItem).filter(
- and_(BizSampleReceptionCheckItem.id == row_id, BizSampleReceptionCheckItem.del_flag == 0)
- )
- if tenant_no:
- q = q.filter(BizSampleReceptionCheckItem.tenant_no == tenant_no)
- return q.first()
- def create_check_item(
- db: Session,
- *,
- item_code: str,
- item_name: str,
- status: str = "1",
- tenant_no: str,
- order_num: Optional[int] = None,
- remark: Optional[str] = None,
- user_id: Optional[str] = None,
- create_dept_id: str = "",
- ) -> BizSampleReceptionCheckItem:
- code = (item_code or "").strip()
- if not code:
- raise ValueError("检查项编码不能为空")
- if item_code_exists(db, code, tenant_no):
- raise ValueError("检查项编码已存在")
- name = (item_name or "").strip()
- if not name:
- raise ValueError("检查项名称不能为空")
- st = (status or "1").strip()
- if st not in ("0", "1"):
- raise ValueError("状态无效")
- obj = BizSampleReceptionCheckItem(
- id=generate_uuid(),
- item_code=code,
- item_name=name,
- status=st,
- tenant_no=tenant_no,
- order_num=order_num if order_num is not None else 0,
- remark=(remark or "").strip() or None,
- create_by=user_id,
- create_dept_id=create_dept_id or "",
- create_time=datetime.now(),
- update_by=user_id,
- update_time=datetime.now(),
- del_flag=0,
- )
- db.add(obj)
- db.commit()
- db.refresh(obj)
- return obj
- def update_check_item(
- db: Session,
- row_id: str,
- *,
- item_code: Optional[str] = None,
- item_name: Optional[str] = None,
- status: Optional[str] = None,
- order_num: Optional[int] = None,
- remark: Optional[str] = None,
- user_id: Optional[str] = None,
- tenant_no: Optional[str] = None,
- ) -> BizSampleReceptionCheckItem:
- obj = get_check_item(db, row_id, tenant_no=tenant_no)
- if not obj:
- raise ValueError("检查项不存在")
- if item_code is not None:
- code = item_code.strip()
- if not code:
- raise ValueError("检查项编码不能为空")
- if code != obj.item_code and item_code_exists(db, code, obj.tenant_no, exclude_id=row_id):
- raise ValueError("检查项编码已存在")
- obj.item_code = code
- if item_name is not None:
- name = (item_name or "").strip()
- if not name:
- raise ValueError("检查项名称不能为空")
- obj.item_name = name
- if status is not None:
- st = status.strip()
- if st not in ("0", "1"):
- raise ValueError("状态无效")
- obj.status = st
- if order_num is not None:
- obj.order_num = order_num
- if remark is not None:
- obj.remark = remark.strip() or None
- obj.update_by = user_id
- obj.update_time = datetime.now()
- db.add(obj)
- db.commit()
- db.refresh(obj)
- return obj
- def soft_delete_check_item(
- db: Session, row_id: str, tenant_no: Optional[str] = None
- ) -> None:
- obj = get_check_item(db, row_id, tenant_no=tenant_no)
- if not obj:
- raise ValueError("检查项不存在")
- obj.del_flag = 1
- obj.update_time = datetime.now()
- db.add(obj)
- db.commit()
|