"""实验室自检项目 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 BizQcLabTestItem def _row_to_dict(r: BizQcLabTestItem) -> Dict[str, Any]: return { "id": r.id, "itemCode": r.item_code, "itemName": r.item_name, "description": r.description or "", "status": r.status or "1", "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(BizQcLabTestItem).filter( BizQcLabTestItem.item_code == c, BizQcLabTestItem.tenant_no == tenant_no, BizQcLabTestItem.del_flag == 0, ) if exclude_id: q = q.filter(BizQcLabTestItem.id != exclude_id) return q.first() is not None def list_lab_items( db: Session, *, page: int = 1, page_size: int = 10, keyword: Optional[str] = None, status: Optional[str] = None, tenant_no: Optional[str] = None, user_info=None, ) -> Tuple[List[Dict[str, Any]], int]: def _filtered(): q0 = db.query(BizQcLabTestItem).filter(BizQcLabTestItem.del_flag == 0) if tenant_no: q0 = q0.filter(BizQcLabTestItem.tenant_no == tenant_no) kw = (keyword or "").strip() if kw: like = f"%{kw}%" q0 = q0.filter( or_(BizQcLabTestItem.item_code.like(like), BizQcLabTestItem.item_name.like(like)) ) st = (status or "").strip() if st in ("0", "1"): q0 = q0.filter(BizQcLabTestItem.status == st) if user_info: from app.core.data_scope import apply_data_scope q0 = apply_data_scope(q0, BizQcLabTestItem, user_info, db, tenant_no) return q0 total = _filtered().order_by(None).with_entities(func.count(BizQcLabTestItem.id)).scalar() or 0 page = max(1, page) page_size = min(max(1, page_size), 100) rows = ( _filtered() .order_by(BizQcLabTestItem.order_num.asc(), BizQcLabTestItem.create_time.desc()) .offset((page - 1) * page_size) .limit(page_size) .all() ) return [_row_to_dict(x) for x in rows], int(total) def list_active_lab_items(db: Session, tenant_no: str, *, user_info=None) -> List[Dict[str, Any]]: q = db.query(BizQcLabTestItem).filter( BizQcLabTestItem.tenant_no == tenant_no, BizQcLabTestItem.del_flag == 0, BizQcLabTestItem.status == "1", ) if user_info: from app.core.data_scope import apply_data_scope q = apply_data_scope(q, BizQcLabTestItem, user_info, db, tenant_no) rows = q.order_by(BizQcLabTestItem.order_num.asc(), BizQcLabTestItem.create_time.desc()).all() return [_row_to_dict(x) for x in rows] def get_lab_item( db: Session, row_id: str, tenant_no: Optional[str] = None ) -> Optional[BizQcLabTestItem]: q = db.query(BizQcLabTestItem).filter( BizQcLabTestItem.id == row_id, BizQcLabTestItem.del_flag == 0 ) if tenant_no: q = q.filter(BizQcLabTestItem.tenant_no == tenant_no) return q.first() def create_lab_item( db: Session, *, item_code: str, item_name: str, description: Optional[str] = None, status: str = "1", tenant_no: str, order_num: Optional[int] = None, remark: Optional[str] = None, user_id: Optional[str] = None, create_dept_id: str = "", ) -> BizQcLabTestItem: 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 = BizQcLabTestItem( id=generate_uuid(), item_code=code, item_name=name, description=(description or "").strip() or None, 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_lab_item( db: Session, row_id: str, *, item_code: Optional[str] = None, item_name: Optional[str] = None, description: 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, ) -> BizQcLabTestItem: obj = get_lab_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 description is not None: obj.description = (description or "").strip() or None 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 or "").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_lab_item(db: Session, row_id: str, tenant_no: Optional[str] = None) -> None: obj = get_lab_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()