按客户与证用品类自动选证,未配置生效时间拦住安排;齐备仅放行安排,归档前须完善挂证,并补充迁移与测试造数脚本。 Co-authored-by: Cursor <cursoragent@cursor.com>
78 lines
2.6 KiB
Python
78 lines
2.6 KiB
Python
"""客户品类额度规则切点:系统信息配置;未配置则拦住安排。"""
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
from uuid import uuid4
|
|
|
|
from fastapi import HTTPException
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.models.pl_quota_models import PlCertQuotaRuleConfig
|
|
|
|
RULE_NOT_CONFIGURED = '请先在系统信息中配置客户品类额度生效时间后再安排运输'
|
|
VERSION_LEGACY = 'legacy'
|
|
VERSION_CUSTOMER_CATEGORY = 'customer_category'
|
|
|
|
|
|
def get_config(db: Session, tenant_no: str) -> Optional[PlCertQuotaRuleConfig]:
|
|
return (
|
|
db.query(PlCertQuotaRuleConfig)
|
|
.filter_by(tenant_no=tenant_no, del_flag=0)
|
|
.first()
|
|
)
|
|
|
|
|
|
def get_effective_at(db: Session, tenant_no: str):
|
|
row = get_config(db, tenant_no)
|
|
return row.effective_at if row else None
|
|
|
|
|
|
def read_config(db: Session, tenant_no: str) -> dict:
|
|
row = get_config(db, tenant_no)
|
|
return {
|
|
'effectiveAt': row.effective_at.isoformat(sep=' ', timespec='seconds') if row and row.effective_at else None,
|
|
'configured': bool(row and row.effective_at),
|
|
}
|
|
|
|
|
|
def save_effective_at(db: Session, user, effective_at: Optional[datetime]) -> dict:
|
|
row = get_config(db, user.tenant_no)
|
|
if row is None:
|
|
row = PlCertQuotaRuleConfig(
|
|
id=str(uuid4()),
|
|
tenant_no=user.tenant_no,
|
|
create_by=user.id,
|
|
create_dept_id=getattr(user, 'dept_id', None) or '',
|
|
update_by=user.id,
|
|
)
|
|
db.add(row)
|
|
row.effective_at = effective_at
|
|
row.update_by = user.id
|
|
db.flush()
|
|
return read_config(db, user.tenant_no)
|
|
|
|
|
|
def assert_cert_quota_rule_configured(db: Session, tenant_no: str) -> datetime:
|
|
effective_at = get_effective_at(db, tenant_no)
|
|
if effective_at is None:
|
|
raise HTTPException(409, RULE_NOT_CONFIGURED)
|
|
return effective_at
|
|
|
|
|
|
def resolve_rule_version_for_create(db: Session, tenant_no: str, create_time: Optional[datetime] = None) -> str:
|
|
"""计划创建时打标;未配置生效时间时仍标 legacy(安排会被拦住)。"""
|
|
effective_at = get_effective_at(db, tenant_no)
|
|
when = create_time or datetime.now()
|
|
if effective_at is not None and when >= effective_at:
|
|
return VERSION_CUSTOMER_CATEGORY
|
|
return VERSION_LEGACY
|
|
|
|
|
|
def stamp_plan_rule_version(db: Session, user, plan) -> str:
|
|
version = resolve_rule_version_for_create(db, user.tenant_no, getattr(plan, 'create_time', None) or datetime.now())
|
|
plan.cert_quota_rule_version = version
|
|
return version
|
|
|
|
|
|
def plan_uses_customer_category(plan) -> bool:
|
|
return (getattr(plan, 'cert_quota_rule_version', None) or VERSION_LEGACY) == VERSION_CUSTOMER_CATEGORY
|