feat(sample): 展示样本状态并支持样本废弃与作废原因
生产/质检入库卡片展示样本状态并支持【样本废弃】;市场 Web 列表与移动市场部卡片补充样本状态及问号查看作废原因;样本表新增 void_desc,人工作废不联动接收但全部作废时自动作废协议(#131)。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
7751cc0f4a
commit
0b4c8dfda8
53 files changed
+1527
-82
No files matched your search
@@ -7,6 +7,8 @@
|
||||
- 协议下全部样本接收结论自动为废弃(bj);
|
||||
- 协议下全部有效样本 `sample_status` 置为已作废(yzf),并写入 `void_date`。
|
||||
3. 协议下全部有效样本均已废弃/拒收(bj)时,协议自动已作废,作废类型为「实验室废弃」。
|
||||
4. 人工【样本废弃】:仅将该样本置 yzf 并写 void_date/void_desc,不改接收结论;
|
||||
若协议下有效样本全部为 yzf,则自动作废协议(不联动接收结论)。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -32,6 +34,7 @@ SAMPLE_VOID_STATUS = "yzf"
|
||||
|
||||
_AUTO_DISCARD_REMARK = "协议作废自动废弃"
|
||||
_AUTO_VOID_DESC = "全部样本接收废弃/拒收,系统自动作废"
|
||||
_AUTO_VOID_BY_SAMPLE_STATUS_DESC = "全部样本已作废,系统自动作废协议"
|
||||
|
||||
|
||||
def _void_date_yyyymmdd(
|
||||
@@ -620,6 +623,7 @@ def clear_sample_void_for_accepted_reception(
|
||||
now = datetime.now()
|
||||
sample.sample_status = None
|
||||
sample.void_date = None
|
||||
sample.void_desc = None
|
||||
sample.update_by = user_id
|
||||
sample.update_time = now
|
||||
db.add(sample)
|
||||
@@ -688,3 +692,224 @@ def after_protocol_voided(
|
||||
db, source=source, protocol_id=protocol_id, tenant_no=tenant_no
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def _all_samples_status_voided(
|
||||
db: Session,
|
||||
*,
|
||||
source: str,
|
||||
protocol_id: str,
|
||||
tenant_no: str,
|
||||
) -> bool:
|
||||
src = (source or "").strip()
|
||||
if src == "cell_storage":
|
||||
samples = _list_cell_samples(db, protocol_id=protocol_id, tenant_no=tenant_no)
|
||||
else:
|
||||
samples = _list_donation_samples(db, donation_id=protocol_id, tenant_no=tenant_no)
|
||||
if not samples:
|
||||
return False
|
||||
for sample in samples:
|
||||
if (sample.sample_status or "").strip() != SAMPLE_VOID_STATUS:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def maybe_void_protocol_when_all_samples_status_voided(
|
||||
db: Session,
|
||||
*,
|
||||
source: str,
|
||||
protocol_id: str,
|
||||
tenant_no: str,
|
||||
user_id: Optional[str] = None,
|
||||
) -> bool:
|
||||
"""若协议下有效样本全部已作废,则自动作废协议;不联动接收结论。"""
|
||||
src = (source or "").strip()
|
||||
pid = (protocol_id or "").strip()
|
||||
tenant = (tenant_no or "").strip()
|
||||
if src not in ("cell_storage", "research_donation") or not pid or not tenant:
|
||||
return False
|
||||
if not _all_samples_status_voided(db, source=src, protocol_id=pid, tenant_no=tenant):
|
||||
return False
|
||||
|
||||
void_type = ensure_lab_void_type(db, tenant_no=tenant)
|
||||
now = datetime.now()
|
||||
|
||||
if src == "cell_storage":
|
||||
obj = (
|
||||
db.query(BizCollectionProtocol)
|
||||
.filter(
|
||||
BizCollectionProtocol.id == pid,
|
||||
BizCollectionProtocol.tenant_no == tenant,
|
||||
BizCollectionProtocol.del_flag == 0,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if not obj:
|
||||
return False
|
||||
status = (obj.protocol_status or "").strip()
|
||||
if status in INACTIVE_PROTOCOL_STATUSES:
|
||||
return False
|
||||
from app.core.protocol_status_display import assert_protocol_status_transition
|
||||
from app.crud.collection_protocol import update_collection_protocol
|
||||
|
||||
try:
|
||||
assert_protocol_status_transition(status, "yzf", kind="protocol")
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
update_collection_protocol(
|
||||
db,
|
||||
pid,
|
||||
tenant_no=tenant,
|
||||
user_id=user_id,
|
||||
protocol_status="yzf",
|
||||
void_time=now,
|
||||
void_desc=_AUTO_VOID_BY_SAMPLE_STATUS_DESC,
|
||||
void_type=void_type,
|
||||
commit=False,
|
||||
sync_reception_discard=False,
|
||||
)
|
||||
return True
|
||||
|
||||
obj = (
|
||||
db.query(BizResearchDonation)
|
||||
.filter(
|
||||
BizResearchDonation.id == pid,
|
||||
BizResearchDonation.tenant_no == tenant,
|
||||
BizResearchDonation.del_flag == 0,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if not obj:
|
||||
return False
|
||||
status = (obj.donation_status or "").strip()
|
||||
if status in INACTIVE_PROTOCOL_STATUSES:
|
||||
return False
|
||||
|
||||
from app.core.protocol_status_display import assert_protocol_status_transition
|
||||
from app.crud.dict_data import normalize_void_type
|
||||
from app.crud.research_donation import (
|
||||
_DONATION_STATUS_CHANGE_TYPE,
|
||||
append_donation_status_change,
|
||||
)
|
||||
|
||||
current = obj.donation_status or ""
|
||||
try:
|
||||
assert_protocol_status_transition(current, "yzf", kind="donation")
|
||||
except ValueError:
|
||||
return False
|
||||
normalized = normalize_void_type(db, value=void_type, tenant_no=tenant)
|
||||
obj.donation_status = "yzf"
|
||||
obj.void_time = now
|
||||
obj.void_desc = _AUTO_VOID_BY_SAMPLE_STATUS_DESC
|
||||
obj.void_type = normalized
|
||||
obj.update_by = user_id
|
||||
obj.update_time = now
|
||||
change_type = _DONATION_STATUS_CHANGE_TYPE.get("yzf")
|
||||
if change_type:
|
||||
append_donation_status_change(
|
||||
db,
|
||||
research_donation_id=obj.id,
|
||||
change_type=change_type,
|
||||
from_status=current,
|
||||
to_status="yzf",
|
||||
change_time=now,
|
||||
change_desc=_AUTO_VOID_BY_SAMPLE_STATUS_DESC,
|
||||
void_type=normalized,
|
||||
tenant_no=tenant,
|
||||
user_id=user_id,
|
||||
)
|
||||
db.add(obj)
|
||||
db.flush()
|
||||
return True
|
||||
|
||||
|
||||
def void_sample_manually(
|
||||
db: Session,
|
||||
*,
|
||||
source: str,
|
||||
tenant_no: str,
|
||||
void_desc: str,
|
||||
user_id: Optional[str] = None,
|
||||
sample_id: Optional[str] = None,
|
||||
sample_code: Optional[str] = None,
|
||||
protocol_id: Optional[str] = None,
|
||||
commit: bool = True,
|
||||
) -> Union[BizProtocolSample, BizResearchDonationSample]:
|
||||
"""人工样本废弃:写 yzf/void_date/void_desc;不改接收;全作废则自动作废协议。"""
|
||||
src = (source or "").strip()
|
||||
tenant = (tenant_no or "").strip()
|
||||
reason = (void_desc or "").strip()
|
||||
sid = (sample_id or "").strip()
|
||||
code = (sample_code or "").strip()
|
||||
pid = (protocol_id or "").strip()
|
||||
if src not in ("cell_storage", "research_donation"):
|
||||
raise ValueError("来源类型无效")
|
||||
if not tenant:
|
||||
raise ValueError("租户编号不能为空")
|
||||
if not reason:
|
||||
raise ValueError("请填写作废原因")
|
||||
if not sid and not code:
|
||||
raise ValueError("样本标识不能为空")
|
||||
|
||||
now = datetime.now()
|
||||
sample: Optional[Union[BizProtocolSample, BizResearchDonationSample]] = None
|
||||
parent_id = ""
|
||||
|
||||
if src == "cell_storage":
|
||||
q = db.query(BizProtocolSample).filter(
|
||||
BizProtocolSample.tenant_no == tenant,
|
||||
BizProtocolSample.del_flag == 0,
|
||||
)
|
||||
if sid:
|
||||
q = q.filter(BizProtocolSample.id == sid)
|
||||
else:
|
||||
q = q.filter(BizProtocolSample.sample_code == code)
|
||||
if pid:
|
||||
q = q.filter(BizProtocolSample.collection_protocol_id == pid)
|
||||
sample = q.first()
|
||||
if not sample:
|
||||
raise ValueError("样本不存在")
|
||||
parent_id = (sample.collection_protocol_id or "").strip()
|
||||
else:
|
||||
q = db.query(BizResearchDonationSample).filter(
|
||||
BizResearchDonationSample.tenant_no == tenant,
|
||||
BizResearchDonationSample.del_flag == 0,
|
||||
)
|
||||
if sid:
|
||||
q = q.filter(BizResearchDonationSample.id == sid)
|
||||
else:
|
||||
q = q.filter(BizResearchDonationSample.sample_code == code)
|
||||
if pid:
|
||||
q = q.filter(BizResearchDonationSample.research_donation_id == pid)
|
||||
sample = q.first()
|
||||
if not sample:
|
||||
raise ValueError("样本不存在")
|
||||
parent_id = (sample.research_donation_id or "").strip()
|
||||
|
||||
if _protocol_is_inactive(db, source=src, protocol_id=parent_id, tenant_no=tenant):
|
||||
raise ValueError("所属协议已作废或属地废弃,无法操作")
|
||||
|
||||
sample.sample_status = SAMPLE_VOID_STATUS
|
||||
sample.void_date = _void_date_yyyymmdd(void_time=now)
|
||||
sample.void_desc = reason
|
||||
sample.update_by = user_id
|
||||
sample.update_time = now
|
||||
db.add(sample)
|
||||
db.flush()
|
||||
|
||||
if parent_id:
|
||||
maybe_void_protocol_when_all_samples_status_voided(
|
||||
db,
|
||||
source=src,
|
||||
protocol_id=parent_id,
|
||||
tenant_no=tenant,
|
||||
user_id=user_id,
|
||||
)
|
||||
|
||||
if commit:
|
||||
db.commit()
|
||||
db.refresh(sample)
|
||||
else:
|
||||
db.flush()
|
||||
return sample
|
||||
Reference in new issue
Block a user