@taopao/memoir 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +8 -2
  3. package/assets/migrations-pg/0100_baseline.sql +1115 -0
  4. package/assets/migrations-pg/0101_functions.sql +93 -0
  5. package/assets/migrations-pg/0102_views.sql +2425 -0
  6. package/assets/migrations-pg/0103_constraints.sql +147 -0
  7. package/assets/migrations-pg/0104_protection.sql +33 -0
  8. package/assets/migrations-pg/0105_bocom_credit_card.sql +43 -0
  9. package/assets/migrations-pg/0106_psbc_debit_card.sql +87 -0
  10. package/assets/migrations-pg/0107_tailong_debit_card.sql +87 -0
  11. package/assets/migrations-pg/0108_ccb_debit_card.sql +87 -0
  12. package/assets/migrations-pg/0109_ccb_credit_card.sql +63 -0
  13. package/assets/migrations-pg/0110_icbc_debit_card.sql +87 -0
  14. package/assets/migrations-pg/0111_icbc_credit_card.sql +66 -0
  15. package/assets/migrations-pg/0112_boc_debit_card.sql +88 -0
  16. package/assets/migrations-pg/0113_jsb_debit_card.sql +88 -0
  17. package/assets/migrations-pg/0114_cmbc_debit_card.sql +88 -0
  18. package/assets/migrations-pg/0115_hzbank_debit_card.sql +88 -0
  19. package/assets/migrations-pg/0116_unified_bank_documents.sql +197 -0
  20. package/assets/migrations-pg/0117_agent_submissions.sql +50 -0
  21. package/assets/migrations-pg/0118_agent_reversions_analysis.sql +34 -0
  22. package/assets/migrations-pg/0119_agent_extension_registry.sql +72 -0
  23. package/assets/migrations-pg/0120_agent_record_sources.sql +22 -0
  24. package/assets/migrations-pg/0121_agent_extension_deprecation.sql +30 -0
  25. package/assets/migrations-pg/0122_agent_extension_controlled_revert.sql +107 -0
  26. package/assets/migrations-pg/0123_agent_extension_versions.sql +54 -0
  27. package/assets/migrations-pg/0124_agent_extension_retirement_groups.sql +26 -0
  28. package/assets/migrations-pg/0125_agent_candidate_reviews.sql +30 -0
  29. package/assets/migrations-pg/0126_agent_module_lifecycle.sql +93 -0
  30. package/assets/migrations-pg/0127_agent_tasks.sql +64 -0
  31. package/assets/migrations-pg/0128_agent_task_leases.sql +80 -0
  32. package/assets/migrations-pg/0129_agent_task_plans.sql +66 -0
  33. package/assets/migrations-pg/0130_agent_task_submissions.sql +72 -0
  34. package/assets/migrations-pg/0131_agent_task_completions.sql +93 -0
  35. package/assets/migrations-pg/0132_agent_task_changes.sql +113 -0
  36. package/assets/migrations-pg/0133_agent_imported_source_snapshots.sql +152 -0
  37. package/assets/migrations-pg/0134_agent_analysis_datasets.sql +6 -0
  38. package/assets/migrations-pg/0135_agent_maintenance_tasks.sql +139 -0
  39. package/assets/migrations-pg/0136_agent_task_structures.sql +66 -0
  40. package/assets/migrations-pg/0137_agent_personal_identity.sql +149 -0
  41. package/assets/migrations-pg/0138_agent_work_records.sql +24 -0
  42. package/assets/migrations-pg/0139_shared_files.sql +43 -0
  43. package/assets/migrations-pg/0140_shared_script_runs.sql +36 -0
  44. package/assets/migrations-pg/0141_payment_bank_links.sql +96 -0
  45. package/assets/migrations-pg/0142_consumption_order_types_and_places.sql +287 -0
  46. package/assets/migrations-pg/0143_skill_direct_write.sql +330 -0
  47. package/assets/migrations-pg/0144_skill_schema_archive.sql +73 -0
  48. package/assets/migrations-pg/0145_skill_ingest_wiki.sql +91 -0
  49. package/assets/migrations-pg/0146_skill_load.sql +17 -0
  50. package/assets/migrations-pg/0147_drop_agent_schema.sql +147 -0
  51. package/assets/migrations-pg/0148_ingest_lanes.sql +19 -0
  52. package/assets/migrations-pg/0149_account_ownership.sql +233 -0
  53. package/assets/migrations-pg/0150_bank_card_exact_link.sql +24 -0
  54. package/assets/migrations-pg/0151_account_ownership_enforce.sql +35 -0
  55. package/assets/migrations-pg/0152_account_ownership_views.sql +750 -0
  56. package/assets/migrations-pg/0153_web_frontend.sql +34 -0
  57. package/assets/migrations-pg/0154_audio_record_owners.sql +103 -0
  58. package/assets/migrations-pg/0155_ingest_knowledge_tables.sql +171 -0
  59. package/assets/migrations-pg/0156_batch_playbook_note.sql +17 -0
  60. package/assets/migrations-pg/0157_last_contact.sql +166 -0
  61. package/assets/migrations-pg/0158_refund_match_hash_join.sql +91 -0
  62. package/assets/migrations-pg/0159_fact_occurred_time.sql +88 -0
  63. package/assets/migrations-pg/0160_agent_notes.sql +118 -0
  64. package/assets/migrations-pg/0161_life_history.sql +314 -0
  65. package/assets/migrations-pg/0162_person_card.sql +427 -0
  66. package/assets/migrations-pg/0163_parking_drop_personal_defaults.sql +12 -0
  67. package/assets/native/darwin-arm64.node +0 -0
  68. package/assets/playbooks/audio.md +83 -0
  69. package/assets/playbooks/chat.md +37 -0
  70. package/assets/playbooks/common.md +66 -0
  71. package/assets/playbooks/intimate.md +19 -0
  72. package/assets/playbooks/journal.md +17 -0
  73. package/assets/playbooks/orders.md +15 -0
  74. package/assets/playbooks/payments.md +16 -0
  75. package/assets/playbooks/self.md +29 -0
  76. package/bin/install-codex-skill.mjs +77 -0
  77. package/bin/memoir.mjs +5 -0
  78. package/dist/cli.js +34062 -0
  79. package/package.json +40 -4
  80. package/release-manifest.json +432 -0
  81. package/skill/SKILL.md +120 -0
  82. package/skill/references//345/256/211/350/243/205/344/275/215/347/275/256.md +21 -0
  83. package/skill/references//346/225/260/346/215/256/345/217/243/345/276/204.md +36 -0
  84. package/skill/scripts/memoir.mjs +82 -0
@@ -0,0 +1,750 @@
1
+ -- 仅 PostgreSQL:账户归属第三期——基础视图与统计口径(2026-11-06 账户归属方案)。
2
+ --
3
+ -- 业务背景:
4
+ -- 0149 起 payments / orders / bank_cards 带 person_id(付款/下单/持卡账户的主人)。本迁移把归属带进视图,并按决策调整口径:
5
+ -- * Q10 基础视图保留所有行,多一列 owner_person_id,过滤交给下游;
6
+ -- * Q2 “我的…”类统计(月度消费、花在她身上、按地点/场景)只取我名下账户——只有我名下账户时数值与改前一致,加入别人的账户后才有差异;
7
+ -- * Q21 往来类视图(v_money_flow 及 v_us_money_flow、q_money_*)只取我名下账户,避免同一笔钱在两个人的账户里各算一遍;
8
+ -- * Q22 消费归属取“谁掏的钱”:订单有已确认(可信)的支付关联时取付款流水的 person_id(多笔付款取 payment_key 最小的那笔),否则取订单自己的 person_id。
9
+ -- 兼容:全部使用 CREATE OR REPLACE VIEW,新列追加在末尾,原列名和顺序不变(参照 0141 做法);
10
+ -- 依赖 SELECT * 的下游视图在库里保存的是展开后的列,不会自动多出新列,所以 _v_spending_quality、v_us_spending_unassigned 用显式列表重写。
11
+ -- _v_payment_bases 保持不变(p.* 已按旧列展开),v_payments 通过主键关联 payments 取 owner_person_id。
12
+ SELECT pg_advisory_xact_lock(hashtext('relation-vault:write'));
13
+
14
+
15
+ -- v_payments:付款账户归属(owner_person_id)
16
+ CREATE OR REPLACE VIEW v_payments AS
17
+ WITH identity_candidates AS (
18
+ SELECT
19
+ p.target_key,
20
+ i.person_id,
21
+ CASE WHEN i.match_kind = 'account_id' THEN 0 ELSE 1 END AS priority
22
+ FROM _v_payment_bases AS p
23
+ JOIN identities AS i
24
+ ON i.source = p.platform
25
+ AND i.deleted_at IS NULL
26
+ AND (
27
+ (
28
+ i.match_kind = 'account_id'
29
+ AND VAULT_IDENTITY_MATCH(i.match_mode, i."value", p.counterparty_account) = 1
30
+ )
31
+ OR (
32
+ i.match_kind = 'counterparty'
33
+ AND VAULT_IDENTITY_MATCH(i.match_mode, i."value", p.counterparty) = 1
34
+ )
35
+ )
36
+ AND (
37
+ i.valid_from IS NULL OR i.valid_from <= SUBSTRING(p.occurred_at FROM 1 FOR 10)
38
+ )
39
+ AND (
40
+ i.valid_to IS NULL OR i.valid_to >= SUBSTRING(p.occurred_at FROM 1 FOR 10)
41
+ )
42
+ JOIN persons AS person
43
+ ON person.id = i.person_id AND person.deleted_at IS NULL
44
+ ), ranked AS (
45
+ SELECT
46
+ *,
47
+ MIN(priority) OVER (PARTITION BY target_key) AS best
48
+ FROM identity_candidates
49
+ ), matched AS (
50
+ SELECT
51
+ target_key,
52
+ MIN(person_id) AS person_id,
53
+ COUNT(DISTINCT person_id) AS persons
54
+ FROM ranked
55
+ WHERE
56
+ priority = best
57
+ GROUP BY
58
+ target_key
59
+ ), refunds AS (
60
+ SELECT
61
+ platform,
62
+ txn_id,
63
+ SUM(amount_cents) AS refund_cents
64
+ FROM _v_payment_refund_matches
65
+ GROUP BY
66
+ platform,
67
+ txn_id
68
+ ), enriched AS (
69
+ SELECT
70
+ p.*,
71
+ CASE WHEN i.persons = 1 THEN i.person_id END AS counterparty_person_id,
72
+ CASE WHEN i.persons = 1 THEN person.display_name END AS counterparty_person_name,
73
+ COALESCE(CAST(i.persons > 1 AS INT), 0) AS identity_ambiguous,
74
+ a.category AS analysis_category,
75
+ COALESCE(a.hidden, 0) AS hidden,
76
+ COALESCE(a.sensitivity, 'private') AS sensitivity,
77
+ LEAST(
78
+ p.amount_cents,
79
+ GREATEST(
80
+ COALESCE(p.refund_cents, 0),
81
+ COALESCE(r.refund_cents, 0),
82
+ CASE
83
+ WHEN p.status IN ('已全额退款', '对方已退还', '已退还', '退款成功', '已退款')
84
+ THEN p.amount_cents
85
+ ELSE 0
86
+ END
87
+ )
88
+ ) AS effective_refund_cents,
89
+ CASE
90
+ WHEN POSITION(
91
+ '转账' IN COALESCE(p.txn_type, '') || ' ' || COALESCE(p.platform_category, '') || ' ' || COALESCE(a.category, '')
92
+ ) > 0
93
+ OR POSITION(
94
+ '红包' IN COALESCE(p.txn_type, '') || ' ' || COALESCE(p.platform_category, '') || ' ' || COALESCE(a.category, '')
95
+ ) > 0
96
+ THEN 1
97
+ ELSE 0
98
+ END AS is_transfer
99
+ FROM _v_payment_bases AS p
100
+ LEFT JOIN refunds AS r
101
+ ON r.platform = p.platform AND r.txn_id = p.txn_id
102
+ LEFT JOIN matched AS i
103
+ ON i.target_key = p.target_key
104
+ LEFT JOIN persons AS person
105
+ ON person.id = i.person_id
106
+ LEFT JOIN _v_annotations AS a
107
+ ON a.target_type = 'payment' AND a.target_key = p.target_key
108
+ )
109
+ SELECT
110
+ e.*,
111
+ e.amount_cents / NULLIF(100.0, 0) AS amount_yuan,
112
+ CASE
113
+ WHEN e.status IN ('交易关闭', '已关闭', '已取消', '待付款', '等待买家付款')
114
+ THEN 0
115
+ ELSE e.amount_cents - e.effective_refund_cents
116
+ END AS net_amount_cents,
117
+ CASE
118
+ WHEN e.status IN ('交易关闭', '已关闭', '已取消', '待付款', '等待买家付款')
119
+ THEN 0
120
+ ELSE (
121
+ e.amount_cents - e.effective_refund_cents
122
+ ) / NULLIF(100.0, 0)
123
+ END AS net_amount_yuan,
124
+ own.person_id AS owner_person_id
125
+ FROM enriched AS e
126
+ LEFT JOIN payments AS own
127
+ ON own.platform = e.platform AND own.txn_id = e.txn_id;
128
+
129
+ -- v_spending:Q22 消费归属(owner_person_id)
130
+ CREATE OR REPLACE VIEW v_spending AS
131
+ WITH order_lines AS (
132
+ SELECT
133
+ o.platform,
134
+ o.order_no,
135
+ i.item_key,
136
+ COALESCE(o.paid_at, o.ordered_at) AS occurred_at,
137
+ COALESCE(i.product_name, o.shop_name, '订单') AS title,
138
+ o.shop_name,
139
+ o.gross_cents AS order_gross_cents,
140
+ o.net_cents,
141
+ o.amount_basis,
142
+ o.refund_basis,
143
+ o.amount_is_estimated,
144
+ COALESCE(i.weight, 1) AS weight,
145
+ o.payment_count,
146
+ o.payment_key,
147
+ GREATEST(COALESCE(oa.hidden, 0), COALESCE(ia.hidden, 0), o.payment_hidden) AS hidden,
148
+ COALESCE(ia.category, oa.category, pa.category) AS category,
149
+ CASE
150
+ WHEN ia.sensitivity = 'intimate'
151
+ OR oa.sensitivity = 'intimate'
152
+ OR pa.sensitivity = 'intimate'
153
+ THEN 'intimate'
154
+ ELSE COALESCE(ia.sensitivity, oa.sensitivity, pa.sensitivity, 'private')
155
+ END AS sensitivity,
156
+ CAST(COALESCE(
157
+ ib.beneficiaries_json,
158
+ ob.beneficiaries_json,
159
+ CASE WHEN o.payment_count = 1 THEN pb.beneficiaries_json END,
160
+ '[]'
161
+ ) AS TEXT) AS beneficiaries_json,
162
+ -- Q22:谁掏的钱——已确认关联的付款流水(payment_key = 'platform:txn_id',按首个冒号拆开以走主键)优先,否则订单自己的归属。
163
+ COALESCE(
164
+ (
165
+ SELECT pay.person_id
166
+ FROM payments AS pay
167
+ WHERE pay.platform = SPLIT_PART(o.payment_key, ':', 1)
168
+ AND pay.txn_id = SUBSTRING(o.payment_key FROM POSITION(':' IN o.payment_key) + 1)
169
+ ),
170
+ (
171
+ SELECT ord.person_id
172
+ FROM orders AS ord
173
+ WHERE ord.platform = o.platform AND ord.order_no = o.order_no
174
+ )
175
+ ) AS owner_person_id
176
+ FROM _v_order_net AS o
177
+ LEFT JOIN _v_order_item_weights AS i
178
+ ON i.platform = o.platform AND i.order_no = o.order_no
179
+ LEFT JOIN _v_annotations AS oa
180
+ ON oa.target_type = 'order' AND oa.target_key = o.platform || ':' || o.order_no
181
+ LEFT JOIN _v_annotations AS ia
182
+ ON ia.target_type = 'order_item'
183
+ AND ia.target_key = o.platform || ':' || o.order_no || ':' || i.item_key
184
+ LEFT JOIN _v_annotations AS pa
185
+ ON pa.target_type = 'payment' AND pa.target_key = o.payment_key
186
+ LEFT JOIN _v_beneficiary_groups AS ob
187
+ ON ob.target_type = 'order' AND ob.target_key = o.platform || ':' || o.order_no
188
+ LEFT JOIN _v_beneficiary_groups AS ib
189
+ ON ib.target_type = 'order_item'
190
+ AND ib.target_key = o.platform || ':' || o.order_no || ':' || i.item_key
191
+ LEFT JOIN _v_beneficiary_groups AS pb
192
+ ON pb.target_type = 'payment' AND pb.target_key = o.payment_key
193
+ WHERE
194
+ COALESCE(i.refunded, 0) = 0
195
+ ), weights AS (
196
+ SELECT
197
+ *,
198
+ SUM(weight) OVER (PARTITION BY platform, order_no) AS total_weight,
199
+ ROW_NUMBER() OVER (
200
+ PARTITION BY platform, order_no
201
+ ORDER BY (
202
+ weight > 0
203
+ ) DESC NULLS LAST, item_key NULLS FIRST
204
+ ) AS ordinal
205
+ FROM order_lines
206
+ ), rounded AS (
207
+ SELECT
208
+ *,
209
+ CAST(TRUNC(net_cents * CAST(1.0 AS DOUBLE PRECISION) * weight / NULLIF(total_weight, 0)) AS BIGINT) AS base_cents
210
+ FROM weights
211
+ ), allocated AS (
212
+ SELECT
213
+ *,
214
+ base_cents + CASE
215
+ WHEN ordinal = 1
216
+ THEN net_cents - SUM(base_cents) OVER (PARTITION BY platform, order_no)
217
+ ELSE 0
218
+ END AS amount_cents
219
+ FROM rounded
220
+ ), rows AS (
221
+ SELECT
222
+ CASE WHEN item_key IS NULL THEN 'order' ELSE 'order_item' END AS source_type,
223
+ platform || ':' || order_no || CASE WHEN item_key IS NULL THEN '' ELSE ':' || item_key END AS source_key,
224
+ platform,
225
+ order_no,
226
+ item_key,
227
+ occurred_at,
228
+ title,
229
+ shop_name,
230
+ category,
231
+ sensitivity,
232
+ amount_cents,
233
+ amount_basis,
234
+ refund_basis,
235
+ amount_is_estimated,
236
+ beneficiaries_json,
237
+ hidden,
238
+ CASE
239
+ WHEN title LIKE '%火车票%'
240
+ OR title LIKE '%高铁票%'
241
+ OR title LIKE '%12306%'
242
+ OR shop_name LIKE '%火车票%'
243
+ OR shop_name LIKE '%铁路%'
244
+ OR shop_name LIKE '%12306%'
245
+ OR category IN ('火车票', '车票', 'train_ticket', 'train')
246
+ THEN 1
247
+ ELSE 0
248
+ END AS is_train,
249
+ CAST(EXISTS(
250
+ SELECT
251
+ 1
252
+ FROM payment_order_links AS l
253
+ WHERE
254
+ l.order_platform = allocated.platform
255
+ AND l.order_no = allocated.order_no
256
+ AND l.deleted_at IS NULL
257
+ AND l.confidence <> 'high'
258
+ AND l.match_method <> 'manual'
259
+ ) AS INT) AS needs_match_review,
260
+ owner_person_id
261
+ FROM allocated
262
+ UNION ALL
263
+ SELECT
264
+ 'payment',
265
+ p.target_key,
266
+ p.platform,
267
+ NULL,
268
+ NULL,
269
+ p.occurred_at,
270
+ COALESCE(p.item_desc, p.counterparty, '支付'),
271
+ p.counterparty,
272
+ COALESCE(p.analysis_category, p.platform_category),
273
+ p.sensitivity,
274
+ p.net_amount_cents,
275
+ 'payment',
276
+ CASE WHEN p.effective_refund_cents > 0 THEN 'payment' ELSE 'none' END,
277
+ 0,
278
+ COALESCE(b.beneficiaries_json, '[]'),
279
+ p.hidden,
280
+ CASE
281
+ WHEN p.item_desc LIKE '%火车票%'
282
+ OR p.item_desc LIKE '%高铁票%'
283
+ OR p.item_desc LIKE '%12306%'
284
+ OR p.counterparty LIKE '%铁路%'
285
+ OR p.counterparty LIKE '%12306%'
286
+ OR p.analysis_category IN ('火车票', '车票', 'train_ticket', 'train')
287
+ THEN 1
288
+ ELSE 0
289
+ END,
290
+ CAST(EXISTS(
291
+ SELECT
292
+ 1
293
+ FROM payment_order_links AS l
294
+ WHERE
295
+ l.payment_platform = p.platform
296
+ AND l.payment_txn_id = p.txn_id
297
+ AND l.deleted_at IS NULL
298
+ AND l.confidence <> 'high'
299
+ AND l.match_method <> 'manual'
300
+ ) AS INT),
301
+ p.owner_person_id
302
+ FROM v_payments AS p
303
+ LEFT JOIN _v_beneficiary_groups AS b
304
+ ON b.target_type = 'payment' AND b.target_key = p.target_key
305
+ WHERE
306
+ p.direction = 'expense'
307
+ AND p.is_refund = 0
308
+ AND p.is_transfer = 0
309
+ AND NOT EXISTS(
310
+ SELECT
311
+ 1
312
+ FROM _v_trusted_payment_orders AS l
313
+ WHERE
314
+ l.payment_platform = p.platform AND l.payment_txn_id = p.txn_id
315
+ )
316
+ )
317
+ SELECT
318
+ source_type,
319
+ source_key,
320
+ platform,
321
+ order_no,
322
+ item_key,
323
+ occurred_at,
324
+ title,
325
+ shop_name,
326
+ category,
327
+ sensitivity,
328
+ amount_cents,
329
+ amount_cents / NULLIF(100.0, 0) AS amount_yuan,
330
+ amount_basis,
331
+ refund_basis,
332
+ amount_is_estimated,
333
+ beneficiaries_json,
334
+ needs_match_review,
335
+ owner_person_id
336
+ FROM rows
337
+ WHERE
338
+ hidden = 0 AND is_train = 0 AND (
339
+ amount_cents IS NULL OR amount_cents > 0
340
+ );
341
+
342
+ -- _v_spending_quality:原为 s.*(17 列),改显式列表后追加 owner_person_id,原列顺序不变。
343
+ CREATE OR REPLACE VIEW _v_spending_quality AS
344
+ WITH allocations AS (
345
+ SELECT
346
+ source_type,
347
+ source_key,
348
+ SUM(allocated_cents) AS allocated_cents,
349
+ MAX(invalid_shares) AS invalid_shares
350
+ FROM _v_spending_share_rows
351
+ GROUP BY
352
+ source_type,
353
+ source_key
354
+ )
355
+ SELECT
356
+ s.source_type,
357
+ s.source_key,
358
+ s.platform,
359
+ s.order_no,
360
+ s.item_key,
361
+ s.occurred_at,
362
+ s.title,
363
+ s.shop_name,
364
+ s.category,
365
+ s.sensitivity,
366
+ s.amount_cents,
367
+ s.amount_yuan,
368
+ s.amount_basis,
369
+ s.refund_basis,
370
+ s.amount_is_estimated,
371
+ s.beneficiaries_json,
372
+ s.needs_match_review,
373
+ COALESCE(a.invalid_shares, 0) AS invalid_shares,
374
+ CASE
375
+ WHEN s.amount_cents IS NULL
376
+ THEN NULL
377
+ ELSE s.amount_cents - COALESCE(a.allocated_cents, 0)
378
+ END AS unassigned_cents,
379
+ s.owner_person_id
380
+ FROM v_spending AS s
381
+ LEFT JOIN allocations AS a
382
+ ON a.source_type = s.source_type AND a.source_key = s.source_key;
383
+
384
+
385
+ -- v_us_spending_unassigned:同理改显式列表,owner_person_id 追加在 reason 之后。
386
+ CREATE OR REPLACE VIEW v_us_spending_unassigned AS
387
+ SELECT
388
+ source_type,
389
+ source_key,
390
+ platform,
391
+ order_no,
392
+ item_key,
393
+ occurred_at,
394
+ title,
395
+ shop_name,
396
+ category,
397
+ sensitivity,
398
+ amount_cents,
399
+ amount_yuan,
400
+ amount_basis,
401
+ refund_basis,
402
+ amount_is_estimated,
403
+ beneficiaries_json,
404
+ needs_match_review,
405
+ invalid_shares,
406
+ unassigned_cents,
407
+ CASE
408
+ WHEN needs_match_review = 1
409
+ THEN 'pending_match'
410
+ WHEN amount_cents IS NULL
411
+ THEN 'unknown_amount'
412
+ WHEN invalid_shares = 1
413
+ THEN 'invalid_shares'
414
+ ELSE 'unassigned_beneficiary'
415
+ END AS reason,
416
+ owner_person_id
417
+ FROM _v_spending_quality
418
+ WHERE
419
+ needs_match_review = 1
420
+ OR amount_cents IS NULL
421
+ OR invalid_shares = 1
422
+ OR unassigned_cents > 0;
423
+
424
+
425
+ -- v_us_spending_by_beneficiary:保持受益人口径(不过滤),多一列付款归属人;“花在她身上”的统计在 q_spend_on_her_* 里再取我名下。
426
+ CREATE OR REPLACE VIEW v_us_spending_by_beneficiary AS
427
+ SELECT
428
+ s.source_type,
429
+ s.source_key,
430
+ s.platform,
431
+ s.occurred_at,
432
+ s.category,
433
+ s.title,
434
+ s.sensitivity,
435
+ b.person_id,
436
+ b.ratio AS share_ratio,
437
+ b.allocated_cents AS amount_cents,
438
+ b.allocated_cents / NULLIF(100.0, 0) AS amount_yuan,
439
+ s.amount_is_estimated,
440
+ s.needs_match_review,
441
+ s.owner_person_id
442
+ FROM v_spending AS s
443
+ JOIN _v_spending_share_rows AS b
444
+ ON b.source_type = s.source_type AND b.source_key = s.source_key
445
+ WHERE
446
+ b.invalid_shares = 0 AND b.ratio > 0;
447
+
448
+ -- q_spend_on_her_yearly:Q2 只统计我名下账户付的钱
449
+ CREATE OR REPLACE VIEW q_spend_on_her_yearly AS
450
+ WITH years AS (
451
+ SELECT DISTINCT
452
+ SUBSTRING(occurred_at FROM 1 FOR 4) AS year
453
+ FROM v_spending
454
+ WHERE
455
+ owner_person_id = VAULT_SELF_PERSON_ID()
456
+ ), amounts AS (
457
+ SELECT
458
+ SUBSTRING(s.occurred_at FROM 1 FOR 4) AS year,
459
+ SUM(s.amount_cents) AS amount_cents
460
+ FROM v_us_spending_by_beneficiary AS s
461
+ JOIN _v_module_people AS p
462
+ ON p.id = s.person_id AND p.module = 'her'
463
+ WHERE
464
+ s.needs_match_review = 0 AND s.owner_person_id = VAULT_SELF_PERSON_ID()
465
+ GROUP BY
466
+ SUBSTRING(s.occurred_at FROM 1 FOR 4)
467
+ ), quality AS (
468
+ SELECT
469
+ SUBSTRING(occurred_at FROM 1 FOR 4) AS year,
470
+ SUM(CAST(amount_cents IS NULL AS INT)) AS unknown_amount_count,
471
+ SUM(CAST(amount_is_estimated = 1 AND NOT amount_cents IS NULL AS INT)) AS estimated_count,
472
+ SUM(needs_match_review) AS pending_match_count,
473
+ SUM(CASE WHEN unassigned_cents > 0 OR invalid_shares = 1 THEN 1 ELSE 0 END) AS unassigned_count
474
+ FROM _v_spending_quality
475
+ WHERE
476
+ owner_person_id = VAULT_SELF_PERSON_ID()
477
+ GROUP BY
478
+ SUBSTRING(occurred_at FROM 1 FOR 4)
479
+ )
480
+ SELECT
481
+ y.year,
482
+ COALESCE(a.amount_cents, 0) AS amount_cents,
483
+ COALESCE(a.amount_cents, 0) / NULLIF(100.0, 0) AS amount_yuan,
484
+ q.unknown_amount_count,
485
+ q.estimated_count,
486
+ q.pending_match_count,
487
+ q.unassigned_count
488
+ FROM years AS y
489
+ LEFT JOIN amounts AS a
490
+ ON a.year IS NOT DISTINCT FROM y.year
491
+ JOIN quality AS q
492
+ ON q.year IS NOT DISTINCT FROM y.year;
493
+
494
+ CREATE OR REPLACE VIEW q_spend_on_her_2026 AS
495
+ SELECT
496
+ COALESCE(SUM(amount_cents), 0) / NULLIF(100.0, 0) AS "value",
497
+ COALESCE(SUM(amount_cents), 0) AS amount_cents,
498
+ COALESCE(SUM(unknown_amount_count), 0) AS unknown_amount_count,
499
+ COALESCE(SUM(estimated_count), 0) AS estimated_count,
500
+ COALESCE(SUM(pending_match_count), 0) AS pending_match_count,
501
+ COALESCE(SUM(unassigned_count), 0) AS unassigned_count,
502
+ (
503
+ SELECT
504
+ COUNT(*)
505
+ FROM v_spending
506
+ WHERE
507
+ occurred_at IS NULL AND owner_person_id = VAULT_SELF_PERSON_ID()
508
+ ) AS undated_count
509
+ FROM q_spend_on_her_yearly
510
+ WHERE
511
+ year = '2026';
512
+
513
+ -- v_me_spending_monthly(q_spending_monthly 读它):Q2 只含我名下账户
514
+ CREATE OR REPLACE VIEW v_me_spending_monthly AS
515
+ SELECT
516
+ SUBSTRING(occurred_at FROM 1 FOR 7) AS month,
517
+ platform,
518
+ COALESCE(category, '未分类') AS category,
519
+ COUNT(*) AS row_count,
520
+ SUM(CASE WHEN needs_match_review = 0 THEN amount_cents END) AS amount_cents,
521
+ SUM(CASE WHEN needs_match_review = 0 THEN amount_cents END) / NULLIF(100.0, 0) AS amount_yuan,
522
+ SUM(
523
+ CASE WHEN needs_match_review = 0 AND amount_is_estimated = 0 THEN amount_cents END
524
+ ) AS exact_cents,
525
+ SUM(
526
+ CASE
527
+ WHEN needs_match_review = 0 AND amount_is_estimated = 1
528
+ THEN amount_cents
529
+ ELSE 0
530
+ END
531
+ ) AS estimated_cents,
532
+ SUM(CASE WHEN needs_match_review = 0 THEN unassigned_cents ELSE 0 END) AS unassigned_cents,
533
+ SUM(CAST(amount_cents IS NULL AS INT)) AS unknown_amount_count,
534
+ SUM(needs_match_review) AS pending_match_count,
535
+ SUM(CASE WHEN needs_match_review = 1 THEN amount_cents ELSE 0 END) AS pending_candidate_cents,
536
+ SUM(invalid_shares) AS invalid_shares_count
537
+ FROM _v_spending_quality
538
+ WHERE
539
+ owner_person_id = VAULT_SELF_PERSON_ID()
540
+ GROUP BY
541
+ SUBSTRING(occurred_at FROM 1 FOR 7),
542
+ platform,
543
+ COALESCE(category, '未分类');
544
+
545
+ -- v_money_flow(v_us_money_flow、q_money_* 读它):Q21 只取我名下账户,避免同一笔钱在双方账户里各算一遍
546
+ CREATE OR REPLACE VIEW v_money_flow AS
547
+ WITH participants AS (
548
+ SELECT
549
+ p.*,
550
+ me.id AS self_id,
551
+ partner.id AS partner_id,
552
+ CASE WHEN p.direction = 'expense' THEN me.id ELSE partner.id END AS from_person_id,
553
+ CASE WHEN p.direction = 'expense' THEN partner.id ELSE me.id END AS to_person_id
554
+ FROM v_payments AS p
555
+ JOIN persons AS me
556
+ ON me.is_self = 1 AND me.deleted_at IS NULL
557
+ JOIN persons AS partner
558
+ ON partner.is_default = 1 AND partner.deleted_at IS NULL
559
+ WHERE
560
+ p.owner_person_id = me.id
561
+ AND p.counterparty_person_id = partner.id
562
+ AND p.is_transfer = 1
563
+ AND p.is_refund = 0
564
+ AND p.direction IN ('expense', 'income')
565
+ AND p.hidden = 0
566
+ AND p.net_amount_cents > 0
567
+ )
568
+ SELECT
569
+ SUBSTRING(occurred_at FROM 1 FOR 10) AS flow_on,
570
+ from_person_id,
571
+ to_person_id,
572
+ COUNT(*) AS transfer_count,
573
+ SUM(amount_cents) AS gross_cents,
574
+ SUM(effective_refund_cents) AS refund_cents,
575
+ SUM(net_amount_cents) AS amount_cents,
576
+ SUM(net_amount_cents) / NULLIF(100.0, 0) AS amount_yuan
577
+ FROM participants
578
+ GROUP BY
579
+ SUBSTRING(occurred_at FROM 1 FOR 10),
580
+ from_person_id,
581
+ to_person_id;
582
+
583
+ -- v_spending_by_location:Q2 只含我名下账户
584
+ CREATE OR REPLACE VIEW v_spending_by_location AS
585
+ SELECT
586
+ p.city,
587
+ coalesce(p.district, '主城区') AS district,
588
+ coalesce(p.area_landmark, '其他商户') AS area_landmark,
589
+ count(DISTINCT s.source_key) AS order_count,
590
+ round(sum(s.amount_cents) / 100.0, 2) AS total_amount_yuan
591
+ FROM spending_location_links l
592
+ JOIN spending_places p ON p.id = l.place_id
593
+ JOIN v_spending s ON (
594
+ (
595
+ (l.target_type = 'order' AND s.source_type IN ('order', 'order_item') AND s.platform = l.target_platform AND s.order_no = l.target_key)
596
+ OR
597
+ (l.target_type = 'payment' AND s.source_type = 'payment' AND s.platform = l.target_platform AND s.source_key = (l.target_platform || ':' || l.target_key))
598
+ )
599
+ AND s.owner_person_id = vault_self_person_id()
600
+ )
601
+ GROUP BY p.city, p.district, p.area_landmark
602
+ ORDER BY total_amount_yuan DESC;
603
+
604
+ -- v_spending_by_scene:Q2 只含我名下账户
605
+ CREATE OR REPLACE VIEW v_spending_by_scene AS
606
+ SELECT
607
+ CASE l.scene
608
+ WHEN 'dating' THEN '🌹 浪漫约会与心意礼物'
609
+ WHEN 'home_daily' THEN '🏠 居家日常与生活百货'
610
+ WHEN 'dining_out' THEN '🍽️ 外出堂食与便民快餐'
611
+ WHEN 'car_transit' THEN '🚗 车辆保养与通勤路费'
612
+ WHEN 'medical' THEN '💊 医疗健康与药房就医'
613
+ WHEN 'shopping' THEN '🛍️ 商场购物与服饰专柜'
614
+ ELSE '📦 其他日常消费'
615
+ END AS scene_name,
616
+ count(DISTINCT s.source_key) AS count,
617
+ round(sum(s.amount_cents) / 100.0, 2) AS total_amount_yuan
618
+ FROM spending_location_links l
619
+ JOIN v_spending s ON (
620
+ (
621
+ (l.target_type = 'order' AND s.source_type IN ('order', 'order_item') AND s.platform = l.target_platform AND s.order_no = l.target_key)
622
+ OR
623
+ (l.target_type = 'payment' AND s.source_type = 'payment' AND s.platform = l.target_platform AND s.source_key = (l.target_platform || ':' || l.target_key))
624
+ )
625
+ AND s.owner_person_id = vault_self_person_id()
626
+ )
627
+ GROUP BY l.scene
628
+ ORDER BY total_amount_yuan DESC;
629
+
630
+ -- v_cross_platform_orders:多一列下单账户归属人,不过滤
631
+ CREATE OR REPLACE VIEW v_cross_platform_orders AS
632
+ WITH order_item_agg AS (
633
+ SELECT
634
+ platform,
635
+ order_no,
636
+ string_agg(product_name, ' + ' ORDER BY line_no) AS products_summary,
637
+ count(*) AS item_count
638
+ FROM order_items
639
+ GROUP BY platform, order_no
640
+ ),
641
+ linked_chains AS (
642
+ SELECT
643
+ ol.order_platform,
644
+ ol.order_no,
645
+ p.platform AS payment_platform,
646
+ p.txn_id AS payment_txn_id,
647
+ p.pay_method AS payment_method,
648
+ b.bank_name AS bank_card_name,
649
+ b.card_last_four,
650
+ b.amount_cents AS bank_paid_cents
651
+ FROM payment_order_links ol
652
+ JOIN payments p
653
+ ON p.platform = ol.payment_platform AND p.txn_id = ol.payment_txn_id
654
+ LEFT JOIN payment_bank_links bl
655
+ ON bl.payment_platform = p.platform AND bl.payment_txn_id = p.txn_id AND bl.state = 'confirmed'
656
+ LEFT JOIN _v_finance_bank_entries b
657
+ ON b.transaction_key = COALESCE(bl.debit_transaction_key, bl.credit_transaction_key)
658
+ WHERE ol.deleted_at IS NULL
659
+ )
660
+ SELECT
661
+ o.platform AS order_platform,
662
+ CASE o.platform
663
+ WHEN 'taobao' THEN '淘宝/天猫'
664
+ WHEN 'jd' THEN '京东商城'
665
+ WHEN 'meituan' THEN '美团外卖'
666
+ WHEN 'pdd' THEN '拼多多'
667
+ WHEN 'douyin' THEN '抖音电商'
668
+ WHEN 'taobao_takeout' THEN '淘宝闪购'
669
+ ELSE o.platform
670
+ END AS order_platform_name,
671
+ o.order_no,
672
+ coalesce(o.paid_at, o.ordered_at) AS ordered_at,
673
+ o.status AS order_status,
674
+ o.shop_name,
675
+ coalesce(ia.products_summary, o.shop_name, '商品') AS products_summary,
676
+ o.total_paid_cents AS order_paid_cents,
677
+ round(o.total_paid_cents / 100.0, 2) AS order_paid_yuan,
678
+ lc.payment_platform,
679
+ lc.payment_method,
680
+ lc.bank_card_name,
681
+ lc.card_last_four,
682
+ lc.bank_paid_cents,
683
+ CASE
684
+ WHEN o.status IN ('交易关闭', '已退款', '已取消') THEN 'closed'
685
+ WHEN lc.bank_paid_cents IS NOT NULL THEN 'three_tier'
686
+ WHEN lc.payment_platform IS NOT NULL THEN 'two_tier'
687
+ WHEN o.platform = 'pdd' AND o.status = '待收货' THEN 'pending_settle'
688
+ ELSE 'unlinked'
689
+ END AS chain_state,
690
+ o.person_id AS owner_person_id
691
+ FROM orders o
692
+ LEFT JOIN order_item_agg ia
693
+ ON ia.platform = o.platform AND ia.order_no = o.order_no
694
+ LEFT JOIN linked_chains lc
695
+ ON lc.order_platform = o.platform AND lc.order_no = o.order_no;
696
+
697
+ -- v_typed_orders_explorer:多一列下单账户归属人,不过滤
698
+ CREATE OR REPLACE VIEW v_typed_orders_explorer AS
699
+ SELECT
700
+ o.platform,
701
+ o.order_no,
702
+ coalesce(o.paid_at, o.ordered_at) AS occurred_at,
703
+ round(o.total_paid_cents / 100.0, 2) AS amount_yuan,
704
+ o.status AS order_status,
705
+ CASE
706
+ WHEN t.order_no IS NOT NULL THEN '即时外卖'
707
+ WHEN v.order_no IS NOT NULL THEN '到店团购'
708
+ WHEN p.order_no IS NOT NULL THEN '电商快递'
709
+ WHEN o.shop_name = '火车票' THEN '城际高铁'
710
+ ELSE '其他消费'
711
+ END AS order_category,
712
+ CASE
713
+ WHEN t.order_no IS NOT NULL THEN concat_ws(' · 送至:', t.shop_branch, t.delivery_address)
714
+ WHEN v.order_no IS NOT NULL THEN concat_ws(' · ', v.applicable_mall, v.merchant_name)
715
+ WHEN p.order_no IS NOT NULL THEN concat_ws(' · 收件:', p.carrier, p.shipping_address)
716
+ WHEN o.shop_name = '火车票' THEN '京沪高铁沿线车站'
717
+ ELSE o.shop_name
718
+ END AS consumption_place,
719
+ CASE
720
+ WHEN t.order_no IS NOT NULL THEN t.shop_name
721
+ WHEN v.order_no IS NOT NULL THEN v.package_title
722
+ WHEN p.order_no IS NOT NULL THEN concat_ws(' ', p.goods_name, p.goods_spec)
723
+ WHEN o.shop_name = '火车票' THEN '12306 车票'
724
+ ELSE o.shop_name
725
+ END AS order_detail,
726
+ o.person_id AS owner_person_id
727
+ FROM orders o
728
+ LEFT JOIN ext_takeout_orders t ON t.order_platform = o.platform AND t.order_no = o.order_no
729
+ LEFT JOIN ext_voucher_orders v ON v.order_platform = o.platform AND v.order_no = o.order_no
730
+ LEFT JOIN ext_parcel_orders p ON p.order_platform = o.platform AND p.order_no = o.order_no;
731
+
732
+ -- _v_finance_bank_entries:经 bank_cards 带出持卡人(流水自身不存人物列)
733
+ CREATE OR REPLACE VIEW _v_finance_bank_entries AS
734
+ SELECT 'debit'::text AS kind,t.transaction_key,t.source_kind,t.bank_card_id,
735
+ t.occurred_at,t.date_precision,t.currency,t.amount_cents,t.direction,
736
+ CASE WHEN t.direction='out' THEN 'outflow' ELSE 'inflow' END AS transaction_kind,
737
+ t.card_last_four,coalesce(c.bank_name,'') AS bank_name,
738
+ concat_ws(' ',t.counterparty,t.description,t.institution,t.bank_txn_id,t.transaction_card) AS description,
739
+ t.bank_txn_id AS reference,t.last_batch_id,
740
+ c.person_id AS owner_person_id
741
+ FROM bank_card_transactions t LEFT JOIN bank_cards c ON c.id=t.bank_card_id AND c.deleted_at IS NULL
742
+ UNION ALL
743
+ SELECT 'credit',t.transaction_key,t.source_kind,t.bank_card_id,
744
+ coalesce(nullif(t.occurred_at,''),nullif(t.transaction_date,''),t.posting_date),
745
+ coalesce(t.date_precision,'day'),t.settlement_currency,abs(t.settlement_amount_cents),
746
+ CASE WHEN t.settlement_amount_cents>0 THEN 'out' ELSE 'in' END,t.transaction_kind,
747
+ t.card_last_four,coalesce(c.bank_name,''),
748
+ concat_ws(' ',t.description,t.counterparty,t.payment_channel,t.remark),'' AS reference,t.last_batch_id,
749
+ c.person_id AS owner_person_id
750
+ FROM credit_card_transactions t LEFT JOIN bank_cards c ON c.id=t.bank_card_id AND c.deleted_at IS NULL;