feat(shipping-notice): 添加识别定义和规则集管理功能,支持客户和站点的可配置识别规则
This commit is contained in:
1 parent
88408490d7
commit
34d489a2d1
167 files changed
+10365
-7978
No files matched your search
@@ -24,6 +24,7 @@ from app.models import (
|
||||
ShippingCustomerLifecycleEvent,
|
||||
ShippingCustomerSite,
|
||||
ShippingCustomerSiteIdentifier,
|
||||
ShippingIdentityEvidence,
|
||||
)
|
||||
from app.services.shipping_notice.customer_normalization import (
|
||||
clean_text,
|
||||
@@ -118,6 +119,7 @@ def serialize_customer(
|
||||
include_identifiers: bool = False,
|
||||
case_count: int | None = None,
|
||||
identifier_count: int | None = None,
|
||||
identity_evidence_count: int | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
序列化标准客户,并可选附带识别证据和业务引用数量。
|
||||
@@ -126,7 +128,8 @@ def serialize_customer(
|
||||
record: 标准客户 ORM 记录。
|
||||
include_identifiers: 是否附带识别证据明细。
|
||||
case_count: 列表查询预聚合的业务单数量;为空时现场查询。
|
||||
identifier_count: 列表查询预聚合的识别证据数量;为空时现场查询。
|
||||
identifier_count: 历史固定类型证据数量,仅供存量审计显示。
|
||||
identity_evidence_count: 配置化客户主体证据数量;为空时现场查询。
|
||||
Returns:
|
||||
管理端客户 JSON 字典。
|
||||
"""
|
||||
@@ -143,9 +146,19 @@ def serialize_customer(
|
||||
'updated_at': record.updated_at.isoformat() if record.updated_at else None,
|
||||
'case_count': case_count if case_count is not None else db.session.query(ShippingCase.id).filter_by(customer_id=record.id).count(),
|
||||
'identifier_count': identifier_count if identifier_count is not None else db.session.query(ShippingCustomerIdentifier.id).filter_by(customer_id=record.id).count(),
|
||||
'identity_evidence_count': identity_evidence_count if identity_evidence_count is not None else db.session.query(
|
||||
ShippingIdentityEvidence.id,
|
||||
).filter_by(customer_id=record.id).count(),
|
||||
}
|
||||
if include_identifiers:
|
||||
payload['identifiers'] = [serialize_identifier(item) for item in record.identifiers]
|
||||
payload['identity_evidences'] = [
|
||||
_serialize_identity_evidence(item)
|
||||
for item in db.session.query(ShippingIdentityEvidence)
|
||||
.filter_by(customer_id=record.id)
|
||||
.order_by(ShippingIdentityEvidence.id.desc())
|
||||
.all()
|
||||
]
|
||||
payload['external_refs'] = [serialize_external_ref(item) for item in record.external_refs]
|
||||
payload['sites'] = [serialize_site(item) for item in record.sites]
|
||||
return payload
|
||||
@@ -173,14 +186,36 @@ def list_customers(params: dict[str, Any]) -> dict[str, Any]:
|
||||
.group_by(ShippingCustomerIdentifier.customer_id)
|
||||
.subquery()
|
||||
)
|
||||
identity_evidence_counts = (
|
||||
db.session.query(
|
||||
ShippingIdentityEvidence.customer_id.label('customer_id'),
|
||||
func.count(ShippingIdentityEvidence.id).label('identity_evidence_count'),
|
||||
)
|
||||
.filter(ShippingIdentityEvidence.customer_id.isnot(None))
|
||||
.group_by(ShippingIdentityEvidence.customer_id)
|
||||
.subquery()
|
||||
)
|
||||
site_identity_evidence_counts = (
|
||||
db.session.query(
|
||||
ShippingCustomerSite.customer_id.label('customer_id'),
|
||||
func.count(ShippingIdentityEvidence.id).label('identity_evidence_count'),
|
||||
)
|
||||
.join(ShippingIdentityEvidence, ShippingIdentityEvidence.site_id == ShippingCustomerSite.id)
|
||||
.group_by(ShippingCustomerSite.customer_id)
|
||||
.subquery()
|
||||
)
|
||||
query = (
|
||||
db.session.query(
|
||||
ShippingCustomer,
|
||||
func.coalesce(case_counts.c.case_count, 0),
|
||||
func.coalesce(identifier_counts.c.identifier_count, 0),
|
||||
func.coalesce(identity_evidence_counts.c.identity_evidence_count, 0)
|
||||
+ func.coalesce(site_identity_evidence_counts.c.identity_evidence_count, 0),
|
||||
)
|
||||
.outerjoin(case_counts, case_counts.c.customer_id == ShippingCustomer.id)
|
||||
.outerjoin(identifier_counts, identifier_counts.c.customer_id == ShippingCustomer.id)
|
||||
.outerjoin(identity_evidence_counts, identity_evidence_counts.c.customer_id == ShippingCustomer.id)
|
||||
.outerjoin(site_identity_evidence_counts, site_identity_evidence_counts.c.customer_id == ShippingCustomer.id)
|
||||
)
|
||||
keyword = clean_text(params.get('q'))
|
||||
status = clean_text(params.get('status')).lower()
|
||||
@@ -208,8 +243,11 @@ def list_customers(params: dict[str, Any]) -> dict[str, Any]:
|
||||
items = query.order_by(order(sort_col), ShippingCustomer.id.desc()).offset((page - 1) * page_size).limit(page_size).all()
|
||||
return {
|
||||
'items': [
|
||||
serialize_customer(customer, case_count=int(case_count), identifier_count=int(identifier_count))
|
||||
for customer, case_count, identifier_count in items
|
||||
serialize_customer(
|
||||
customer, case_count=int(case_count), identifier_count=int(identifier_count),
|
||||
identity_evidence_count=int(identity_evidence_count),
|
||||
)
|
||||
for customer, case_count, identifier_count, identity_evidence_count in items
|
||||
],
|
||||
'total': total,
|
||||
'page': page,
|
||||
@@ -293,6 +331,13 @@ def serialize_external_ref(record: ShippingCustomerExternalRef) -> dict[str, Any
|
||||
}
|
||||
|
||||
|
||||
def _serialize_identity_evidence(record: ShippingIdentityEvidence) -> dict[str, Any]:
|
||||
"""序列化配置化身份识别证据及定义快照,供客户主数据页直接渲染。"""
|
||||
from app.services.shipping_notice.identity_rules.service import serialize_evidence
|
||||
|
||||
return serialize_evidence(record)
|
||||
|
||||
|
||||
def serialize_site(record: ShippingCustomerSite) -> dict[str, Any]:
|
||||
"""序列化送货站点及其名称/外部编码标识。"""
|
||||
return {
|
||||
@@ -304,6 +349,13 @@ def serialize_site(record: ShippingCustomerSite) -> dict[str, Any]:
|
||||
'source_system': item.source_system, 'raw_value': item.raw_value,
|
||||
'normalized_value': item.normalized_value, 'status': item.status,
|
||||
} for item in record.identifiers],
|
||||
'identity_evidences': [
|
||||
_serialize_identity_evidence(item)
|
||||
for item in db.session.query(ShippingIdentityEvidence)
|
||||
.filter_by(site_id=record.id)
|
||||
.order_by(ShippingIdentityEvidence.id.desc())
|
||||
.all()
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user