Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0346bc0b1d | ||
|
|
76ddb81818 |
No files matched your search
@@ -1045,7 +1045,6 @@ def create_sample_reception(
|
|||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
create_dept_id=create_dept_id or "",
|
create_dept_id=create_dept_id or "",
|
||||||
)
|
)
|
||||||
if conclusion == "bj":
|
|
||||||
from app.services.protocol_reception_discard_sync import after_sample_reception_saved
|
from app.services.protocol_reception_discard_sync import after_sample_reception_saved
|
||||||
|
|
||||||
after_sample_reception_saved(
|
after_sample_reception_saved(
|
||||||
@@ -1169,7 +1168,6 @@ def update_sample_reception(
|
|||||||
)
|
)
|
||||||
check_result_svc.sync_legacy_columns_from_results(db, rec, saved_rows)
|
check_result_svc.sync_legacy_columns_from_results(db, rec, saved_rows)
|
||||||
db.add(rec)
|
db.add(rec)
|
||||||
if conclusion == "bj":
|
|
||||||
from app.services.protocol_reception_discard_sync import after_sample_reception_saved
|
from app.services.protocol_reception_discard_sync import after_sample_reception_saved
|
||||||
|
|
||||||
after_sample_reception_saved(
|
after_sample_reception_saved(
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
"""市场协议无效态 ↔ 样本接收废弃 / 样本状态作废联动。
|
"""市场协议无效态 ↔ 样本接收废弃 / 样本状态作废联动。
|
||||||
|
|
||||||
规则(细胞存储 / 科研捐赠):
|
规则(细胞存储 / 科研捐赠):
|
||||||
1. 协议变为已作废(yzf)或属地废弃(sdfq)时:
|
1. 单条样本接收结论为废弃(bj)时:仅该样本 `sample_status` 置为已作废(yzf)并写 `void_date`;
|
||||||
|
协议状态保持不变。接收结论改回接收(js)且协议仍有效时:若该样本为 yzf,则清空状态与 void_date。
|
||||||
|
2. 协议变为已作废(yzf)或属地废弃(sdfq)时:
|
||||||
- 协议下全部样本接收结论自动为废弃(bj);
|
- 协议下全部样本接收结论自动为废弃(bj);
|
||||||
- 协议下全部有效样本 `sample_status` 置为已作废(yzf),并写入 `void_date`。
|
- 协议下全部有效样本 `sample_status` 置为已作废(yzf),并写入 `void_date`。
|
||||||
2. 协议下全部有效样本均已废弃/拒收(bj)时,协议自动已作废,作废类型为「实验室废弃」。
|
3. 协议下全部有效样本均已废弃/拒收(bj)时,协议自动已作废,作废类型为「实验室废弃」。
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
@@ -471,6 +473,160 @@ def maybe_void_protocol_when_all_samples_discarded(
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def _find_sample_for_reception(
|
||||||
|
db: Session,
|
||||||
|
*,
|
||||||
|
reception: BizSampleReception,
|
||||||
|
tenant_no: str,
|
||||||
|
) -> tuple[str, Optional[Union[BizProtocolSample, BizResearchDonationSample]]]:
|
||||||
|
source = (reception.reception_source or "cell_storage").strip() or "cell_storage"
|
||||||
|
tenant = (tenant_no or "").strip()
|
||||||
|
code = (reception.sample_code or "").strip()
|
||||||
|
if source == "cell_storage":
|
||||||
|
sid = (reception.protocol_sample_id or "").strip()
|
||||||
|
if sid:
|
||||||
|
obj = (
|
||||||
|
db.query(BizProtocolSample)
|
||||||
|
.filter(
|
||||||
|
BizProtocolSample.id == sid,
|
||||||
|
BizProtocolSample.tenant_no == tenant,
|
||||||
|
BizProtocolSample.del_flag == 0,
|
||||||
|
)
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
if obj:
|
||||||
|
return source, obj
|
||||||
|
pid = (reception.collection_protocol_id or "").strip()
|
||||||
|
if code and pid:
|
||||||
|
return source, (
|
||||||
|
db.query(BizProtocolSample)
|
||||||
|
.filter(
|
||||||
|
BizProtocolSample.collection_protocol_id == pid,
|
||||||
|
BizProtocolSample.sample_code == code,
|
||||||
|
BizProtocolSample.tenant_no == tenant,
|
||||||
|
BizProtocolSample.del_flag == 0,
|
||||||
|
)
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
return source, None
|
||||||
|
|
||||||
|
sid = (reception.donation_sample_id or "").strip()
|
||||||
|
if sid:
|
||||||
|
obj = (
|
||||||
|
db.query(BizResearchDonationSample)
|
||||||
|
.filter(
|
||||||
|
BizResearchDonationSample.id == sid,
|
||||||
|
BizResearchDonationSample.tenant_no == tenant,
|
||||||
|
BizResearchDonationSample.del_flag == 0,
|
||||||
|
)
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
if obj:
|
||||||
|
return source, obj
|
||||||
|
pid = (reception.research_donation_id or "").strip()
|
||||||
|
if code and pid:
|
||||||
|
return source, (
|
||||||
|
db.query(BizResearchDonationSample)
|
||||||
|
.filter(
|
||||||
|
BizResearchDonationSample.research_donation_id == pid,
|
||||||
|
BizResearchDonationSample.sample_code == code,
|
||||||
|
BizResearchDonationSample.tenant_no == tenant,
|
||||||
|
BizResearchDonationSample.del_flag == 0,
|
||||||
|
)
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
return source, None
|
||||||
|
|
||||||
|
|
||||||
|
def _protocol_is_inactive(
|
||||||
|
db: Session,
|
||||||
|
*,
|
||||||
|
source: str,
|
||||||
|
protocol_id: str,
|
||||||
|
tenant_no: str,
|
||||||
|
) -> bool:
|
||||||
|
src = (source or "").strip()
|
||||||
|
pid = (protocol_id or "").strip()
|
||||||
|
tenant = (tenant_no or "").strip()
|
||||||
|
if not pid or not tenant:
|
||||||
|
return False
|
||||||
|
if src == "cell_storage":
|
||||||
|
obj = (
|
||||||
|
db.query(BizCollectionProtocol)
|
||||||
|
.filter(
|
||||||
|
BizCollectionProtocol.id == pid,
|
||||||
|
BizCollectionProtocol.tenant_no == tenant,
|
||||||
|
BizCollectionProtocol.del_flag == 0,
|
||||||
|
)
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
status = (obj.protocol_status or "").strip() if obj else ""
|
||||||
|
else:
|
||||||
|
obj = (
|
||||||
|
db.query(BizResearchDonation)
|
||||||
|
.filter(
|
||||||
|
BizResearchDonation.id == pid,
|
||||||
|
BizResearchDonation.tenant_no == tenant,
|
||||||
|
BizResearchDonation.del_flag == 0,
|
||||||
|
)
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
status = (obj.donation_status or "").strip() if obj else ""
|
||||||
|
return status in INACTIVE_PROTOCOL_STATUSES
|
||||||
|
|
||||||
|
|
||||||
|
def void_sample_for_discarded_reception(
|
||||||
|
db: Session,
|
||||||
|
*,
|
||||||
|
reception: BizSampleReception,
|
||||||
|
tenant_no: str,
|
||||||
|
user_id: Optional[str] = None,
|
||||||
|
) -> bool:
|
||||||
|
"""单条接收废弃:仅将该样本置为已作废。"""
|
||||||
|
_source, sample = _find_sample_for_reception(db, reception=reception, tenant_no=tenant_no)
|
||||||
|
if not sample:
|
||||||
|
return False
|
||||||
|
now = datetime.now()
|
||||||
|
sample.sample_status = SAMPLE_VOID_STATUS
|
||||||
|
sample.void_date = _void_date_yyyymmdd(void_time=reception.reception_time or now)
|
||||||
|
sample.update_by = user_id
|
||||||
|
sample.update_time = now
|
||||||
|
db.add(sample)
|
||||||
|
db.flush()
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def clear_sample_void_for_accepted_reception(
|
||||||
|
db: Session,
|
||||||
|
*,
|
||||||
|
reception: BizSampleReception,
|
||||||
|
tenant_no: str,
|
||||||
|
user_id: Optional[str] = None,
|
||||||
|
) -> bool:
|
||||||
|
"""接收改回接收:协议仍有效且样本为 yzf 时,清空样本作废状态。"""
|
||||||
|
source, sample = _find_sample_for_reception(db, reception=reception, tenant_no=tenant_no)
|
||||||
|
if not sample:
|
||||||
|
return False
|
||||||
|
if (sample.sample_status or "").strip() != SAMPLE_VOID_STATUS:
|
||||||
|
return False
|
||||||
|
if source == "cell_storage":
|
||||||
|
protocol_id = (reception.collection_protocol_id or "").strip()
|
||||||
|
else:
|
||||||
|
protocol_id = (reception.research_donation_id or "").strip()
|
||||||
|
if _protocol_is_inactive(
|
||||||
|
db, source=source, protocol_id=protocol_id, tenant_no=tenant_no
|
||||||
|
):
|
||||||
|
return False
|
||||||
|
now = datetime.now()
|
||||||
|
sample.sample_status = None
|
||||||
|
sample.void_date = None
|
||||||
|
sample.update_by = user_id
|
||||||
|
sample.update_time = now
|
||||||
|
db.add(sample)
|
||||||
|
db.flush()
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def after_sample_reception_saved(
|
def after_sample_reception_saved(
|
||||||
db: Session,
|
db: Session,
|
||||||
*,
|
*,
|
||||||
@@ -478,16 +634,19 @@ def after_sample_reception_saved(
|
|||||||
tenant_no: str,
|
tenant_no: str,
|
||||||
user_id: Optional[str] = None,
|
user_id: Optional[str] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""样本接收保存后:若结论为废弃,尝试自动作废所属协议。"""
|
"""样本接收保存后:同步该样本状态;若全部废弃则尝试自动作废协议。"""
|
||||||
if (reception.accept_conclusion or "").strip() != "bj":
|
conclusion = (reception.accept_conclusion or "").strip()
|
||||||
return
|
|
||||||
source = (reception.reception_source or "cell_storage").strip() or "cell_storage"
|
source = (reception.reception_source or "cell_storage").strip() or "cell_storage"
|
||||||
if source == "cell_storage":
|
if source == "cell_storage":
|
||||||
protocol_id = (reception.collection_protocol_id or "").strip()
|
protocol_id = (reception.collection_protocol_id or "").strip()
|
||||||
else:
|
else:
|
||||||
protocol_id = (reception.research_donation_id or "").strip()
|
protocol_id = (reception.research_donation_id or "").strip()
|
||||||
if not protocol_id:
|
|
||||||
return
|
if conclusion == "bj":
|
||||||
|
void_sample_for_discarded_reception(
|
||||||
|
db, reception=reception, tenant_no=tenant_no, user_id=user_id
|
||||||
|
)
|
||||||
|
if protocol_id:
|
||||||
maybe_void_protocol_when_all_samples_discarded(
|
maybe_void_protocol_when_all_samples_discarded(
|
||||||
db,
|
db,
|
||||||
source=source,
|
source=source,
|
||||||
@@ -495,6 +654,12 @@ def after_sample_reception_saved(
|
|||||||
tenant_no=tenant_no,
|
tenant_no=tenant_no,
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
)
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
if conclusion == "js":
|
||||||
|
clear_sample_void_for_accepted_reception(
|
||||||
|
db, reception=reception, tenant_no=tenant_no, user_id=user_id
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def after_protocol_voided(
|
def after_protocol_voided(
|
||||||
|
|||||||
@@ -399,6 +399,143 @@ class TestProtocolReceptionDiscardSync(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
self.assertEqual(rec.accept_conclusion, "bj")
|
self.assertEqual(rec.accept_conclusion, "bj")
|
||||||
|
|
||||||
|
def test_single_reception_discard_voids_only_that_sample(self):
|
||||||
|
db = _session()
|
||||||
|
now = _now()
|
||||||
|
db.add(
|
||||||
|
BizCollectionProtocol(
|
||||||
|
id="p-multi",
|
||||||
|
protocol_no="CP-MULTI",
|
||||||
|
protocol_status="sxz",
|
||||||
|
tenant_no="tenant-a",
|
||||||
|
create_dept_id="",
|
||||||
|
create_time=now,
|
||||||
|
update_time=now,
|
||||||
|
del_flag=0,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
for sid, code, status in (
|
||||||
|
("sa", "ZKA", "ycj"),
|
||||||
|
("sb", "ZKB", "ycj"),
|
||||||
|
("sc", "ZKC", "ycj"),
|
||||||
|
):
|
||||||
|
db.add(
|
||||||
|
BizProtocolSample(
|
||||||
|
id=sid,
|
||||||
|
collection_protocol_id="p-multi",
|
||||||
|
sample_code=code,
|
||||||
|
sample_status=status,
|
||||||
|
tenant_no="tenant-a",
|
||||||
|
create_dept_id="",
|
||||||
|
create_time=now,
|
||||||
|
update_time=now,
|
||||||
|
del_flag=0,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
rec_a = BizSampleReception(
|
||||||
|
id="ra",
|
||||||
|
protocol_sample_id="sa",
|
||||||
|
collection_protocol_id="p-multi",
|
||||||
|
reception_time=now,
|
||||||
|
accept_conclusion="bj",
|
||||||
|
reception_source="cell_storage",
|
||||||
|
sample_code="ZKA",
|
||||||
|
tenant_no="tenant-a",
|
||||||
|
create_dept_id="",
|
||||||
|
create_time=now,
|
||||||
|
update_time=now,
|
||||||
|
del_flag=0,
|
||||||
|
check_packaging=0,
|
||||||
|
check_sealing=0,
|
||||||
|
check_no_damage=0,
|
||||||
|
check_no_leakage=0,
|
||||||
|
check_color=0,
|
||||||
|
check_tying_rope=0,
|
||||||
|
check_no_stratification=0,
|
||||||
|
)
|
||||||
|
db.add(rec_a)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
after_sample_reception_saved(
|
||||||
|
db, reception=rec_a, tenant_no="tenant-a", user_id="u1"
|
||||||
|
)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
proto = db.query(BizCollectionProtocol).filter(BizCollectionProtocol.id == "p-multi").one()
|
||||||
|
self.assertEqual(proto.protocol_status, "sxz")
|
||||||
|
by_id = {
|
||||||
|
s.id: s
|
||||||
|
for s in db.query(BizProtocolSample)
|
||||||
|
.filter(BizProtocolSample.collection_protocol_id == "p-multi")
|
||||||
|
.all()
|
||||||
|
}
|
||||||
|
self.assertEqual(by_id["sa"].sample_status, SAMPLE_VOID_STATUS)
|
||||||
|
self.assertEqual(by_id["sa"].void_date, "20261105")
|
||||||
|
self.assertEqual(by_id["sb"].sample_status, "ycj")
|
||||||
|
self.assertEqual(by_id["sc"].sample_status, "ycj")
|
||||||
|
|
||||||
|
def test_reception_accept_clears_sample_void_when_protocol_active(self):
|
||||||
|
db = _session()
|
||||||
|
now = _now()
|
||||||
|
db.add(
|
||||||
|
BizCollectionProtocol(
|
||||||
|
id="p-clear",
|
||||||
|
protocol_no="CP-CLEAR",
|
||||||
|
protocol_status="sxz",
|
||||||
|
tenant_no="tenant-a",
|
||||||
|
create_dept_id="",
|
||||||
|
create_time=now,
|
||||||
|
update_time=now,
|
||||||
|
del_flag=0,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
db.add(
|
||||||
|
BizProtocolSample(
|
||||||
|
id="s-clear",
|
||||||
|
collection_protocol_id="p-clear",
|
||||||
|
sample_code="ZKCLR",
|
||||||
|
sample_status=SAMPLE_VOID_STATUS,
|
||||||
|
void_date="20261101",
|
||||||
|
tenant_no="tenant-a",
|
||||||
|
create_dept_id="",
|
||||||
|
create_time=now,
|
||||||
|
update_time=now,
|
||||||
|
del_flag=0,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
rec = BizSampleReception(
|
||||||
|
id="r-clear",
|
||||||
|
protocol_sample_id="s-clear",
|
||||||
|
collection_protocol_id="p-clear",
|
||||||
|
reception_time=now,
|
||||||
|
accept_conclusion="js",
|
||||||
|
reception_source="cell_storage",
|
||||||
|
sample_code="ZKCLR",
|
||||||
|
tenant_no="tenant-a",
|
||||||
|
create_dept_id="",
|
||||||
|
create_time=now,
|
||||||
|
update_time=now,
|
||||||
|
del_flag=0,
|
||||||
|
check_packaging=0,
|
||||||
|
check_sealing=0,
|
||||||
|
check_no_damage=0,
|
||||||
|
check_no_leakage=0,
|
||||||
|
check_color=0,
|
||||||
|
check_tying_rope=0,
|
||||||
|
check_no_stratification=0,
|
||||||
|
)
|
||||||
|
db.add(rec)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
after_sample_reception_saved(
|
||||||
|
db, reception=rec, tenant_no="tenant-a", user_id="u1"
|
||||||
|
)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
sample = db.query(BizProtocolSample).filter(BizProtocolSample.id == "s-clear").one()
|
||||||
|
self.assertFalse((sample.sample_status or "").strip())
|
||||||
|
self.assertFalse((sample.void_date or "").strip())
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
-- Issue #129:样本接收结论已为废弃(bj)、但样本状态尚未作废的历史回刷
|
||||||
|
-- 细胞存储 / 科研捐赠;可重复执行。
|
||||||
|
-- 仅处理协议仍有效(非 yzf/sdfq)的样本行;已作废协议样本由 #127 回刷脚本覆盖。
|
||||||
|
|
||||||
|
-- 细胞存储
|
||||||
|
UPDATE biz_protocol_sample s
|
||||||
|
INNER JOIN biz_sample_reception r
|
||||||
|
ON r.tenant_no = s.tenant_no
|
||||||
|
AND r.del_flag = 0
|
||||||
|
AND r.reception_source = 'cell_storage'
|
||||||
|
AND r.accept_conclusion = 'bj'
|
||||||
|
AND r.sample_code = s.sample_code
|
||||||
|
AND r.collection_protocol_id = s.collection_protocol_id
|
||||||
|
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 r.reception_time IS NOT NULL THEN DATE_FORMAT(r.reception_time, '%Y%m%d')
|
||||||
|
WHEN s.update_time IS NOT NULL THEN DATE_FORMAT(s.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 NOT 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_sample_reception r
|
||||||
|
ON r.tenant_no = s.tenant_no
|
||||||
|
AND r.del_flag = 0
|
||||||
|
AND r.reception_source = 'research_donation'
|
||||||
|
AND r.accept_conclusion = 'bj'
|
||||||
|
AND r.sample_code = s.sample_code
|
||||||
|
AND r.research_donation_id = s.research_donation_id
|
||||||
|
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 r.reception_time IS NOT NULL THEN DATE_FORMAT(r.reception_time, '%Y%m%d')
|
||||||
|
WHEN s.update_time IS NOT NULL THEN DATE_FORMAT(s.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 NOT 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