fix(protocol): 协议作废/属地废弃同步样本状态为已作废 (#127)
This commit was merged in pull request #128.
This commit is contained in:
commit
76ddb81818
5 files changed
+245
-10
No files matched your search
@@ -2677,7 +2677,7 @@ def update_collection_protocol(
|
||||
next_status = protocol_status.strip()
|
||||
assert_protocol_status_transition(obj.protocol_status or "", next_status, kind="protocol")
|
||||
obj.protocol_status = next_status
|
||||
if next_status == "yzf" and from_status != "yzf":
|
||||
if next_status in ("yzf", "sdfq") and from_status not in ("yzf", "sdfq"):
|
||||
voided_now = True
|
||||
if next_status == "yzf" and void_time is not None:
|
||||
if void_type is None or not str(void_type).strip():
|
||||
|
||||
@@ -1702,7 +1702,8 @@ def change_status(
|
||||
)
|
||||
|
||||
db.add(obj)
|
||||
if ns == "yzf" and (current or "").strip() != "yzf":
|
||||
from_status = (current or "").strip()
|
||||
if ns in ("yzf", "sdfq") and from_status not in ("yzf", "sdfq"):
|
||||
from app.services.protocol_reception_discard_sync import after_protocol_voided
|
||||
|
||||
after_protocol_voided(
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
"""市场协议作废 ↔ 质检样本接收废弃双向联动。
|
||||
"""市场协议无效态 ↔ 样本接收废弃 / 样本状态作废联动。
|
||||
|
||||
规则(细胞存储 / 科研捐赠):
|
||||
1. 协议变为已作废(yzf)时,协议下全部样本接收结论自动为废弃(bj)。
|
||||
1. 协议变为已作废(yzf)或属地废弃(sdfq)时:
|
||||
- 协议下全部样本接收结论自动为废弃(bj);
|
||||
- 协议下全部有效样本 `sample_status` 置为已作废(yzf),并写入 `void_date`。
|
||||
2. 协议下全部有效样本均已废弃/拒收(bj)时,协议自动已作废,作废类型为「实验室废弃」。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from typing import List, Optional
|
||||
from typing import List, Optional, Union
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.protocol_status_display import INACTIVE_PROTOCOL_STATUSES
|
||||
from app.core.utils import generate_uuid
|
||||
from app.models.models import (
|
||||
BizCollectionProtocol,
|
||||
@@ -23,11 +26,24 @@ from app.models.research_donation import BizResearchDonation, BizResearchDonatio
|
||||
LAB_VOID_TYPE_CODE = "sysfq"
|
||||
LAB_VOID_TYPE_LABEL = "实验室废弃"
|
||||
VOID_TYPE_DICT = "xyzflx"
|
||||
SAMPLE_VOID_STATUS = "yzf"
|
||||
|
||||
_AUTO_DISCARD_REMARK = "协议作废自动废弃"
|
||||
_AUTO_VOID_DESC = "全部样本接收废弃/拒收,系统自动作废"
|
||||
|
||||
|
||||
def _void_date_yyyymmdd(
|
||||
*,
|
||||
void_time: Optional[datetime] = None,
|
||||
void_date: Optional[str] = None,
|
||||
) -> str:
|
||||
raw = (void_date or "").strip()
|
||||
if len(raw) == 8 and raw.isdigit():
|
||||
return raw
|
||||
ts = void_time or datetime.now()
|
||||
return ts.strftime("%Y%m%d")
|
||||
|
||||
|
||||
def ensure_lab_void_type(db: Session, *, tenant_no: str) -> str:
|
||||
"""确保租户字典 xyzflx 存在「实验室废弃」,返回 dict_value。"""
|
||||
tenant = (tenant_no or "").strip()
|
||||
@@ -204,7 +220,7 @@ def discard_receptions_for_voided_protocol(
|
||||
tenant_no: str,
|
||||
user_id: Optional[str] = None,
|
||||
) -> int:
|
||||
"""协议已作废后,将其下样本接收全部置为废弃。返回处理样本数。"""
|
||||
"""协议无效后,将其下样本接收全部置为废弃。返回处理样本数。"""
|
||||
src = (source or "").strip()
|
||||
pid = (protocol_id or "").strip()
|
||||
tenant = (tenant_no or "").strip()
|
||||
@@ -242,6 +258,75 @@ def discard_receptions_for_voided_protocol(
|
||||
return len(samples)
|
||||
|
||||
|
||||
def void_samples_for_inactive_protocol(
|
||||
db: Session,
|
||||
*,
|
||||
source: str,
|
||||
protocol_id: str,
|
||||
tenant_no: str,
|
||||
user_id: Optional[str] = None,
|
||||
void_time: Optional[datetime] = None,
|
||||
void_date: Optional[str] = None,
|
||||
) -> int:
|
||||
"""协议无效后,将其下有效样本状态一律置为已作废并写入 void_date。返回处理样本数。"""
|
||||
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 0
|
||||
|
||||
date_s = _void_date_yyyymmdd(void_time=void_time, void_date=void_date)
|
||||
now = datetime.now()
|
||||
if src == "cell_storage":
|
||||
samples: List[Union[BizProtocolSample, BizResearchDonationSample]] = list(
|
||||
_list_cell_samples(db, protocol_id=pid, tenant_no=tenant)
|
||||
)
|
||||
else:
|
||||
samples = list(_list_donation_samples(db, donation_id=pid, tenant_no=tenant))
|
||||
|
||||
for sample in samples:
|
||||
sample.sample_status = SAMPLE_VOID_STATUS
|
||||
sample.void_date = date_s
|
||||
sample.update_by = user_id
|
||||
sample.update_time = now
|
||||
db.add(sample)
|
||||
db.flush()
|
||||
return len(samples)
|
||||
|
||||
|
||||
def _resolve_protocol_void_time(
|
||||
db: Session,
|
||||
*,
|
||||
source: str,
|
||||
protocol_id: str,
|
||||
tenant_no: str,
|
||||
) -> Optional[datetime]:
|
||||
src = (source or "").strip()
|
||||
pid = (protocol_id or "").strip()
|
||||
tenant = (tenant_no or "").strip()
|
||||
if src == "cell_storage":
|
||||
obj = (
|
||||
db.query(BizCollectionProtocol)
|
||||
.filter(
|
||||
BizCollectionProtocol.id == pid,
|
||||
BizCollectionProtocol.tenant_no == tenant,
|
||||
BizCollectionProtocol.del_flag == 0,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
return obj.void_time if obj else None
|
||||
obj = (
|
||||
db.query(BizResearchDonation)
|
||||
.filter(
|
||||
BizResearchDonation.id == pid,
|
||||
BizResearchDonation.tenant_no == tenant,
|
||||
BizResearchDonation.del_flag == 0,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
return obj.void_time if obj else None
|
||||
|
||||
|
||||
def _all_samples_discarded(
|
||||
db: Session,
|
||||
*,
|
||||
@@ -299,7 +384,7 @@ def maybe_void_protocol_when_all_samples_discarded(
|
||||
if not obj:
|
||||
return False
|
||||
status = (obj.protocol_status or "").strip()
|
||||
if status in ("yzf", "sdfq"):
|
||||
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
|
||||
@@ -319,7 +404,8 @@ def maybe_void_protocol_when_all_samples_discarded(
|
||||
void_desc=_AUTO_VOID_DESC,
|
||||
void_type=void_type,
|
||||
commit=False,
|
||||
sync_reception_discard=False,
|
||||
# 接收已全部废弃;仍需同步样本状态为已作废
|
||||
sync_reception_discard=True,
|
||||
)
|
||||
return True
|
||||
|
||||
@@ -335,7 +421,7 @@ def maybe_void_protocol_when_all_samples_discarded(
|
||||
if not obj:
|
||||
return False
|
||||
status = (obj.donation_status or "").strip()
|
||||
if status in ("yzf", "sdfq"):
|
||||
if status in INACTIVE_PROTOCOL_STATUSES:
|
||||
return False
|
||||
|
||||
# 捐赠 change_status 默认 commit;此处内联等价逻辑避免提前提交。
|
||||
@@ -374,6 +460,14 @@ def maybe_void_protocol_when_all_samples_discarded(
|
||||
)
|
||||
db.add(obj)
|
||||
db.flush()
|
||||
# 接收已全部废弃;仍需同步样本状态为已作废
|
||||
after_protocol_voided(
|
||||
db,
|
||||
source="research_donation",
|
||||
protocol_id=pid,
|
||||
tenant_no=tenant,
|
||||
user_id=user_id,
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
@@ -411,7 +505,7 @@ def after_protocol_voided(
|
||||
tenant_no: str,
|
||||
user_id: Optional[str] = None,
|
||||
) -> None:
|
||||
"""协议作废后:同步废弃其下全部样本接收。"""
|
||||
"""协议作废/属地废弃后:同步废弃样本接收,并将样本状态置为已作废。"""
|
||||
discard_receptions_for_voided_protocol(
|
||||
db,
|
||||
source=source,
|
||||
@@ -419,3 +513,13 @@ def after_protocol_voided(
|
||||
tenant_no=tenant_no,
|
||||
user_id=user_id,
|
||||
)
|
||||
void_samples_for_inactive_protocol(
|
||||
db,
|
||||
source=source,
|
||||
protocol_id=protocol_id,
|
||||
tenant_no=tenant_no,
|
||||
user_id=user_id,
|
||||
void_time=_resolve_protocol_void_time(
|
||||
db, source=source, protocol_id=protocol_id, tenant_no=tenant_no
|
||||
),
|
||||
)
|
||||
@@ -23,6 +23,7 @@ from app.models.research_donation import (
|
||||
)
|
||||
from app.services.protocol_reception_discard_sync import (
|
||||
LAB_VOID_TYPE_CODE,
|
||||
SAMPLE_VOID_STATUS,
|
||||
after_protocol_voided,
|
||||
after_sample_reception_saved,
|
||||
ensure_lab_void_type,
|
||||
@@ -77,6 +78,7 @@ class TestProtocolReceptionDiscardSync(unittest.TestCase):
|
||||
id="p1",
|
||||
protocol_no="CP-1",
|
||||
protocol_status="sxz",
|
||||
void_time=now,
|
||||
tenant_no="tenant-a",
|
||||
create_dept_id="",
|
||||
create_time=now,
|
||||
@@ -89,6 +91,7 @@ class TestProtocolReceptionDiscardSync(unittest.TestCase):
|
||||
id="s1",
|
||||
collection_protocol_id="p1",
|
||||
sample_code="ZK001",
|
||||
sample_status="yrk",
|
||||
tenant_no="tenant-a",
|
||||
create_dept_id="",
|
||||
create_time=now,
|
||||
@@ -101,6 +104,7 @@ class TestProtocolReceptionDiscardSync(unittest.TestCase):
|
||||
id="s2",
|
||||
collection_protocol_id="p1",
|
||||
sample_code="ZK002",
|
||||
sample_status="jcz",
|
||||
tenant_no="tenant-a",
|
||||
create_dept_id="",
|
||||
create_time=now,
|
||||
@@ -152,6 +156,16 @@ class TestProtocolReceptionDiscardSync(unittest.TestCase):
|
||||
self.assertEqual(by_code["ZK001"], "bj")
|
||||
self.assertEqual(by_code["ZK002"], "bj")
|
||||
|
||||
samples = (
|
||||
db.query(BizProtocolSample)
|
||||
.filter(BizProtocolSample.collection_protocol_id == "p1")
|
||||
.all()
|
||||
)
|
||||
self.assertEqual(len(samples), 2)
|
||||
for s in samples:
|
||||
self.assertEqual(s.sample_status, SAMPLE_VOID_STATUS)
|
||||
self.assertEqual(s.void_date, "20261105")
|
||||
|
||||
def test_all_samples_discarded_auto_voids_protocol_with_lab_type(self):
|
||||
db = _session()
|
||||
now = _now()
|
||||
@@ -172,6 +186,7 @@ class TestProtocolReceptionDiscardSync(unittest.TestCase):
|
||||
id="s3",
|
||||
collection_protocol_id="p2",
|
||||
sample_code="ZK003",
|
||||
sample_status="ycj",
|
||||
tenant_no="tenant-a",
|
||||
create_dept_id="",
|
||||
create_time=now,
|
||||
@@ -212,6 +227,9 @@ class TestProtocolReceptionDiscardSync(unittest.TestCase):
|
||||
self.assertEqual(proto.protocol_status, "yzf")
|
||||
self.assertEqual(proto.void_type, LAB_VOID_TYPE_CODE)
|
||||
self.assertIn("全部样本接收", proto.void_desc or "")
|
||||
sample = db.query(BizProtocolSample).filter(BizProtocolSample.id == "s3").one()
|
||||
self.assertEqual(sample.sample_status, SAMPLE_VOID_STATUS)
|
||||
self.assertTrue((sample.void_date or "").strip())
|
||||
|
||||
def test_partial_discard_does_not_void_protocol(self):
|
||||
db = _session()
|
||||
@@ -282,6 +300,7 @@ class TestProtocolReceptionDiscardSync(unittest.TestCase):
|
||||
id="d1",
|
||||
donation_no="RD-1",
|
||||
donation_status="sxz",
|
||||
void_time=now,
|
||||
tenant_no="tenant-a",
|
||||
create_dept_id="",
|
||||
create_time=now,
|
||||
@@ -294,6 +313,7 @@ class TestProtocolReceptionDiscardSync(unittest.TestCase):
|
||||
id="ds1",
|
||||
research_donation_id="d1",
|
||||
sample_code="RD001",
|
||||
sample_status="yrk",
|
||||
tenant_no="tenant-a",
|
||||
create_dept_id="",
|
||||
create_time=now,
|
||||
@@ -322,6 +342,62 @@ class TestProtocolReceptionDiscardSync(unittest.TestCase):
|
||||
)
|
||||
self.assertEqual(rec.accept_conclusion, "bj")
|
||||
self.assertEqual(rec.research_donation_id, "d1")
|
||||
sample = (
|
||||
db.query(BizResearchDonationSample)
|
||||
.filter(BizResearchDonationSample.id == "ds1")
|
||||
.one()
|
||||
)
|
||||
self.assertEqual(sample.sample_status, SAMPLE_VOID_STATUS)
|
||||
self.assertEqual(sample.void_date, "20261105")
|
||||
|
||||
def test_sdfq_also_voids_sample_status(self):
|
||||
db = _session()
|
||||
now = _now()
|
||||
db.add(
|
||||
BizCollectionProtocol(
|
||||
id="p-sdfq",
|
||||
protocol_no="CP-SDFQ",
|
||||
protocol_status="sdfq",
|
||||
tenant_no="tenant-a",
|
||||
create_dept_id="",
|
||||
create_time=now,
|
||||
update_time=now,
|
||||
del_flag=0,
|
||||
)
|
||||
)
|
||||
db.add(
|
||||
BizProtocolSample(
|
||||
id="s-sdfq",
|
||||
collection_protocol_id="p-sdfq",
|
||||
sample_code="ZKSDFQ",
|
||||
sample_status="ycj",
|
||||
tenant_no="tenant-a",
|
||||
create_dept_id="",
|
||||
create_time=now,
|
||||
update_time=now,
|
||||
del_flag=0,
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
|
||||
after_protocol_voided(
|
||||
db,
|
||||
source="cell_storage",
|
||||
protocol_id="p-sdfq",
|
||||
tenant_no="tenant-a",
|
||||
user_id="u1",
|
||||
)
|
||||
db.commit()
|
||||
|
||||
sample = db.query(BizProtocolSample).filter(BizProtocolSample.id == "s-sdfq").one()
|
||||
self.assertEqual(sample.sample_status, SAMPLE_VOID_STATUS)
|
||||
self.assertTrue((sample.void_date or "").strip())
|
||||
rec = (
|
||||
db.query(BizSampleReception)
|
||||
.filter(BizSampleReception.sample_code == "ZKSDFQ")
|
||||
.one()
|
||||
)
|
||||
self.assertEqual(rec.accept_conclusion, "bj")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
-- Issue #127:已作废 / 属地废弃协议下样本状态回刷为已作废
|
||||
-- 细胞存储 + 科研捐赠;可重复执行。
|
||||
-- 规则:协议 status 为 yzf/sdfq 且样本未删除时,sample_status 置 yzf,
|
||||
-- void_date 为空时按协议 void_time / update_time 补齐 YYYYMMDD。
|
||||
|
||||
-- 细胞存储协议样本
|
||||
UPDATE biz_protocol_sample s
|
||||
INNER JOIN biz_collection_protocol p
|
||||
ON p.id = s.collection_protocol_id
|
||||
AND p.tenant_no = s.tenant_no
|
||||
AND p.del_flag = 0
|
||||
SET
|
||||
s.sample_status = 'yzf',
|
||||
s.void_date = CASE
|
||||
WHEN s.void_date IS NOT NULL AND TRIM(s.void_date) <> '' THEN s.void_date
|
||||
WHEN p.void_time IS NOT NULL THEN DATE_FORMAT(p.void_time, '%Y%m%d')
|
||||
WHEN p.update_time IS NOT NULL THEN DATE_FORMAT(p.update_time, '%Y%m%d')
|
||||
ELSE DATE_FORMAT(NOW(), '%Y%m%d')
|
||||
END,
|
||||
s.update_time = NOW()
|
||||
WHERE s.del_flag = 0
|
||||
AND p.protocol_status IN ('yzf', 'sdfq')
|
||||
AND (
|
||||
s.sample_status IS NULL
|
||||
OR TRIM(s.sample_status) = ''
|
||||
OR s.sample_status <> 'yzf'
|
||||
OR s.void_date IS NULL
|
||||
OR TRIM(s.void_date) = ''
|
||||
);
|
||||
|
||||
-- 科研捐赠样本
|
||||
UPDATE biz_research_donation_sample s
|
||||
INNER JOIN biz_research_donation d
|
||||
ON d.id = s.research_donation_id
|
||||
AND d.tenant_no = s.tenant_no
|
||||
AND d.del_flag = 0
|
||||
SET
|
||||
s.sample_status = 'yzf',
|
||||
s.void_date = CASE
|
||||
WHEN s.void_date IS NOT NULL AND TRIM(s.void_date) <> '' THEN s.void_date
|
||||
WHEN d.void_time IS NOT NULL THEN DATE_FORMAT(d.void_time, '%Y%m%d')
|
||||
WHEN d.update_time IS NOT NULL THEN DATE_FORMAT(d.update_time, '%Y%m%d')
|
||||
ELSE DATE_FORMAT(NOW(), '%Y%m%d')
|
||||
END,
|
||||
s.update_time = NOW()
|
||||
WHERE s.del_flag = 0
|
||||
AND d.donation_status IN ('yzf', 'sdfq')
|
||||
AND (
|
||||
s.sample_status IS NULL
|
||||
OR TRIM(s.sample_status) = ''
|
||||
OR s.sample_status <> 'yzf'
|
||||
OR s.void_date IS NULL
|
||||
OR TRIM(s.void_date) = ''
|
||||
);
|
||||
Reference in new issue
Block a user