498da05995
(工作区既有改动, 非本次会话产出)
6.0 KiB
6.0 KiB
cps_order — CPS 对账订单(美团联盟 query_order 拉回的订单明细)
模型
app/models/cps_order.py· 仓库app/admin/repositories/cps.py(reconcile_orders/_map_order_fields/list_orders、统计group_stats) · 接口 adminPOST /admin/api/cps/orders/reconcile、GET /admin/api/cps/orders、GET /admin/api/cps/stats· ← 索引 · 总览
从美团联盟 query_order 按时间窗拉回、按 sid 归群的订单明细,是 CPS 群发漏斗的最下游"赚了多少佣金":用户点 cps_link(携群 cps_group 的 sid)下单后,美团把订单连同 sid 回传,这里按 sid 归群做对账,与 cps_click 的点击量在 group_stats 汇成"点击→下单→佣金"漏斗。仅美团有此表——淘宝/京东无对账 API,统计里对账字段显示 -。
用在哪 / 增删改查
- C/U(upsert):
POST /admin/api/cps/orders/reconcile(reconcile_orders,需finance角色)。调meituan.query_order(sid?, start_time, end_time, page, limit=100)分页拉单(max_pages=200防死循环),每条经_map_order_fields转字段,按order_idupsert:不存在则 insert,存在则逐字段覆盖(订单状态会随时间变 付款→完成→结算/退款,重复拉则更新)。返回{fetched, inserted, updated, pages}。order_id全局唯一即幂等键。 - R(读):
GET /admin/api/cps/orders(list_orders,游标分页,可按sid/mt_status过滤);统计group_stats拉全部订单按sid分桶,算order_count/gmv/est_commission/settled_commission等。 - D:无。
字段
字段对齐
query_order实测返回:金额(payPrice/profit)是「元」字符串 → 入库_yuan_to_cents转「分」(全站口径);时间(payTime/updateTime)是秒级时间戳 →_ts_to_dt转 tz-aware UTC。
| 列 | 类型 | 约束 / 默认 | 说明(取值 / join / 源字段) |
|---|---|---|---|
id |
Integer | PK, autoincrement | |
order_id |
String(64) | UNIQUE, index, NOT NULL | 美团订单号(加密串),orderId。upsert 幂等键 |
sid |
String(64) | index, nullable | 渠道追踪位 = 群 sid,源 sid。按它归群聚合;历史无 sid 订单为空 |
act_id |
String(64) | index, nullable | 活动物料 ID,源 actId(转字符串) |
biz_line |
Integer | nullable | 业务线,源 businessLine。1=外卖 |
trade_type |
Integer | nullable | 源 tradeType。1=cps 2=cpa |
pay_price_cents |
Integer | nullable | 付款金额(分),源 payPrice(元)。统计算 GMV |
commission_cents |
Integer | nullable | 预估佣金(分),源 profit(元)。统计算预估/已结算佣金 |
commission_rate |
String(16) | nullable | 佣金率,源 commissionRate。"300"=3%、"10"=0.1%(原样字符串,前端解释) |
refund_price_cents |
Integer | nullable | 退款金额(分),源 refundPrice(元) |
refund_profit_cents |
Integer | nullable | 退款佣金(分),源 refundProfit(元) |
mt_status |
String(8) | index, nullable | 美团订单状态,源 status:2付款 3完成 4取消 5风控 6结算 |
invalid_reason |
String(128) | nullable | 失效原因,源 invalidReason |
product_name |
String(512) | nullable | 商品名,源 productName(超 500 截断) |
pay_time |
DateTime(tz) | index, nullable | 付款时间,源 payTime(秒级 ts)。统计按时间窗过滤的就是它 |
mt_update_time |
DateTime(tz) | nullable | 美团侧更新时间,源 updateTime(秒级 ts) |
raw |
JSON / JSONB | NOT NULL, default {} |
query_order 单条原始 dataList,留底排查/补字段 |
first_seen |
DateTime(tz) | server_default now(), NOT NULL | 首次拉到入库时间(本地) |
updated_at |
DateTime(tz) | server_default now(), onupdate now() | 最近 upsert 时间(本地) |
关系 / Join Key
sid≈cps_group.sid(订单按 sid 归群,语义关联,无 ForeignKey)。group_stats即据此把订单挂到群行。- 与
cps_link/cps_click无直接列关联:下单链路是"美团短链(带 sid)→ 美团 App 下单",订单只回传sid、不回传我们的link.code或某次点击 id。所以点击与订单只能在群(sid)维度汇合,无法把单笔订单对到单次点击。 - 本表无
user_id:CPS 联盟订单是别人(被推广的微信群用户)在美团下的单,与本 App 的user表无关,纯渠道佣金对账,不归属本 App 用户。
索引与约束
- PK
id;UNIQUE+indexorder_id(uq_cps_order_order_id+ix_cps_order_order_id);indexsid、act_id、mt_status、pay_time。 - 无外键。
注意
- 状态决定佣金口径(
group_stats._order_agg):4取消/5风控不计佣金(_INVALID_STATUS);6结算才是佣金真正到账(_SETTLED_STATUS)。统计区分order_count(有效=非取消非风控)、settled_count(已结算)、canceled_count,佣金分est_commission(有效单预估)与settled_commission(结算单)。 - "未归群"独立行:
group_stats遍历完所有群后,对剩下的、有订单但sid不属任何现存群的 sid(历史遗留如wonderableai、或别处来源、或群被删),单列一行group_id=None、有对账无点击。所以本表sid可空/可孤立是设计内的,不是脏数据。 - 金额/时间统一转换的原因:
query_order返回金额是「元」字符串、时间是秒级 ts;_yuan_to_cents(Decimal 防浮点,"null"/空 → None)与_ts_to_dt(转 tz-aware UTC,前端按北京展示)在入库时归一,与全站"金额存分、时间存 tz-aware"口径对齐。raw整条留底,字段不够时不用重拉。 - upsert 而非 append:同一订单会被多次拉到(状态变化),按
order_id覆盖即可拿到最新状态;reconcile可重复跑(幂等)。 - 本表自始至终只服务美团:初版(
277f9b1)就定型,3a40f61接淘宝/京东时没动本表——那俩平台无对账 API,统计里对账列直接给None(前端显示-)。这是"对账 = 美团专属"的边界。