153 lines
4.4 KiB
Python
153 lines
4.4 KiB
Python
"""返利班结果:查号 / 录协议 → 表 4 更新与插入、表 3 状态。"""
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field
|
|
|
|
KIND_OK = "ok"
|
|
KIND_FAIL = "fail"
|
|
STAMP_CONTRACT = "合同班"
|
|
STAMP_CLAIMING = "查号中"
|
|
STAMP_DONE = "返利班"
|
|
PROCESS_RESULT_MAX = 1000
|
|
FAIL_REASON_MAX = 500
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class AgreementOutcome:
|
|
kind: str
|
|
agreement_id: str | None
|
|
agreement_dtl_id: str | None
|
|
reason: str | None
|
|
|
|
|
|
@dataclass
|
|
class RebateGroup:
|
|
contract_no: str
|
|
drafts: list[dict] = field(default_factory=list)
|
|
protocols: list[dict] = field(default_factory=list)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class GroupSave:
|
|
table4_updates: list[dict]
|
|
table4_inserts: list[dict]
|
|
table4_release_fids: list[int]
|
|
table3_updates: list[dict]
|
|
|
|
|
|
def clip(text: str, max_len: int) -> str:
|
|
text = (text or "").strip()
|
|
if len(text) <= max_len:
|
|
return text
|
|
return text[: max_len - 1] + "…"
|
|
|
|
|
|
def agreement_ok(outcome: AgreementOutcome) -> bool:
|
|
return outcome.kind == KIND_OK and bool((outcome.agreement_id or "").strip())
|
|
|
|
|
|
def build_not_found(group: RebateGroup) -> GroupSave:
|
|
return GroupSave(
|
|
table4_updates=[],
|
|
table4_inserts=[],
|
|
table4_release_fids=[int(row["fid"]) for row in group.drafts],
|
|
table3_updates=[
|
|
{"fid": int(row["fid"]), "process_status": 0, "process_result": ""}
|
|
for row in group.protocols
|
|
],
|
|
)
|
|
|
|
|
|
def build_found(group: RebateGroup, outcome: AgreementOutcome) -> GroupSave:
|
|
ok = agreement_ok(outcome)
|
|
number = (group.contract_no or "").strip()
|
|
updates = [
|
|
{
|
|
"fid": int(row["fid"]),
|
|
"contract_status": 2,
|
|
"update_user_name": STAMP_DONE,
|
|
}
|
|
for row in group.drafts
|
|
]
|
|
inserts: list[dict] = []
|
|
seen_protocol: dict[int, dict] = {}
|
|
for row in group.protocols:
|
|
seen_protocol.setdefault(int(row["oms_id"]), row)
|
|
for row in seen_protocol.values():
|
|
inserts.append(_action1_success(row, number))
|
|
seen_oms: dict[int, dict] = {}
|
|
for row in group.drafts + group.protocols:
|
|
seen_oms.setdefault(int(row["oms_id"]), row)
|
|
for row in seen_oms.values():
|
|
inserts.append(_action2(row, number, outcome, ok))
|
|
if ok:
|
|
process_status = 1
|
|
process_result = "协议已录入"
|
|
else:
|
|
process_status = 2
|
|
process_result = clip(outcome.reason or "协议录入失败", PROCESS_RESULT_MAX)
|
|
table3_updates = [
|
|
{
|
|
"fid": int(row["fid"]),
|
|
"process_status": process_status,
|
|
"process_result": process_result,
|
|
}
|
|
for row in group.protocols
|
|
]
|
|
return GroupSave(
|
|
table4_updates=updates,
|
|
table4_inserts=inserts,
|
|
table4_release_fids=[],
|
|
table3_updates=table3_updates,
|
|
)
|
|
|
|
|
|
def _action1_success(row: dict, contract_no: str) -> dict:
|
|
return {
|
|
"oms_id": int(row["oms_id"]),
|
|
"action_code": 1,
|
|
"contract_status": 2,
|
|
"contract_fail_reason": None,
|
|
"contract_no": contract_no,
|
|
"agreement_status": None,
|
|
"agreement_fail_reason": None,
|
|
"agreement_id": None,
|
|
"agreement_dtl_id": None,
|
|
"push_status": 0,
|
|
"org_code": row.get("org_code"),
|
|
"create_user_id": 0,
|
|
"create_user_name": STAMP_DONE,
|
|
"update_user_id": 0,
|
|
"update_user_name": STAMP_DONE,
|
|
}
|
|
|
|
|
|
def _action2(row: dict, contract_no: str, outcome: AgreementOutcome, ok: bool) -> dict:
|
|
if ok:
|
|
status = 1
|
|
fail_reason = None
|
|
agreement_id = (outcome.agreement_id or "").strip() or None
|
|
agreement_dtl_id = (outcome.agreement_dtl_id or "").strip() or None
|
|
else:
|
|
status = 2
|
|
fail_reason = clip(outcome.reason or "协议录入失败", FAIL_REASON_MAX)
|
|
agreement_id = None
|
|
agreement_dtl_id = None
|
|
return {
|
|
"oms_id": int(row["oms_id"]),
|
|
"action_code": 2,
|
|
"contract_status": None,
|
|
"contract_fail_reason": None,
|
|
"contract_no": contract_no,
|
|
"agreement_status": status,
|
|
"agreement_fail_reason": fail_reason,
|
|
"agreement_id": agreement_id,
|
|
"agreement_dtl_id": agreement_dtl_id,
|
|
"push_status": 0,
|
|
"org_code": row.get("org_code"),
|
|
"create_user_id": 0,
|
|
"create_user_name": STAMP_DONE,
|
|
"update_user_id": 0,
|
|
"update_user_name": STAMP_DONE,
|
|
}
|