309 lines
9.6 KiB
Python
309 lines
9.6 KiB
Python
"""采集地区及下属医院 CRUD"""
|
|
from __future__ import annotations
|
|
|
|
import math
|
|
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 BizCollectionHospital, BizCollectionRegion
|
|
|
|
|
|
def _region_to_dict(r: BizCollectionRegion, hospital_names: Optional[List[str]] = None) -> Dict[str, Any]:
|
|
return {
|
|
"id": r.id,
|
|
"regionName": r.region_name,
|
|
"hospitalNames": hospital_names or [],
|
|
"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 _hospital_to_dict(h: BizCollectionHospital) -> Dict[str, Any]:
|
|
return {
|
|
"id": h.id,
|
|
"regionId": h.region_id,
|
|
"hospitalName": h.hospital_name,
|
|
"tenantNo": h.tenant_no,
|
|
"orderNum": h.order_num if h.order_num is not None else 0,
|
|
"remark": h.remark or "",
|
|
"createTime": h.create_time.strftime("%Y-%m-%d %H:%M:%S") if h.create_time else "",
|
|
"updateTime": h.update_time.strftime("%Y-%m-%d %H:%M:%S") if h.update_time else "",
|
|
}
|
|
|
|
|
|
def list_collection_regions(
|
|
db: Session,
|
|
*,
|
|
page: int = 1,
|
|
page_size: int = 10,
|
|
keyword: Optional[str] = None,
|
|
tenant_no: Optional[str] = None,
|
|
) -> Tuple[List[Dict[str, Any]], int]:
|
|
def _filtered():
|
|
q0 = db.query(BizCollectionRegion).filter(BizCollectionRegion.del_flag == 0)
|
|
if tenant_no:
|
|
q0 = q0.filter(BizCollectionRegion.tenant_no == tenant_no)
|
|
kw = (keyword or "").strip()
|
|
if kw:
|
|
like = f"%{kw}%"
|
|
q0 = q0.filter(or_(BizCollectionRegion.region_name.like(like), BizCollectionRegion.remark.like(like)))
|
|
return q0
|
|
|
|
total = _filtered().order_by(None).with_entities(func.count(BizCollectionRegion.id)).scalar() or 0
|
|
page = max(1, page)
|
|
page_size = min(max(1, page_size), 100)
|
|
rows = (
|
|
_filtered()
|
|
.order_by(BizCollectionRegion.create_time.desc())
|
|
.offset((page - 1) * page_size)
|
|
.limit(page_size)
|
|
.all()
|
|
)
|
|
|
|
# 批量查询所有 region 的医院名称,避免 N+1
|
|
region_ids = [r.id for r in rows]
|
|
hospital_map: Dict[str, List[str]] = {}
|
|
if region_ids:
|
|
hosp_rows = (
|
|
db.query(BizCollectionHospital.region_id, BizCollectionHospital.hospital_name)
|
|
.filter(
|
|
and_(
|
|
BizCollectionHospital.region_id.in_(region_ids),
|
|
BizCollectionHospital.del_flag == 0,
|
|
)
|
|
)
|
|
.order_by(BizCollectionHospital.order_num.asc(), BizCollectionHospital.create_time.desc())
|
|
.all()
|
|
)
|
|
for h in hosp_rows:
|
|
hospital_map.setdefault(h.region_id, []).append(h.hospital_name)
|
|
|
|
return [_region_to_dict(x, hospital_names=hospital_map.get(x.id, [])) for x in rows], int(total)
|
|
|
|
|
|
def get_collection_region(db: Session, region_id: str, tenant_no: Optional[str] = None) -> Optional[BizCollectionRegion]:
|
|
q = db.query(BizCollectionRegion).filter(and_(BizCollectionRegion.id == region_id, BizCollectionRegion.del_flag == 0))
|
|
if tenant_no:
|
|
q = q.filter(BizCollectionRegion.tenant_no == tenant_no)
|
|
return q.first()
|
|
|
|
|
|
def create_collection_region(
|
|
db: Session,
|
|
*,
|
|
region_name: str,
|
|
tenant_no: str = "zkxw",
|
|
order_num: Optional[int] = None,
|
|
remark: Optional[str] = None,
|
|
user_id: Optional[str] = None,
|
|
) -> BizCollectionRegion:
|
|
name = (region_name or "").strip()
|
|
if not name:
|
|
raise ValueError("地区名称不能为空")
|
|
obj = BizCollectionRegion(
|
|
id=generate_uuid(),
|
|
region_name=name,
|
|
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_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_collection_region(
|
|
db: Session,
|
|
region_id: str,
|
|
*,
|
|
region_name: Optional[str] = None,
|
|
order_num: Optional[int] = None,
|
|
remark: Optional[str] = None,
|
|
user_id: Optional[str] = None,
|
|
tenant_no: Optional[str] = None,
|
|
) -> BizCollectionRegion:
|
|
obj = get_collection_region(db, region_id, tenant_no=tenant_no)
|
|
if not obj:
|
|
raise ValueError("采集地区不存在")
|
|
if region_name is not None:
|
|
n = region_name.strip()
|
|
if not n:
|
|
raise ValueError("地区名称不能为空")
|
|
obj.region_name = n
|
|
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_collection_region(db: Session, region_id: str, tenant_no: Optional[str] = None) -> None:
|
|
obj = get_collection_region(db, region_id, tenant_no=tenant_no)
|
|
if not obj:
|
|
raise ValueError("采集地区不存在")
|
|
now = datetime.now()
|
|
obj.del_flag = 1
|
|
obj.update_time = now
|
|
db.add(obj)
|
|
subs = (
|
|
db.query(BizCollectionHospital)
|
|
.filter(
|
|
and_(
|
|
BizCollectionHospital.region_id == region_id,
|
|
BizCollectionHospital.del_flag == 0,
|
|
)
|
|
)
|
|
.all()
|
|
)
|
|
for h in subs:
|
|
h.del_flag = 1
|
|
h.update_time = now
|
|
db.add(h)
|
|
db.commit()
|
|
|
|
|
|
def list_hospitals(
|
|
db: Session,
|
|
*,
|
|
region_id: str,
|
|
page: int = 1,
|
|
page_size: int = 10,
|
|
keyword: Optional[str] = None,
|
|
tenant_no: Optional[str] = None,
|
|
) -> Tuple[List[Dict[str, Any]], int]:
|
|
region = get_collection_region(db, region_id, tenant_no=tenant_no)
|
|
if not region:
|
|
raise ValueError("采集地区不存在")
|
|
|
|
def _filtered():
|
|
q0 = db.query(BizCollectionHospital).filter(
|
|
and_(BizCollectionHospital.region_id == region_id, BizCollectionHospital.del_flag == 0)
|
|
)
|
|
if tenant_no:
|
|
q0 = q0.filter(BizCollectionHospital.tenant_no == tenant_no)
|
|
kw = (keyword or "").strip()
|
|
if kw:
|
|
like = f"%{kw}%"
|
|
q0 = q0.filter(BizCollectionHospital.hospital_name.like(like))
|
|
return q0
|
|
|
|
total = _filtered().order_by(None).with_entities(func.count(BizCollectionHospital.id)).scalar() or 0
|
|
page = max(1, page)
|
|
page_size = min(max(1, page_size), 100)
|
|
rows = (
|
|
_filtered()
|
|
.order_by(BizCollectionHospital.create_time.desc())
|
|
.offset((page - 1) * page_size)
|
|
.limit(page_size)
|
|
.all()
|
|
)
|
|
return [_hospital_to_dict(x) for x in rows], int(total)
|
|
|
|
|
|
def get_hospital(db: Session, hospital_id: str, tenant_no: Optional[str] = None) -> Optional[BizCollectionHospital]:
|
|
q = db.query(BizCollectionHospital).filter(and_(BizCollectionHospital.id == hospital_id, BizCollectionHospital.del_flag == 0))
|
|
if tenant_no:
|
|
q = q.filter(BizCollectionHospital.tenant_no == tenant_no)
|
|
return q.first()
|
|
|
|
|
|
def create_hospital(
|
|
db: Session,
|
|
*,
|
|
region_id: str,
|
|
hospital_name: str,
|
|
tenant_no: str = "zkxw",
|
|
order_num: Optional[int] = None,
|
|
remark: Optional[str] = None,
|
|
user_id: Optional[str] = None,
|
|
) -> BizCollectionHospital:
|
|
region = get_collection_region(db, region_id, tenant_no=tenant_no)
|
|
if not region:
|
|
raise ValueError("采集地区不存在")
|
|
hn = (hospital_name or "").strip()
|
|
if not hn:
|
|
raise ValueError("医院名称不能为空")
|
|
obj = BizCollectionHospital(
|
|
id=generate_uuid(),
|
|
region_id=region_id,
|
|
hospital_name=hn,
|
|
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_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_hospital(
|
|
db: Session,
|
|
hospital_id: str,
|
|
*,
|
|
hospital_name: Optional[str] = None,
|
|
order_num: Optional[int] = None,
|
|
remark: Optional[str] = None,
|
|
user_id: Optional[str] = None,
|
|
tenant_no: Optional[str] = None,
|
|
) -> BizCollectionHospital:
|
|
obj = get_hospital(db, hospital_id, tenant_no=tenant_no)
|
|
if not obj:
|
|
raise ValueError("医院不存在")
|
|
if hospital_name is not None:
|
|
hn = hospital_name.strip()
|
|
if not hn:
|
|
raise ValueError("医院名称不能为空")
|
|
obj.hospital_name = hn
|
|
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_hospital(db: Session, hospital_id: str, tenant_no: Optional[str] = None) -> None:
|
|
obj = get_hospital(db, hospital_id, tenant_no=tenant_no)
|
|
if not obj:
|
|
raise ValueError("医院不存在")
|
|
obj.del_flag = 1
|
|
obj.update_time = datetime.now()
|
|
db.add(obj)
|
|
db.commit()
|
|
|
|
|
|
def total_pages(total: int, page_size: int) -> int:
|
|
if total <= 0:
|
|
return 0
|
|
return int(math.ceil(total / float(page_size)))
|