"""客户画像变更日志仓储。""" from __future__ import annotations import json from sqlalchemy import text class CustomerProfileChangeLogRepo: """记录画像更新原因,供缓存失效和后续审计复用。""" def __init__(self, db): self.db = db async def record( self, *, customer_id: int, tag: str | None, old_value, new_value, source: str | None, confidence: float | None, reason: str | None, operator_id: int | None = None, ) -> None: """写入一次画像变更日志。""" await self.db.execute( text( """ INSERT INTO customer_profile_change_log (customer_id, tag, old_value, new_value, source, confidence, reason, operator_id) VALUES (:customer_id, :tag, :old_value, :new_value, :source, :confidence, :reason, :operator_id) """ ), { "customer_id": customer_id, "tag": tag, "old_value": json.dumps(old_value, ensure_ascii=False) if isinstance(old_value, (dict, list)) else old_value, "new_value": json.dumps(new_value, ensure_ascii=False) if isinstance(new_value, (dict, list)) else new_value, "source": source, "confidence": confidence, "reason": reason, "operator_id": operator_id, }, ) await self.db.commit() __all__ = ["CustomerProfileChangeLogRepo"]