67 lines
1.7 KiB
Python
67 lines
1.7 KiB
Python
"""CRM 合同编号变更时,回写运输计划侧合同号快照。"""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.models.pl_models import PlPlan, PlPlanContract
|
|
|
|
|
|
def sync_plan_contract_no_snapshots(
|
|
db: Session,
|
|
*,
|
|
tenant_no: str,
|
|
contract_id: str,
|
|
contract_no: str,
|
|
user_id: Optional[str] = None,
|
|
) -> int:
|
|
"""按 contract_id 刷新 pl_plan_contract / pl_plan 上的 contract_no 快照。
|
|
|
|
归档列表等读快照字段;仅改 CRM 主表会导致列表与详情不一致。
|
|
返回实际改写的行数(关联表 + 主表遗留字段合计)。
|
|
"""
|
|
cid = (contract_id or "").strip()
|
|
if not cid:
|
|
return 0
|
|
new_no = (contract_no or "").strip()
|
|
now = datetime.now()
|
|
changed = 0
|
|
|
|
link_rows = (
|
|
db.query(PlPlanContract)
|
|
.filter(
|
|
PlPlanContract.tenant_no == tenant_no,
|
|
PlPlanContract.contract_id == cid,
|
|
PlPlanContract.del_flag == 0,
|
|
)
|
|
.all()
|
|
)
|
|
for row in link_rows:
|
|
if (row.contract_no or "") == new_no:
|
|
continue
|
|
row.contract_no = new_no or None
|
|
row.update_by = user_id
|
|
row.update_time = now
|
|
changed += 1
|
|
|
|
plan_rows = (
|
|
db.query(PlPlan)
|
|
.filter(
|
|
PlPlan.tenant_no == tenant_no,
|
|
PlPlan.contract_id == cid,
|
|
PlPlan.del_flag == 0,
|
|
)
|
|
.all()
|
|
)
|
|
for row in plan_rows:
|
|
if (row.contract_no or "") == new_no:
|
|
continue
|
|
row.contract_no = new_no or None
|
|
row.update_by = user_id
|
|
row.update_time = now
|
|
changed += 1
|
|
|
|
return changed
|