汇总近期业务改动:催缴菜单与后端、样本信息快捷登记与进度、运费关联、客户档案扩展、协议优惠/期次与押金划转,并同步 SQL 迁移与管理端页面收敛。 Co-authored-by: Cursor <cursoragent@cursor.com>
185 lines
6.5 KiB
Python
185 lines
6.5 KiB
Python
"""采集协议样本采集信息 API"""
|
|
from typing import List, Optional
|
|
|
|
from fastapi import APIRouter, Depends, Query
|
|
from pydantic import BaseModel, ConfigDict, Field
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.database import get_db
|
|
from app.core.menu_guard import require_any_menu_code
|
|
from app.core.response import error_response, success_response
|
|
from app.core.utils import parse_datetime
|
|
from app.crud import protocol_sample_collection as svc
|
|
from app.crud import sample_info_quick_register as quick_svc
|
|
from app.crud.collection_protocol import total_pages
|
|
from app.models.models import SysUser
|
|
|
|
router = APIRouter(prefix="/web/admin", tags=["采集协议样本采集信息"])
|
|
|
|
_SAMPLE_INFO_MENU = require_any_menu_code(
|
|
"cell-storage:sample-info:list",
|
|
)
|
|
|
|
_QUICK_REGISTER_MENU = require_any_menu_code(
|
|
"cell-storage:sample-info:list",
|
|
"research-donation:sample-info:list",
|
|
)
|
|
|
|
|
|
def _get_tenant_no(db: Session, user: dict) -> str:
|
|
user_id = user.get("user_id")
|
|
if not user_id:
|
|
raise PermissionError("用户身份信息缺失,无法确定租户")
|
|
sys_user = db.query(SysUser).filter(SysUser.id == user_id).first()
|
|
if not sys_user or not sys_user.tenant_no:
|
|
raise PermissionError("用户租户信息缺失")
|
|
return sys_user.tenant_no
|
|
|
|
|
|
def _page_payload(items: list, total: int, page: int, page_size: int) -> dict:
|
|
return {
|
|
"list": items,
|
|
"total": total,
|
|
"page": page,
|
|
"pageSize": page_size,
|
|
"totalPages": total_pages(total, page_size),
|
|
}
|
|
|
|
|
|
@router.get("/protocol-sample-collections/list")
|
|
async def list_protocol_sample_collections(
|
|
page: int = Query(1, ge=1),
|
|
pageSize: int = Query(10, ge=1, le=100),
|
|
keyword: Optional[str] = None,
|
|
sampleStatus: Optional[str] = Query(None, alias="sampleStatus"),
|
|
collectionFlag: Optional[str] = Query(None, alias="collectionFlag"),
|
|
receptionFlag: Optional[str] = Query(None, alias="receptionFlag"),
|
|
qcFlag: Optional[str] = Query(None, alias="qcFlag"),
|
|
storageFlag: Optional[str] = Query(None, alias="storageFlag"),
|
|
storageCertHandled: Optional[str] = Query(None, alias="storageCertHandled"),
|
|
insuranceHandled: Optional[str] = Query(None, alias="insuranceHandled"),
|
|
db: Session = Depends(get_db),
|
|
user: dict = Depends(_SAMPLE_INFO_MENU),
|
|
):
|
|
try:
|
|
tenant_no = _get_tenant_no(db, user)
|
|
items, total = svc.list_protocol_sample_collections(
|
|
db,
|
|
page=page,
|
|
page_size=pageSize,
|
|
tenant_no=tenant_no,
|
|
keyword=keyword,
|
|
sample_status=sampleStatus,
|
|
collection_flag=collectionFlag,
|
|
reception_flag=receptionFlag,
|
|
qc_flag=qcFlag,
|
|
storage_flag=storageFlag,
|
|
storage_cert_handled=storageCertHandled,
|
|
insurance_handled=insuranceHandled,
|
|
user_info=user,
|
|
)
|
|
return success_response(_page_payload(items, total, page, pageSize))
|
|
except PermissionError as e:
|
|
return error_response(403, str(e))
|
|
except Exception as e:
|
|
return error_response(400, str(e))
|
|
|
|
|
|
def _get_current_user_id(user: dict) -> str:
|
|
user_id = user.get("user_id")
|
|
if not user_id:
|
|
raise PermissionError("用户身份信息缺失")
|
|
return str(user_id)
|
|
|
|
|
|
class QuickRegisterItem(BaseModel):
|
|
model_config = ConfigDict(populate_by_name=True)
|
|
|
|
sample_code: str = Field(..., alias="sampleCode")
|
|
protocol_id: str = Field(..., alias="protocolId")
|
|
|
|
|
|
class QuickRegisterBody(BaseModel):
|
|
model_config = ConfigDict(populate_by_name=True)
|
|
|
|
kind: str
|
|
reception_source: str = Field(..., alias="receptionSource")
|
|
items: List[QuickRegisterItem]
|
|
handle_time: Optional[str] = Field(None, alias="handleTime")
|
|
handler_user_id: Optional[str] = Field(None, alias="handlerUserId")
|
|
attachments: Optional[str] = None
|
|
remark: Optional[str] = None
|
|
|
|
|
|
@router.post("/sample-info/quick-register")
|
|
async def quick_register_sample_info(
|
|
body: QuickRegisterBody,
|
|
db: Session = Depends(get_db),
|
|
user: dict = Depends(_QUICK_REGISTER_MENU),
|
|
):
|
|
try:
|
|
tenant_no = _get_tenant_no(db, user)
|
|
user_id = _get_current_user_id(user)
|
|
ht = parse_datetime(body.handle_time) if body.handle_time else None
|
|
if ht is None:
|
|
return error_response(400, "请填写办理时间")
|
|
items = [
|
|
{"sampleCode": it.sample_code, "protocolId": it.protocol_id} for it in (body.items or [])
|
|
]
|
|
protocol_ids = [it["protocolId"] for it in items]
|
|
kind = (body.kind or "").strip()
|
|
source = (body.reception_source or "").strip()
|
|
attachments = body.attachments or ""
|
|
remark = body.remark or ""
|
|
if kind == "storage":
|
|
result = quick_svc.batch_quick_register_storage(
|
|
db,
|
|
items=items,
|
|
reception_source=source,
|
|
tenant_no=tenant_no,
|
|
user=user,
|
|
user_id=user_id,
|
|
handle_time=ht,
|
|
handler_user_id=body.handler_user_id,
|
|
attachments=attachments,
|
|
remark=remark,
|
|
)
|
|
elif kind == "storage_cert":
|
|
if source != "cell_storage":
|
|
return error_response(400, "储存证仅支持细胞存储样本")
|
|
result = quick_svc.batch_handle_storage_certs(
|
|
db,
|
|
protocol_ids=protocol_ids,
|
|
tenant_no=tenant_no,
|
|
user=user,
|
|
user_id=user_id,
|
|
handle_time=ht,
|
|
handler_user_id=body.handler_user_id,
|
|
attachments=attachments,
|
|
remark=remark,
|
|
)
|
|
elif kind == "insurance":
|
|
if source != "cell_storage":
|
|
return error_response(400, "保险仅支持细胞存储样本")
|
|
result = quick_svc.batch_handle_insurance(
|
|
db,
|
|
protocol_ids=protocol_ids,
|
|
tenant_no=tenant_no,
|
|
user=user,
|
|
user_id=user_id,
|
|
handle_time=ht,
|
|
handler_user_id=body.handler_user_id,
|
|
attachments=attachments,
|
|
remark=remark,
|
|
)
|
|
else:
|
|
return error_response(400, "登记类型无效")
|
|
return success_response(result, message="登记完成")
|
|
except PermissionError as e:
|
|
return error_response(403, str(e))
|
|
except ValueError as e:
|
|
return error_response(400, str(e))
|
|
except Exception as e:
|
|
return error_response(400, str(e))
|
|
|