"""比价记录表。 每完成一次比价(外卖/电商/领券),客户端在 done 帧后用带 JWT 的通道上报一条,落这里。 是未来「我的比价记录」页的数据源,也沉淀用户级行为画像(哪个用户在哪两家之间比了什么)。 与 savings_record 的区别:savings_record 是「省了多少钱」的视角(只有省到才有意义,当前由 demo seeder 灌),本表是「每一次比价的完整明细」——不省钱、甚至失败的比价也照记一条。 两表独立,互不影响。 「越详细越好」的落地:结构化列给查询/排序/聚合用,raw_payload(JSONB)把客户端上报的 原始 calibration + done.params 原样存一份,未来前端要展示什么都能拿到、不丢信息。 """ from __future__ import annotations from datetime import datetime from sqlalchemy import ( JSON, Boolean, DateTime, ForeignKey, Index, Integer, String, UniqueConstraint, func, ) from sqlalchemy.dialects.postgresql import JSONB from sqlalchemy.orm import Mapped, mapped_column from app.db.base import Base # PG 上用 JSONB(可建 GIN 索引),SQLite(本地/测试)退化为通用 JSON—— # 否则 SQLite 无 JSONB,Base.metadata.create_all 编译报错(同 savings_record.dishes)。 _JSON = JSON().with_variant(JSONB(), "postgresql") class ComparisonRecord(Base): __tablename__ = "comparison_record" __table_args__ = ( # 同一用户同一次比价(trace_id)只存一条:客户端重试/误点重复上报时幂等覆盖。 UniqueConstraint("user_id", "trace_id", name="uq_comparison_user_trace"), # 首页轮播 / 省钱战绩聚合都按 status='success' 过滤 + created_at 近期排序; # 复合索引避免随数据量增大退化成全表扫(单列 created_at 索引不含 status)。 Index("ix_comparison_status_created", "status", "created_at"), ) id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True) user_id: Mapped[int] = mapped_column( Integer, ForeignKey("user.id"), index=True, nullable=False ) # 仍记录设备号(同一用户多设备的行为区分 / 与不鉴权期 device_id 数据对账) device_id: Mapped[str | None] = mapped_column(String(64), nullable=True) # 业务类型:food(外卖,当前唯一接通)/ ecom(电商)/ coupon(领券)。预留扩展。 business_type: Mapped[str] = mapped_column( String(16), nullable=False, default="food", index=True ) # pricebot 侧 trace_id:关联调试落盘 + 幂等去重键 trace_id: Mapped[str] = mapped_column(String(64), nullable=False) # 本次比价的公网调试链接(price.shaguabijia.com/traces/{dir}/)。pricebot done 帧给、 # 客户端上报带上——dir 名含 pricebot 落盘的时分秒,前端/server 都拼不出,必须存。 # 查看接口按 user.debug_trace_enabled 决定返不返回。旧记录 / 未开上云为 None。 trace_url: Mapped[str | None] = mapped_column(String(512), nullable=True) # ===== 源平台(发起比价的那家)===== source_platform_id: Mapped[str | None] = mapped_column(String(32), nullable=True) source_platform_name: Mapped[str | None] = mapped_column(String(32), nullable=True) source_package: Mapped[str | None] = mapped_column(String(128), nullable=True) source_price_cents: Mapped[int | None] = mapped_column(Integer, nullable=True) # ===== 最优结果(全平台最便宜的一家,= comparison_results 里 rank=1)===== best_platform_id: Mapped[str | None] = mapped_column(String(32), nullable=True) best_platform_name: Mapped[str | None] = mapped_column(String(32), nullable=True) best_price_cents: Mapped[int | None] = mapped_column(Integer, nullable=True) # 最优平台的商家/商品深链(客户端比价时从剪贴板采到 = collectedLinks[best_index]); # 「再次比价」写剪贴板 + launch 该平台 App 直达。仅 2026-06 起新比价有,旧记录为 None。 best_deeplink: Mapped[str | None] = mapped_column(String(1024), nullable=True) # 源价 - 最优价(可为 0 / 负:源平台本来就最便宜时没省到) saved_amount_cents: Mapped[int | None] = mapped_column(Integer, nullable=True) # 源平台就是最便宜的一家(= 这次没省到钱) is_source_best: Mapped[bool | None] = mapped_column(Boolean, nullable=True) # ===== 订单概要 ===== store_name: Mapped[str | None] = mapped_column(String(128), nullable=True) total_dish_count: Mapped[int | None] = mapped_column(Integer, nullable=True) skipped_dish_count: Mapped[int | None] = mapped_column(Integer, nullable=True) # success(拿到有效对比)/ failed(出错或没采到目标价) status: Mapped[str] = mapped_column(String(16), nullable=False, default="success") # done 帧 information 文案。成功:"在美团找到同店,到手价 ¥X…"; # 失败:具体原因(如"美团、京东外卖均未找到该商品")。前端在比价失败时当原因展示。 information: Mapped[str | None] = mapped_column(String(256), nullable=True) # ===== 明细(JSON,越详细越好)===== # 下单菜品 [{name, qty, specs?}] items: Mapped[list] = mapped_column(_JSON, nullable=False, default=list) # 逐平台对比 [{platform_id, platform_name, package, price, is_source, rank, coupon_saved, coupon_name, applied_coupons}](price/coupon_saved 单位:元,原样存;coupon_name=优惠来源名;applied_coupons=[{name,amount}] 多券明细) comparison_results: Mapped[list] = mapped_column(_JSON, nullable=False, default=list) # 目标平台未找到、跳过的菜名 skipped_dish_names: Mapped[list] = mapped_column(_JSON, nullable=False, default=list) # 客户端上报的原始 payload(calibration + done.params 全量),未来取数兜底 raw_payload: Mapped[dict | None] = mapped_column(_JSON, nullable=True) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), index=True, nullable=False ) def __repr__(self) -> str: # pragma: no cover return ( f"" )