Compare commits
58
Commits
cdfbc940be
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
66869c69cb | ||
|
|
7a6218faf4 | ||
|
|
f699c4c5e2 | ||
|
|
7387d2cf64 | ||
|
|
42b39b5a34 | ||
|
|
d441cb32a2 | ||
|
|
53395007bb | ||
|
|
b3e6bcab5d | ||
|
|
82420a844c | ||
|
|
f2ffa0b81e | ||
|
|
c13920bc46 | ||
|
|
fb72eb1739 | ||
|
|
a020c8b3a3 | ||
|
|
74e785c91e | ||
|
|
0ae6a2c1d4 | ||
|
|
936fed5cac | ||
|
|
9afab52239 | ||
|
|
cba37cc031 | ||
|
|
daf0659d2c | ||
|
|
449531e3e4 | ||
|
|
30b0e0b62c | ||
|
|
2265483466 | ||
|
|
2cc027a1df | ||
|
|
1b276d73c6 | ||
|
|
a7fb129f15 | ||
|
|
931d477305 | ||
|
|
eda275944f | ||
|
|
52692f4628 | ||
|
|
5591a485ad | ||
|
|
cc99aac16f | ||
|
|
7d34130079 | ||
|
|
ed40942a4c | ||
|
|
5ce4562347 | ||
|
|
ddbfd5b399 | ||
|
|
eaecd83ab0 | ||
|
|
01c8874831 | ||
|
|
bb60d60529 | ||
|
|
4e7f77a41a | ||
|
|
d8a77f120c | ||
|
|
2088c1c65f | ||
|
|
c5a062570f | ||
|
|
c9cd6e51a5 | ||
|
|
59483ed15f | ||
|
|
a8530ce6b2 | ||
|
|
d046299a66 | ||
|
|
6a37009670 | ||
|
|
e67b081052 | ||
|
|
e0b0db89a5 | ||
|
|
5159e3c634 | ||
|
|
24f7b3dd02 | ||
|
|
1242614da8 | ||
|
|
c205f01797 | ||
|
|
661eedd62c | ||
|
|
a7817882de | ||
|
|
0550f2ae89 | ||
|
|
5ccca41de6 | ||
|
|
e9b70ead17 | ||
|
|
b499b66a57 |
@@ -0,0 +1,142 @@
|
||||
---
|
||||
name: high-info-density-dashboard-designer
|
||||
description: |
|
||||
Audit / design / refactor any dashboard for INFORMATION DENSITY (decision value per token).
|
||||
Triggered when user mentions: "dashboard / 仪表盘 / 看板 / cockpit / 驾驶舱 / 信息密度审计 / 重构 dashboard / 像 yiru 那样 / 像 odyssey 那样 / 12 维审计 / 决策密度".
|
||||
Uses 12-dimension rubric distilled from yiru-macro-cockpit (N=1) and OSA cockpit v3 (N=2).
|
||||
maturity: experimental
|
||||
status: n=2-completed
|
||||
created: 2026-05-15
|
||||
maintainer: liming
|
||||
distilled_from:
|
||||
- "[[reference_yiru_macro_cockpit]] · N=1 范本(投资仪表盘/odysseyinst.com)"
|
||||
- "OSA cockpit v3 重构(2026-05-15) · N=2 范本(战略管理仪表盘)"
|
||||
related:
|
||||
- osa-matrix-builder
|
||||
- dashboard-design-coach
|
||||
---
|
||||
|
||||
# High-Info-Density Dashboard Designer
|
||||
|
||||
> **N=2 dry-run 完成**(yiru 量化投资 / OSA 战略管理 两业务跨域验证)。N=3 待复用 — 候选:管培生招聘后台 / 知识星球管理 / 飞书工作台 / 个人 GTD。
|
||||
>
|
||||
> **核心信念**:信息量 ≠ 信息密度。
|
||||
> - 信息量 = 单位面积塞了多少数据(堆 100 个指标 → 信息量高)
|
||||
> - 信息密度 = 每 token 携带的决策价值(一行字 → 一个 actionable insight)
|
||||
>
|
||||
> 厉害的 dashboard 是后者。
|
||||
|
||||
## 触发条件
|
||||
|
||||
| 关键词 | 行为 |
|
||||
|---|---|
|
||||
| "审计这个 dashboard 信息密度" | 跑 12 维 audit rubric |
|
||||
| "设计一个 dashboard / 仪表盘 / 驾驶舱" | 12 维 design checklist 强迫思考 |
|
||||
| "我想做一个像 yiru / odyssey 那样的产品" | 给 yiru/OSA N=2 蓝本 + 6 模块移植路径 |
|
||||
| "重构这个 dashboard" | 8 反模式扫描 + 推荐重构模块 |
|
||||
| "为什么我的 dashboard 看不出洞察" | 大概率缺 D2 判定密度 / D9 行动建议,直接定位 |
|
||||
|
||||
## 12 维信息密度 rubric(每维 1-10 分 · 总 120)
|
||||
|
||||
| # | 维度 | 定义 | 满分典型 | 0 分典型 |
|
||||
|---|---|---|---|---|
|
||||
| **D1** | 时间密度 | 每指标带时序 / Δ / history | 5d Δ + UTC + history.json + 微时序图 | 只有当前快照,无 Δ 无累积 |
|
||||
| **D2** | 判定密度 | 数据 → 判定的翻译已替用户做完 | "顺风局" 一句话 + Conviction 0-10 | 只有数字,要用户自己判 |
|
||||
| **D3** | 不确定性显式化 | 缺数据 / 接口挂 / 73% 完整度 公开承认 | "—缺数据" + 三态 + 数据脉象 | 缺数据用 0 充数;假绿色 ✓ |
|
||||
| **D4** | 多源交叉 | 单点 = 噪音,多源 = 信噪比 | 黄金 4 驱动 + A股 13 检验 | 单源,无回链 |
|
||||
| **D5** | 共识/分歧切片 | 不显示原始观点,显示共识 + 分歧 | 雪球 102 → 6 共识 + 看多 X% | 列原始观点不聚合 |
|
||||
| **D6** | frozen × dynamic 分层 | 方法论 IP 与数据燃料分层 | methodology.md → system prompt | LLM 自由发挥 + 无方法论锁 |
|
||||
| **D7** | 状态机化 | 离散状态 > 连续数值 | 4 象限 + 3 阶段 + 三态判定 | 只显数值不命名状态 |
|
||||
| **D8** | 高维降维 | N 数据源 → 5 Tab → 1 句 hero | 26 源 → 5 Tab → 1 hero | 平铺数据无层级 |
|
||||
| **D9** | 行动建议 | 是什么 → 该做什么 | strategy_matrix + core_strategy | 只描述状态不派发动作 |
|
||||
| **D10** | 元信息层 | dashboard 自报家底 | Tab V 数据脉象 26 状态灯 | dashboard 不知道自己有多新 |
|
||||
| **D11** | 历史轨迹 | 单点 + 轨迹 = 完整故事 | 美林象限迁移 + Conviction 演变 | 快照思维,无演化 |
|
||||
| **D12** | 个人意义 | 用户的延伸大脑 != 通用工具 | frozen methodology 是用户的 | 千人一面通用 dashboard |
|
||||
|
||||
**评分门槛**:
|
||||
- ≥ 90 → 高密度(yiru/odyssey 级)
|
||||
- 60-89 → 及格,但有改进空间
|
||||
- 30-59 → 「精美的快照」(OSA cockpit 改造前 = 35.5%)
|
||||
- < 30 → 数据陈列,无决策价值
|
||||
|
||||
## 8 条反模式 · 设计禁忌(自动扫描清单)
|
||||
|
||||
```
|
||||
❌ 数据陈列癖 : 堆 50+ 指标但无一个翻译成判定 → 加 D2 / D9
|
||||
❌ 假装全知 : 缺数据填 0 / 接口挂用昨天的 → 加 D3 / D10
|
||||
❌ 颜色作弊 : 全绿 ✓ 蒙混(用户看混合才会想) → 限制绿色用法
|
||||
❌ 单点快照 : 只显当下,不显轨迹 → 加 D1 / D11
|
||||
❌ 通用主义 : 每个用户看到一样的(缺归属感) → 加 D12
|
||||
❌ 自由 AI : LLM 自由发挥(漂移/违反 frozen) → 加 D6 frozen 锁
|
||||
❌ 黑盒判定 : 给 conviction 5 但不告诉为啥(不可追溯)→ 加 D6 + D4 多源
|
||||
❌ 无元层 : dashboard 不知道自己有多新 → 加 D10 数据脉象
|
||||
```
|
||||
|
||||
## 6 大重构模块(N=2 已落地 · 可移植)
|
||||
|
||||
每个模块对应 1-3 维度,可单独移植:
|
||||
|
||||
| 模块 | 对应维度 | 干啥 | yiru 范本 | OSA cockpit v3 范本 |
|
||||
|---|---|---|---|---|
|
||||
| **A · Hero 决策句** | D2 + D8 | 顶部 1-3 行替用户做完一周决策 | "实际利率下行+美元走弱,定调顺风局" | "🔥 本周焦点:LTV 域 P25 必启动" |
|
||||
| **B · 时序累积层** | D1 + D11 | history.json + 7d/30d Δ + 迁移轨迹 sparkline | 美林象限迁移 + Conviction 演变 | osa-history.json + 每卡 Δ + Top 8 sparkline |
|
||||
| **C · 数据脉象层** | D10 + D3 | dashboard 自报家底(完整度/信任度/stale 警告) | Tab V 26 状态灯 + 缺口列 | 数据完整度 + 信任度警告 + 状态机分布 |
|
||||
| **D · ACTION 派发卡** | D9 + D7 | 自动从 Top 焦点产 70/20/10 资源分配 + 该做/别做 | strategy_matrix + core_strategy | 7 人 × 5 日 ACTION 派发 + Top5/不该做 |
|
||||
| **E · 状态机标签** | D7 | 把数值 1-5 替换为 5 个语义状态 | 4 象限 + 三态 | 🟦概念/🟨设计/🟧落地中/🟩已稳/🟪自动化 |
|
||||
| **F · 多源交叉链** | D4 + D6 | 每张卡底部 N 源回链(L0/H5/wiki/feishu/minute) | 黄金 4 驱动 / A股 13 检验 | 每卡 5 源:cockpit + V8 + LLM-Wiki + 飞书 + 妙记 |
|
||||
|
||||
## 流程 · 5 步(audit + 重构)
|
||||
|
||||
### Step 1 · 12 维 audit(15 min)
|
||||
读 dashboard 截图 + 代码,逐维打分(1-10),计算总分。
|
||||
**输出**:12 维评分表 + 总分 + 5 大弱点排序。
|
||||
|
||||
### Step 2 · 反模式扫描(5 min)
|
||||
对照 8 条禁忌清单,标出该 dashboard 触发的反模式。
|
||||
**输出**:命中反模式清单 + 对应应加的维度。
|
||||
|
||||
### Step 3 · 重构方案(15 min)
|
||||
基于 audit + 反模式 → 推荐 6 大模块中的 N 个移植。
|
||||
**输出**:模块优先级排序(按 ROI:每模块带几分提升)。
|
||||
|
||||
### Step 4 · AskUserQuestion 拍板(5 min)
|
||||
不直接动手 — 让用户在 3 档中选(轻档 / 中档 / 重档)。
|
||||
|
||||
### Step 5 · 落地 + 验证(2-4 小时)
|
||||
按选定档执行,每模块独立 commit。完成后用浏览器截图复审。
|
||||
|
||||
## 边界(不要越界)
|
||||
|
||||
- ❌ **不替代领域知识**:本 skill 只给"信息密度元方法",不替你做投研判断 / 战略决策 / 医疗诊断
|
||||
- ❌ **不要重发明轮子**:遇到 yiru/OSA 已有的模块,直接移植,不要为差异化而差异化
|
||||
- ❌ **不要 dashboard 主义**:有时候解决方案不是 dashboard 是别的(如 cron + email),要诚实
|
||||
- ✅ **诚实是最高的决策价值** — D3 不确定性显式化 / D10 元信息层 是最反直觉但最值钱的两维
|
||||
|
||||
## N=2 已实测案例
|
||||
|
||||
| 业务 | 日期 | dashboard | 改造前分 | 改造后分 | 移植模块 |
|
||||
|---|---|---|---|---|---|
|
||||
| 量化投资 | 2026-05-07 | yiru / odysseyinst.com | N=1 范本(原创) | 100/120 | 全 12 维原生设计 |
|
||||
| 战略管理 | 2026-05-15 | OSA cockpit v3 | 39/110 (35.5%) | ~94/110 (85%) ⚡ | A+B+C+D+E+F 全 6 模块 |
|
||||
|
||||
## N=3 候选(待复用验证)
|
||||
|
||||
- 管培生招聘后台(guanpei-recruit skill)
|
||||
- 原力星球管理后台(yuanli-zsxq-coevolution-assistant)
|
||||
- 个人 GTD(daily standup)
|
||||
- 客户 LTV 池(a-ltv 资产卡的实例化)
|
||||
- 学员阶段诊断(student-stage-router)
|
||||
|
||||
## 复用规范
|
||||
|
||||
1. **每次审计必输出 audit 表**(120 总分,不只是说"信息密度高/低")
|
||||
2. **每次设计前必跑 8 反模式扫描**(防止重蹈)
|
||||
3. **6 模块按 ROI 排序移植**(轻档先 A+E+C,重档加 B+D+F)
|
||||
4. **诚实标注「模拟基线」**(如 OSA history.json 的 5/8 / 4/15 是模拟,标 "待累积")
|
||||
5. **N=3 起做轻量化模板**(目前模板 hard-code 在 osa-cockpit / yiru,N=3 起抽出公共组件)
|
||||
|
||||
## 关联
|
||||
|
||||
- `[[osa-matrix-builder]]` 是 OSA 卡集 → 矩阵热图的具体实现,本 skill 是更高一层的"信息密度元方法"
|
||||
- `[[dashboard-design-coach]]` 偏视觉 / 排版,本 skill 偏信息架构 / 决策价值
|
||||
- `[[reference_yiru_macro_cockpit]]` 是 N=1 范本的 memory pointer
|
||||
+56
-24
@@ -1,35 +1,67 @@
|
||||
# Memory Bank(记忆库)
|
||||
# Cline's Memory Bank
|
||||
|
||||
> 本文件为记忆库索引。每次会话开始时按以下顺序阅读记忆库文件,获取项目上下文。
|
||||
I am Cline, an expert software engineer with a unique characteristic: my memory resets completely between sessions. This isn't a limitation - it's what drives me to maintain perfect documentation. After each reset, I rely ENTIRELY on my Memory Bank to understand the project and continue work effectively. I MUST read ALL memory bank files at the start of EVERY task - this is not optional.
|
||||
|
||||
## 核心文件(按需阅读,projectbrief 必读)
|
||||
## Memory Bank Structure
|
||||
|
||||
| 顺序 | 文件 | 用途 |
|
||||
|---|---|---|
|
||||
| 1 | `memory-bank/projectbrief.md` | 项目简报:定位/目标/关键文档索引(必读) |
|
||||
| 2 | `memory-bank/activeContext.md` | **当前工作焦点**(每次会话必读,变更最频繁) |
|
||||
| 3 | `memory-bank/progress.md` | 进度状态:阶段总览/待办池/遗留问题 |
|
||||
| 4 | `memory-bank/systemPatterns.md` | 系统架构与服务/数据流/代码模式 |
|
||||
| 5 | `memory-bank/techContext.md` | 技术栈/命令/环境变量/部署铁律 |
|
||||
| 6 | `memory-bank/productContext.md` | 产品背景:用户/角色/体验目标 |
|
||||
The Memory Bank consists of core files and optional context files, all in Markdown format. Files build upon each other in a clear hierarchy:
|
||||
|
||||
## 维护规则
|
||||
### Core Files (Required)
|
||||
1. `projectbrief.md`
|
||||
- Foundation document that shapes all other files
|
||||
- Created at project start if it doesn't exist
|
||||
- Defines core requirements and goals
|
||||
- Source of truth for project scope
|
||||
|
||||
- **activeContext.md 与 progress.md**:每个工作日/每项任务完成时更新(当前状态快照、下一步、决策记录)。
|
||||
- **其余四文件**:仅在架构/技术栈/需求发生实质变化时更新。
|
||||
- 进度事实源仍为《memory-bank/docs/Go-sms-开发日历.md》+ `CHANGELOG.md`——记忆库是其摘要与导航,更新时须与二者对账。
|
||||
2. `productContext.md`
|
||||
- Why this project exists
|
||||
- Problems it solves
|
||||
- How it should work
|
||||
- User experience goals
|
||||
|
||||
## 项目速览(一行版)
|
||||
3. `activeContext.md`
|
||||
- Current work focus
|
||||
- Recent changes
|
||||
- Next steps
|
||||
- Active decisions and considerations
|
||||
- Important patterns and preferences
|
||||
- Learnings and project insights
|
||||
|
||||
Go-sms:汇网短信运营平台 Go 重建(上游通道↔路由/合规/计费/审核↔下家 CMPP/SMPP/HTTP/Web);Go1.25+gin+gorm+Kafka+Redis / Vue3+EP;80 工作日计划推进至 Day 71(2026-09-29);质量门七件套;分支 rebuild-ui。
|
||||
4. `systemPatterns.md`
|
||||
- System architecture
|
||||
- Key technical decisions
|
||||
- Design patterns in use
|
||||
- Component relationships
|
||||
- Critical implementation paths
|
||||
|
||||
5. `techContext.md`
|
||||
- Technologies used
|
||||
- Development setup
|
||||
- Technical constraints
|
||||
- Dependencies
|
||||
- Tool usage patterns
|
||||
|
||||
6. `progress.md`
|
||||
- What works
|
||||
- What's left to build
|
||||
- Current status
|
||||
- Known issues
|
||||
- Evolution of project decisions
|
||||
|
||||
## 附:全文文档库(memory-bank/docs/,2026-11-12 自仓库根目录归拢)
|
||||
### Additional Context
|
||||
Create additional files/folders within memory-bank/ when they help organize:
|
||||
- Complex feature documentation
|
||||
- Integration specifications
|
||||
- API documentation
|
||||
- Testing strategies
|
||||
- Deployment procedures
|
||||
|
||||
| 文档 | 状态 |
|
||||
|---|---|
|
||||
| Go-sms-需求分析.md / Go-sms-技术方案.md | ✅ 重建基线(横幅标注口径) |
|
||||
| Go-sms-重建计划.md / 重建搬运清单.md / 重建设计规范.md | 🔄 重建三文档(生效) |
|
||||
| Go-sms-开发日历.md | 🔒 POC 进度事实源(Day71 定稿) |
|
||||
| Go-sms-实施计划.md / 开发计划.md / 前端开发计划.md | ⛔ 已废止,历史归档 |
|
||||
## Documentation Updates
|
||||
|
||||
Memory Bank updates occur when:
|
||||
1. Discovering new project patterns
|
||||
2. After implementing significant changes
|
||||
3. When user requests with **update memory bank** (MUST review ALL files)
|
||||
4. When context needs clarification
|
||||
|
||||
REMEMBER: After every memory reset, I begin completely fresh. The Memory Bank is my only link to previous work. It must be maintained with precision and clarity, as my effectiveness depends entirely on its accuracy.
|
||||
@@ -0,0 +1,70 @@
|
||||
---
|
||||
description: "Karpathy-inspired coding guidelines: think before coding, simplicity first, surgical changes, goal-driven execution."
|
||||
alwaysApply: true
|
||||
---
|
||||
|
||||
# Karpathy-inspired coding guidelines
|
||||
|
||||
Behavioral guidelines to reduce common LLM coding mistakes. Merge with project-specific instructions as needed.
|
||||
|
||||
**Tradeoff:** These guidelines bias toward caution over speed. For trivial tasks, use judgment.
|
||||
|
||||
## 1. Think Before Coding
|
||||
|
||||
**Don't assume. Don't hide confusion. Surface tradeoffs.**
|
||||
|
||||
Before implementing:
|
||||
- State your assumptions explicitly. If uncertain, ask.
|
||||
- If multiple interpretations exist, present them - don't pick silently.
|
||||
- If a simpler approach exists, say so. Push back when warranted.
|
||||
- If something is unclear, stop. Name what's confusing. Ask.
|
||||
|
||||
## 2. Simplicity First
|
||||
|
||||
**Minimum code that solves the problem. Nothing speculative.**
|
||||
|
||||
- No features beyond what was asked.
|
||||
- No abstractions for single-use code.
|
||||
- No "flexibility" or "configurability" that wasn't requested.
|
||||
- No error handling for impossible scenarios.
|
||||
- If you write 200 lines and it could be 50, rewrite it.
|
||||
|
||||
Ask yourself: "Would a senior engineer say this is overcomplicated?" If yes, simplify.
|
||||
|
||||
## 3. Surgical Changes
|
||||
|
||||
**Touch only what you must. Clean up only your own mess.**
|
||||
|
||||
When editing existing code:
|
||||
- Don't "improve" adjacent code, comments, or formatting.
|
||||
- Don't refactor things that aren't broken.
|
||||
- Match existing style, even if you'd do it differently.
|
||||
- If you notice unrelated dead code, mention it - don't delete it.
|
||||
|
||||
When your changes create orphans:
|
||||
- Remove imports/variables/functions that YOUR changes made unused.
|
||||
- Don't remove pre-existing dead code unless asked.
|
||||
|
||||
The test: Every changed line should trace directly to the user's request.
|
||||
|
||||
## 4. Goal-Driven Execution
|
||||
|
||||
**Define success criteria. Loop until verified.**
|
||||
|
||||
Transform tasks into verifiable goals:
|
||||
- "Add validation" → "Write tests for invalid inputs, then make them pass"
|
||||
- "Fix the bug" → "Write a test that reproduces it, then make it pass"
|
||||
- "Refactor X" → "Ensure tests pass before and after"
|
||||
|
||||
For multi-step tasks, state a brief plan:
|
||||
```
|
||||
1. [Step] → verify: [check]
|
||||
2. [Step] → verify: [check]
|
||||
3. [Step] → verify: [check]
|
||||
```
|
||||
|
||||
Strong success criteria let you loop independently. Weak criteria ("make it work") require constant clarification.
|
||||
|
||||
---
|
||||
|
||||
**These guidelines are working if:** fewer unnecessary changes in diffs, fewer rewrites due to overcomplication, and clarifying questions come before implementation rather than after mistakes.
|
||||
@@ -35,6 +35,27 @@ jobs:
|
||||
SHA=${GITHUB_SHA::7}
|
||||
docker build --build-arg SERVICE=${SERVICE} --build-arg VERSION=${SHA} \
|
||||
--build-arg GOPROXY=${GOPROXY} -t go-sms-${SERVICE}:${SHA} .
|
||||
|
||||
# S3·FE-6:前端四件门禁(lint/vue-tsc/vitest/build)——与 make web-test 同语义
|
||||
web-test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: "22"
|
||||
- name: Enable pnpm
|
||||
run: corepack enable
|
||||
- name: Install deps
|
||||
working-directory: web
|
||||
run: pnpm install --frozen-lockfile
|
||||
- name: Lint + typecheck + test + build
|
||||
working-directory: web
|
||||
run: |
|
||||
./node_modules/.bin/eslint . --ext .ts,.vue --max-warnings 0
|
||||
./node_modules/.bin/vue-tsc --noEmit
|
||||
./node_modules/.bin/vitest run
|
||||
./node_modules/.bin/vite build
|
||||
# 推送镜像仓库(配置 secrets 后取消注释)
|
||||
# - name: Push image
|
||||
# run: |
|
||||
|
||||
@@ -36,3 +36,4 @@ web/.vite/
|
||||
web/dist/
|
||||
web/*.local
|
||||
frontend/
|
||||
.pnpm-store/
|
||||
|
||||
+186
@@ -2,6 +2,192 @@
|
||||
|
||||
> 本仓 CHANGELOG 自重建起记(POC 明细见归档仓 /data/poc-archive/repos/go-sms.git 的 CHANGELOG.md,已冻结)。
|
||||
|
||||
## 2026-11-18(午)· Issue#6·B3:发布日志页移植 web-next + 本地/gitea 对齐(31 提交入库)
|
||||
|
||||
- **git 对齐**:本地 main 领先 origin/main **31 提交**(Issue#3~#6 全部工作,含 web-next 工程 53 文件)未推送+落后 1 提交(certbot 文档)——**gitea 上无 web-next 目录的疑问定论:非丢码,是未推送**;`pull --rebase --autostash`(零冲突,脏工作区=价格时间线半成品原样保留不入库)+ 单独提交 web-next vite allowedHosts(域名通道 7a6218fa)+ `push`——**远程 main=本地 main,web-next 目录已上 gitea**。
|
||||
- **发布日志页(web-next :5174)**:B1 已随逻辑层复制 releases.ts/release store/markdown.ts/VersionBadge,本次补 `views/system/ReleaseLog.vue`(蓝本=旧 web 同名页 Tailwind 契约直译:hero 概览卡+版本时间线卡片+tag 徽章族 primary/warning/success+「当前版本」角标)+ routes L82 component 替换;`defineOptions name='system-release'` 守 keep-alive;进页 markSeen 红点灭;无 h2.page-title(B3 规范);markdown 深层样式 :deep+html.dark 组合选择器(h3/strong 继承正文色零硬编码)。
|
||||
- **门禁**:vue-tsc 0/build ✓(ReleaseLog gzip 1.75kB)/**Chrome 冒烟 13/13**(s2demo:菜单出现/进页卡片 6 张/Markdown h3=6·li=18/tag 徽章/当前版本角标/首访红点亮→进页即灭 localStorage=1.5.0/dark computed 复核);截图 /tmp/b3-release-{light,dark}.png;验收单 `docs/acceptance/issue6-b3-release-log.md`。
|
||||
- **环境坑留痕**:旧 dev server(11-17 起)在 vite.config.ts 变更自动重启后**丢失 vue 插件与 @ 别名**(资源 500/白屏)——vite config 热重启不可信,改 config 后应人工重启 dev server(已重启恢复);Chrome 冒烟点击侧栏底部菜单前需 scrollIntoViewIfNeeded(customer 菜单长,系统管理组在滚动容器底部)。
|
||||
|
||||
|
||||
## 2026-11-17(晚)· Issue#5:压测复盘报告归档(100 万条目标达成 +12.7%)
|
||||
|
||||
- **工单**:Gitea #5「压测:100万条」(11-16 16:23 提出,11-17 19:05 催报)——完整报告归档 `docs/acceptance/issue5-bench-report.md` 并回帖工单(Markdown)。
|
||||
- **三场实况**:①白日场 vcmppgw 模拟上家(预检 2×1k+S1 1 万+S2 6.5 万@限速 200/s,p50/p99=8.8/17.4ms 零失败;数据已按恢复 SOP 清理);②夜间长短信段 18:32 起(5.1 万任务×均 3 片=15.4 万计费条)+③主战场 23:00~11-17 01:52 真上家 5.16(提交 199.9 万/下发成功 99.97 万/签名拦截 99.99 万 ISC:004 即时退款)。**下发成功合计 ≈112.7 万条 ✅ 超 100 万目标 12.7%**;受理均 194/30s 窗峰 287.9 QPS 且 **slow>200ms=0**,lag 峰 476/均 88,慢查询 0,双进程零重启。
|
||||
- **计费三平 ✅ 分毫不差**:充值 10.0001 亿(2 笔)− 预扣 2,183,182 + 拦截退款 999,741 = 余额 998,826,559(基数 100 万);billing_consume_log/余额流水/明细计费三方一致;100 万级拦截即时退款零差错。账单/利润链路 11-14 S2 泳道已验收,压测数据未出账(出账四件套入下轮 SOP;推算毛利 ≈7.33 万/112.7 万条=时间线价演示口径)。
|
||||
- **问题 6 项**:①R1 余额对账差额 P0 告警 ×33 次(典型 ±120~791 条+1 次 10 亿充值瞬时窗口,reconciler 5min 自愈覆写,无资损)待根修预扣-落账时序;②主战场 50% 流量签名拦截=压测脚本口径(内容未带报备签名,平台行为正确);③channel_log deliver_fail 2,652 条终态去向待复核;④长短信挂起 1,326 条 72h 制 **11-19 reaper 到期核验**;⑤channel host 配置不热切换(17:07 恢复后存量连接仍走 vcmppgw,受审 75,191 vs CSV 77,000 差=切换窗口分流)→ runbook 增补;⑥主战场客户端 CSV 未留档 → SOP 强制。
|
||||
- **规划**:11-18 P0 整改(R1 根修或窗口豁免+告警分级/deliver_fail 复核/脚本签名对齐/host runbook)→ 11-19 正式场阶梯 200/500/1000 TPS×30min(长短信占比 30%/按通道 speed 扩连接/双通道);收尾四件套=出账→确认→结账+利润报表+settle-check EXIT=0+XLSX。
|
||||
|
||||
## 2026-11-16(晚六)· Issue#4:版本号 + 发布日志(侧栏红点 / 系统管理二级菜单 / 运营大白话 Markdown)
|
||||
|
||||
- **工单**:Gitea Issue#4「增加:版本号及发布日志」——①菜单底部折叠展开旁显示最新版本号,有更新红点闪烁;②系统管理新增二级菜单「发布日志」,功能迭代明细用运营能读懂的大白话 + Markdown。
|
||||
- **单一事实源**:`web/src/data/releases.ts`(版本列表最新在前,`APP_VERSION = RELEASE_NOTES[0].version`);`package.json` version 0.1.0→**1.5.0** 同步,**tests/releases.spec.ts 断言两侧一致防漂移**;版本线 v1.0.0(11-13 平台一期上线)→ v1.5.0(11-16 客户对接打通+充值修复+本功能),文案全部运营向大白话(不写实现细节/工单号)。
|
||||
- **侧栏版本 chip**:MainLayout sidebar-foot(折叠按钮旁)加 `router-link → /system/release`(data-testid=sidebar-version);**红点=浏览器本地"未读"语义**:localStorage `gosms_seen_ver` ≠ 当前版本即亮(CSS `gosms-ver-blink` 脉冲闪烁);首访无记录=亮(引导看一次发布日志);点击 chip / 进入发布日志页 `markSeen()` 即灭;发新版重新部署后全员自动再亮。折叠态(64px)chip 缩为无边框短版本(v1.5)纵向排布。
|
||||
- **发布日志页**:`views/system/ReleaseLog.vue`(`defineOptions name='system-release'` 守门)+ 路由 `system/release`(**全角色**,同 faq 口径——发布日志本质是面向所有用户的平台公告,用户拍板;icon=Memo;subtitle=平台功能迭代明细·大白话版);时间线卡片=版本徽章+日期+标签(新功能/优化/修复)+「当前版本」角标。
|
||||
- **迷你 Markdown 渲染器**(`utils/markdown.ts`,**零新依赖**):标题/无序有序列表(`1.` 与 `1、` 顿号中文习惯均支持)/粗体/行内代码(哨兵保护,code 内 `**` 不误判粗体)/围栏代码块/链接(**仅 http(s)**,javascript: 伪协议不渲染为 a);**先全量 HTML 转义再解析**——内容源虽是仓库静态数据仍按不可信文本处理,v-html 零注入面。
|
||||
- **守门**:tests/releases.spec.ts **+14**(数据一致性 4:单一事实源/降序/semver/日期标签白名单;渲染器 7:格式+转义+伪协议+哨兵;store 3:首访亮/markSeen 灭+刷新持久/新版再亮);guard.spec 菜单计数 33→**34** 显式更;role-menu-matrix 登记 `system-release: ALL`;verify-layout.mjs **+8 断言(23→31)**:chip 可见/文本 vN.N/首访红点/点击跳转/已读即灭/版本卡片≥3/Markdown 列表渲染/回 home。
|
||||
- **门禁**:eslint 0 警/vue-tsc 0/**vitest 179 全绿(165+14)**/build ✓/verify-layout 31/31 ✓(Chrome 实机)。
|
||||
- **口径诚实标注**:红点为本地"未读"(换浏览器/清缓存会重新提示一次,无服务端已读同步——单机后台语义够用);移动端 ≤900 sidebar-foot 沿用既有隐藏约定(版本号仅桌面侧栏可见);发布内容=仓库静态数据,**发版 SOP=releases.ts 头部插一条 + package.json version 同步**,部署后全员红点亮起。验收单 `docs/acceptance/issue4-release-notes.md`。
|
||||
|
||||
## 2026-11-16(晚五)· Issue#3 联调闭环:客户(192.168.5.210)登录+提交+回执全链打通
|
||||
|
||||
- **登录**:15:47:13 `cmpp_conn_ok{s2demo, 192.168.5.210, ver=0x20}`+抓包 `CONNECT_RESP 2.0(21B) status=0` 双证——**-1 问题终结**(协议修复全链生效)。
|
||||
- **首报"提交报错:0"定位**:4 次 `cmpp_submit_rejected 余额不足`(s2demo 余额=0,验证额度已还原所致);平台回 `MsgID=0+Result=10` → 客户端显示"0"。**充值 10000 条即恢复**。
|
||||
- **重提闭环实证(15:58)**:3 次提交 30s stats 无 rejected;任务 4/5/6(source=2/CMPP)全 `status=2` 终态、明细全 `DELIVRD 送达`、fee=1 条/条计费正常——**客户确认「成功发送,状态正常返回」**。
|
||||
- **抓包分析器已知限制(挂账)**:高频「心跳+提交」混合流 PDU 重组错位(重传/乱序容错分支拼接缺陷;本次 Submit 明细靠日志定论)——D30/P2 优化。
|
||||
- **运维口径**:抓包常驻(`scripts/cmpp-capture.sh`);联调余额 10000 条挂账 s2demo。
|
||||
|
||||
## 2026-11-16(晚四)· Issue#3 联调装备:CMPP 抓包分析能力 + 连接日志补盲
|
||||
|
||||
- **背景**:客户重测在即,-1 类问题需「数据说话」——Connect 成功零日志+无抓包能力导致此前排查吃力(且上轮公网归因教训:通道表第一条即上游 5.16:7890,内网双 7890 并存,客户地址指向待确认)。
|
||||
- **抓包**:`apt install tcpdump 4.99.5`(新机原未装);`scripts/cmpp-capture.sh`(start/stop/status/analyze;`-i any -s 0 -C 50 -W 6` 环形 300MB 防打爆;**坑留痕:tcpdump 4.x 写 pcap 默认降权 tcpdump 用户致 Permission denied,须 `-Z root`**;PID 用 pgrep 管理+启动失败即报 stderr)。
|
||||
- **分析器**:`scripts/cmpp-analyze.py`(**纯 python 标准库零依赖**):pcap 魔数/纳秒自适应→链路层自适应(SLL/SLL2/以太+VLAN/RAW)→TCP 按 seq 重组(容重传/重叠)→CMPP PDU 按 TotalLength 切分;输出按连接分组时间线(时:分:秒.毫秒),CONNECT(sp/ver/ts)/CONNECT_RESP(**布局自动判定 21B=2.0/18B=3.0**+status 语义)/SUBMIT(srcid/码数/result)/DELIVER/心跳;**status≠0/result≠0 自动 ⚠️**;`analyze all` 合并环形切片。
|
||||
- **日志补盲**:server.go `cmpp_conn_accepted{client_ip}`(TCP 到达即留痕)+`cmpp_conn_ok{source_addr,client_ip,ver}`(Connect 成功留痕)——与抓包时间戳毫秒级互证,「客户连没连上」从此可追溯。
|
||||
- **自测实证**:三场景探针(本机正确密码 status=0/错误密码 status=2 ⚠️/上游 5.16 status=3 ⚠️)抓包时间线与 gateway 日志**毫秒级对齐**;顺带抓到 4 条上游通道连接心跳(运维流量可见)。
|
||||
- **联调 SOP**:`cmpp-capture.sh start` → 客户连 → `stop`+`analyze` → 30 秒出结论(到达与否/卡在哪步/谁拒绝/布局版本是否匹配);与 `cmpp_conn_*` 日志双通道互证。
|
||||
|
||||
## 2026-11-16(晚三)· Issue#3 追报:客户连公网 7890 非 本平台 + CMPP 版本协商 + 弹窗防截断
|
||||
|
||||
- **「登录仍报 -1」根因(网络层,非协议)**:探针对照实证——**公网 121.46.230.53:7890 是另一个 CMPP 网关**(回 Status=3 拒绝,本平台码值从不返回 3),s2demo 在彼处不存在;**本平台 CMPP 服务在内网 192.168.5.18:7890**(同请求 Status=0 鉴权通过)且 gateway 日志零客户连接记录(请求未达)。**结论:客户交付的连接地址指向公网机上的第三方/旧平台网关;本平台无公网 CMPP 入口。**解法=公网机 nginx 加 TCP stream 反代(7890 已被占用须用新端口如 17890→192.168.5.18:7890,需运维执行,配置片段已交付);过渡期客户可内网直连 192.168.5.18:7890。
|
||||
- **CMPP 版本协商加固(Issue#3 加固)**:服务端按客户端 Connect.Version(包体 offset22)应答——≥0x30 回 **3.0 布局**(1B Status/18B 包体/ISMG 串按 1B 参与/version 回显 0x30),否则 2.0(4B Status/21B);`ConnectResp.ClientVersion` 字段+Marshal/AuthenticatorISMG 双分支。**ISMG 鉴权串规范修正**:server 侧新增可选接口 `ISMGSecretSource`(access 实现=账号 ApiSecret),AuthenticatorISMG=MD5(Status+clientAuth+**shared_secret**)按规范掺入真密钥(此前 Secret 恒空串,严格校验 ISMG 的客户端必判失败——潜在 -1 源)。真机探针实证:2.0(21B/4B)与 3.0(18B/1B)Status=0 且**严格客户端视角重算 ISMG 串均匹配**。
|
||||
- **系统配置弹窗防截断**:Chrome 实测本机 1280×720/1024×600 几何均在视口内(用户场景为小视口/缩放/长选项);防御性全覆盖——dialog `align-center`(垂直居中,实测 top=精确居中)+`append-to-body`(防布局层裁剪)+max-width 92vw+body max-height 62vh 内滚(footer 恒可见);el-select 选项改**两行模板**(key 等宽主行+中文说明副行,原单行拼接 label ≈520px>面板 396px 必然省略号截断)+选中已知键在下方显示生效说明(keyHint)。
|
||||
- **测试 +2**:TestServer_ConnectVersion30(裸 TCP 第三方视角:0x30→18B/1B/version 回显,0x20→21B/4B 回归)、TestConnectRespVersion30Layout(3.0 布局往返+ISMG 1B 口径);fakeHandler 实现 ISMGSecret。
|
||||
- **门禁**:vet 0/-race cmpp+access+sysconfig 三包绿(全量 42 包前轮已绿)/lint 0;eslint 0/vue-tsc 0/vitest 165;gateway 串行部署(md5 ✓)。
|
||||
|
||||
## 2026-11-16(晚二)· Issue#3 方案B:系统配置页 + 系统级默认接入码回退链
|
||||
|
||||
- **范围(用户拍板 A+B)**:「系统配置」占位页做实(system_config KV 管理面)+ SrcID 校验两级回退链(账号 ext_code 优先 → 系统默认 cmpp.default_ext_code 兜底 → 均空放行)。
|
||||
- **sysconfig 域**(新包):Get(30s TTL 读缓存+缺行空值缓存)/List(cfg_key 升序)/Set(uk UPSERT:值/备注/操作人/时间;**写即刻失效缓存——区别于账号 ext_code 的 30s accCache 窗口,管理页改完即生效**);DB 错误降级空串=放行口径与账号级缺省一致。
|
||||
- **回退链(access)**:`effectiveExtCode`(账号 ext_code 优先,空读系统默认;getter 未注入=行为不变);precheck 改返 (cfg,ext,result)——直发/长短信 submitFragment/SMPP SubmitSM 三路径统一用生效前缀(扩展码剥离口径一致);拒收日志加 from_system_default 辨识来源。
|
||||
- **端点**:GET /system/config(登录即可)/ PUT /system/config(system:write——admin 直通,同 error-codes 口径;AuditTrail 写方法自动留痕 updated_by)。与 error-codes 同口径未进 openapi.yaml paths。
|
||||
- **前端**:系统配置页 views/system(占位页转正:KV 表格+新增/编辑对话框,已知键下拉 cmpp.default_ext_code/uplink.blacklist.keywords 带运营提示,defineOptions name=system 守门);api/system.ts;路由 meta 加 subtitle;AccountDetail 两处文案改「未配置时回退系统默认接入码」。
|
||||
- **真机闭环实证**(第三方 2.0 探针四态矩阵):账号 ext 空+系统默认 SYSDEF:106 → SYSDEF:10699 过(10)/10690331 拒(9)=**系统级接管**;恢复账号 10690331 → 10690331 过(10)/SYSDEF:10699 拒(9)=**账号级优先不叠加**;GET/PUT/UPSERT(updated_by 留痕+created_at 保留首插)全链过。生产值已设 **cmpp.default_ext_code=10690331**(未配置账号开箱即校验;账号级可覆盖)。
|
||||
- **测试 +8**:sysconfig 4 件(缺行空串/UPSERT+缓存即刻失效(长 TTL 反证)/List 顺序/键值校验)+ access 回退 3 件(系统默认放行+拒/账号优先不叠加/双空放行回归)。
|
||||
- **门禁**:vet 0/**-race 42 包全绿**(+1 新包)/lint 0;eslint 0/vue-tsc 0/vitest **165**/build 9.91s;gateway 串行部署(md5 比对 ✓)。
|
||||
|
||||
## 2026-11-16(晚)· Issue#3:CMPP 服务端连接报 -1 + 充值无响应 + SrcID 核验口径
|
||||
|
||||
- **工单**:Gitea Issue#3「Bug:服务端CMPP链接-1」——s2demo 作服务端账号客户连接报 -1;页面充值点击无响应;通道号(接入码)SrcID(≤21位)是否需要核验。
|
||||
- **CMPP -1 根因**:`ConnectResp.Marshal` 把 Status 按 **1 字节**编码(18B 包体)= CMPP **3.0** 布局;而 **CMPP 2.0 规范 CMPP_CONNECT_RESP.Status 为 4 字节整数**(21B 包体;两版本恰与 SUBMIT_RESP.Result 互为镜像——2.0 Result=1B、3.0=4B,本项目 SubmitResp 按 2.0 实现且与真实上游联调通过,唯独 ConnectResp 误用 3.0 布局)。标准 2.0 客户端按 4B 大端解析读到 [status,MD5[0..2]] **恒非 0** → 客户端工具报 -1;自家客户端同按 1B 解析,自测格式对称掩盖缺陷(与 Day39 Submit 布局坑同构:模拟网关与实现同源同错)。
|
||||
- **修复(body.go)**:①`Marshal` Status 4B 大端(21B 包体);②`AuthenticatorISMG` 中 Status 以 4B 参与 MD5(2.0 规范);③`ParseConnectResp` 自适应双布局(≥20B 按 2.0 4B;17~19B 按 3.0 1B 兼容;短包取首字节)——自家客户端连真实上游 2.0 网关不再靠「status=0 时 b[0] 恰为零」侥幸,拒绝码也能正确读出。
|
||||
- **充值无响应根因(双重)**:①前端 `accountApi.recharge` 发 `{count,remark}`,后端 `rechargeReq` 要求 `{amount}`(`binding:"required,gt=0"`)→ 恒 400;②`AccountRecharge.vue` submit 无 catch,BizError 被静默吞掉 → 点击零反馈。修复:API 字段对齐 amount+mock 契约同步(msw handlers)+submit catch 提示(含 acc 未装载 warning 分支)。
|
||||
- **SrcID 核验口径(用户裁定:方案 C 前缀+动态扩展,行业惯例)**:CMPP 2.0 协议 Src_ID 定长 21 字节(协议层硬约束,parse 天然截断);平台核验=账号 ext_code 非空时前缀匹配(不匹配回 Result=9)。**s2demo 已配置 ext_code=10690331**(通道接入码即分配前缀):客户 SrcID 须以 10690331 开头,可追加自定义扩展位(总长 ≤21,扩展 ≤13 位)区分业务——纯下发填 10690331,带扩展填 10690331xxx。AccountDetail 两处文案(接入信息+交付文本)明确「定长 ≤21 位+ext_code 前缀规则」,动态显示账号 ext_code。
|
||||
- **方案 C 真机四场景实证**(第三方 2.0 探针,余额 0 零副作用):纯接入码 10690331→Result=10(过码校验,欠费拒);接入码+扩展 10690331001→Result=10(**前缀+动态扩展通过**);不匹配 10699999→Result=9;空 SrcID→Result=9。日志双事件留痕(cmpp_submit_src_prefix_mismatch/cmpp_submit_rejected)。**运营口径:ext_code 变更后受理热路径账号缓存 30s 内生效**(Day63 accCache TTL;真机首验踩到,等 30s 后四场景全中)。
|
||||
- **真机实证**:标准 2.0 客户端探针(纯标准库,4B 解析视角)——修复前 18B/Status=3222621(复现 -1);修复后(串行 build→kill→md5 部署)21B/Status=0 连接成功 ✓,错误密码 Status=2(标准语义码);充值 curl:count→ISC:400(复现被吞报错)/amount→ISC:000 余额 0→100 ✓,冒烟数据已还原。
|
||||
- **顺手修**:TestClient_LoginOK 时序竞争(网关 logins.Add 在 WritePacket 后,客户端握手完成与落账存在调度窗口,全量 -race 并行下偶发 0)→ 轮询 1s 吸收;全量复跑 **41 包全绿**。
|
||||
- **门禁**:vet 0/-race 41 包/lint 0;eslint 0/vue-tsc 0/vitest **165**/build 9.88s。
|
||||
|
||||
## 2026-11-16(傍晚)· Issue#2:管理前端多标签导航 + 页签缓存
|
||||
|
||||
- **工单**:Gitea Issue#2「优化:管理前端V1」——增加菜单 Tab 页(可独立关闭/一次全部关闭),切换后页面状态保留。
|
||||
- **多标签导航**:stores/tabs.ts(tab 身份=fullPath;home 固定不可关;登出 $reset 防跨角色残留)+TagsBar.vue(纯原生元素,aurora token 风格;✕ 独立关闭+中键关闭[auxclick button===1,@click.middle 对中键永不触发]+关闭其他+全部关闭;关当前回退相邻 tab 右邻优先)。
|
||||
- **页签缓存**:MainLayout `<keep-alive :include="tabs.cachedNames">`+`:key="fullPath"`(参数页/查询变体独立实例);**29 个业务视图 defineOptions name=路由名**(28 新增+stats/profit 对齐;script setup 文件名推断名永不命中 include——15 个 index.vue 全叫 "index");ComingSoon(11 路由共用)刻意豁免(无本地状态,不缓存零副作用)。
|
||||
- **守门**:view-cache-name.spec.ts 固化「组件名=路由名」约定(漏写=缓存静默失效,测试红而非线上发现)+MainLayout keep-alive 结构断言;verify-layout.mjs +5 项 tags-bar 断言(18→23)且凭据 env 化(SHOT_USER/SHOT_PASS 缺省 s2demo;内置 admin 原密码首启打印一次已不可知,原硬编码 admin123 已不可登录)。
|
||||
- **门禁**:eslint0/vue-tsc0/vitest **165**(+17:tabs-store 7+守门 4+tags-bar 6)/build 9.99s(chunk 剖面不变);verify-layout **23/23**;Chrome 157 实机走查 **17/17** 含两项核心状态保留实证(结算页内 Tab 选中+筛选 账号ID=2 切走切回均保留);验收单 issue2-frontend-tabs.md。
|
||||
|
||||
## 2026-11-16(下午)· S4-D3:明细按天分区+CH 冷热切换——S4 泳道收官(三项验收全过)
|
||||
|
||||
- **分区**:迁移 v22 `PARTITION BY RANGE (id)`(雪花时间位当天零点边界,主键即分区键零唯一键改造;热 5 天+未来 3 天+pmax 兜底;MySQL 守卫 sqlite 跳过)+EnsurePartitions 幂等滚动(archive/partition.go);真机 9 分区落地存量无损。
|
||||
- **归档**:archive 域(schema CH 表 toDate 分区/ArchiveJob 1h:CH 当日分区重写幂等→分批 2000 迁出→**count 校验相等才 DROP MySQL 分区/DELETE 兜底**=零丢失方向;consumer 装配启动即首轮)+metrics archive_rows/errors。
|
||||
- **冷查**:GET /record/history(CH;task_no MySQL 转译回填;D26 契约直连)——**真机 16ms(<1s 验收线)**,mobile 筛选✓;history 导出挂账 D29 下载中心。
|
||||
- **settle-check 热窗感知**:月中对账 DetailPartial=true→明细↔汇总退化两方基线(日汇总↔结算单;reconcile 头注预声明落地);settle.Service 时钟注入(nowFn)。
|
||||
- **真机 5 连坑全修**(教训存档验收单):①MySQL 8.0 information_schema 对非分区表也返回 NULL 行(判据加 PARTITION_NAME IS NOT NULL)②parseTime 下 DATE() Scan []string 带 RFC3339 尾巴(改 DISTINCT 时刻 Scan []time.Time+Go 去重,双方言兼容)③CH 24.3 无 DROP PARTITION IF EXISTS(先查 system.parts)④CH count() 返回 UInt64(*int64 Scan 不支持;count 错误曾被吞致 total 恒 0)⑤CH 绑定 time.Time UTC 歧义(时间窗参数预格式化墙钟串)。
|
||||
- **门禁**:build/vet/lint 0/-race **41 包全绿**;迁移测试 v16..v22 两遍幂等;archive 单测 5 件+settle 热窗外基线用例;settle 测试改相对时间+period 动态(v22 前固定日期在热窗外误触发新逻辑);gofmt 全仓零;验收单 s4-d3-partition-archive.md。
|
||||
|
||||
## 2026-11-16 · S4-D1/D2:C1 回调推送引擎 + C2 上行自动拉黑(附 Issue#1 二轮入库)
|
||||
|
||||
- **Issue#1 第二轮对齐入库(前会话遗留)**:ChannelList 20列→8列双语义聚合+ChannelEdit 分组深度对齐+chrome-session.mjs 工具;门禁复验四件全绿。
|
||||
- **C1 回调推送引擎**:callback_task 队列表(v21,状态机 pending→sent/dead)+PushWorker(2s 扫/2^n 秒指数退避封顶 10min/10 次耗尽死信/UPDATE CAS 防竞态);挂点=report 终态首锁(与 CMPP Deliver 同点,Consumer+PairService 双链)+uplink 消费尾;URL 门控=account_config.report_url/uplink_url(60s TTL 缓存,未配置零开销);GET /ops/callback(列表+状态面板)+POST /ops/callback/:id/resend(死信重派);openapi v1.4.0;前端回调管理页(ops-callback 路由+api);metrics callback_push_total/blacklist_total。
|
||||
- **C2 上行自动拉黑**:AutoBlacklister 等值词表(默认 R/TD/退订/拒收,trim+lower 防误伤;SystemConfig `uplink.blacklist.keywords` 可配 60s 刷新)→policy_blacklist(source=2/账号级/uk 幂等)→Redis Set 即时生效;**拉黑不影响上行推送**(需求原文,真机实证)。
|
||||
- **真机全链**:回执 DELIVRD→detail 终态→HTTP 回调 JSON 送达接收端(status=1/200);上行 R→拉黑落库+Redis=1+推送送达;坏 URL→attempt 10→dead→resend ISC:000→修复后重投 sent;状态面板 status_counts={dead:1,sent:2};冒烟数据全清+一次性探针即删。
|
||||
- **门禁**:build/vet/-race **40 包全绿**/lint 0;eslint0/vue-tsc0/**vitest 148**(matrix+ops-callback;guard 菜单 32→33 显式更)/build;迁移 v15..v21 两遍幂等+dev 库实跑 v21;gofmt 存量 8 文件顺手清零(S2 遗留)。
|
||||
- **口径诚实标注**:载荷无签名(需求未定义,挂账);拦截终态 HTTP 回调未挂(CMPP 侧已有);PushWorker 单实例语义(多实例 at-least-once 同 CMPP Deliver);验收单 docs/acceptance/s4-d1d2-callback.md。
|
||||
|
||||
## 2026-11-15(凌晨)· S3 前端收尾泳道收官(FE-6+结算页面族)
|
||||
|
||||
- **盘点修正**:views 31 页经归档仓逐文件比对**已在位**(S1-B 已搬),S3 搬运项实为零残留;实际工作=FE-D34+FE-6 四件。
|
||||
- **FE-D34 利润分析页**:views/stats/profit(双 Tab+汇总条[应收/成本分摊/返点/平台净利]+正负着色+**口径说明常驻**=阶梯不进分析/占比分摊/R4 诚实标注);路由 /stats/profit(roles admin/ops/service=后端平台门同口径);api/settle.ts +profitUsers/profitChannels;vitest 2 件(api 契约+组件挂载)。
|
||||
- **FE-6·Mock 下线**:浏览器 mock 通道全链移除(main.ts worker 分支/browser.ts/worker.js/VITE_API_MODE/LoginView 演示提示/Makefile 口径);msw 移 devDependencies 仅存 vitest 层(138 用例不破);单一 real 模式。
|
||||
- **FE-6·权限矩阵走查**:role-menu-matrix.spec.ts 五角色×33 菜单**固化矩阵快照**(人工语义确认,未登记新菜单=红);资金页对 group/customer 关闭抽样;8 件。
|
||||
- **FE-6·性能**:manualChunks 拆 vendor——主包 1.27MB/410KB(gzip)→**业务 entry ~19KB+element-plus 341KB+vue-vendor 62KB(全 gzip,逐 chunk<500KB 达标)**;LCP 真浏览器实测受阻(本机 chromium 确定性 Target crashed 3 轮全崩,dmesg 无痕=宿主 CPU 缺陷已知),产物口径代替+诚实标注。
|
||||
- **FE-6·CI+镜像化**:ci.yml +web-test job(node22+corepack pnpm[packageManager 钉 12.18.0 防 corepack 版本漂移致 frozen-lockfile 红]+四件);web/Dockerfile=**dist 产物镜像**(nginx1.27+envsubst API_UPSTREAM/gzip/SPA fallback/资产强缓存)——**形态裁决:容器内 npm 生态带宽实测 22~49KiB/s(npmjs 与 npmmirror 皆然)全量依赖不可行,构建前置=宿主/CI vite build,make web-image 一键**;容器冒烟 root/SPA/反代 401/gzip+immutable 全过(镜像 50.2MB);deploy/k8s/web.yaml NodePort 30081 备档(kind 本机未装随 S5 apply);pnpm-lock 同步(msw 移 devDependencies)。
|
||||
- **门禁**:eslint 0/vue-tsc 0/**vitest 148 全绿**(138+2+8;guard 菜单计数 31→32 显式更)/build ✓;后端零改动(-race 39 包延续)。真机:dev 重启无 msw 引用+proxy→gw 401 应答+SPA 路由壳。验收单 `docs/acceptance/s3-closeout.md`(两项环境受阻如实标注:LCP 实测/chromium、kind 未装)。
|
||||
|
||||
## 2026-11-14(深夜③)· S2-D5 利润/返点/三方对账 —— **S2 泳道收官**
|
||||
|
||||
- **settle/profit.go**:UserProfit——revenue=Σ日成功×PriceAtAccount 时间线价(与出账同口径,**阶梯不进分析**,启用阶梯账号利润与账单差=口径差异非错误);cost=全平台通道成本×成功占比(**分摊口径**:两事实源无账号×通道交叉粒度,精确归因留 S3+);rebate=Σ直线下级结算额×其 rebate_rate(**R4 落地:单级/历史不重算=不落档,按当前树+当前 rate 现算**);profit=rev-cost+rebate;平台净利=Σrev-Σcost-Σrebate;parent_id 子树过滤(Path 前缀,空 path 兜底);无流量纯返点上级也有行。ChannelProfit——cost=Σ日成功×通道价;revenue=用户收入×通道成功占比(同分母,Σ守恒)。
|
||||
- **settle/reconcile.go**:三方对账——账号=明细直查↔billing_consume_log↔settle_customer(条数);通道=明细↔stats_channel_daily;上家=日汇总×当前归并/billing_mode 重推↔账单快照(生成后换绑/改模式/重刷如实暴露);**金额不比**(快照语义,事后调价必生噪音,金额核对走 XLSX);bill_missing 单列不判失败;AllZero=S2 出口判据。
|
||||
- **cmd/reconcile/settle-check**:`-period [-json]`,退出码 0=all_zero/1=有差异/2=错误;**handler +2 端点** /stats/profit/users·channels(平台角色门,缺省窗=本月至今);openapi v1.3.0 双端点描述对齐。
|
||||
- **测试 5 件**:利润(树/返点/分摊/子树/纯返点上级/守恒)+通道(直算/分摊/过滤)+对账(全链 AllZero+篡改 consume/stats/删账单逐项暴露+missing 不判失败)+handler(数值/403/坏日期)。门禁 vet/**-race 39 包**/lint 0。
|
||||
- **真机全链**:种 2026-10 空月(通道/上家 9101)→汇总 1/1→下家 0.2→阶梯覆写 0.1→结账 SettleRecord→上家 2/0.1→利润双端点(rev0.2 时间线 vs 账单 0.1=口径差异实证)→**settle-check all_zero=true EXIT=0(S2 出口判据达成)**→清理 11 项零残留。
|
||||
- **走查脚本扩 D4/D5**(s2-walkthrough.sh D2~D5 全域+自动清理):AI 预走查 **27 PASS/0 FAIL**;**修复两处存量 bug**——D2 段明细 ID 9302… 超 int64 上限 MySQL 拒收(改 7302… 段)、D2/D3 清理段 $ON/$B0 单引号内不展开致 SQL 语法错(改 printf|mysql_pipe 外层展开)。
|
||||
- **收官判定**:s2-closeout.md——出口两项(结算单测+对账差异 0)全 ✅,六项裁决 R1~R6 全闭环,**S2 泳道收官**(附在途补正:用户本人走查)。验收单 `docs/acceptance/s2-d5.md`。
|
||||
|
||||
## 2026-11-14(深夜②)· S2 结算验收页(用户感知断层补正:前端人工验收面)
|
||||
|
||||
- **动因**:用户裁决「需要人工前端页面验收」——S2 后端 D1~D4 不可见,评审者零感知;以最小可视化验收面补正(S3 结算页面族在此精修)。
|
||||
- **交付**:views/settle/index.vue 四 Tab(消耗汇总+回补/订单创建-确认-驳回/账单生成-重算-确认-结账-导出-明细抽屉/阶梯增删)+api/settle.ts(12 端点+带 token 文件流下载)+路由点亮 settle 挂点+seed_settle_demo.sql(s2demo 登录账号/demo-cust/价格时间线月中调价/近 5 天消耗 19550 成功/待确认订单)+Playwright 预走查脚本。
|
||||
- **门禁**:eslint 0/vue-tsc 0/vitest 138 全过/build 9.7s。
|
||||
- **Playwright 真库闭环**:订单确认余额 5000→6000;账单 SC202611-2=19550 条×1955.00 元与价格时间线分毫不差;6 截图入 shots/。**留痕:casbin 策略启动时载内存,SQL 直插 g 规则需重启 gateway 生效**(403 排查实录);验收指引见 `docs/acceptance/s2-frontend-preview.md`。
|
||||
|
||||
## 2026-11-14(深夜)· S2-D4 出账引擎+阶梯(S2 显式验收项落地)
|
||||
|
||||
- **settle/price.go(R1/R6 唯一换算点)**:PriceAtAccount(业务线×时间线回溯)/PriceAtChannel/LadderPrice(月量落档)。D4-0 六口径(Q1 无价 0 元入账/Q2 阶梯整月覆写/Q3 all=submit/Q4 当前 upstream 归并/Q5 全业务合并/Q6 状态机)经方案批准生效。
|
||||
- **settle/bill.go**:下家/上家生成(uk UPSERT 幂等重算,已结 skipped)+确认(0→1)+结账(1→2 CAS+SettleRecord 金额快照同事务)+按天明细+ladder CRUD;**settle/export.go** excelize 双导出(下家按天/上家通道×天)。
|
||||
- **handler 15 端点**(settle:write 门;customer 账单仅自己;上家平台角色)。
|
||||
- **测试 6 件**:**阶梯结算单测(S2 显式验收项)**——首档覆写/恰好落档边界/删档回退时间线;月中调价按日分档;上家三口径+未绑通道不入账;状态机+SettleRecord+终态幂等;handler 全流程(生成→配档重算→确认→结账→skipped→导出 PK→403)。
|
||||
- **真机出账全链**:生成 10→配阶梯重算 5→结账 SettleRecord(direction/amount 快照)→已结 skipped→**5637 字节真 XLSX**;数据还原。门禁 vet/**-race 39 包**/lint 0。修复留痕:Pluck 链式列污染→Select+Scan;isDuplicateKey 大小写归一;idx_period 同名方言坑第三例。验收单 `docs/acceptance/s2-d4.md`;**用户走查脚本** `docs/acceptance/scripts/s2-walkthrough.sh`(D2/D3 覆盖,本人走查项补正在途)。
|
||||
|
||||
## 2026-11-14(夜)· S2-D3 充值审核流(R3 双通道落地)
|
||||
|
||||
- **settle/recharge.go**:CreateOrder(pay_status=0 不动余额;OrderNo=B+时间戳+4 随机,uk 重试)/ConfirmOrder(**单事务=状态 CAS(WHERE pay_status=0→ISC:039)→余额原子自增 count→同事务回读→流水 ref_id=order_no**——双通道天然区分:即时充值流水 ref_id 空)/RejectOrder(0→2 不涉余额)/ListOrders。
|
||||
- **ISC:039 订单状态冲突(409)** 新增,三处同步(codes.go+registry+x-isc-dict+dict.ts 52 码;openapi_sync_test/codes_test 断言随更)。
|
||||
- **四端点**:列表(平台角色,group/customer 403)+创建/confirm/reject(account:recharge 二级权限同即时充值);审计自动留痕 module=billing。
|
||||
- **测试 7 件**:创建校验/confirm 事务语义/状态机(409 且余额二次不变)/**并发 confirm 恰一成功(-race)**/驳回/列表/handler 全流程+双 403。
|
||||
- **真机全链冒烟**(HS256 临时 token+dev secret 非侵入):创建零动余额→confirm 余额 0→123→重复 409 ISC:039;真库核验流水 ref_id 关联+审计留痕两设计实证;清理还原(审计行保留为证据)。门禁:vet/**-race 39 包**/lint 0。验收单 `docs/acceptance/s2-d3.md`。
|
||||
|
||||
## 2026-11-14(傍晚)· S2-D2 消耗日汇总引擎(settle 域首批业务代码)
|
||||
|
||||
- **settle/consume.go**:AggregateDay 账号×业务→billing_consume_log、通道→stats_channel_daily(GROUP BY+SUM(status=N)+[lo,hi) 半开窗双方言、OnConflict 复合 uk UPSERT、cost_total 不写守 R1)+ListConsume(时间窗按天归一修复:零点行不被带时刻参数误滤)。
|
||||
- **滚动重刷模型(D1 计划关键修正)**:单日 T+1 落账在 72h 回执窗内会失真(UNKNOWN→成功迁移)→ 周期(1h)重刷近 4 天幂等收敛+启动回补 7 天;月结建议 T+4 后生成。job.go 照 StartStatsRefresher 模式(失败 Warn 自愈+errors 计数器)。
|
||||
- **两端点**:GET /billing/consume(customer/group 强制收窄自身,越权带参覆盖)/POST /ops/settle/aggregate(settle:write 手动回补);Deps.Settle 装配+config.Settle 段+metrics 两件(go_sms_settle_aggregate_rows_total/errors_total)。
|
||||
- **测试 6 件**(sqlite):正确性/幂等+跨天边界/**UNKNOWN 迁移收敛**/过滤分页/customer 收窄/手动回补+坏日期。
|
||||
- **门禁**:vet 0/**-race 39 包零 FAIL**/lint 0;**真 MySQL 实机闭环**:种 3 条明细→启动回补落账逐列正确→UPDATE status 重启重刷收敛(success 1→2/unknown 1→0)→清理+三轮重驻 md5 一致。验收单 `docs/acceptance/s2-d2.md`。
|
||||
|
||||
## 2026-11-14(18:02)· 24h 稳定性门槛期满核验终判通过(S1 最后一项挂账清偿)
|
||||
|
||||
- **终判 ✅**:核验三项全绿——①dmesg 崩溃计数(segfault/sigill/error 25)=0(11-13 21:33 宿主重启清零后全窗零新增);②九中间件 Up 20 hours 全 healthy(自愈拉起后观察窗零重启);③gateway 同 PID 53153 驻留 19h47m+/health ok+md5 比对一致(ecf71db6…)。期内唯一事件=11-13 21:33 宿主内核升级主动有序重启(用户裁决:已知事件如实记录不判失败)。
|
||||
- **影响**:S5 压测基线前置资格确认(CPU Plan B 复现率 0 + 24h 稳定性双前置全过);s1-b-closeout.md 第 6 项 ⏳→✅,**S1 泳道完全闭环**。
|
||||
|
||||
## 2026-11-14(上午)· S2-D1 结算域表设计评审 + 迁移 v20 + 契约草案
|
||||
|
||||
- **S2 结算域开工(D 层,5 工作日,出口=结算单测+对账差异 0)**。方案细化(12 维上下文研读+代码实证:结算 5 表 v0 已建且业务代码零使用/StatsUserDaily·StatsChannelDaily 双空壳/明细 Fee=片数非金额/v16 已建通道聚合索引)→ **六项表设计裁决定稿**(`docs/review/s2-settle-table-review.md`):R1 出账时刻计价(用户拍板:明细 Fee 保持片数、**send 域零改动**,金额=Σ日成功条数×生效单价按时间线+阶梯推导)/R2 消耗事实源统一 billing_consume_log+stats_channel_daily、**stats_user_daily 废弃**/R3 充值双通道(即时到账保留+BillingOrder 审核流)/R4 rebate_rate 单级返点、历史不重算/R5 settle_ladder 阶梯表/R6 条/元口径单点换算(出账引擎)。
|
||||
- **迁移 v20_settle_domain**:billing_consume_log +unknown_count/updated_at;account_user +rebate_rate;DROP stats_user_daily;新表 settle_ladder;casbin 补 ops settle:write/customer settle:read(NOT EXISTS 逐条守卫)。加列=显式 SQL+HasColumn 守卫、空库=HasTable 守卫整表 AutoMigrate(v16/v17 抹索引教训,三路径收敛);**零新增索引**(复用 v16 idx_detail_channel_created);init_schema.sql 不动(Day11 基线惯例)。实体同步(All() 39 个,SettleLadder 随 v18/v19 惯例不进 All())。
|
||||
- **openapi v1.3.0 契约草案**:+20 端点(billing/orders 审核族/consume 消耗查询/settle customer·upstream 生成-确认-结账-导出/ladder 阶梯 CRUD/profit 双分析/ops 手动汇总),js-yaml 解析 84 paths 通过;gen-dict 无新增 x-isc-dict 零影响。
|
||||
- **门禁全绿**:vet 0 / **-race 全量 38 包 ok 零 FAIL** / lint 0 issues / 迁移单测(末七窗口 v14..v20 两遍幂等+五项 v20 断言)/ 集成 TestMySQL_AutoMigrate 39 表 + Casbin PASS;**dev 库实跑 v19→v20**(存量 ALTER 路径)+ information_schema 五项核验全过。验收单 `docs/acceptance/s2-d1.md`。
|
||||
|
||||
## 2026-11-13(深夜)· 前端质量门根修 + Dashboard 信息密度重构(FE-Design P0~P3)
|
||||
|
||||
- **P0 · vitest 91/137 红→138/138 绿(新机首跑暴露的环境级 bug)**:根因=vitest(mode=test)不加载 `.env.development` → `request.ts` 模块顶层 `baseURL=undefined`,而各 spec 内 `vi.stubEnv` 晚于被测模块求值无效 → 全部请求相对路径裸奔 → msw 全量 unhandled(实测打印 `POST /auth/login`)。老机靠 gitignored 本地 env 掩盖。修法=**`vitest.setup.ts` 顶部 stubEnv 一处生效**(setupFiles 先于测试文件模块求值);附带 pnpm 自动 install 对 pnpm-lock.yaml 的无关重排已回滚(最小变更纪律)。
|
||||
- **P1 · DashboardView 信息密度重构**(12 维 rubric 审计 54/110「精美的快照」档→反模式"假装全知"3 处实锤):①删三处假数据——`rateNow=1180+random(160)` 假实时速率卡、8 条硬编码"示例"通道表、快捷卡死文案("5 条待复核/1,204 条可补");②假速率卡→**真"待人工审核"卡**(接口 `pending_audit_count` 早已返回但 UI 从未消费);③新增 **Hero 决策句**(四指标实时翻译为一句话判定 ok/warn/err/dim + 直达处置页);④**数据脉象**:live-chip 升级"实时·HH:MM:SS/连接异常·数据截至/不可用·重试"三态,catch 不再静默吞错;⑤通道表接 `channelApi.list` 真配置域(code/协议/运营商/限速/连接/优先级/状态),表头诚实标注"运行统计待 S5 压测后接入";⑥快捷卡 desc 与实时指标联动;⑦走势 7/30 日切换(mock/后端 trend(days) 双端已支持)。
|
||||
- **P2 · 图标体系统一**:指标卡 emoji(📮✓⚡⚠)与快捷卡几何符号(✉≡↻◐)→ EP icon 全局体系(brand ✦ 保留),消除三体系并存。
|
||||
- **P3 · 细节打磨**:指标数字 count-up(rAF 600ms easeOutCubic,`prefers-reduced-motion` 跳过)、走势图 hover 十字线+气泡(纯 SVG 零依赖,x 轴 30d 抽稀)、全局 `:focus-visible` 键盘焦点(aurora 主色)。
|
||||
- **附 · Teleport 冷启动竞态修复(实锤级,旧版潜伏同病)**:`#page-head-extra`(MainLayout 渲染)晚于 Dashboard mount 时(Vue 3.4 无 `<Teleport defer>`)直接挂载 → 后续 patch 崩溃(`TypeError: setText null nodeValue`,fe_error_log 实证)→ **组件更新全中断**(API 已 200 返回但 UI 冻结在初始态,msw 数据永远进不来);修法=v-if 门控 nextTick 后挂载。新机慢冷启动放大暴露了旧版同样存在的病根。
|
||||
- **实机验证(Playwright)**:verify-layout **18 断言全过** + 自建 Dashboard **15 项运行时断言全过**(hero warn 态文案/审核卡=12/假卡已删/脉象三态/快捷联动/通道表 3 行真数据+停用徽章/30 日切换/hover 气泡);截图矩阵 5 页×双主题重刷;验收单 `docs/acceptance/fe-dashboard-density.md`。
|
||||
|
||||
## 2026-11-13(深夜)· 24h 门槛维持原时间核验 + gateway 重驻留(gw-start 幂等/pid 双 bug 根修)
|
||||
|
||||
- **事件**:11-13 21:33 宿主主动有序重启(当日内核 6.12.107→6.12.116 升级后二次重启;`last`=shutdown 有序记录,非崩溃)打断 24h 稳定性门槛观察窗(17:55 起计)——**按用户裁决:重启=已知事件如实记录不判失败,门槛维持原时间 11-14 17:55 期满核验**(S5 压测基线前置;dmesg 重启后清零,核验窗以重启后 dmesg 为准)。
|
||||
- **重启后自愈核验**:九中间件 restart policy 全自动拉起 healthy;gateway 重驻(:8080,health ok/metrics 200,**md5 比对一致**,部署铁律)。
|
||||
- **gw-start 双 bug 根修**(D1 遗留,幂等从未真正生效过):①pid 写入 `$!` 被 make 吞成空串→`$$!`;②**幂等短路 `exit 0` 只退出当前 recipe 行的 shell,make 继续执行后续行**——网关在跑时重跑 gw-start 会重复 build+nohup(新进程 bind 失败即死,health 打到老网关假成功,pid 文件被死 pid 污染);修法=幂等行 if/else 递归 `$(MAKE) gw-start-real`(curl /health 探活替代端口 grep——端口被占≠网关在跑);gw-stop 增强 pid 文件失效时按端口兜底。三场景实测:在跑跳过 ✓/脏 pid 按端口停 ✓/干净启动+md5 一致 ✓。**诊断教训留痕:`| tail -N` 截掉首行">> 网关已在"致两轮误诊"玄学 NOMATCH"——排查管道类输出须看全量**。
|
||||
- 三处 :8081 陈旧注释校准(端口归位 8080 遗留)。
|
||||
|
||||
## 2026-11-13(夜)· S1-B 收口五件 + S1 泳道收官
|
||||
|
||||
- **send P2 拆三子包**(0550f2a):submit/consume/dispatch + facade 别名门面(外部零改动),契约族(事件/状态常量)迁 submit 并导出,依赖单向无环;**dayNN 19 文件语义改名**(5ccca41);**storage minio→s3 归一**(b499b66);**audit_mode=2 文案校准**(「2自动拒绝」→「2自动审核·黑词」,后端 2+openapi 1+前端已对);**gofmt 存量清零**(e9b70ea)。
|
||||
- **S1 泳道出口判定 ✅**:B 层搬运+三项结构改造+迁移链落地+**全仓 -race 38 包 RC=0**+lint 0+集成七件绿;验收单 `docs/acceptance/s1-b-closeout.md`(24h 稳定性门槛在途,11-14 17:55 期满回填)。
|
||||
- 唯一测试适配:clickhouse TestInsertSQL 断言随标识符反引号化更新(非业务语义)。
|
||||
|
||||
## 2026-11-13(晚)· 对象存储 MinIO→RustFS 切换
|
||||
|
||||
- **动因**:MinIO 社区版收费化;**方案=换服务端不换客户端**(minio-go SDK=Apache 2.0 通用 S3 客户端无授权风险)。
|
||||
|
||||
@@ -113,7 +113,7 @@ docker-build: ## 构建服务镜像(默认 gateway,可 SERVICE=business 等)
|
||||
# ---- Web 端线上测试(Day 36 增补;Mock 模式零依赖,无需后端/中间件) ----
|
||||
WEB_PORT ?= 5173
|
||||
|
||||
web-dev: ## 启动 Web 线上测试(Mock 模式,后台常驻;WEB_PORT=5173 可覆盖)
|
||||
web-dev: ## 启动 Web 开发服务(单一 real 模式;【铁律】测试端口固定=5173,WEB_PORT 勿覆盖)
|
||||
@mkdir -p logs
|
||||
@if ss -tln 2>/dev/null | grep -q ":$(WEB_PORT) "; then \
|
||||
echo ">> 端口 $(WEB_PORT) 已被占用(可能已在运行): make web-log 查看 / make web-stop 停止"; exit 1; fi
|
||||
@@ -122,9 +122,9 @@ web-dev: ## 启动 Web 线上测试(Mock 模式,后台常驻;WEB_PORT=5173 可
|
||||
curl -sf -o /dev/null http://127.0.0.1:$(WEB_PORT)/ && { ok=1; break; }; done; \
|
||||
if [ "$$ok" != 1 ]; then echo ">> 启动失败,查看 logs/web-dev.log"; exit 1; fi
|
||||
@ss -tlnp 2>/dev/null | grep ":$(WEB_PORT) " | grep -oP 'pid=\K[0-9]+' | head -1 > logs/web-dev.pid
|
||||
@echo ">> Web 线上测试已启动(Mock 模式,pid=$$(cat logs/web-dev.pid))"
|
||||
@echo ">> Web 开发服务已启动(real 模式,pid=$$(cat logs/web-dev.pid))"
|
||||
@echo ">> 本机: http://127.0.0.1:$(WEB_PORT)"
|
||||
@echo ">> 局域网: http://<服务器IP>:$(WEB_PORT) (Mock 登录: admin/admin123 或 customer/123456)"
|
||||
@echo ">> 局域网: http://<服务器IP>:$(WEB_PORT) (登录: make seed-admin 打印的 admin 密码)"
|
||||
@echo ">> 日志: make web-log | 停止: make web-stop"
|
||||
|
||||
web-stop: ## 停止 Web 线上测试(端口占用兜底清理)
|
||||
@@ -143,31 +143,39 @@ web-test: ## Web 质量门:lint + Vitest + build(与 L1 DoD 一致)
|
||||
cd web && ./node_modules/.bin/eslint . --ext .ts,.vue --max-warnings 0 \
|
||||
&& ./node_modules/.bin/vitest run && npm run build
|
||||
|
||||
# ---- 真实模式联调(Day 31:网关 :8081 + Web real;首启先 make seed-admin) ----
|
||||
GW_PORT ?= 8081
|
||||
web-image: ## S3·FE-6:构建 web 产物镜像(宿主 vite build + nginx 打包 dist;API_UPSTREAM 运行态注入)
|
||||
cd web && ./node_modules/.bin/vite build
|
||||
sg docker -c 'docker build -f web/Dockerfile -t go-sms-web:dev .'
|
||||
|
||||
# ---- 真实模式联调(网关默认 :8080 + Web real;首启先 make seed-admin) ----
|
||||
GW_PORT ?= 8080
|
||||
|
||||
seed-admin: ## 首启引导:迁移 + 内置超管 admin(密码仅打印一次;已有账号则跳过)
|
||||
go run ./cmd/migrate -config configs/config.dev.yaml seed-admin
|
||||
|
||||
gw-start: ## 启动网关(后台,:8081;8080 被宿主旧进程占用故用 8081,可用 GW_PORT 覆盖)
|
||||
gw-start: ## 启动网关(后台,默认 :8080,可用 GW_PORT 覆盖;已在运行则跳过)
|
||||
@mkdir -p logs
|
||||
@if ss -tln 2>/dev/null | grep -q ":$(GW_PORT) "; then echo ">> 网关已在 :$(GW_PORT) 运行"; exit 0; fi
|
||||
@if curl -sf -m 2 http://127.0.0.1:$(GW_PORT)/health >/dev/null 2>&1; then \
|
||||
echo ">> 网关已在 :$(GW_PORT) 运行"; \
|
||||
else $(MAKE) --no-print-directory gw-start-real; fi
|
||||
|
||||
gw-start-real:
|
||||
@go build -o bin/gateway ./cmd/gateway
|
||||
@nohup env GOSMS_LOG_FILE_PATH=./logs/app.log GOSMS_SERVER_HTTP_ADDR=:$(GW_PORT) ./bin/gateway > logs/gateway-run.log 2>&1 & echo $! > logs/gateway.pid
|
||||
@nohup env GOSMS_LOG_FILE_PATH=./logs/app.log GOSMS_SERVER_HTTP_ADDR=:$(GW_PORT) ./bin/gateway > logs/gateway-run.log 2>&1 & echo $$! > logs/gateway.pid
|
||||
@sleep 2
|
||||
@curl -sf http://127.0.0.1:$(GW_PORT)/health || { echo ">> 启动失败,查看 logs/gateway-run.log"; exit 1; }
|
||||
@echo ""
|
||||
@echo ">> 网关已启动(:$(GW_PORT),pid=$$(cat logs/gateway.pid))"
|
||||
|
||||
gw-stop: ## 停止网关
|
||||
gw-stop: ## 停止网关(pid 文件优先,脏时按端口兜底)
|
||||
@if [ -f logs/gateway.pid ] && kill -0 $$(cat logs/gateway.pid) 2>/dev/null; then \
|
||||
kill $$(cat logs/gateway.pid); echo ">> 已停止"; else echo ">> 未在运行"; fi
|
||||
kill $$(cat logs/gateway.pid); echo ">> 已停止(pid=$$(cat logs/gateway.pid))"; \
|
||||
elif PID=$$(ss -ltnp 2>/dev/null | grep ":$(GW_PORT) " | grep -oP 'pid=\K[0-9]+' | head -1); [ -n "$$PID" ]; then \
|
||||
kill $$PID; echo ">> pid 文件失效,按端口停止(pid=$$PID)"; \
|
||||
else echo ">> 未在运行"; fi
|
||||
|
||||
web-real: ## 启动 Web 真实模式(依赖 gw-start;vite proxy 已指向 :8081)
|
||||
web-real: ## 启动 Web 真实联调(依赖 gw-start;S3 起 dev 即 real,Mock 已下线)
|
||||
@$(MAKE) gw-start
|
||||
@if [ ! -f web/.env.development.local ]; then \
|
||||
echo 'VITE_API_MODE=real' > web/.env.development.local; \
|
||||
echo ">> 已创建 web/.env.development.local(不入库)"; fi
|
||||
@if ss -tln 2>/dev/null | grep -q ":$(WEB_PORT) "; then echo ">> Web 已在 :$(WEB_PORT) 运行"; \
|
||||
else $(MAKE) web-dev; fi
|
||||
@echo ">> 真实模式就绪: http://<服务器IP>:$(WEB_PORT) (登录: make seed-admin 打印的 admin 密码)"
|
||||
|
||||
+281
-2
@@ -1,7 +1,7 @@
|
||||
openapi: 3.0.3
|
||||
info:
|
||||
title: go-sms API v1(阶段 2:账号与权限)
|
||||
version: 1.2.0
|
||||
version: 1.5.0
|
||||
description: |
|
||||
阶段 2(Day 21-30)交付的账号与权限域全部端点。
|
||||
统一响应 {code,msg,data}:code=ISC:000 成功;错误码三位制(ISC:4xx/5xx)。
|
||||
@@ -19,6 +19,8 @@ tags:
|
||||
- {name: permissions, description: 二级权限与导出(Day 25)}
|
||||
- {name: audit, description: 审计查询(Day 29)}
|
||||
- {name: channel, description: 通道 CRUD/测试工具/拦截配置(Day 31-36)}
|
||||
- {name: billing, description: 充值订单审核/消耗日汇总(S2)}
|
||||
- {name: settle, description: 下家/上家月结与阶梯价(S2)}
|
||||
paths:
|
||||
/auth/login:
|
||||
post:
|
||||
@@ -101,7 +103,7 @@ paths:
|
||||
/accounts/{id}/audit-mode:
|
||||
put:
|
||||
tags: [accounts]
|
||||
summary: 审核模式(0 免审/1 手动/2 自动拒绝)
|
||||
summary: 审核模式(0 免审/1 手动/2 自动审核——黑词命中拒/未命中过)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody: {content: {application/json: {schema: {type: object, required: [business, mode], properties: {business: {type: string}, mode: {type: integer, enum: [0, 1, 2]}}}}}}
|
||||
responses: {"200": {description: ok}}
|
||||
@@ -751,6 +753,282 @@ paths:
|
||||
cmpp_max_conns: {type: integer, description: "Day 62:每账号最大 CMPP 连接数(0=不修改;-1=恢复系统默认;1~1024=设置)"}
|
||||
cmpp_speed: {type: integer, description: "Day 62:单链提交限速 条/s(0=不修改;-1=恢复系统默认;1~100000=设置)"}
|
||||
responses: {"200": {description: "secret(=库值原文,重发/自定义时返回);cmpp_password(库值即密码)"}}
|
||||
# ---- S2 结算域(契约草案 2026-11-14;表设计裁决 docs/review/s2-settle-table-review.md;
|
||||
# 金额口径=元,条数口径=条;R1=出账时刻按日成功条数×生效单价推导,明细 Fee 不金额化) ----
|
||||
/billing/orders:
|
||||
get:
|
||||
tags: [billing]
|
||||
summary: 充值订单分页(account_id/pay_status 过滤;admin/ops)
|
||||
parameters:
|
||||
- {name: page, in: query, schema: {type: integer, default: 1}}
|
||||
- {name: page_size, in: query, schema: {type: integer, default: 20, maximum: 100}}
|
||||
- {name: account_id, in: query, schema: {type: integer, format: int64}}
|
||||
- {name: pay_status, in: query, schema: {type: integer, enum: [0, 1, 2], description: "0待确认 1已到账 2失败"}}
|
||||
responses: {"200": {description: total+list}}
|
||||
post:
|
||||
tags: [billing]
|
||||
summary: 创建充值订单(pay_status=0 待确认,不动余额;复用 account:recharge 二级权限;count=到账条数,amount=元记账)
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [account_id, count]
|
||||
properties:
|
||||
account_id: {type: integer, format: int64}
|
||||
count: {type: integer, format: int64, description: 到账条数(>0;确认后余额自增此值)}
|
||||
amount: {type: number, description: 金额(元;记账与账单展示,不参与余额)}
|
||||
pay_type: {type: string}
|
||||
remark: {type: string}
|
||||
responses: {"200": {description: 订单(含 order_no)}, "400": {description: 参数非法}}
|
||||
/billing/orders/{id}/confirm:
|
||||
post:
|
||||
tags: [billing]
|
||||
summary: 付款审核确认(0→1;事务=状态 CAS+余额原子自增 count+流水 ref_id=order_no;并发/重复确认被 CAS 拒)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: 新余额}, "409": {description: "状态非 0(ISC:039)"}}
|
||||
/billing/orders/{id}/reject:
|
||||
post:
|
||||
tags: [billing]
|
||||
summary: 驳回(0→2;不涉余额)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: ok}, "409": {description: "状态非 0(ISC:039)"}}
|
||||
/billing/consume:
|
||||
get:
|
||||
tags: [billing]
|
||||
summary: 消耗日汇总查询(账号×业务×天;admin/ops 全量,customer 仅自己;默认近 7 天)
|
||||
parameters:
|
||||
- {name: account_id, in: query, schema: {type: integer, format: int64}}
|
||||
- {name: business, in: query, schema: {type: string}}
|
||||
- {name: date_from, in: query, schema: {type: string, format: date}}
|
||||
- {name: date_to, in: query, schema: {type: string, format: date}}
|
||||
- {name: page, in: query, schema: {type: integer, default: 1}}
|
||||
- {name: page_size, in: query, schema: {type: integer, default: 20, maximum: 100}}
|
||||
responses: {"200": {description: "list{account_id,business,stat_date,submit/success/fail/unknown_count,fee_total(单位=条)"}}
|
||||
/settle/customer:
|
||||
get:
|
||||
tags: [settle]
|
||||
summary: 下家月结单分页(period/account_id/status 过滤;admin/ops 全量,customer 仅自己)
|
||||
parameters:
|
||||
- {name: page, in: query, schema: {type: integer, default: 1}}
|
||||
- {name: page_size, in: query, schema: {type: integer, default: 20, maximum: 100}}
|
||||
- {name: period, in: query, schema: {type: string, description: 账期 YYYY-MM}}
|
||||
- {name: account_id, in: query, schema: {type: integer, format: int64}}
|
||||
- {name: status, in: query, schema: {type: integer, enum: [0, 1, 2], description: "0草稿 1已确认 2已结账"}}
|
||||
responses: {"200": {description: total+list}}
|
||||
/settle/customer/generate:
|
||||
post:
|
||||
tags: [settle]
|
||||
summary: 生成/重算下家账期结算单(settle:write;幂等 uk(account,period);status=2 拒重算;金额=Σ日成功条数×生效单价,阶梯档覆写)
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [period]
|
||||
properties:
|
||||
period: {type: string, description: 账期 YYYY-MM}
|
||||
account_id: {type: integer, format: int64, description: 缺省=全账号逐个生成}
|
||||
responses: {"200": {description: 生成/重算统计}, "409": {description: 含已结账单}}
|
||||
/settle/customer/{id}:
|
||||
get:
|
||||
tags: [settle]
|
||||
summary: 结算单详情(单头+按天明细,取自 billing_consume_log;customer 仅自己)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: "单头+days[]"}, "404": {description: 不存在}}
|
||||
/settle/customer/{id}/confirm:
|
||||
post:
|
||||
tags: [settle]
|
||||
summary: 确认结算单(0草稿→1已确认;人工核对锁账,拒绝静默重算;settle:write)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: ok}, "409": {description: 状态非 0}}
|
||||
/settle/customer/{id}/settle:
|
||||
post:
|
||||
tags: [settle]
|
||||
summary: 结账(1→2;写 settle_record(direction=1);终态拒重算;settle:write)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
pay_type: {type: string}
|
||||
remark: {type: string}
|
||||
responses: {"200": {description: ok}, "409": {description: 状态非 1}}
|
||||
/settle/customer/{id}/export:
|
||||
get:
|
||||
tags: [settle]
|
||||
summary: 结算单 XLSX 导出(单头+按天明细)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: application/octet-stream}}
|
||||
/settle/upstream:
|
||||
get:
|
||||
tags: [settle]
|
||||
summary: 上家月结单分页(period/upstream_id/status 过滤;admin/ops)
|
||||
parameters:
|
||||
- {name: page, in: query, schema: {type: integer, default: 1}}
|
||||
- {name: page_size, in: query, schema: {type: integer, default: 20, maximum: 100}}
|
||||
- {name: period, in: query, schema: {type: string}}
|
||||
- {name: upstream_id, in: query, schema: {type: integer, format: int64}}
|
||||
- {name: status, in: query, schema: {type: integer, enum: [0, 1, 2]}}
|
||||
responses: {"200": {description: total+list}}
|
||||
/settle/upstream/generate:
|
||||
post:
|
||||
tags: [settle]
|
||||
summary: 生成/重算上家账期结算单(settle:write;按 channel.upstream_id 归并;计费口径随通道 billing_mode=success/success_unknown/all)
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [period]
|
||||
properties:
|
||||
period: {type: string, description: 账期 YYYY-MM}
|
||||
upstream_id: {type: integer, format: int64, description: 缺省=全上家逐个生成}
|
||||
responses: {"200": {description: 生成/重算统计}, "409": {description: 含已结账单}}
|
||||
/settle/upstream/{id}:
|
||||
get:
|
||||
tags: [settle]
|
||||
summary: 上家结算单详情(单头+按通道×天明细,取自 stats_channel_daily)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: "单头+channels[]days[]"}, "404": {description: 不存在}}
|
||||
/settle/upstream/{id}/settle:
|
||||
post:
|
||||
tags: [settle]
|
||||
summary: 结账(1→2;写 settle_record(direction=2);settle:write)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
pay_type: {type: string}
|
||||
remark: {type: string}
|
||||
responses: {"200": {description: ok}, "409": {description: 状态非 1}}
|
||||
/settle/upstream/{id}/export:
|
||||
get:
|
||||
tags: [settle]
|
||||
summary: 上家结算单 XLSX 导出(单头+通道×天明细)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: application/octet-stream}}
|
||||
/settle/ladder:
|
||||
get:
|
||||
tags: [settle]
|
||||
summary: 阶梯价档位列表(owner_type/owner_id 过滤;settle:write 可写,read 全角色)
|
||||
parameters:
|
||||
- {name: owner_type, in: query, schema: {type: string, enum: [account, upstream]}}
|
||||
- {name: owner_id, in: query, schema: {type: integer, format: int64}}
|
||||
responses: {"200": {description: 档位列表(按 tier_from 升序)}}
|
||||
post:
|
||||
tags: [settle]
|
||||
summary: 新增档位(owner×tier_from 唯一;0=首档下限;settle:write)
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [owner_type, owner_id, tier_from, price]
|
||||
properties:
|
||||
owner_type: {type: string, enum: [account, upstream]}
|
||||
owner_id: {type: integer, format: int64}
|
||||
tier_from: {type: integer, format: int64, description: 档位下限(含,条/月)}
|
||||
price: {type: number, description: 该档单价(元/条)}
|
||||
responses: {"200": {description: ok}, "400": {description: 档位重叠/参数非法}}
|
||||
/settle/ladder/{id}:
|
||||
put:
|
||||
tags: [settle]
|
||||
summary: 修改档位(price/tier_from;settle:write)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema: {type: object, properties: {tier_from: {type: integer, format: int64}, price: {type: number}}}
|
||||
responses: {"200": {description: ok}}
|
||||
delete:
|
||||
tags: [settle]
|
||||
summary: 删除档位(settle:write)
|
||||
parameters: [{name: id, in: path, required: true, schema: {type: integer}}]
|
||||
responses: {"200": {description: ok}}
|
||||
/stats/biz:
|
||||
get:
|
||||
tags: [stats]
|
||||
summary: 业务类型分布(Issue#6 Dashboard donut;近 N 自然日,1~30 缺省 7)
|
||||
parameters:
|
||||
- {name: days, in: query, schema: {type: integer, default: 7, minimum: 1, maximum: 30}}
|
||||
responses:
|
||||
"200": {description: '{days,by_business:[{key:sms|video,count}],by_send_type:[{key:batch|personal|timer|file|other,count}]}'}
|
||||
/stats/realtime:
|
||||
get:
|
||||
tags: [stats]
|
||||
summary: 实时提交速率(Issue#6 Dashboard KPI;60s 窗口均值,覆盖索引 COUNT)
|
||||
responses:
|
||||
"200": {description: '{ts,window_s:60,global_submit_ps,top_accounts:[{account_id,name,submit_ps}]Top5(客户口径仅自己)}'}
|
||||
/ops/system/overview:
|
||||
get:
|
||||
tags: [ops]
|
||||
summary: 系统健康总览(Issue#6 Dashboard 系统健康卡;admin/ops,gateway 视角五子系统)
|
||||
responses:
|
||||
"200": {description: '{uptime_s,subsystems:[{name:gateway|consumer|mysql|redis|kafka,status:up|degraded|down|unknown,detail,latency_ms}]}'}
|
||||
/stats/profit/users:
|
||||
get:
|
||||
tags: [settle]
|
||||
summary: 用户利润分析(平台角色)——revenue=Σ日成功×用户时间线价(阶梯不进分析);cost=通道成本按成功条数占比分摊;rebate=直线下级结算额×其 rebate_rate(R4 单级,历史不重算);profit=revenue-cost+rebate;parent_id=子树过滤(Path)
|
||||
parameters:
|
||||
- {name: date_from, in: query, schema: {type: string, format: date}}
|
||||
- {name: date_to, in: query, schema: {type: string, format: date}}
|
||||
- {name: parent_id, in: query, schema: {type: integer, format: int64, description: 以该账号为根的子树(缺省=全平台)}}
|
||||
- {name: page, in: query, schema: {type: integer, default: 1}}
|
||||
- {name: page_size, in: query, schema: {type: integer, default: 20, maximum: 100}}
|
||||
responses: {"200": {description: "{list[...],total,summary{total_revenue,total_cost,total_rebate,platform_profit}}(元)"}}
|
||||
/stats/profit/channels:
|
||||
get:
|
||||
tags: [settle]
|
||||
summary: 通道利润分析(平台角色)——cost=Σ日成功×通道时间线价;revenue=全平台用户收入×通道成功占比分摊(无账号×通道交叉粒度事实源);profit=revenue-cost;date_from/date_to/channel_id
|
||||
parameters:
|
||||
- {name: date_from, in: query, schema: {type: string, format: date}}
|
||||
- {name: date_to, in: query, schema: {type: string, format: date}}
|
||||
- {name: channel_id, in: query, schema: {type: integer, format: int64}}
|
||||
- {name: page, in: query, schema: {type: integer, default: 1}}
|
||||
- {name: page_size, in: query, schema: {type: integer, default: 20, maximum: 100}}
|
||||
responses: {"200": {description: "list{channel_id,name,upstream_id,success_count,revenue,cost,profit}(元)"}}
|
||||
/ops/settle/aggregate:
|
||||
post:
|
||||
tags: [settle]
|
||||
summary: 手动触发消耗日汇总回补(date 缺省=昨日;幂等 UPSERT;运维口径,admin)
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema: {type: object, properties: {date: {type: string, format: date, description: 可回补历史日期(明细热窗内)}}}
|
||||
responses: {"200": {description: "落账行数(账号/通道两表)"}}
|
||||
/ops/callback:
|
||||
get:
|
||||
tags: [callback]
|
||||
summary: 回调推送任务列表(S4·C1;死信可查;status 0待推送/1已送达/2死信,ops)
|
||||
parameters:
|
||||
- {name: status, in: query, schema: {type: integer, enum: [0, 1, 2]}, description: 缺省=全部}
|
||||
- {name: type, in: query, schema: {type: integer, enum: [1, 2]}, description: "1回执终态 2上行消息;缺省=全部"}
|
||||
- {name: account_id, in: query, schema: {type: integer}}
|
||||
- {name: page, in: query, schema: {type: integer, default: 1}}
|
||||
- {name: page_size, in: query, schema: {type: integer, default: 20}}
|
||||
responses:
|
||||
"200":
|
||||
description: "{total,list[],status_counts{pending,sent,dead}}"
|
||||
/ops/callback/{id}/resend:
|
||||
post:
|
||||
tags: [callback]
|
||||
summary: 死信重派(status 2→0/attempt 清零/立即投递;仅死信可派,幂等拒绝非死信,ops)
|
||||
parameters:
|
||||
- {name: id, in: path, required: true, schema: {type: integer}}
|
||||
responses:
|
||||
"200": {description: "{id,resent:true}"}
|
||||
"404": {description: "任务不存在或非死信状态(ISC:404)"}
|
||||
|
||||
components:
|
||||
securitySchemes:
|
||||
bearerAuth: {type: http, scheme: bearer, bearerFormat: JWT}
|
||||
@@ -809,6 +1087,7 @@ x-isc-dict:
|
||||
ISC:036: 地区屏蔽
|
||||
ISC:037: 审核状态冲突(非待审)
|
||||
ISC:038: 任务状态冲突(不可取消/改期)
|
||||
ISC:039: 订单状态冲突(非待确认)
|
||||
ISC:040: 夜间频控
|
||||
ISC:043: 5G消息拦截
|
||||
ISC:400: 参数错误
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
✅ **`git.i7l.cn` 全链路上线完成**——公网实测:`https://git.i7l.cn/` 正常渲染 Gitea 1.27.1 页面("inscom-git")。`http://` 自动 301 跳转 HTTPS。
|
||||
|
||||
**手册已落地**:`/root/workspace/letsencrypt/ADD_DOMAIN_GUIDE.md`(与你的运维脚本同目录),以下是核心流程——以后新增任何 `域名 → 后端` 站点,四步搞定(以下全部在 5.20 上以 root 执行,把 `git.i7l.cn` 和 `192.168.5.27:24400` 换成你的新值):
|
||||
|
||||
## 标准四步(示例:git.i7l.cn → 192.168.5.27:24400)
|
||||
|
||||
**前置**:DNSPod 加 A 记录(域名 → 121.46.230.53);确认后端可达 `wget -q -S -O /dev/null http://192.168.5.27:24400/`
|
||||
|
||||
**① 写 80 临时配置**(签证书的前提)
|
||||
```bash
|
||||
cat > /etc/nginx/sites-enabled/git.i7l.cn.conf <<'EOF'
|
||||
server {
|
||||
listen 80;
|
||||
server_name git.i7l.cn;
|
||||
location ^~ /.well-known/acme-challenge/ {
|
||||
root /var/www/html;
|
||||
default_type text/plain;
|
||||
}
|
||||
location / {
|
||||
proxy_pass http://192.168.5.27:24400;
|
||||
}
|
||||
}
|
||||
EOF
|
||||
nginx -t && systemctl reload nginx
|
||||
```
|
||||
|
||||
**② 签发证书**(webroot 模式,自动纳入续期)
|
||||
```bash
|
||||
certbot certonly --webroot -w /var/www/html -d git.i7l.cn --non-interactive --agree-tos
|
||||
# 成功标志:Successfully received certificate.
|
||||
```
|
||||
|
||||
**③ 写最终配置**(443 + 80 跳转;Git 类服务保留 `client_max_body_size 0`)
|
||||
```bash
|
||||
cat > /etc/nginx/sites-enabled/git.i7l.cn.conf <<'EOF'
|
||||
server {
|
||||
listen 443 ssl;
|
||||
server_name git.i7l.cn;
|
||||
ssl_certificate /etc/letsencrypt/live/git.i7l.cn/fullchain.pem;
|
||||
ssl_certificate_key /etc/letsencrypt/live/git.i7l.cn/privkey.pem;
|
||||
client_max_body_size 0; # Git 大仓库推送;普通站点可删
|
||||
location / {
|
||||
proxy_pass http://192.168.5.27:24400;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto https;
|
||||
proxy_http_version 1.1; # WebSocket(需要时保留)
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection "upgrade";
|
||||
}
|
||||
}
|
||||
server {
|
||||
listen 80;
|
||||
server_name git.i7l.cn;
|
||||
location ^~ /.well-known/acme-challenge/ { # 勿删!续期依赖
|
||||
root /var/www/html;
|
||||
default_type text/plain;
|
||||
}
|
||||
location / {
|
||||
return 301 https://$host$request_uri;
|
||||
}
|
||||
}
|
||||
EOF
|
||||
nginx -t && systemctl reload nginx
|
||||
```
|
||||
|
||||
**④ 验证**
|
||||
```bash
|
||||
curl -sI https://git.i7l.cn/ | head -3 # 期望 200
|
||||
curl -sI http://git.i7l.cn/ | head -3 # 期望 301
|
||||
certbot certificates | grep -A3 git.i7l.cn # 证书有效期
|
||||
```
|
||||
|
||||
## 关键维护命令
|
||||
|
||||
```bash
|
||||
certbot renew --dry-run --cert-name git.i7l.cn # 续期演练(不动真证书)
|
||||
~/workspace/letsencrypt/list_domain.sh # 列出全部证书
|
||||
~/workspace/letsencrypt/delete_domain.sh git.i7l.cn && rm /etc/nginx/sites-enabled/git.i7l.cn.conf && nginx -t && systemctl reload nginx # 删除域名
|
||||
```
|
||||
|
||||
## 两条铁律 + 排障(手册内有完整表格)
|
||||
|
||||
1. **conf 里的 `acme-challenge` location 永远别删**——webroot 自动续期(cron 每日 3 点 `renew.sh` + certbot.timer)依赖它
|
||||
2. **Vite 类站点**记得在 `web-next/vite.config.ts` 的 `allowedHosts` 加新域名,且**不要**配 `hmr.clientPort`(同源即可,硬编码端口会导致内网 HMR 疯狂刷新)
|
||||
|
||||
排障速查(403 入口拦截 / PR_CONNECT_RESET 域名被墙等,均来自本次实战)详见 `ADD_DOMAIN_GUIDE.md` 末节。
|
||||
+38
-3
@@ -19,8 +19,11 @@ import (
|
||||
"go-sms/internal/model/entity"
|
||||
"go-sms/internal/pkg/metrics"
|
||||
"go-sms/internal/pkg/queue"
|
||||
chrepo "go-sms/internal/repository/clickhouse"
|
||||
"go-sms/internal/service/archive"
|
||||
"go-sms/internal/service/audit"
|
||||
"go-sms/internal/service/balance"
|
||||
"go-sms/internal/service/callback"
|
||||
"go-sms/internal/service/channel"
|
||||
"go-sms/internal/service/interceptor"
|
||||
"go-sms/internal/service/report"
|
||||
@@ -193,13 +196,45 @@ func main() {
|
||||
zap.Bool("pacer", pacer.Open()),
|
||||
zap.String("reclaim", reclaimEvery.String()), zap.Bool("lag_reader", lagErr == nil))
|
||||
|
||||
// S4·C1/C2:回调推送引擎(HTTP 回执终态+上行推送,指数退避+死信可查)
|
||||
// + 上行自动拉黑(词表 SystemConfig 可配,拒收即拉黑)。
|
||||
cbSvc := callback.NewService(db)
|
||||
upBl := uplink.NewAutoBlacklister(db, rc)
|
||||
blCtx, stopBl := context.WithCancel(context.Background())
|
||||
go upBl.Start(blCtx)
|
||||
a.OnShutdown(stopBl)
|
||||
cbW := callback.NewPushWorker(db)
|
||||
cbWCtx, stopCbW := context.WithCancel(context.Background())
|
||||
go cbW.Run(cbWCtx, callback.DefaultScanEvery)
|
||||
a.OnShutdown(stopCbW)
|
||||
a.Lg().Info("callback_engine_ready",
|
||||
zap.Bool("blacklist", true), zap.Duration("scan_every", callback.DefaultScanEvery))
|
||||
|
||||
// S4·D3:明细冷热归档 job(CH 不可达=分区维护仍跑、迁出禁用自愈)。
|
||||
var chConn chrepo.Conn
|
||||
if c, chErr := chrepo.Open(chrepo.Config{
|
||||
Addrs: cfg.ClickHouse.Addrs, Database: cfg.ClickHouse.Database,
|
||||
Username: cfg.ClickHouse.Username, Password: cfg.ClickHouse.Password,
|
||||
DialTimeout: cfg.ClickHouse.DialTimeout,
|
||||
}); chErr != nil {
|
||||
a.Lg().Warn("clickhouse_open_failed_archive_degraded", zap.Error(chErr))
|
||||
} else {
|
||||
chConn = c
|
||||
a.OnShutdown(func() { _ = chConn.Close() })
|
||||
}
|
||||
arcSvc := archive.NewService(db, chConn)
|
||||
arcCtx, stopArc := context.WithCancel(context.Background())
|
||||
go arcSvc.Run(arcCtx, archive.DefaultArchiveEvery)
|
||||
a.OnShutdown(stopArc)
|
||||
a.Lg().Info("archive_job_ready", zap.Bool("clickhouse", chConn != nil))
|
||||
|
||||
// Day 49:上行消费组(sms-up;15 天窗口匹配下行明细回填 match_task_id;
|
||||
// 客户推送/自动拉黑属阶段 5 Day54)。独立 group 前缀防 offset 互扰。
|
||||
// S4:上行推送+自动拉黑挂消费尾部,不阻断匹配主链)。独立 group 前缀防 offset 互扰。
|
||||
upTopic := cfg.Kafka.Topics["up"]
|
||||
if upTopic == "" {
|
||||
upTopic = queue.TopicUp
|
||||
}
|
||||
uc := uplink.NewConsumer(db)
|
||||
uc := uplink.NewConsumer(db).WithCallback(cbSvc).WithBlacklist(upBl)
|
||||
ucg, err := queue.NewConsumerGroup(queue.ConsumerGroupConfig{
|
||||
Consumer: queue.ConsumerConfig{
|
||||
Brokers: cfg.Kafka.Brokers,
|
||||
@@ -256,7 +291,7 @@ func main() {
|
||||
go comp.Run(compCtx, 10*time.Minute)
|
||||
a.OnShutdown(stopComp)
|
||||
|
||||
repC := report.NewConsumer(db).WithBilling(bill)
|
||||
repC := report.NewConsumer(db).WithBilling(bill).WithCallback(cbSvc) // S4·C1:回执终态 HTTP 回调入队
|
||||
// Day 61(P0.5):CMPP 回执协议侧下发桥——终态锁定后投 cmpp-deliver
|
||||
// (gateway 进程消费→PushDeliver);producer 缺省时不注入(HTTP 来源无此诉求)。
|
||||
if p := a.Producer(); p != nil {
|
||||
|
||||
+59
-5
@@ -20,17 +20,22 @@ import (
|
||||
"go-sms/internal/pkg/storage"
|
||||
cmppproto "go-sms/internal/protocol/cmpp"
|
||||
smppproto "go-sms/internal/protocol/smpp"
|
||||
chrepo "go-sms/internal/repository/clickhouse"
|
||||
"go-sms/internal/service/access"
|
||||
"go-sms/internal/service/account"
|
||||
"go-sms/internal/service/archive"
|
||||
"go-sms/internal/service/audit"
|
||||
"go-sms/internal/service/auth"
|
||||
"go-sms/internal/service/balance"
|
||||
"go-sms/internal/service/callback"
|
||||
"go-sms/internal/service/channel"
|
||||
"go-sms/internal/service/interceptor"
|
||||
"go-sms/internal/service/openapi"
|
||||
"go-sms/internal/service/report"
|
||||
"go-sms/internal/service/router"
|
||||
"go-sms/internal/service/send"
|
||||
"go-sms/internal/service/settle"
|
||||
"go-sms/internal/service/sysconfig"
|
||||
"go-sms/internal/service/template"
|
||||
"go-sms/internal/service/uplink"
|
||||
"go-sms/internal/util"
|
||||
@@ -97,6 +102,8 @@ func main() {
|
||||
// Day 48:全流程状态机——拦截链(Day42)+sms-submit 统一出口(ProducerSink,
|
||||
// 替代 openapi 自投,预扣/拦截退费/审核拒绝退费在 SubmitService 内闭环)。
|
||||
// Kafka 降级(producer=nil)不注入 sink:发送由 ReclaimScanner DB 认领兜底。
|
||||
// Issue#6·P2:系统健康探针(lagR 未就绪时为 nil=unknown)。
|
||||
var healthLag func(ctx context.Context) (int64, error)
|
||||
pipelineOpts := []send.SubmitOption{send.WithInterceptorChain(ichain)}
|
||||
if p := a.Producer(); p != nil {
|
||||
pipelineOpts = append(pipelineOpts,
|
||||
@@ -141,6 +148,10 @@ func main() {
|
||||
}
|
||||
}()
|
||||
a.OnShutdown(func() { stopLag(); _ = lagR.Close() })
|
||||
// Issue#6·P2:健康探针复用同一 LagReader(只读)。
|
||||
healthLag = func(ctx context.Context) (int64, error) {
|
||||
return lagR.Total(ctx, []string{lagTopic})
|
||||
}
|
||||
}
|
||||
} else {
|
||||
a.Lg().Warn("submit_pipeline_kafka_disabled",
|
||||
@@ -176,7 +187,7 @@ func main() {
|
||||
submitSvc := send.NewSubmitService(db, sf, reviewSvc, pipelineOpts...)
|
||||
// Day 44:MinIO 对象存储(号码文件);初始化失败降级 nil → /files/numbers/import 不注册。
|
||||
var fileImportSvc *send.FileImportService
|
||||
if mo, merr := storage.NewMinIO(context.Background(),
|
||||
if mo, merr := storage.NewS3(context.Background(),
|
||||
cfg.MinIO.Endpoint, cfg.MinIO.AccessKey, cfg.MinIO.SecretKey, cfg.MinIO.Bucket, cfg.MinIO.UseSSL); merr != nil {
|
||||
a.Lg().Warn("minio_init_failed_file_import_disabled", zap.Error(merr))
|
||||
} else {
|
||||
@@ -204,10 +215,35 @@ func main() {
|
||||
}
|
||||
// 阶段5收尾(D28 挂账清偿):回执重推+手动配对服务(终态链复用
|
||||
// consumer 同款 finalizeDetail 单一实现;计费走 balStore 同一 Store)。
|
||||
pairSvc := report.NewPairService(db, report.NewBilling(db, balStore), sf)
|
||||
// S4·C1:手动配对终态同步入 HTTP 回调(与 consumer 回执链同挂点)。
|
||||
cbSvc := callback.NewService(db)
|
||||
pairSvc := report.NewPairService(db, report.NewBilling(db, balStore), sf).WithCallback(cbSvc)
|
||||
if p := a.Producer(); p != nil {
|
||||
pairSvc = pairSvc.WithPushSink(&report.ProducerPushSink{P: p})
|
||||
}
|
||||
// S4·D3:明细冷热归档——gateway 侧仅挂 /record/history 冷查
|
||||
// (归档 job 在 consumer 进程);CH 不可达=端点不注册(404 优于半可用)。
|
||||
var arcSvc *archive.Service
|
||||
if chConn, chErr := chrepo.Open(chrepo.Config{
|
||||
Addrs: cfg.ClickHouse.Addrs, Database: cfg.ClickHouse.Database,
|
||||
Username: cfg.ClickHouse.Username, Password: cfg.ClickHouse.Password,
|
||||
DialTimeout: cfg.ClickHouse.DialTimeout,
|
||||
}); chErr != nil {
|
||||
a.Lg().Warn("clickhouse_open_failed_history_disabled", zap.Error(chErr))
|
||||
} else {
|
||||
arcSvc = archive.NewService(db, chConn)
|
||||
if err := arcSvc.EnsureCHSchema(context.Background()); err != nil {
|
||||
a.Lg().Warn("archive_ch_schema_failed", zap.Error(err))
|
||||
}
|
||||
a.OnShutdown(func() { _ = chConn.Close() })
|
||||
}
|
||||
// Issue#3 方案B:系统参数配置服务(system_config KV;/system/config
|
||||
// 管理端点 + access 系统默认接入码回退源;30s 读缓存+写即刻失效)。
|
||||
sysCfgSvc := sysconfig.New(db, 30*time.Second)
|
||||
healthDeps := &v1.HealthDeps{
|
||||
DB: db, Redis: a.Redis(), Lag: healthLag,
|
||||
StartTime: time.Now(), HTTPAddr: cfg.Server.HTTP.Addr, RedisMode: cfg.Redis.Mode,
|
||||
}
|
||||
v1.Register(rg, v1.Deps{
|
||||
Account: svc,
|
||||
Warning: warnSvc,
|
||||
@@ -233,10 +269,24 @@ func main() {
|
||||
CMPPCfg: &cfg.Server.CMPP,
|
||||
// 阶段5收尾(D28):/record/repush 域四端点
|
||||
Pair: pairSvc,
|
||||
// S2:结算域(D2=消耗日汇总;充值审核/月结/阶梯/利润随 D3~D5 追加)
|
||||
Settle: settle.NewService(db),
|
||||
// S4·C1:/ops/callback 死信查询与重派(管理面;投递在 consumer 进程)
|
||||
Callback: cbSvc,
|
||||
// S4·D3:/record/history 冷查(CH;nil=不注册)
|
||||
Archive: arcSvc,
|
||||
// Issue#3 方案B:系统参数配置(/system/config;access 系统默认
|
||||
// 接入码回退数据源)。30s 读缓存+写即刻失效。
|
||||
SysConfig: sysCfgSvc,
|
||||
// Issue#6·P2:系统健康总览
|
||||
Health: healthDeps,
|
||||
})
|
||||
// Day68:运营总览汇总刷新(回补 30 天+60s ticker 增量 UPSERT stats_daily;
|
||||
// 幂等多实例无害,详见 stats_handler.go StartStatsRefresher)。
|
||||
v1.StartStatsRefresher(db, a.Lg())
|
||||
// S2-D2:消耗日汇总滚动重刷(回补 7 天+1h ticker 重刷近 4 天;72h 回执
|
||||
// 窗内 UNKNOWN 会迁移,幂等 UPSERT 收敛;详见 service/settle/job.go)。
|
||||
v1.StartSettleAggregateJob(db, cfg.Settle, a.Lg())
|
||||
|
||||
// Day 46:外部 HTTP API 提交(SMS_HTTP_1.6;/sms/api 独立协议域,MD5 验签+
|
||||
// 双桶限流:全局 IP 桶(静态配置)+账号桶(动态 speed_limit);
|
||||
@@ -285,10 +335,14 @@ func main() {
|
||||
// 提交复用 openapi 受理链;addr 空=不启用)。
|
||||
// Day 62:注入 CMPP 系统默认配额(账号级 0 回退;连接数/单链速度强制)。
|
||||
accSvc := access.New(db, openapiSvc, access.SystemLimits{
|
||||
MaxConns: cfg.Server.CMPP.MaxConnsPerAccount,
|
||||
SubmitRate: cfg.Server.CMPP.SubmitRatePerConn,
|
||||
LongSmsWindow: cfg.Server.CMPP.LongSmsWindow, // Day 71:≤0 默认 72h
|
||||
MaxConns: cfg.Server.CMPP.MaxConnsPerAccount,
|
||||
SubmitRate: cfg.Server.CMPP.SubmitRatePerConn,
|
||||
LongSmsWindow: cfg.Server.CMPP.LongSmsWindow, // Day 71:≤0 默认 72h
|
||||
LongSmsMaxGroup: cfg.Server.CMPP.LongSmsMaxGroup, // ≤0 默认 20 万(乱序容忍窗)
|
||||
})
|
||||
// Issue#3 方案B:系统配置源注入——账号 ext_code 为空时回退系统默认
|
||||
// 接入码 cmpp.default_ext_code(/system/config 可配)。
|
||||
accSvc.SetSystemConfig(sysCfgSvc)
|
||||
// Day 71:长短信挂起制——重启恢复+72h 到期扫描(不齐不发,LP 失败
|
||||
// 终态+UNDELIV 回执经 Kafka cmpp-deliver 回环推下家在线连接)。
|
||||
{
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
// reconcile/settle-check S2-D5:三方对账子命令——明细直查 vs 日汇总 vs
|
||||
// 结算单(diff=0 为 S2 泳道出口判据)。
|
||||
//
|
||||
// 用法(仓库根目录):go run ./cmd/reconcile/settle-check -period 2026-11 [-json]
|
||||
// 退出码:0=三方差异全零;1=存在差异(明细见输出);2=运行错误。
|
||||
// 注意:金额不在比对范围(结算单金额=出账时点价/阶梯快照,事后调价重算必生
|
||||
// 噪音,口径见 settle/reconcile.go 头注);账单未生成=流程状态,计入
|
||||
// bill_missing 但不影响 AllZero 判定。
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"go-sms/internal/app"
|
||||
"go-sms/internal/service/settle"
|
||||
)
|
||||
|
||||
func main() {
|
||||
period := flag.String("period", time.Now().Format("2006-01"), "对账账期 YYYY-MM(缺省=当前月)")
|
||||
asJSON := flag.Bool("json", false, "输出完整 JSON 报告(缺省=人类可读摘要+异常行)")
|
||||
flag.Parse()
|
||||
|
||||
a, err := app.New(app.Options{ServiceName: "reconcile-settle-check"})
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "app init:", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
rep, err := settle.NewService(a.DB()).ReconcilePeriod(context.Background(), *period)
|
||||
if err != nil {
|
||||
a.Lg().Error("settle_check_failed", zap.String("period", *period), zap.Error(err))
|
||||
fmt.Fprintln(os.Stderr, "reconcile:", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
if *asJSON {
|
||||
out, _ := json.MarshalIndent(rep, "", " ")
|
||||
fmt.Println(string(out))
|
||||
} else {
|
||||
s := rep.Summary
|
||||
fmt.Printf("settle-check period=%s as_of=%s\n", rep.Period, rep.AsOf.Format(time.RFC3339))
|
||||
fmt.Printf("accounts: checked=%d diffs=%d bill_missing=%d\n", s.AccountsChecked, s.AccountDiffs, s.BillMissing)
|
||||
fmt.Printf("channels: checked=%d diffs=%d\n", s.ChannelsChecked, s.ChannelDiffs)
|
||||
fmt.Printf("upstreams: checked=%d diffs=%d\n", s.UpstreamsChecked, s.UpstreamDiffs)
|
||||
for _, r := range rep.Accounts {
|
||||
fmt.Printf(" [DIFF] account=%d detail=%d consume=%d bill=%v\n",
|
||||
r.AccountID, r.DetailSuccess, r.ConsumeSuccess, r.BillSuccess)
|
||||
}
|
||||
for _, r := range rep.Channels {
|
||||
fmt.Printf(" [DIFF] channel=%d detail=%d daily=%d\n",
|
||||
r.ChannelID, r.DetailSuccess, r.DailySuccess)
|
||||
}
|
||||
for _, r := range rep.Upstreams {
|
||||
fmt.Printf(" [DIFF] upstream=%d daily=%d bill=%v\n",
|
||||
r.UpstreamID, r.DailyCount, r.BillSuccess)
|
||||
}
|
||||
fmt.Printf("all_zero=%v\n", s.AllZero)
|
||||
}
|
||||
if !rep.Summary.AllZero {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -101,3 +101,8 @@ rate_limit: # Redis 令牌桶分布式限流(Day 9)
|
||||
balance: # 资金预扣模式(Day54 R1,评审 docs/review/r1-balance.md)
|
||||
mode: redis # P1 压测(09-27):mysql→redis——行锁扣款(HDD 单行串行 ~140ms)把受理锁死在 30/s(实测 20:41-20:45);SubmitFast 快路径设计前提=Redis Lua(112μs);对账覆写 5min 兜底
|
||||
reconcile_interval: 5m # 对账周期(评审约束 ≤5min;MySQL 流水重算覆写 Redis)
|
||||
|
||||
settle: # S2 结算域:消耗日汇总滚动重刷(72h 回执窗内 UNKNOWN 会迁移,昨日行非终值须滚动收敛;月结建议 T+4 后生成)
|
||||
refresh_every: 1h # 重刷周期
|
||||
refresh_days: 4 # 每轮重刷窗口天数(today-3..today)
|
||||
backfill_days: 7 # 启动回补天数(明细热窗 5 天内余量)
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
# deploy/k8s/web.yaml S3·FE-6:web 镜像 k8s 部署清单(NodePort 30081,POC FE-6
|
||||
# 既定口径)。备档说明:本机 kind 未装(go-sms-dev 集群在老机),本清单随 S5 灰度
|
||||
# 泳道实际 apply;镜像由 make web-image 构建、kind load 摆渡。
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: go-sms-web
|
||||
namespace: go-sms-dev
|
||||
labels: { app: go-sms-web }
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels: { app: go-sms-web }
|
||||
template:
|
||||
metadata:
|
||||
labels: { app: go-sms-web }
|
||||
spec:
|
||||
containers:
|
||||
- name: web
|
||||
image: go-sms-web:dev
|
||||
ports:
|
||||
- containerPort: 80
|
||||
env:
|
||||
# 集群内网关 Service(与 ci.yml deploy-dev 的 deployment 同名口径)
|
||||
- name: API_UPSTREAM
|
||||
value: go-sms-gateway.go-sms-dev.svc.cluster.local:8080
|
||||
resources:
|
||||
requests: { cpu: 50m, memory: 64Mi }
|
||||
limits: { cpu: 500m, memory: 128Mi }
|
||||
readinessProbe:
|
||||
httpGet: { path: /, port: 80 }
|
||||
initialDelaySeconds: 3
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: go-sms-web
|
||||
namespace: go-sms-dev
|
||||
spec:
|
||||
type: NodePort
|
||||
selector: { app: go-sms-web }
|
||||
ports:
|
||||
- port: 80
|
||||
targetPort: 80
|
||||
nodePort: 30081
|
||||
@@ -0,0 +1,112 @@
|
||||
# 前端质量门根修 + Dashboard 信息密度重构 人工验收单(2026-11-13 深夜)
|
||||
|
||||
> 范围:web/ 前端(FE-Design P0~P3)。设计基线 demo-c-aurora 的 tokens/EP 换肤/布局壳/31 业务页零改动。
|
||||
|
||||
## 一、变更清单
|
||||
|
||||
| # | 任务 | 文件 | 说明 |
|
||||
|---|---|---|---|
|
||||
| P0 | vitest 91/137 红 → **138/138 绿** | `web/vitest.setup.ts`(+7 行) | 根因:vitest(mode=test)不加载 `.env.development` → `request.ts` 顶层 `baseURL=undefined`,spec 内 `vi.stubEnv` 晚于模块求值无效 → msw 全量 unhandled(实测请求裸奔为 `POST /auth/login`);修法=setup 顶部 stubEnv(setupFiles 先于被测模块求值,一处生效)。老机靠 gitignored 本地 env 掩盖,新机首跑暴露 |
|
||||
| P1 | Dashboard 信息密度重构 | `web/src/views/home/DashboardView.vue`(241→约 470 行重写) | 见下节 |
|
||||
| P2 | 图标体系统一 | 同上 | 指标卡 emoji(📮✓⚡⚠)+快捷卡符号(✉≡↻◐)→ EP icon;brand ✦ 保留 |
|
||||
| P3 | 细节打磨 | 同上 + `web/src/styles/shell.scss`(+6 行) | count-up(rAF 600ms/reduced-motion 跳过)、走势 hover 十字线+气泡(纯 SVG)、全局 `:focus-visible` |
|
||||
| 附 | **Teleport 冷启动竞态修复** | 同上 | `#page-head-extra`(MainLayout 渲染)晚于 Dashboard mount 时(Vue 3.4 无 `<Teleport defer>`)直接挂载 → 后续 patch 崩溃(`TypeError: setText null nodeValue`,fe_error_log 实证 2 条)→ 组件更新全中断(数据已返回但 UI 冻结);修法=v-if 门控延后一拍(**该 bug 旧版同样存在**,新机慢冷启动放大暴露) |
|
||||
|
||||
## 二、P1 信息密度(12 维 rubric 审计 54/110「精美的快照」档 → 预计 ~78)
|
||||
|
||||
| 变更 | 维度 | 内容 |
|
||||
|---|---|---|
|
||||
| 删假数据×3 | D3 | `rateNow=1180+random(160)` 假速率卡、8 条硬编码"示例"通道表、快捷卡死文案("5 条待复核/1,204 条可补") |
|
||||
| 假速率→真审核卡 | D3/D9 | 「实时速率」→「待人工审核」(接口 `pending_audit_count` 早已返回但 UI 从未消费;点击直跳 /audit) |
|
||||
| Hero 决策句 | D2/D8 | 四指标实时翻译为一句话判定(ok/warn/err/dim 四态+左光条+直达处置页),数据不可用时诚实降级 |
|
||||
| 数据脉象 | D10 | live-chip → 「实时·HH:MM:SS / 连接异常·数据截至 / 不可用·点击重试」三态;catch 不再静默吞错 |
|
||||
| 通道表真数据 | D3/D4 | `channelApi.list` 配置域快照(code/协议/运营商/限速/连接/优先级/状态徽章),表头诚实标注"运行统计待 S5 压测后接入";失败显式重试态 |
|
||||
| 快捷卡联动 | D9 | desc 接实时指标(12 条待审/3 个预警),热态 amber |
|
||||
| 7/30 日切换 | D1/D11 | trend(days) mock/后端双端已支持;30d x 轴抽稀 |
|
||||
|
||||
## 三、质量门(前端四件 + 实机)
|
||||
|
||||
| 项 | 结果 | 证据 |
|
||||
|---|---|---|
|
||||
| eslint(--max-warnings 0) | ✅ RC=0 | /tmp/fe-lint3.log |
|
||||
| vue-tsc --noEmit | ✅ RC=0 | /tmp/fe-tsc3.log |
|
||||
| vitest | ✅ **138/138**(修复前 91 红) | /tmp/fe-test4.log |
|
||||
| vite build | ✅ RC=0 | /tmp/fe-build3.log |
|
||||
| verify-layout(18 断言) | ✅ 全过(Playwright 实渲染,mock 实例 :5174) | 唯一侧栏/双菜单/EP 壳零残留/收起 224→64/汉堡抽屉等 |
|
||||
| Dashboard 运行时断言(15 项,自建脚本) | ✅ 全过 | hero warn 态+12 待审+3 预警文案、stat-audit=12、stat-speed 已删、脉象"实时·时刻"、快捷卡联动、通道表 3 行真数据+停用徽章、30 日切换、hover 气泡含提交/成功数 |
|
||||
| 截图矩阵 | ✅ 5 页×双主题重刷 | web/shots/(home 修复后含真实数据态) |
|
||||
| data-testid 兼容 | ✅ | 全保留(stat-speed 删除,tests/scripts grep 确认无引用) |
|
||||
|
||||
## 四、人工走查明细(协议 v3 · 本人执行,逐项勾选)
|
||||
|
||||
> 环境:`cd web && VITE_API_MODE=mock npx vite`(:5173,admin/admin123);real 走查另起默认 `.env.development`(:5173→proxy→:8080)。标 ★ 为自动化已覆盖、人工抽验即可;标 ☆ 为必须人眼/人手验证。
|
||||
|
||||
### A · Hero 决策句(D2/D8,新增件)
|
||||
| # | 操作 | 预期 | 级 |
|
||||
|---|---|---|---|
|
||||
| A1 | mock 登录 → /home | amber 左光条 + Warning 图标:「今日需要关注:3 个账号余额低于预警线 · 12 条短信待人工审核」,副行「其余指标正常 · 回执成功率 98.3%」 | ★ |
|
||||
| A2 | 点击 hero | 跳转 /audit(待审优先);Tab 聚焦时 hero 有 teal focus 描边,Enter 可跳 | ☆ |
|
||||
| A3 | real 模式 /home | 按真实指标呈对应态:ok=teal「平台运行平稳…」/ err=红「回执成功率…低于 95%」 | ☆ |
|
||||
|
||||
### B · 四指标卡(P2 图标/P3 count-up)
|
||||
| # | 操作 | 预期 | 级 |
|
||||
|---|---|---|---|
|
||||
| B1 | 刷新 /home 观察数字 | 今日提交从 0 滚动至 1,286,430(600ms,千分位);成功率滚至 98.3% | ☆ |
|
||||
| B2 | 检查图标 | 四卡均为 EP icon(Promotion/CircleCheck/Bell/Wallet),**全页无 emoji** | ★ |
|
||||
| B3 | 「待人工审核」卡 | mock=12 条、数值与铃铛 amber;点击整卡 → /audit | ★ |
|
||||
| B4 | 「余额预警」卡 | mock=3 条、数值红;点击 → /account/list | ★ |
|
||||
| B5 | 确认假卡已删 | 页面不存在「实时速率」卡(原 1180+random 假数据) | ★ |
|
||||
| B6 | 系统「减少动态」开启后刷新 | 数字直接显示终值,无滚动 | ☆ |
|
||||
|
||||
### C · 数据脉象 chip(D10,page-head 右侧)
|
||||
| # | 操作 | 预期 | 级 |
|
||||
|---|---|---|---|
|
||||
| C1 | 静置观察 | teal 脉动圆点 +「实时 · HH:MM:SS」;悬停出 title;**停留 >60s 时刻自动更新** | ☆ |
|
||||
| C2 | 点击 chip | 立即刷新,时刻跳变 | ☆ |
|
||||
| C3 | DevTools → Network Offline → 点 chip | 变 amber「连接异常 · 数据截至 HH:MM:SS」,旧数据保留不闪空;恢复网络后点击回 teal | ☆ |
|
||||
| C4 | 保持 Offline 刷新整页 | chip 红「指标不可用 · 点击重试」+ hero 同步 err 态;恢复后点击重试回绿 | ☆ |
|
||||
|
||||
### D · 发送走势(P3 hover/7-30 日切换)
|
||||
| # | 操作 | 预期 | 级 |
|
||||
|---|---|---|---|
|
||||
| D1 | 点「30 日」 | 图重载不崩溃;real 模式下 30 点、x 轴标签抽稀(≤8 个 + 「今日」恒显);mock 仅 7 点属正常(mock 数据源限制) | ★ |
|
||||
| D2 | 鼠标在图上横移 | teal 虚线十字 + 放大圆点 + 玻璃气泡(日期 / 提交 x,xxx · 成功 x,xxx);气泡近右缘时自动左翻不裁切 | ★ |
|
||||
| D3 | 移出图表 | 十字线与气泡消失 | ☆ |
|
||||
|
||||
### E · 快捷操作(D9 联动)
|
||||
| # | 操作 | 预期 | 级 |
|
||||
|---|---|---|---|
|
||||
| E1 | 看「待人工审核」desc | mock:「12 条待审,点击进入审核中心」+ 图标底 amber;随 C3 断网联动显示 | ★ |
|
||||
| E2 | 看「余额预警」desc | mock:「3 个账号低于预警线」 | ★ |
|
||||
| E3 | 看「失败补推」desc | 诚实文案「按批次 / 手机号查询重推」(**无编造数字**) | ☆ |
|
||||
| E4 | 四项逐一点击/Enter | 分别跳 /sms/batch、/audit、/record/repush、/account/list | ☆ |
|
||||
|
||||
### F · 通道配置快照(D3/D4 真数据)
|
||||
| # | 操作 | 预期 | 级 |
|
||||
|---|---|---|---|
|
||||
| F1 | mock 表内容 | 3 行:CM-001 移动全国(CMPP·violet)/HX-201 联信HTTP(HTTP·warn)/DX-301 电信SGIP(SGIP)且状态=**停用红徽章**;运营商列「移动 / 联通 / 电信」映射正确 | ★ |
|
||||
| F2 | 表头 small | 「N 条 · 配置域数据 · 运行统计待 S5 压测后接入」诚实标注;右上 ch-refresh 可刷新 | ★ |
|
||||
| F3 | real 模式 | 与「通道配置」页列表一致(配置域字段);管理员视角可用 | ☆ |
|
||||
| F4 | 停网关后点 ch-refresh | 显式「通道列表加载失败 · 重试」,重试链接可用 | ☆ |
|
||||
| F5 | 窗口缩至 <1100px | 表格横向滚动,不挤压布局 | ☆ |
|
||||
|
||||
### G · 布局与主题回归(verify-layout 已覆盖,抽验)
|
||||
| # | 操作 | 预期 | 级 |
|
||||
|---|---|---|---|
|
||||
| G1 | 侧栏收起/展开 | 224↔64;≤900px 汉堡抽屉正常 | ★ |
|
||||
| G2 | 暗/亮主题切换 | hero 光条、amber warn、气泡、focus 描边在**亮色**下对比度均可读 | ☆ |
|
||||
| G3 | brand 区 | 「✦ go-sms / AURORA CONSOLE」不变;菜单 icon 不变 | ★ |
|
||||
| G4 | Tab 全键盘走查 | hero→指标卡→radio→刷新→快捷卡…,focus-visible teal 描边全程可见 | ☆ |
|
||||
|
||||
### H · 双模式与回归面
|
||||
| # | 操作 | 预期 | 级 |
|
||||
|---|---|---|---|
|
||||
| H1 | mock 全流程 | A~G 全部在 mock 模式通过 | — |
|
||||
| H2 | real 全流程 | 真网关登录后指标/通道/趋势为真实数;customer 角色登录 /home 无 JS 报错(后端按角色返回) | ☆ |
|
||||
| H3 | 浏览器 Console | 全程无 error(fe_error_log 无新增 VueHandler 记录) | ☆ |
|
||||
| H4 | 旁证回归 | account / channel / sms-batch / sms-downlink-detail 四页与旧版无差异(本次未改动,截图已重刷) | ☆ |
|
||||
|
||||
## 五、环境遗留(非本次范围,已知悉)
|
||||
|
||||
- pnpm-lock.yaml 曾被 pnpm 自动 install 重排 → 已 `git checkout` 回滚(最小变更纪律);**新机跑 pnpm 命令会自动 install 且可能改 lockfile,建议后续统一 `pnpm install --frozen-lockfile`**。
|
||||
- Playwright 浏览器新机首装:官方 CDN 停滞 → `PLAYWRIGHT_DOWNLOAD_HOST=https://cdn.npmmirror.com/binaries/playwright` 镜像 + apt 系统库(libnspr4/libnss3 等)。
|
||||
@@ -0,0 +1,40 @@
|
||||
# Issue#2 验收单:管理前端多标签导航 + 页签缓存(2026-11-16)
|
||||
|
||||
> 工单:Gitea `Tory/go-sms-V2` Issue #2「优化:管理前端V1」(2026-11-16 11:07)。
|
||||
> 需求原文:*增加菜单Tab页(可独立关闭,一次全部关闭),现状是每个菜单独立打开,且切换后没有缓存,对运营非常不友好。*
|
||||
|
||||
## 一、交付范围
|
||||
|
||||
| 组件 | 文件 | 说明 |
|
||||
|---|---|---|
|
||||
| 标签状态 | `web/src/stores/tabs.ts` | pinia:visited[](tab 身份=fullPath);addTab 去重/错误页与 redirect 过滤;close(右邻优先回退)/closeOthers/closeAll;cachedNames(include 锚,同名去重);home 固定不可关 |
|
||||
| 标签条 | `web/src/components/TagsBar.vue` | 纯原生元素(G2 断言语义:EP 导航壳不进布局壳);✕ 独立关闭+中键关闭(auxclick button===1;@click.middle 对中键永不触发)+关闭其他+全部关闭;aurora token 风格(primary-soft 激活/圆角 10px) |
|
||||
| 布局挂载 | `web/src/layouts/MainLayout.vue` | `<router-view v-slot>`+`<keep-alive :include="tabs.cachedNames">`+`<component :key="viewRoute.fullPath">`;watch fullPath→addTab(immediate);登出 `$reset()` 清标签防跨角色残留 |
|
||||
| 缓存命名 | 29 个业务视图 SFC | 各加 `defineOptions({ name: <路由名> })`(28 新增+stats/profit 原名 StatsProfitView 对齐 'stats-profit');**ComingSoon(11 路由共用)刻意豁免**:无本地状态可保,不缓存零副作用 |
|
||||
| 守门测试 | `web/tests/view-cache-name.spec.ts` | 遍历路由断言「组件名=路由名」——新增页面漏写 defineOptions 会**缓存静默失效**(页面照常、仅切 tab 丢状态),此类回归必须在测试红而非线上发现 |
|
||||
| 配套测试 | `web/tests/tabs-store.spec.ts`(7)+`web/tests/tags-bar.spec.ts`(6) | 状态机全链(去重/关闭导航语义/固定标签/缓存名去重)+组件渲染与交互 |
|
||||
| 断言扩展 | `web/scripts/verify-layout.mjs` | +5 项:tags-bar 唯一/登录即 home chip/固定无 ✕/点菜单 chip+1/独立关闭回退;凭据 env 化(SHOT_USER/SHOT_PASS,缺省 s2demo——内置 admin 原密码首启打印一次已不可知) |
|
||||
|
||||
## 二、验证证据(2026-11-16)
|
||||
|
||||
| 门禁 | 结果 |
|
||||
|---|---|
|
||||
| eslint(max-warnings 0) | ✅ 0 |
|
||||
| vue-tsc --noEmit | ✅ 0 |
|
||||
| vitest | ✅ **165/165**(148→+17:tabs-store 7+view-cache-name 4+tags-bar 6) |
|
||||
| vite build | ✅ 9.99s(chunk 剖面不变,gzip 逐块<500KB 基线保持) |
|
||||
| verify-layout(实渲染) | ✅ **23/23**(原 18+新 5) |
|
||||
| Chrome 157 实机走查(:5173,s2demo,单 page 铁律) | ✅ **17/17**,两项**核心 keep-alive 状态保留实证**:①结算页内 Tab 切第 2 个→切走通道配置→切回**仍选中**;②筛选 账号ID=2→切走→切回**保留**;独立关后台不跳转/关当前回退相邻/全部关闭仅剩 home 回 /home;截图 /tmp/issue2-{settle-state,state-preserved,close-all}.png |
|
||||
|
||||
## 三、设计口径(评审可追溯)
|
||||
|
||||
1. **tab 身份=route.fullPath**:参数页(account/:id、channel/edit/:id?)每实例一 tab(标题带 `#id` 后缀);query 变体亦独立成 tab,与 keep-alive key 同口径。
|
||||
2. **缓存身份=组件名=路由名**:keep-alive include 按组件名匹配,而 script setup 文件名推断名(15 个 index.vue 全叫 "index";PascalCase≠kebab 路由名)永不命中——守门测试固化此约定。
|
||||
3. **同名多实例**(account/1 与 account/2):关闭其一缓存条目随名保留,最后一个同名 tab 关闭才真正释放(keep-alive 按名驱逐,有限驻留可接受)。
|
||||
4. **刷新不持久化**:工单未要求;刷新后仅重开当前页 tab(watch immediate 兜底),增强项留待运营反馈。
|
||||
|
||||
## 四、已知边界与挂账
|
||||
|
||||
- ComingSoon 占位页每次进入重挂载(无状态可丢,刻意不缓存)。
|
||||
- 标签不随刷新恢复(挂账增强项,非工单范围)。
|
||||
- 工单评论回贴:Gitea API 读已验免 token(http://192.168.5.27:24400),写需 token——待用户提供或自行回贴。
|
||||
@@ -0,0 +1,49 @@
|
||||
# Issue#4 验收单:版本号 + 发布日志(2026-11-16)
|
||||
|
||||
> 工单:Gitea Tory/go-sms-V2#4「增加:版本号及发布日志」。**纯前端交付(后端零改动)**;门禁四件 + verify-layout 全绿。
|
||||
|
||||
## 一、需求与落点
|
||||
|
||||
| 需求原文 | 落点 | 状态 |
|
||||
|---|---|---|
|
||||
| ①菜单底部,折叠展开旁边增加最新版本号,有更新时增加红点闪烁提示 | `MainLayout.vue` sidebar-foot 折叠按钮旁版本 chip(`data-testid=sidebar-version`,点击进发布日志页);红点=未读时 `gosms-ver-blink` CSS 脉冲闪烁 | ✅ |
|
||||
| ②系统管理增加二级菜单「发布日志」,功能迭代明细,运营大白话,Markdown 格式 | 路由 `/system/release`(**全角色**,同 faq 口径,用户拍板);`ReleaseLog.vue` 时间线卡片;`utils/markdown.ts` 迷你渲染器 | ✅ |
|
||||
|
||||
## 二、交付清单
|
||||
|
||||
| 文件 | 说明 |
|
||||
|---|---|
|
||||
| `web/src/data/releases.ts` | **单一事实源**:版本列表(最新在前,6 个版本 v1.0.0~v1.5.0,运营大白话 Markdown);`APP_VERSION = RELEASE_NOTES[0].version`;文件头含发版 SOP |
|
||||
| `web/src/utils/markdown.ts` | 迷你 Markdown→HTML(**零新依赖**):标题/无序有序列表(`1.` 与 `1、` 均支持)/粗体/行内代码(哨兵保护)/围栏代码块/链接(仅 http(s));**先全量 HTML 转义再解析** |
|
||||
| `web/src/stores/release.ts` | pinia store:`hasUpdate`(localStorage `gosms_seen_ver` ≠ APP_VERSION)/`markSeen()` |
|
||||
| `web/src/views/system/ReleaseLog.vue` | 发布日志页(`defineOptions name='system-release'`):hero 当前版本 + 版本时间线卡片(版本徽章/日期/标签/当前版本角标)+ Markdown 渲染 |
|
||||
| `web/src/layouts/MainLayout.vue` + `styles/shell.scss` | sidebar-foot 版本 chip(红点闪烁动画/折叠态 64px 短版本纵向/暗亮主题 token) |
|
||||
| `web/src/router/routes.ts` | `system/release` 路由(系统管理组,全角色,icon=Memo) |
|
||||
| `web/package.json` | version 0.1.0 → **1.5.0**(与 APP_VERSION 同步,测试断言防漂移) |
|
||||
| `web/tests/releases.spec.ts` | **+14 用例**(数据一致性 4/渲染器 7/store 3) |
|
||||
| `web/tests/guard.spec.ts` `role-menu-matrix.spec.ts` `web/scripts/verify-layout.mjs` | 菜单计数 33→34 显式更;矩阵登记 `system-release: ALL`;布局断言 23→**31** |
|
||||
|
||||
## 三、设计口径
|
||||
|
||||
1. **单一事实源**:版本号只在 `releases.ts` 头条维护;`APP_VERSION` 自动取首条,`package.json` 同步(测试两侧一致断言)。发版 SOP:**releases.ts 头部插一条(minor=功能/patch=修复)+ package.json version 同步**,部署后全员红点自动亮起。
|
||||
2. **红点语义(本地未读)**:localStorage `gosms_seen_ver` 存"上次已看过的版本";≠ 当前版本即亮;首访无记录=亮(引导看一次);点击 chip / 进入发布日志页即 `markSeen()` 熄灭;发新版后全员再亮。
|
||||
3. **Markdown 安全**:内容源=仓库静态数据(非用户输入),渲染器仍按不可信文本处理——先全量 HTML 转义再解析,链接仅 http(s)(javascript: 伪协议不渲染为 `<a>`),v-html 零注入面。
|
||||
4. **折叠态适配**:64px 窄栏下 chip 缩为无边框短版本(v1.5.0 → v1.5),与折叠按钮纵向排布;hover title 提示完整版本与更新引导。
|
||||
|
||||
## 四、门禁
|
||||
|
||||
- eslint **0 警** / vue-tsc **0** / vitest **179 全绿**(165+14)/ build ✓(chunk 剖面不变)。
|
||||
- verify-layout **31/31**(Chrome 实机,:5173,s2demo 登录):新增 8 项=chip 可见、文本 `vN.N`、首访红点存在、点击进发布日志、已读红点即灭、版本卡片 ≥3、Markdown 列表渲染、回 home。
|
||||
- 浏览器实机走查截图:`/tmp/issue4-*.png`(红点态/已读态/折叠态/发布日志页)。
|
||||
|
||||
## 五、口径与边界(诚实标注)
|
||||
|
||||
1. **红点为浏览器本地"未读"语义**:换浏览器/清缓存会重新提示一次;无服务端已读同步(单机管理后台语义够用,如需全员强提醒再挂后端)。
|
||||
2. **移动端(≤900px)**:sidebar-foot 沿用既有 `display:none` 约定,版本号仅桌面侧栏可见(工单语境为桌面管理后台)。
|
||||
3. **发布内容为仓库静态数据**:运营改文案=编辑 `releases.ts`(TypeScript 文件,格式有测试守门);不提供后台在线编辑(无需求,且内容发布本就应走代码评审)。
|
||||
4. CHANGELOG.md(技术向,仓库根)与发布日志(运营向,前端)是**两份受众不同的内容**,发版时各自更新。
|
||||
|
||||
## 六、遗留与下一步
|
||||
|
||||
- Gitea 工单 #4 完成评论待用户贴(写评论需 token)。
|
||||
- 版本线 v1.0.0~v1.4.0 的日期/内容为按 CHANGELOG 重建的运营向回顾(口径:一期 11-13 上线为 v1.0.0),如与用户预期划分不同,改 `releases.ts` 即可(纯数据)。
|
||||
@@ -0,0 +1,131 @@
|
||||
# Issue#5 压测复盘报告(2026-11-16 · 目标 100 万条)
|
||||
|
||||
> 工单:Gitea Tory/go-sms-V2#5「压测:100万条」(2026-11-16 16:23 提出;11-17 19:05 催报)。
|
||||
> 链路口径:**下家 s2demo**(account 90001,CMPP 2.0 客户端 cmppbench)→ **平台**(gateway :8080/CMPP :7890 + consumer)→ **上家 331004**(通道 9102「Hw测试专用」)。
|
||||
> 总判定:**下发成功 ≈112.7 万条,超额达成 100 万目标(+12.7%);计费全程三平、分毫不差;零失败终态、零慢查询;账单/利润链路就绪,压测数据待出账(列入下一轮 SOP)**。
|
||||
|
||||
## 一、结论速览
|
||||
|
||||
| 维度 | 结果 | 证据 |
|
||||
|---|---|---|
|
||||
| 100 万条目标 | ✅ 下发成功 ≈1,127,383 条(+12.7%);累计提交 ≈213.8 万条 | billing_consume_log + bench CSV + channel_log |
|
||||
| 计费 | ✅ 三平对账成立:充值 10.0001 亿 − 预扣 2,183,182 + 拦截退款 999,741 = 余额 998,826,559(基数 100 万,分毫不差);拦截即时退款 99.97 万笔零差错 | account_balance_log |
|
||||
| 账单 | ⚠️ 出账/确认/结账/阶梯链路 11-14 已验收(S2 泳道),库内仅 seed 演示账单;**本轮 206 万条尚未出账** → 列入正式压测 SOP | settle_customer 仅 account 2(seed) |
|
||||
| 利润 | ⚠️ 利润/返点/三方对账引擎已验收(settle-check EXIT=0);本轮未出报表;按时间线价推算(售价 0.10/成本 0.035)毛利 ≈7.33 万元/112.7 万条(演示口径,未出正式报表) | /stats/profit 引擎 + 价格抽屉走查 |
|
||||
| 稳定性 | ✅ 受理 slow>200ms=0;Kafka lag 峰 476/均 88;慢查询 0;双进程零重启;磁盘 4%~9% | watch_1m.sh 2,719 样本 |
|
||||
| 遗留问题 | 6 项(R1 余额对账差额 P0 ×33 次/签名拦截口径/deliver_fail 复核/长短信挂起/host 不热切换/CSV 留档) | 见 §六 |
|
||||
|
||||
## 二、压测环境与拓扑
|
||||
|
||||
```text
|
||||
cmppbench(自研压测客户端,CMPH 2.0,links/concurrency/rate/expect-receipt 可参)
|
||||
│ CMPP 2.0(纯文本 fmt=0x0f)
|
||||
▼
|
||||
平台 gateway(CMPP :7890 / HTTP :8080)──受理:预扣余额+拦截链+落 task/detail
|
||||
│ Kafka(16 分区)削峰
|
||||
▼
|
||||
consumer ──下发──► 上家通道 9102「Hw测试专用」(331004)
|
||||
│ ├─ 白日场:vcmppgw 模拟网关 127.0.0.1:17890(50ms 回 DELIVRD,17:26 退出)
|
||||
│ └─ 夜间场:真上家 192.168.5.16:7890(单连接)
|
||||
▼
|
||||
回执配对→明细终态→DELIVRD 推回客户端;拦截(status=5+ISC 码)即时退款
|
||||
监控:watch_1m.sh 30s 采样(task/detail/fee/report/lag/MySQL 慢查询/磁盘)+ tcpdump 环形 300MB 抓包
|
||||
```
|
||||
|
||||
- 压测前准备:通道 9102 host 临时切 vcmppgw(`logs/issue5-restore.sql` 预写恢复语句);s2demo 充值 10 亿条(**压测余额预算=TPS×时长×均条×2 教训落地**);价格配置面真浏览器走查(s2demo 售价 0.1000 / 通道 9102 成本 0.0350,`scripts/issue5-price-walkthrough.mjs` PASS 3/3)。
|
||||
|
||||
## 三、三场压测明细
|
||||
|
||||
### 3.1 白日场(17:00~17:26,上家=vcmppgw 模拟网关,数据已清理)
|
||||
|
||||
| 批次 | 时间 | 提交 | 成功 | 失败 | 延迟 p50/p90/p99/max(ms) | TPS |
|
||||
|---|---|---|---|---|---|---|
|
||||
| 预检×2 | 17:00:38 / 17:05 | 2,000 | 2,000 | 0 | 2.7~4.5 | — |
|
||||
| S1 单条 1 万 | 17:09:06~17:09:55(49.0s) | 10,000 | 10,000 | 0 | 6.0 / 8.9 / 12.1 / 22.3 | 均 203.9(限速 200) |
|
||||
| S2 单条(36 万目标) | 17:11:15~17:16:40(5m24.8s,人工中止) | 65,000 | 65,000 | 0 | 8.8 / 12.7 / 17.4 / 37.5 | 均 200.1 / 峰 258 |
|
||||
|
||||
- vcmppgw 侧:受理 75,191 / 回执 75,191 / 拒绝 0(与客户端 CSV 77,000 差 1,809 = host 切换窗口期分流,见 §六-5);17:23~17:26 drain 复跑 0 流量(排空验证 ✅)。
|
||||
- S2 中止说明:该场验证目标(200/s 限速下受理/下发/回执/计费全链零差错)已达成,转入夜间真上家场;36 万未跑完不判失败。
|
||||
- 客户端逐条 CSV 留档:`logs/bench-precheck*.csv / bench-s1-1w.csv / bench-s2-36w.csv`(77,000 行)。
|
||||
|
||||
### 3.2 夜间场·长短信段+小批量(18:32~21:00,上家=真上家 5.16,数据保留)
|
||||
|
||||
| 批次 | 时间 | 提交(任务) | 计费条(均片) | 成功 | 挂起 | 拦截 |
|
||||
|---|---|---|---|---|---|---|
|
||||
| 长短信段 | 18:32~19:00 | 51,264 | 153,786(×3) | 50,083 | 1,177 | 0 |
|
||||
| 小批量 | 20:00~ | 9,862 | 29,586(×3) | 9,862 | 0 | 2 |
|
||||
|
||||
### 3.3 夜间场·主战场(23:00~11-17 01:52,2h52m,上家=真上家 5.16 单连接)
|
||||
|
||||
| 指标 | 数值 |
|
||||
|---|---|
|
||||
| 提交 | 1,999,469 |
|
||||
| 下发成功(DELIVRD 终态) | 999,736 |
|
||||
| 签名拦截(ISC:004,即时退款) | 999,894(≈50%) |
|
||||
| 受理 QPS(30s 窗) | 均值 193.7(=拦截 96.9 + 下发 96.8)/ 峰值 287.9 / **slow>200ms=0** |
|
||||
| 下发吞吐(单连接) | ≈97 条/s |
|
||||
| channel_log | connect=1,submit 1,121,986,deliver 1,119,334,deliver_fail 2,652(待复核) |
|
||||
|
||||
### 3.4 总账(11-16 18:00 起,权威=DB)
|
||||
|
||||
- 提交 **2,060,928**(billing_consume_log 175,078+1,885,850;与余额流水 submit 笔数分毫不差)
|
||||
- 计费条 **2,183,182**(=余额扣款条数;11-17 场单条=提交数,11-16 场含长短信 3 片)
|
||||
- 成功 **1,059,540**(billing)≈ detail 终态成功 1,059,534 ✅
|
||||
- 拦截退款 **999,741 笔 / 999,741 条**(与 send_block_log 999,894 差 153=零星+清理项)
|
||||
- **三平对账**:1,000,000(基数)+1,000,010,000(充值 2 笔)− 2,183,182 + 999,741 = **998,826,559 = 当前余额** ✅
|
||||
|
||||
## 四、计费 / 账单 / 利润核验(工单问题①)
|
||||
|
||||
| 链路 | 状态 | 本轮压测实证 |
|
||||
|---|---|---|
|
||||
| **计费**(实时预扣+拦截退款) | ✅ **完善,支持压测** | 三平对账分毫不差(§3.4);100 万级拦截即时退款 99.97 万笔零差错;日汇总引擎(billing_consume_log)与明细/流水三方一致 |
|
||||
| **账单**(出账/确认/结账/阶梯/XLSX) | ⚠️ 功能完善(11-14 S2 泳道真机验收:生成→确认→结账→SettleRecord→真 XLSX),**本轮压测数据未执行出账**(出账为 T+1 运营动作) | 库内仅 seed 演示账单(account 2:16,050 条/1,605.00 元);**已列入下一轮压测 SOP 必做项** |
|
||||
| **利润**(时间线价收入+成本分摊+返点) | ⚠️ 引擎就绪(11-14 D5 验收 settle-check EXIT=0;价格面走查 s2demo 售 0.10/9102 成本 0.035),**本轮未出报表** | 推算演示:112.7 万条 ×(0.10−0.035)≈ **7.33 万元毛利**(时间线价口径,正式数字以下一轮出账报表为准) |
|
||||
|
||||
## 五、性能与稳定性数据(工单问题②:上下家提交速度)
|
||||
|
||||
| 指标 | 白日场(限速 200/s) | 夜间主战场(不限速) |
|
||||
|---|---|---|
|
||||
| 下家受理 | 200/s 恒定,均值 203.9 | 均 193.7/s、30s 窗峰 287.9/s,**slow>200ms=0** |
|
||||
| 受理延迟(CMPP Submit→Resp) | p99=17.4ms / max=37.5ms | 服务端 avg_ms≈0(快路径) |
|
||||
| 上家下发 | 200/s(模拟网关) | ≈97 条/s(真上家单连接;提吞吐需按通道 speed 扩连接) |
|
||||
| 回执 | vcmppgw 50ms 全量回 | 1,119,334 / 2,652 fail(复核中) |
|
||||
| Kafka lag | 峰 326 | 峰 476 / 均 88 |
|
||||
| 在途明细(status=1)峰 | 4,168 | 2,478 |
|
||||
| MySQL 活跃线程峰 / 慢查询 | 4 / 0 | 13 / **0** |
|
||||
| 磁盘 / 进程 | 3% / 零重启 | 4%~9% / 零重启、零 OOM |
|
||||
|
||||
## 六、发现的问题与整改建议
|
||||
|
||||
1. **R1 余额对账差额 P0 告警 ×33 次**(11-16 23:57 起 firing):Redis 预扣与 MySQL 流水重算差典型 ±120~791 条(另 1 次 10 亿=充值写入与重算的瞬时窗口,outlier);reconciler 5min 自动以流水覆写 Redis(自愈,流水为唯一事实源,无资损)。**建议**:根修预扣 Lua 与流水落库的时序窗口(或对账窗口排除在途)+告警按差额绝对值分级降噪。
|
||||
2. **主战场 50% 流量被签名拦截(ISC:004)**:平台行为正确(无报备签名内容应拦,且拦截+即时退款零差错);暴露的是**压测脚本口径**——下一轮内容须带账号报备签名,或专设「免签名压测账号」并留配置留痕。
|
||||
3. **channel_log deliver_fail 2,652 条终态去向待复核**(billing fail=0 / detail 失败终态=0):疑为通道侧失败回执重试链路计数,需一次明细级核对。
|
||||
4. **长短信段 1,326 条挂起未终态**(status=1 残留):符合 72h 挂起制语义,**11-19 reaper 到期处理**,到期核验终态与零费口径。
|
||||
5. **channel host 配置不热切换**:17:07:27 恢复 host=5.16 后,consumer 存量连接仍指向 vcmppgw(S1/S2 7.5 万条走旧连接;vcmppgw 受理 75,191 vs CSV 77,000 差 1,809=切换窗口分流)。**运维 runbook 增补:改通道 host 必须断连/重启连接池后核对实际流向**。
|
||||
6. **主战场客户端侧 CSV 未留档**(仅服务端 DB/日志齐全):SOP 增补「压测客户端 -csv 参数强制落盘」;白日场数据已按恢复 SOP 清理。
|
||||
|
||||
## 七、下一轮正式压测规划(建议 11-18 整改 / 11-19 正式场)
|
||||
|
||||
**P0 前置整改(11-18)**:①R1 差额根修或对账窗口豁免+告警分级(§六-1);②deliver_fail 复核(§六-3);③压测脚本签名口径对齐(§六-2);④host 热切换 runbook(§六-5)。
|
||||
|
||||
**SOP 增补(压测收尾四件套)**:出账→确认→结账全流程演练 + 利润报表产出与人工核对 + `settle-check` EXIT=0 + XLSX 导出留档;客户端 CSV 强制留档。
|
||||
|
||||
**阶梯方案(每档 30min 稳态,验收判据同行)**:
|
||||
|
||||
| 档 | 受理 TPS | 内容构成 | 上家 | 通过判据 |
|
||||
|---|---|---|---|---|
|
||||
| L1 | 200 | 单条 100% | 真上家单连接 | 三平+P99<50ms+零慢查询 |
|
||||
| L2 | 500 | 单条 70%+长短信 30% | 按通道 speed 扩连接 | 同上+lag<1k |
|
||||
| L3 | 1000 | 同 L2 | 双通道并发 | 同上+出账四件套全过 |
|
||||
|
||||
**观察项**:长短信挂起 1,326 条 11-19 到期核验;R1 告警整改后复测窗口。
|
||||
|
||||
## 附录:证据文件
|
||||
|
||||
- 客户端 CSV:`logs/bench-precheck*.csv`(2×1,000)/`bench-s1-1w.csv`(10,000)/`bench-s2-36w.csv`(65,000)
|
||||
- 压测日志:`logs/bench-s1.log`/`bench-s2.log`(含摘要 JSON);模拟网关 `logs/vcmppgw.log`/`vcmppgw-drain.log`
|
||||
- 监控:`logs/1m-baseline.txt` + `logs/1m-watch.log`(2,719 样本);网关日志 `logs/app-2026-11-16T17-35-43.916.log.gz`(主战场窗口,cmpp_submit_stats QPS 序列+R1 告警)
|
||||
- 抓包:`logs/capture/cmpp-20261116-174359.pcap0~5`(环形 300MB)
|
||||
- 工具:`cmd/cmppbench`(下家客户端)/`cmd/vcmppgw`(模拟上家)/`scripts/issue5-price-walkthrough.mjs`(价格面走查)/`logs/issue5-restore.sql`(通道恢复)
|
||||
- DB 对账:billing_consume_log / account_balance_log / send_block_log / channel_log / settle_*(2026-11-16~17 窗口,本文 §3~§6 数字均出自只读查询)
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
# Issue#6·B3 验收单:发布日志页移植 web-next(2026-11-18)
|
||||
|
||||
> Issue#4 发布日志功能在 **web-next(:5174)** 的落地(B3 逐页指令制,用户点名页)。纯前端交付(后端零改动);基础设施(releases.ts/release store/markdown.ts/VersionBadge)B1 已随逻辑层复制就位,本次补视图 + 路由替换。
|
||||
|
||||
## 一、需求与落点
|
||||
|
||||
| 需求点 | 落点 | 状态 |
|
||||
|---|---|---|
|
||||
| 发布日志页在 web-next 可用 | `web-next/src/views/system/ReleaseLog.vue`(蓝本=旧 web 同名页 Tailwind 契约直译)+ routes.ts component 替换 | ✅ |
|
||||
| 全角色可见 | 路由 meta roles=5 角色不变;侧栏「系统管理」组菜单数据驱动自动出现 | ✅ |
|
||||
| 红点联动 | 进页 `onMounted → release.markSeen()`;VersionBadge 点击跳 /system/release(B1 已有,本次联通) | ✅ |
|
||||
|
||||
## 二、交付清单
|
||||
|
||||
| 文件 | 说明 |
|
||||
|---|---|
|
||||
| `web-next/src/views/system/ReleaseLog.vue` | **新增**。hero 概览卡(v 版本 mono + 日期 + 共 N 版 + 红点提示)+ 版本时间线卡片(mono 版本号 primary 色/tag 徽章/「当前版本」角标);markdown 预渲染 `renderMarkdown` 静态算一次;`defineOptions({ name: 'system-release' })` 守 keep-alive 约定;**无 h2.page-title**(B3 视觉规范,顶栏 AppHeader h1#pageTitle 已显) |
|
||||
| `web-next/src/router/routes.ts` | L82 `system/release` component:ComingSoon → ReleaseLog.vue(meta 全角色/i-clock/系统管理组/subtitle 不变) |
|
||||
|
||||
tag 徽章映射(契约 badge 族):新功能=`badge-primary`(teal)/优化=`badge-warning`(amber)/修复=`badge-success`(emerald)。
|
||||
|
||||
Markdown 深层样式(v-html `:deep()`):h3/h4/strong 色**继承正文**(零硬编码);code/marker/a 用 primary 色值 + `html.dark` 组合选择器覆盖(scoped 编译后 `.rn-md[data-v]` 锚点,与 Tailwind darkMode:'class' 同源)。
|
||||
|
||||
## 三、门禁(Chrome 冒烟 13/13 PASS,s2demo 登录,:5174)
|
||||
|
||||
| # | 断言 | 结果 |
|
||||
|---|---|---|
|
||||
| 1 | 登录进入 /home | PASS |
|
||||
| 2 | 清 `gosms_seen_ver` 刷新 → 首访红点亮 | PASS |
|
||||
| 3 | 侧栏菜单「发布日志」存在 | PASS |
|
||||
| 4 | 点击(滚入视口)→ URL=/system/release | PASS |
|
||||
| 5 | 顶栏页题=发布日志 | PASS |
|
||||
| 6 | 内容页无 h2(B3 规范) | PASS |
|
||||
| 7 | hero:v1.5.0 + 2026-11-16 + 共 6 个版本 | PASS |
|
||||
| 8 | 版本卡片 6 张(v1.0.0~v1.5.0) | PASS |
|
||||
| 9 | Markdown 渲染 h3=6 / li=18 | PASS |
|
||||
| 10 | v1.5.0 tag=badge-primary | PASS |
|
||||
| 11 | v1.5.0 卡「当前版本」角标 | PASS |
|
||||
| 12 | 进页 markSeen(localStorage=1.5.0,红点灭) | PASS |
|
||||
| 13 | dark 复核(computed):strong gray-900→gray-100 / marker teal-600→teal-400 / 版本号 teal-400 | PASS |
|
||||
|
||||
- vue-tsc **0** / `vite build` ✓(ReleaseLog chunk gzip 1.75 kB)。
|
||||
- 截图:`/tmp/b3-release-light.png`、`/tmp/b3-release-dark.png`(全页);脚本 `/tmp/b3-release-smoke.mjs`。
|
||||
- web-next 无 vitest(工程未配),四关=vue-tsc/build/Chrome 冒烟/commit。
|
||||
|
||||
## 四、口径与边界(诚实标注)
|
||||
|
||||
1. 数据/语义与旧 web **完全同源**:releases.ts 6 版大白话数据、红点=本地未读语义(换浏览器重提示,无服务端同步)、发版 SOP=releases.ts 头部插一条 + package.json 同步。
|
||||
2. 版本卡 tag 徽章色与旧 web 略异(旧:primary/amber/green 自定义;新:契约 badge 族),视觉口径以 web-next 契约为准。
|
||||
3. Chrome 冒烟发现并修复环境坑:旧 dev server(11-17 起)在 vite.config.ts 变更自动重启后**丢失 vue 插件与 @ 别名**(资源 500/白屏);杀进程重启 `npx vite --host 0.0.0.0 --port 5174 --strictPort` 即恢复。留痕:vite config 热重启不可信,改 config 后应人工重启 dev server。
|
||||
4. 冒烟点击菜单前需 `scrollIntoViewIfNeeded`(customer 菜单长,系统管理组在侧栏滚动容器底部)。
|
||||
@@ -0,0 +1,34 @@
|
||||
# S1-B 收口 + S1 泳道收官 人工验收单(2026-11-13 晚)
|
||||
|
||||
> 前置:上承 `s1-b-layer-d1.md`(D1+B 层 P1+D5-P1+RustFS+Gitea)。本单覆盖收口五件与 S1 泳道出口判定。
|
||||
|
||||
## 一、收口任务
|
||||
|
||||
| # | 任务 | 结果 | 证据 |
|
||||
|---|---|---|---|
|
||||
| 1 | **send P2 拆三子包**(0550f2a) | ✅ | submit(受理 11 文件+事件/状态契约族)/consume(3)/dispatch(11);根包 facade.go 别名门面 40 符号——**api/openapi/access/report/cmd 调用方零改动**;依赖单向无环;全仓 -race **38 包 RC=0**(router 走 MySQL 硬断言口径) |
|
||||
| 2 | dayNN 语义改名批(5ccca41) | ✅ | 19 文件(api 11/middleware 1/account 5/pool 1/router 1),头注释留 Day 溯源;全仓 `_dayNN_` 模式清零 |
|
||||
| 3 | storage 命名归一(b499b66) | ✅ | minio.go→s3.go/S3Store/NewS3(RustFS Phase C 收口) |
|
||||
| 4 | audit_mode=2 文案校准 | ✅ | 「2自动拒绝」→「2自动审核(黑词命中拒/未命中过)」对齐 audit.ModeAuto 实义;后端 2 处+openapi 1 处+测试措辞;account/audit -race 绿 |
|
||||
| 5 | gofmt 存量清零(e9b70ea) | ✅ | POC 遗留 20 文件全清;`gofmt -l` 全仓零输出 |
|
||||
| 6 | 24h 稳定性门槛 | ✅ **终判通过(2026-11-14 18:02 期满核验)** | 11-13 17:55 起计,期满 11-14 17:55;期内 21:33 宿主主动有序重启(内核升级,`last`=有序 shutdown 非崩溃)=已知事件不判失败。**期满核验三项全绿**:①dmesg 崩溃计数(segfault/sigill/error 25)=0(重启清零后全窗零新增);②九中间件 Up 20 hours 全 healthy(自愈拉起后窗内零重启);③gateway 驻留同 PID 53153(11-13 22:15 重驻起连续 19h47m)+/health ok+md5 比对一致(ecf71db6…)。**S5 压测基线前置资格确认** |
|
||||
| — | ↑期满前基线预检(S2-D1 顺带,09:57) | 📌 对照留痕 | dmesg 崩溃计数(segfault/sigill/error 25)=**0**;九容器 **Up 12 hours** 全 healthy(窗内零重启);gateway 驻留 PID 53153 etime 11:42:01(昨晚重驻以来连续)。17:55 终验以此为对照 → 三项维持,终判通过(见上行) |
|
||||
|
||||
## 二、S1 泳道出口判定(对齐《重建计划》S1 行:「B 层逐域搬运改造+迁移链落地;验收=全包等价 -race 绿」)
|
||||
|
||||
| 出口项 | 判定 |
|
||||
|---|---|
|
||||
| B 层逐域搬运改造 | ✅ 12 域 P1 全量 + send 拆包/channel 拆 pool/router 空壳消除 三项结构改造完成,dayNN 清零 |
|
||||
| 迁移链 v1~v19 落地 | ✅ 空库独建+seed-admin(D1) |
|
||||
| 全包等价 -race 绿 | ✅ **38 包 RC=0**(两阶段等价法全程可审计;唯一测试改动=clickhouse TestInsertSQL 断言随标识符反引号化更新+测试 helper 本地化,均非业务语义) |
|
||||
| lint | ✅ v2.1.6 全仓 0 告警 |
|
||||
| 集成测试 | ✅ 七件绿(storage 新增) |
|
||||
| 附加 | gateway :8081 装配冒烟驻留;Gitea 远端同步;RustFS 对象存储就位 |
|
||||
|
||||
**S1 泳道状态:✅ 收官(附一项在途观察:24h 门槛,11-14 17:55 期满回填)。**
|
||||
|
||||
## 三、遗留移交(不阻塞 S2)
|
||||
|
||||
1. 24h 门槛期满核验(明日)→ 本单回填 + S5 压测基线资格确认;
|
||||
2. FE 侧(views 31 页/FE-6/node 工具链)= S3 泳道;
|
||||
3. C1 回调引擎 = S4;结算域 = S2(下一泳道)。
|
||||
@@ -0,0 +1,51 @@
|
||||
# S2 结算域泳道收官 人工验收单(2026-11-14 深夜②)
|
||||
|
||||
> 前置:s2-d1~d5.md(D1 表设计/D2 消耗日汇总/D3 充值审核/D4 出账+阶梯/D5 利润+对账)。
|
||||
> 本单=《Go-sms-重建计划.md》S2 行出口判定:**结算域后端全量(D 层),出口=结算单测+对账差异 0**。
|
||||
|
||||
## 一、S2 五日交付总览
|
||||
|
||||
| 日 | 主题 | 核心交付 | 状态 |
|
||||
|---|---|---|---|
|
||||
| D1(11-14 上午) | 表设计+v20+契约 | 六项裁决 R1~R6(docs/review/s2-settle-table-review.md)/迁移 v20_settle_domain/实体同步/openapi v1.3.0 +20 端点草案 | ✅ |
|
||||
| D2(11-14 傍晚) | 消耗日汇总引擎 | consume.go+job.go(滚动重刷:1h×近 4 天,72h 回执窗 UNKNOWN 迁移靠重刷收敛)+/billing/consume+/ops/settle/aggregate+config/metrics/装配;sqlite 6 测试+真 MySQL 端到端 | ✅ |
|
||||
| D3(11-14 夜) | 充值审核流 | recharge.go(R3 双通道:confirm 单事务 CAS+余额自增+流水 ref_id=order_no)+订单族四端点(二级权限+平台角色门)+ISC:039 三处;7 测试含并发 confirm | ✅ |
|
||||
| D4(11-14 深夜) | 出账引擎+阶梯 | price.go(R1/R6 唯一换算:业务线×时间线回溯+阶梯月量落档;D4-0 六口径 Q1~Q6 经批准生效)+bill.go(下家/上家生成-确认-结账+SettleRecord+ladder CRUD)+export.go XLSX+15 端点;**阶梯结算单测(S2 显式验收项)**+真机出账全链+真 XLSX 5637B | ✅(用户走查在途) |
|
||||
| 前端验收面(11-14 深夜②) | 结算管理页 | 四 Tab(账单/消耗/订单/阶梯)+api 12 端点+seed 演示(s2demo)+Playwright 预走查真库闭环(余额+1000/账单 1955.00);前端四件全绿 | ✅(用户页面走查在途) |
|
||||
| D5(11-14 深夜③) | 利润/返点/对账 | profit.go(用户利润含下级树 Path/返点 R4+通道利润分摊口径)+reconcile.go(三方对账)+settle-check 子命令+双端点;5 测试+真机全链 **all_zero=true EXIT=0**;走查脚本扩 D4/D5 全域 27 PASS | ✅(用户走查在途) |
|
||||
|
||||
## 二、S2 出口判定(对齐《重建计划》S2 行)
|
||||
|
||||
| 出口项 | 判定 | 证据 |
|
||||
|---|---|---|
|
||||
| **结算单测** | ✅ | 阶梯结算单测(s2-d4 §一#6:首档覆写/恰好落档/删档回退/月中调价分档)+出账状态机+充值 CAS 并发+汇总幂等重刷;settle 包 18 测试件全绿(-race) |
|
||||
| **对账差异 0** | ✅ | settle-check 三方对账(明细直查↔日汇总↔结算单)真机 **all_zero=true 退出码 0**(s2-d5 §三);单测覆盖全零+篡改逐项暴露 |
|
||||
| D 层后端全量 | ✅ | 消耗汇总/充值审核/出账/阶梯/结账/导出/利润/返点/对账 9 域 22 端点(v1.3.0 契约 20 端点全部落地+导出 2) |
|
||||
| 门禁 | ✅ | vet 0 / **-race 全量 39 包 ok** / lint 0 / 前端四件全绿 |
|
||||
| 真机验证 | ✅ | D2 端到端收敛实证/D3 冒烟+审计留痕/D4 出账全链+5637B XLSX/D5 利润+对账全链;数据全部还原零残留 |
|
||||
| 前端验收面 | ✅ | 结算管理页四 Tab+真库闭环 Playwright 预走查(s2demo) |
|
||||
|
||||
**S2 泳道状态:✅ 收官(附一项在途补正:S2 全域用户本人走查,D2~D5 脚本已备 27 PASS 预走查,待用户执行回填)。**
|
||||
|
||||
## 三、六项裁决落点核对(D1 → 实现闭环)
|
||||
|
||||
| 裁决 | 落点 | 状态 |
|
||||
|---|---|---|
|
||||
| R1 出账时刻计价 | price.go 唯一换算;明细 Fee 保持片数,send 域零改动 | ✅ |
|
||||
| R2 事实源统一 | billing_consume_log+stats_channel_daily;stats_user_daily v20 DROP | ✅ |
|
||||
| R3 充值双通道 | 即时到账保留+BillingOrder 审核流(ref_id=order_no 区分,D5 对账口径现成) | ✅ |
|
||||
| R4 单级返点 | rebate_rate 消费于 profit.go(直线下级×rate,历史不重算=不落档现算) | ✅ |
|
||||
| R5 阶梯表 | settle_ladder+落档覆写+独立单测 | ✅ |
|
||||
| R6 条/元口径 | 条→元只在 price.go;余额/Count=条,账单/利润=元 | ✅ |
|
||||
|
||||
## 四、遗留移交(不阻塞 S3)
|
||||
|
||||
1. **用户本人走查在途**:D4 出账链+D5 利润/对账随本单脚本补正(s2-walkthrough.sh D2~D5);
|
||||
2. **账号×通道交叉粒度事实源缺失**:利润 cost 分摊为占比口径(总量守恒、个体近似),精确归因需新增事实粒度(S3+ 择机);
|
||||
3. 层级返点(多级分成)/月结自动生成调度(手动 generate 先行)/est_balance 校正余额录入——D1 评审已列显式不承诺;
|
||||
4. 明细热窗(S4 分区)落地后,历史月对账基线=日汇总↔结算单两方(明细直查侧退场,reconcile.go 头注已声明);
|
||||
5. 利润分析前端页(views/settle/profit,FE D34)= S3 前端泳道;mock handlers 未含 /stats/profit(前端未消费,零影响)。
|
||||
|
||||
## 五、下一步
|
||||
|
||||
S3 前端泳道(views 31 页搬运+FE-6 权限矩阵走查+Mock 下线;见《重建计划》)——开工前置:用户走查回填不阻塞 S3 开工判定(在途补正机制)。
|
||||
@@ -0,0 +1,45 @@
|
||||
# S2-D1 表设计评审 + 迁移 v20 + 契约草案 人工验收单(2026-11-14)
|
||||
|
||||
> 前置:S1 泳道收官(s1-b-closeout.md,24h 门槛在途 11-14 17:55 期满回填)。
|
||||
> 本单覆盖 S2 结算域 D1:六项表设计裁决定稿 + v20_settle_domain 落地 + openapi 契约草案。
|
||||
|
||||
## 一、交付物
|
||||
|
||||
| # | 任务 | 结果 | 证据 |
|
||||
|---|---|---|---|
|
||||
| 1 | **表设计评审纪要**(六项裁决 R1~R6) | ✅ | `docs/review/s2-settle-table-review.md`;R1 计价模型=出账时刻计价(用户 2026-11-14 拍板:明细 Fee 保持片数、send 域零改动) |
|
||||
| 2 | **迁移 v20_settle_domain** | ✅ | migrations.go 末块:billing_consume_log +unknown_count/updated_at;account_user +rebate_rate;DROP stats_user_daily(R2 单一事实源);新表 settle_ladder;casbin 补 ops settle:write / customer settle:read(NOT EXISTS 逐条守卫) |
|
||||
| 3 | **实体同步** | ✅ | entity/billing.go(补列+SettleLadder+SettleCustomer 状态注释对齐 0草稿/1已确认/2已结账);account.go(RebateRate);system.go(StatsUserDaily 删除留裁决注记);base.go All() 39 实体 |
|
||||
| 4 | **openapi 契约草案 v1.3.0** | ✅ | 20 个新端点(billing 5/settle 15:充值订单审核/消耗查询/下家/上家月结/阶梯/利润/手动汇总);js-yaml 解析通过(84 paths);版本 1.2.0→1.3.0 |
|
||||
| 5 | **迁移测试更新** | ✅ | base_test.go:末七窗口 v13..v19→v14..v20,终版断言 v20,新增 v20 五项断言;v13 断言随窗口右移移除(注记留痕) |
|
||||
|
||||
## 二、门禁(七件套后端件)
|
||||
|
||||
| 项 | 结果 |
|
||||
|---|---|
|
||||
| go vet ./... | ✅ RC=0 |
|
||||
| go test -race ./...(全量) | ✅ 全绿(回填于本单尾部) |
|
||||
| golangci-lint v2.1.6 | ✅ 0 issues |
|
||||
| 迁移单测(sqlite 末七两遍幂等) | ✅ TestMigrations_IdempotentAndVersioned 含 v20 断言通过 |
|
||||
| openapi YAML 解析 | ✅ js-yaml 84 paths |
|
||||
| gen-dict/-check | ✅ 未新增 x-isc-dict,dict.ts 无需再生成 |
|
||||
|
||||
## 三、设计要点留痕
|
||||
|
||||
1. **不触碰已收官 send 域**(R1):计价全部收拢 D4 出账引擎 settle/price.go(PriceAt 时间线回溯+阶梯落档);条→元换算全链单点(R6)。
|
||||
2. **v20 加列走显式 SQL+HasColumn 守卫、空库走 HasTable 守卫 AutoMigrate 整表**——glebarez SQLite 下 entity 加字段触发表 diff 重建抹索引(v16/v17 教训,三条路径收敛:全量新库 v0 直含/存量库 ALTER/末七窗口空库建表)。
|
||||
3. **零新增聚合索引**:通道日汇总复用 v16 `idx_detail_channel_created(channel_id, created_at)`。
|
||||
4. **init_schema.sql 不动**(Day11 基线参考,权威=migrate 链,v16/v17 演进均未回写,维持惯例)。
|
||||
5. **stats_channel_daily 零 schema 变更**(unknown_count/cost_total 原有;grain=通道×天,不加 business——通道价格体系不分业务,视频走独立通道)。
|
||||
6. **casbin 策略随 v20 逐条 NOT EXISTS 守卫种入**(v5 的 n>0 整体跳过守卫不适用于增量)。
|
||||
|
||||
## 四、验收结论
|
||||
|
||||
**S2-D1 通过。** D2(消耗日汇总引擎)开工前置=本单归档+CHANGELOG+进度回填。
|
||||
|
||||
## 五、附:真库验证(dev MySQL 13306,2026-11-14)
|
||||
|
||||
- `cmd/migrate version` v19_longsms_pending → `up` → **v20_settle_domain**(存量库 ALTER 路径实测通过);
|
||||
- information_schema 核验:billing_consume_log unknown_count/updated_at 两列 ✓、account_user.rebate_rate ✓、settle_ladder 建表 ✓、stats_user_daily 已 DROP ✓、casbin_rule 新增 ops settle:write / customer settle:read ✓;
|
||||
- 集成测试:TestMySQL_AutoMigrate 39 表 PASS;Casbin 集成 PASS(新策略不破坏 enforcer 加载);
|
||||
- `go test -race ./...` 全量 38 包 ok,零 FAIL。
|
||||
@@ -0,0 +1,42 @@
|
||||
# S2-D2 消耗日汇总引擎 人工验收单(2026-11-14)
|
||||
|
||||
> 前置:s2-d1.md(表设计+v20+契约)。本单覆盖 D2:settle 域首批业务代码——
|
||||
> 消耗日汇总引擎(结算域事实源)+ 两端点 + 后台滚动重刷 job。
|
||||
|
||||
## 一、交付物
|
||||
|
||||
| # | 任务 | 结果 | 证据 |
|
||||
|---|---|---|---|
|
||||
| 1 | **settle/consume.go**(聚合引擎) | ✅ | `AggregateDay`:账号×业务→billing_consume_log、通道→stats_channel_daily,一条 GROUP BY(计 SUM(status=N) 计数+Σfee 条)+OnConflict UPSERT(复合 uk 覆盖更新);`[lo,hi)` 半开窗规避 date() 方言差;cost_total 不写(R1);`ListConsume` 分页/过滤/时间窗按天归一(零点行不被带时刻参数误滤) |
|
||||
| 2 | **settle/job.go**(滚动重刷 job) | ✅ | **对 D1 计划的关键修正**:单日 T+1 落账会在 72h 回执窗内失真(UNKNOWN→成功迁移),改为**周期重刷近 4 天**滚动收敛;启动回补 7 天;配置零值兜底(1h/4/7);失败 Warn 不中断+errors 计数器,幂等下轮自愈 |
|
||||
| 3 | **settle_handler.go**(两端点) | ✅ | `GET /billing/consume`(JWT+customer/group 强制收窄自身,越权带参覆盖不报错)+ `POST /ops/settle/aggregate`(settle:write 手动回补);D3~D5 端点随任务在本文件追加 |
|
||||
| 4 | **装配** | ✅ | Deps.Settle 字段+registerSettleRoutes;`v1.StartSettleAggregateJob`(照 StartStatsRefresher v1 层包装模式);gateway main 挂载于 StatsRefresher 之后;config.Settle 段(1h/4/7)+config.dev.yaml 注释口径;metrics 两件(`go_sms_settle_aggregate_rows_total{kind}`/`errors_total`) |
|
||||
| 5 | **单测** | ✅ | consume_test.go 4 件(正确性多账号×双业务×五状态逐行断言/幂等+跨天边界/**UNKNOWN 状态迁移重刷收敛**/查询过滤分页)+ settle_handler_test.go 2 件(admin 全量 vs customer 收窄带参越权/手动回补落账+坏日期 4xx) |
|
||||
|
||||
## 二、门禁(七件套后端件)
|
||||
|
||||
| 项 | 结果 |
|
||||
|---|---|
|
||||
| go vet ./... | ✅ RC=0 |
|
||||
| go test -race ./...(全量) | ✅ **39 包 ok 零 FAIL**(新增 settle 包) |
|
||||
| golangci-lint v2.1.6 | ✅ 0 issues |
|
||||
| 集成 | ✅ 沿用 schema/casbin(D1 已过;D2 无新集成面,真库验证见 §三) |
|
||||
|
||||
## 三、dev 实机验证(真 MySQL,2026-11-14)
|
||||
|
||||
1. **部署串行铁律**:make gw-stop→gw-start 三轮(重驻/状态迁移验证/清理后),每轮 /health ok+**md5 比对一致**(8fd2dfbc…)。
|
||||
2. **端到端落账**:SQL 直插 3 条今日明细(账号 1×2:成功+未知/账号 100×1 成功,通道 1)→启动回补自动聚合 → billing_consume_log 2 行(1:sms 2/1/0/1/fee3.0;100:sms 1/1/0/0/fee2.5)+ stats_channel_daily 1 行(3/2/1/cost_total=0)——**逐列与 sqlite 单测期望一致**;metrics rows{account}=2/{channel}=1。
|
||||
3. **滚动收敛实证**:UPDATE 明细 status 4→2 → 重启重刷 → 账号 1 行 **success 1→2/unknown 1→0**——72h 回执窗迁移收敛语义在真库闭环。
|
||||
4. **指标暴露**:/metrics 出现 go_sms_settle_aggregate_errors_total=0(HELP/TYPE 正常)。
|
||||
5. **清理**:测试明细 3 条+两表当日行全删(避免僵尸汇总行——热窗内明细不删的边界已文档化),最终干净重驻。
|
||||
|
||||
## 四、口径与边界留痕
|
||||
|
||||
- **滚动重刷窗 4 天**=72h 回执窗+当天增量;月结建议 T+4 后生成(D4 文档化;generate 幂等重算兜底);
|
||||
- 汇总行不随明细删除回退(热窗内明细不删;S4 分区清退后历史行冻结);
|
||||
- 通道维度不含 business/Σfee(通道价格体系不分业务;上家账单按 billing_mode 条数口径);
|
||||
- job 为 ticker 型(照 StatsRefresher),cronx 保留给已有 alarm/健康巡检用途。
|
||||
|
||||
## 五、验收结论
|
||||
|
||||
**S2-D2 通过。** D3(充值审核流)开工前置=本单归档+CHANGELOG+回填+提交。
|
||||
@@ -0,0 +1,39 @@
|
||||
# S2-D3 充值审核流 人工验收单(2026-11-14)
|
||||
|
||||
> 前置:s2-d2.md(消耗日汇总)。本单覆盖 D3:BillingOrder 订单审核族(R3 双通道裁决落地)。
|
||||
|
||||
## 一、交付物
|
||||
|
||||
| # | 任务 | 结果 | 证据 |
|
||||
|---|---|---|---|
|
||||
| 1 | **settle/recharge.go** | ✅ | CreateOrder(count 1~100 亿校验/账号存在性/pay_status=0 **不动余额**;OrderNo=B+时间戳+4 位随机,uk 冲突重试一次)/ConfirmOrder(**单事务四步**:状态 CAS(WHERE pay_status=0,RowsAffected=0→ISC:039)→余额原子自增 count→同事务回读→流水 ref_id=order_no)/RejectOrder(0→2 CAS 不涉余额,remark 留痕)/ListOrders(过滤分页 id 倒序) |
|
||||
| 2 | **错误码 ISC:039** | ✅ | 「订单状态冲突(非待确认)」HTTP 409;**三处同步**:codes.go+registry 手工清单+x-isc-dict+dict.ts 再生成(52 码);openapi_sync_test 双向锁绿 |
|
||||
| 3 | **四端点** | ✅ | GET /billing/orders(平台角色 admin/ops/service;group/customer 403)+POST /billing/orders、/:id/confirm、/:id/reject(挂 account:recharge 二级权限,与即时充值同门);审计自动留痕(module=billing action=orders:post/confirm:post) |
|
||||
| 4 | **测试 7 件** | ✅ | service:创建校验(0/负/超限/账号不存在/OrderNo 唯一批量 21 单)/confirm 事务语义(paid_at+operator+流水四字段+balance_after 一致)/状态机(重复 confirm 409 且余额二次不变;已确认 reject 409;不存在 404)/**并发 confirm 恰一成功**(-race)/驳回/列表;handler:全流程(创建→列表→confirm→409 ISC:039)/权限(恒拒 checker 403+customer 列表 403) |
|
||||
|
||||
## 二、门禁(七件套后端件)
|
||||
|
||||
| 项 | 结果 |
|
||||
|---|---|
|
||||
| go vet ./... | ✅ RC=0 |
|
||||
| go test -race ./...(全量) | ✅ **39 包 ok 零 FAIL** |
|
||||
| golangci-lint v2.1.6 | ✅ 0 issues |
|
||||
| openapi_sync_test + codes_test 数量断言 | ✅ 52 码同步(测试断言随更) |
|
||||
|
||||
## 三、真机验证(dev,2026-11-14 20:50)
|
||||
|
||||
1. **重驻**:gw-stop→gw-start,/health ok+md5 一致(6376ba52…);
|
||||
2. **JWT 临时签发**:HS256+dev secret(config loader 默认值,非侵入不触库),casbin g(1,admin) 通过 recharge 二级权限;
|
||||
3. **全链冒烟**:创建(count=123/amount=12.3,pay_status=0,OrderNo=B202611142050471417,**余额零动**)→confirm(**余额 0→123**+流水 id=1)→重复 confirm **HTTP 409+ISC:039**;
|
||||
4. **真库核验**:account_balance_log(biz_type=recharge/amount=123/balance_after=123/**ref_id=order_no**/operator=admin)+ system_operate_log 自动留痕(module=billing action=orders:post/confirm:post)——**双通道区分锚点(ref_id)与审计适配两项设计实证**;
|
||||
5. **清理还原**:余额归零/订单+流水删除(审计行保留=冒烟证据,日志性质 append-only);临时 token 清除。
|
||||
|
||||
## 四、口径留痕
|
||||
|
||||
- **双通道语义**(R3):即时充值=已线下确认快捷入账(流水 ref_id 空);订单流=付款审核确认(流水 ref_id=order_no)——D5 对账以 ref_id 有无区分两通道;
|
||||
- 并发安全:MySQL 行锁路径(与 account.Recharge 同代码形态,POC 期已真库验证)+ sqlite 单连接串行 CAS 语义等价(单测覆盖);
|
||||
- OrderNo 可读风格与 TaskNo(T 前缀)对齐,B 前缀=billing 域。
|
||||
|
||||
## 五、验收结论
|
||||
|
||||
**S2-D3 通过。** D4(出账引擎+阶梯)开工前置=本单归档+回填+提交。
|
||||
@@ -0,0 +1,43 @@
|
||||
# S2-D4 出账引擎+阶梯 人工验收单(2026-11-14 夜)
|
||||
|
||||
> 前置:s2-d3.md(充值审核流)。本单覆盖 D4:月结出账全链——计价引擎/阶梯/确认/结账/XLSX 导出。
|
||||
> **D4-0 口径**:Q1~Q6 六项业务口径以方案默认值经用户批准方案后生效(Q1 无价日 0 元入账/Q2 阶梯整月覆写/
|
||||
> Q3 上家 all=submit/Q4 按生成时点当前 upstream_id 归并/Q5 全业务合并/Q6 状态机 0→1→2)——
|
||||
> **用户走查补正项见文末**(S2 收官一并人工核)。
|
||||
|
||||
## 一、交付物
|
||||
|
||||
| # | 任务 | 结果 | 证据 |
|
||||
|---|---|---|---|
|
||||
| 1 | **settle/price.go** | ✅ | R1/R6 全链唯一换算点:PriceAtAccount(业务线×时间线回溯 EffectiveAt≤当日)/PriceAtChannel/LadderPrice(月量落档取最高 tier_from) |
|
||||
| 2 | **settle/bill.go** | ✅ | GenerateCustomerBills(逐业务逐日计价,阶梯覆写,uk UPSERT 幂等,已结 skipped)/GenerateUpstreamBills(通道→upstream 当前归并,billing_mode 三口径)/Confirm(0→1)/Settle(1→2 CAS+SettleRecord 金额快照同事务)/列表/按天明细/ladder CRUD(同 owner 同 tier_from 唯一) |
|
||||
| 3 | **settle/export.go** | ✅ | excelize:下家=单头+按天明细;上家=单头+通道×天明细(含单价列) |
|
||||
| 4 | **handler 15 端点** | ✅ | customer 6/generate/upstream 5/ladder 4;settle:write 门;customer 账单仅自己(handler scope);上家域平台角色门 |
|
||||
| 5 | **测试 6 件**(settle 包 5+handler 1) | ✅ | **阶梯结算单测(S2 显式验收项)**:首档覆写 7.5/恰好落档 1000→40.0/删阶梯回退时间线;时间线月中调价按日分档(10.0+10.0);上家三口径(4.4/4.5)+未绑通道不入账;状态机 0→1→2+SettleRecord 快照+终态幂等;ladder CRUD+重复档 400;handler 全流程(生成→配档重算 5→确认→结账→skipped→导出 PK→403 门) |
|
||||
|
||||
## 二、门禁(七件套后端件)
|
||||
|
||||
| 项 | 结果 |
|
||||
|---|---|
|
||||
| go vet ./... | ✅ RC=0 |
|
||||
| go test -race ./...(全量) | ✅ **39 包 ok 零 FAIL**(settle 包 14 件) |
|
||||
| golangci-lint v2.1.6 | ✅ 0 issues |
|
||||
|
||||
## 三、真机验证(dev,2026-11-14 21:34,HS256 临时 token)
|
||||
|
||||
**出账全链**:种价(0.10)+消耗(succ=100)→生成 `{generated:1}`(时间线金额 10)→配阶梯(0 档 0.05)→重算 `{recalculated:1}`(**覆写 5**=100×0.05)→列表 amount=5 ✓→确认→结账(**SettleRecord:direction=1/amount=5/operator=admin/settled_at**)→再生成 `{skipped:1}`(已结拒重算)→**导出 5637 字节真 XLSX**(`file` 识别 Microsoft Excel 2007+)。数据已清理还原。
|
||||
|
||||
## 四、口径留痕(修复过程)
|
||||
|
||||
- settleBill 金额快照:Pluck 链式复用导致列类型污染(sqlite 实测暴露)→ 改 Select+Scan 单查询;
|
||||
- isDuplicateKey 补大小写归一(sqlite 2067 码 "UNIQUE constraint" 小写实测暴露);
|
||||
- 手动建表方言坑第三例:settle_upstream 的 idx_period 与 settle_customer 同名(测试侧无索引+显式 UNIQUE 约束版本)。
|
||||
|
||||
## 五、用户走查(补正项,七件套·本人走查)
|
||||
|
||||
- 走查脚本已备:`docs/acceptance/scripts/s2-walkthrough.sh`(覆盖 D2/D3;D4 出账链建议随 S2 收官走查一并执行——生成/阶梯/结账/导出 curl 序列与 §三一致)。
|
||||
- **结果:待用户执行后回填**(此前 D2/D3 验收为自动化测试+AI 冒烟,本人走查项以此补正)。
|
||||
|
||||
## 六、验收结论
|
||||
|
||||
**S2-D4 通过(自动化门禁与真机冒烟);用户走查项在途待回填。** D5(利润/返点/三方对账)为 S2 收官任务。
|
||||
@@ -0,0 +1,49 @@
|
||||
# S2-D5 利润/返点/三方对账 人工验收单(2026-11-14 深夜②)
|
||||
|
||||
> 前置:s2-d4.md(出账+阶梯)。本单覆盖 D5:profit.go(用户利润含下级树/返点 R4+通道利润)+
|
||||
> reconcile.go(三方对账)+cmd/reconcile/settle-check+双端点;并作为 **S2 泳道收官日**
|
||||
> (收官判定见 s2-closeout.md)。
|
||||
|
||||
## 一、交付物
|
||||
|
||||
| # | 任务 | 结果 | 证据 |
|
||||
|---|---|---|---|
|
||||
| 1 | **settle/profit.go** | ✅ | UserProfit:revenue=Σ日成功×PriceAtAccount 时间线价(与出账同口径,**阶梯不进分析**——启用阶梯账号利润行与账单金额差=口径差异非错误);cost=全平台通道成本×成功占比(**分摊口径**:两事实源无账号×通道交叉粒度,精确归因留 S3+);rebate=Σ直线下级窗口结算额×下级 rebate_rate(R4 单级,**历史不重算**=返点不落档、按当前树+当前 rate 现算);profit=revenue-cost+rebate;parent_id 子树过滤(Path 物化路径前缀,空 path 兜底 "/id>/");平台净利=Σrev-Σcost-Σrebate(返点平台流出/上级流入两口径并存);无流量纯返点上级也有行。ChannelProfit:cost=Σ日成功×PriceAtChannel;revenue=全平台用户收入×通道成功占比(与用户侧同分母,Σ通道rev=Σ用户rev 守恒) |
|
||||
| 2 | **settle/reconcile.go** | ✅ | ReconcilePeriod(period):账号三方=明细直查↔billing_consume_log↔settle_customer(条数);通道两方=明细↔stats_channel_daily;上家=日汇总×**当前**归并/billing_mode 重推↔账单快照(生成后换绑/改模式/重刷如实暴露=Q4/Q6 快照语义的对账价值);**金额不比**(结算单金额=出账时点价/阶梯快照,事后调价重算必生噪音,金额核对走 XLSX 导出人工比对);bill_missing 单列不判失败(流程状态≠数据不一致);AllZero=三方 diff 全零 |
|
||||
| 3 | **cmd/reconcile/settle-check** | ✅ | 子命令 `-period YYYY-MM [-json]`;人类可读摘要+异常行;退出码 0=all_zero/1=有差异/2=运行错误 |
|
||||
| 4 | **handler +2 端点** | ✅ | GET /stats/profit/users(date_from/date_to/parent_id/分页)、GET /stats/profit/channels(…/channel_id/分页);平台角色门(admin/ops/service,group/customer 403,与充值订单列表同口径);缺省窗口=本月 1 号~今天 |
|
||||
| 5 | **openapi v1.3.0 描述对齐** | ✅ | 双端点 summary 补分摊/阶梯不进分析/R4 语义与响应结构 |
|
||||
| 6 | **测试 5 件**(settle 4+handler 1) | ✅ | TestProfit_Users(时间线价/分摊/单级返点/子树/汇总守恒/纯返点上级)、TestProfit_Channels(成本直算/收入分摊/过滤)、TestReconcile_AllZero(全链 diff=0)、TestReconcile_ExposesDiffs(篡改 consume/stats/删账单逐项暴露+missing 不判失败+非法 period)、TestSettleProfit_Endpoints(数值/403/坏日期) |
|
||||
| 7 | **走查脚本扩 D4/D5** | ✅ | s2-walkthrough.sh 增 D4-1~D4-7(出账/阶梯/结账/XLSX)+D5-1~D5-4(利润/403/settle-check)+专用清理;**修复两处存量 bug**(见 §四) |
|
||||
|
||||
## 二、门禁(七件套后端件)
|
||||
|
||||
| 项 | 结果 |
|
||||
|---|---|
|
||||
| go vet ./... | ✅ RC=0 |
|
||||
| go test -race ./...(全量) | ✅ **39 包 ok 零 FAIL**(settle 包 18 件) |
|
||||
| golangci-lint v2.1.6 | ✅ 0 issues |
|
||||
|
||||
## 三、真机验证(dev,2026-11-14 23:00,HS256 临时 token;网关串行重启 md5 一致)
|
||||
|
||||
- **种子**(2026-10 空月):通道/上家 9101(success 口径)+账号1 价 0.10+通道价 0.05+明细 2 条(10-15,ID 7302 段);
|
||||
- **出账链**:汇总 1/1 → 下家生成 amount=0.2(2×0.10)→ 阶梯 0 档 0.05 重算覆写 0.1 → 确认→结账(**SettleRecord direction=1/amount=0.1/operator=admin**)→ 已结 skipped=1 → 上家生成 **US202610-9101 count=2/amount=0.1**;
|
||||
- **利润双端点**:users→acc1 succ2/**rev0.2(时间线价,与阶梯账单 0.1 的差=口径差异实证)**/cost0.1/profit0.1;channels→ch9101 succ2/cost0.1/rev0.2(分摊 2/2)/profit0.1;
|
||||
- **settle-check -period 2026-10**:`accounts checked=1 diffs=0 bill_missing=0; channels 1/0; upstreams 1/0; all_zero=true` **退出码 0 —— S2 出口判据达成**;
|
||||
- **清理**:11 项计数全 0 零残留(仅 demo seed 合法数据留库);
|
||||
- **全脚本 AI 预走查**:s2-walkthrough.sh 三轮实跑 **27 PASS / 0 FAIL**(D2~D5 全域)。
|
||||
|
||||
## 四、口径与修复留痕
|
||||
|
||||
- 明细雪花 ID 直插 9302… 段超 int64 上限(9.22e18)MySQL 拒收——走查脚本 D2 段存量 bug(11-14 写后未实跑),D5 收官全脚本实跑暴露,统一改 7302… 段;
|
||||
- 走查脚本 D2/D3 清理段 `$ON/$B0` 在 `sg docker -c '...'` 单引号内不展开(内层空展开致 `SET balance=` SQL 语法错)——改 printf|mysql_pipe 外层展开模式+空值兜底;
|
||||
- 利润行集不含软删/孤儿账号(其消耗行不入行集,分摊分母走通道侧总量,守恒性如实降级)——边界注释留痕。
|
||||
|
||||
## 五、用户走查(七件套·本人走查项)
|
||||
|
||||
- 走查脚本:`bash docs/acceptance/scripts/s2-walkthrough.sh 2>&1 | tee /tmp/s2-walk.log`(D2~D5 全域+自动清理);
|
||||
- **结果:待用户执行后回填**(AI 预走查 27 PASS 见 §三;本人走查以此补正)。
|
||||
|
||||
## 六、验收结论
|
||||
|
||||
**S2-D5 通过(自动化门禁+真机全链+AI 预走查);用户走查项在途待回填。** S2 泳道收官判定见 `s2-closeout.md`。
|
||||
@@ -0,0 +1,52 @@
|
||||
# S2 结算验收页(前端预验收面) 交付单(2026-11-14 深夜)
|
||||
|
||||
> 动因:用户裁决「需要人工前端页面验收,否则对项目进度完全没有感知」——S2 后端 D1~D4
|
||||
> 已完成但全部不可见,本单以最小可视化验收面补正(评审者感知断层),S3 结算页面族在此基础精修。
|
||||
|
||||
## 一、交付物
|
||||
|
||||
| # | 文件 | 内容 |
|
||||
|---|---|---|
|
||||
| 1 | `web/src/views/settle/index.vue` | 结算管理页 4 Tab:消耗日汇总(查询+手动回补)/充值订单(创建+确认/驳回二次确认)/月度结算单(生成/重算+确认/结账/导出 XLSX+明细抽屉)/阶梯价(增删);页头「S2 验收版·S3 精修」 |
|
||||
| 2 | `web/src/api/settle.ts` | 12 端点封装(TS 类型)+downloadBill(fetch+blob 带 token 文件流下载) |
|
||||
| 3 | `web/src/router/routes.ts` | settle 项 ComingSoon → 真页面(roles admin/ops 不变) |
|
||||
| 4 | `scripts/seed_settle_demo.sql` | 演示种子:s2demo 登录账号(admin,不动内置 admin)/demo-cust 客户/价格时间线(11-01 0.10、11-15 0.15 月中调价)/近 5 天消耗(19550 成功)/待确认订单;幂等 |
|
||||
| 5 | `web/s2-acceptance.mjs` | Playwright 预走查脚本(登录→四 Tab→订单确认实操作→生成账单→明细抽屉→6 截图) |
|
||||
|
||||
## 二、门禁(前端四件)
|
||||
|
||||
| 项 | 结果 |
|
||||
|---|---|
|
||||
| eslint(max-warnings 0) | ✅ 0 |
|
||||
| vue-tsc --noEmit | ✅ 0 |
|
||||
| vitest 全量 | ✅ **138/138**(既有零破坏) |
|
||||
| npm run build | ✅ 9.74s(chunk 警告=既有已知项,S3 性能门) |
|
||||
|
||||
## 三、Playwright 预走查(真库闭环,2026-11-14 22:1x)
|
||||
|
||||
1. 登录 s2demo → /home ✓;菜单进入 /settle ✓;
|
||||
2. Tab1 消耗汇总 5 行(种子数据)展示 ✓;
|
||||
3. **订单确认实操作**:页面按钮→MessageBox→API→**账号 2 余额 5000→6000 真实落账** ✓;
|
||||
4. **生成账单**:SC202611-2 = 19550 条 × **1955.00 元**——与价格时间线分毫不差(11-10~11-14 全部落在 0.10 档,11-15 调价档留走查验证) ✓;
|
||||
5. 明细抽屉(按天行)✓;Tab4 阶梯 ✓;6 张截图 `web/shots/s2-settle-*.png`。
|
||||
|
||||
**过程留痕(casbin 热加载缺口)**:SQL 直插 casbin_rule 在 gateway 运行中不生效(策略启动时载入内存)→ 写操作 403;重启 gateway 后全通。种子脚本已注释该约束(正规路径=走开户 API 自动同步 enforcer)。
|
||||
|
||||
## 四、用户走查指引(3 分钟)
|
||||
|
||||
```bash
|
||||
# 1. 种子(已种可跳过;幂等)——注意:gateway 运行中种后需重启使其 casbin 生效
|
||||
sg docker -c 'docker exec -i go-sms-mysql mysql -uroot -proot123 gosms' < scripts/seed_settle_demo.sql
|
||||
# 2. 确保服务在跑:make gw-start(8080) / make web-dev(5173)
|
||||
# 3. 浏览器 http://127.0.0.1:5173 → 登录 s2demo / S2demo@2026
|
||||
# 菜单「统计结算 → 结算管理」四 Tab 走查:
|
||||
# - 订单:确认到账看余额 / 驳回不涉余额
|
||||
# - 账单:生成 2026-11 → 金额 1955;配阶梯(账号2,0 档 0.08)→ 重算 → 1564;
|
||||
# 确认 → 结账(SettleRecord)→ 导出 XLSX
|
||||
# - 阶梯:增/删即时生效于重算
|
||||
```
|
||||
|
||||
## 五、边界
|
||||
|
||||
- mock 模式不覆盖(msw settle handler 留 S3 结算页面族一起做);页面为验收版,S3 按设计规范精修(ProTable 化/权限走查/LCP);
|
||||
- 上家账单/利润 Tab 未含(后端 D5 未实现),S3/D5 后点亮。
|
||||
@@ -0,0 +1,47 @@
|
||||
# S3 前端收尾泳道收官 人工验收单(2026-11-14 深夜④)
|
||||
|
||||
> 前置:s2-closeout.md(S2 结算域收官)。本单=《Go-sms-重建计划》S3 行出口判定:
|
||||
> **FE-6(权限矩阵走查/Mock 下线/性能/CI 镜像化)+ 结算页面族(含 FE-D34 利润分析)**。
|
||||
|
||||
## 一、交付物
|
||||
|
||||
| # | 任务 | 结果 | 证据 |
|
||||
|---|---|---|---|
|
||||
| 1 | **views 31 页等价核对** | ✅ | `git --git-dir=归档仓 ls-tree poc-final` 与现仓逐文件比对:31 个 .vue 全量在位(S1-B 已搬)+S2 新增 settle/index.vue;api 12 文件+router 3 文件同构+settle.ts 新增——**搬运项实为零残留,S3 仅回归验证** |
|
||||
| 2 | **FE-D34 利润分析页** | ✅ | `views/stats/profit/index.vue`(用户/通道双 Tab+汇总条:应收/成本分摊/返点/平台净利;利润正负着色;**口径说明常驻**=阶梯不进分析/占比分摊/R4 诚实标注);路由 `/stats/profit`(统计结算组,roles admin/ops/service 与后端平台门一致);api/settle.ts +profitUsers/profitChannels(类型+契约);vitest 2 件(api 契约+组件挂载渲染) |
|
||||
| 3 | **FE-6 · Mock 下线** | ✅ | 浏览器 mock 通道全链移除:main.ts worker 分支/mocks/browser.ts/public/mockServiceWorker.js/.env* VITE_API_MODE/LoginView isMock(演示账号提示)/Makefile web-dev·web-real 口径;**msw 移至 devDependencies 仅存 vitest 层**(mocks/node.ts 测试隔离服务器,138 用例不破);单一 real 模式(vite proxy→gateway :8080) |
|
||||
| 4 | **FE-6 · 权限矩阵走查** | ✅ | tests/role-menu-matrix.spec.ts:**五角色×33 菜单固化矩阵快照**(人工确认语义:资金/通道/结算=admin/ops,service=运营执行,group/customer=业务自助不见资金页,与后端 casbin/statsIsPlatform 同口径);矩阵未登记新菜单=测试红(走查强制过目);8 件断言 |
|
||||
| 5 | **FE-6 · 性能** | ✅(产物口径) | manualChunks 拆 vendor:主包 1.27MB→**element-plus 341KB+vue-vendor 62KB+业务 entry ~19KB(全部 gzip)**,逐 chunk <500KB 达标;页面全量路由懒加载;⚠️LCP 真浏览器实测受阻(本机 chromium 确定性 Target crashed,3 轮 flag 组合均崩,见 §四) |
|
||||
| 6 | **FE-6 · CI 集成** | ✅ | .gitea/workflows/ci.yml +web-test job(setup-node22+corepack pnpm+四件门禁,与 make web-test 同语义;Gitea Actions 未启用时同 Go job 不执行不碍仓) |
|
||||
| 7 | **FE-6 · web 镜像化** | ✅(构建+容器冒烟实测) | web/Dockerfile(**dist 产物镜像** nginx:1.27-alpine——容器内 npm 带宽实测 22~49KiB/s 全量依赖不可行,构建前置=宿主/CI vite build,make web-image 一键)+nginx.conf.template(envsubst 注入 API_UPSTREAM;gzip/SPA fallback/资产一年强缓存);**容器冒烟:root 200/SPA 路由 200/api 反代→宿主网关 401 应答/gzip+immutable 生效**;deploy/k8s/web.yaml(Deployment+**Service NodePort 30081**,POC 既定口径)——kind 本机未装,清单随 S5 灰度实际 apply |
|
||||
|
||||
## 二、门禁(七件套前端件;后端零改动)
|
||||
|
||||
| 项 | 结果 |
|
||||
|---|---|
|
||||
| eslint(max-warnings 0) | ✅ 0 |
|
||||
| vue-tsc --noEmit | ✅ 0 |
|
||||
| vitest | ✅ **148 用例全绿**(138 存量+利润 2+矩阵 8;guard 菜单计数随 FE-D34 31→32 显式更新) |
|
||||
| vite build | ✅ 产物全 chunk gzip<500KB(最大 element-plus 341KB) |
|
||||
| go vet/-race/lint | ✅ S2-D5 终态延续(后端零触碰) |
|
||||
|
||||
## 三、真机验证(dev,2026-11-15 00:0x)
|
||||
|
||||
- dev server 重启(Mock 下线生效):`/` 200、SPA 路由 `/stats/profit` 返回壳、**index.html 零 msw 引用**;
|
||||
- vite proxy→gateway 链路:`/api/v1/billing/consume`、`/api/v1/stats/profit/users` → 401(未带 token,网关正常应答);
|
||||
- web 镜像:`make web-image` 构建实测(结果见 CHANGELOG 行;拉取慢=宿主带宽已知特性)。
|
||||
|
||||
## 四、受阻与诚实标注
|
||||
|
||||
- **LCP<2s 真浏览器实测不可执行**:本机 chromium(playwright/headless shell)newPage 即 Target crashed(3 轮:默认/--disable-gpu/+GLIBC_TUNABLES 关 AVX/--no-sandbox 全崩;dmesg 无内核记录=渲染进程层崩溃)。宿主 CPU 指令缺陷为已知系统级事实(techContext)。**以产物口径代替判定**:首屏传输=entry ~19KB+两 vendor ~404KB(gzip)+css,局域网/光纤下 LCP<2s 结构性成立;S5 压测泳道若 chromium 可用则补实测。
|
||||
- **kind 未装**(老机 go-sms-dev 集群未随迁):deploy/k8s/web.yaml 为备档,S5 灰度实际 apply+kind load 摆渡镜像。
|
||||
|
||||
## 五、遗留移交(不阻塞 S4)
|
||||
|
||||
1. element-plus 按需引入(裸 1.09MB 的根治项,涉及全站组件注册改造)=二期优化;
|
||||
2. Playwright 截图矩阵/e2e 本机不可跑(chromium 崩),回归依赖 vitest+真机走查;
|
||||
3. S2 用户本人走查补正(D2~D5 脚本 27 PASS 已备)与本单页面走查(浏览器 :5173 → 统计结算/利润分析)一并待用户执行。
|
||||
|
||||
## 六、验收结论
|
||||
|
||||
**S3 泳道通过(出口:LCP/单包口径达标+走查归档自动化矩阵+CI 镜像化落地;两项环境受阻如实标注)。** 下一步=S4(C1/C2+CH 冷热+明细分区)。
|
||||
@@ -0,0 +1,50 @@
|
||||
# S4 验收单 D1/D2:C1 回调推送引擎 + C2 上行自动拉黑(2026-11-16)
|
||||
|
||||
> 泳道:S4 C1/C2+冷热(3 计划日)第 1-2 日;验收项「死信可查 / 拒收即拉黑」✅(冷查<1s 属 D3 未验)。
|
||||
|
||||
## 一、交付范围
|
||||
|
||||
### C1 HTTP 回调推送引擎(需求 2.5 / 实施计划 5.8)
|
||||
|
||||
| 组件 | 文件 | 说明 |
|
||||
|---|---|---|
|
||||
| 队列表 | `entity/callback.go` + 迁移 `v21_callback_domain` | callback_task(状态机 0 待推送/1 已送达/2 死信;attempt/next_retry_at/last_code/last_err;idx_cb_status_retry 扫描索引);casbin 种 ops callback:read/write |
|
||||
| 入队服务 | `service/callback/callback.go` | EnqueueReport(终态首锁后)/EnqueueUplink(上行消费后);URL 门控=account_config.report_url/uplink_url(60s TTL 内存缓存,未配置零任务);Resend(死信 CAS 重派) |
|
||||
| 推送引擎 | `service/callback/worker.go` | PushWorker 2s 扫描/批 100/并发 8/单请求 5s 超时;2xx→sent;失败 2^attempt 秒指数退避(封顶 10min);10 次耗尽→dead;UPDATE CAS 防并发竞态 |
|
||||
| 挂点 | `report/consumer.go` + `report/pair.go` | 与 CMPP Deliver 同点(finalizeDetail 首锁后,事务提交后动作,与 bill.Confirm 同构);手动配对终态同推 |
|
||||
| 挂点 | `uplink/consumer.go` | 匹配回填→拉黑→推送(拉黑不影响推送,需求原文) |
|
||||
| 管理端点 | `api/http/v1/callback_handler.go` | GET /ops/callback(列表+状态计数面板)/ POST /ops/callback/:id/resend;openapi v1.4.0 |
|
||||
| 管理页 | `web/src/views/ops/callback/index.vue` + `api/callback.ts` | 状态面板(待推送/已送达/死信存量)+筛选+载荷展开+死信重派;路由 ops-callback(admin/ops,系统管理组) |
|
||||
| 观测 | metrics | go_sms_callback_push_total{type,result=sent/retry/dead} + go_sms_callback_blacklist_total |
|
||||
| 装配 | `cmd/consumer`(引擎+双挂点)/ `cmd/gateway`(PairService+管理面) | — |
|
||||
|
||||
### C2 上行自动拉黑(需求 2.5 / 实施计划 5.7)
|
||||
|
||||
| 组件 | 说明 |
|
||||
|---|---|
|
||||
| `uplink/blacklist.go` | AutoBlacklister:词表等值匹配(trim+lower,默认 R/TD/退订/拒收)——防「我要退订它」类正常回复误伤;SystemConfig `uplink.blacklist.keywords` 逗号分隔可配,60s 刷新 |
|
||||
| 落库 | policy_blacklist INSERT OnConflict DoNothing(**source=2 上行拒收,账号级=归属账号**,operator=uplink-auto;幂等锚 uk_mobile_account) |
|
||||
| 即时生效 | interceptor.AddBlacklistCache → Redis Set `gosms:blacklist:local`(拦截链一级缓存,下一单即拦) |
|
||||
|
||||
## 二、真机全链冒烟(dev MySQL/Kafka/Redis,2026-11-16 11:15)
|
||||
|
||||
1. **回执终态→HTTP 回调**:种子 detail(73021000101,status=1)+seg(cbprobe-m1)→ 投 sms-report DELIVRD → 明细 2(SUCCESS)→ callback_task#2 type=1 → PushWorker 推送 → 接收端收到 `/report {"type":"report","detail_id":73021000101,…,"status":2,"stat":"SUCCESS"}`,status=1/last_code=200 ✅
|
||||
2. **上行→拉黑+推送**:投 sms-up(content="R",account=90001)→ policy_blacklist(mobile=13877776666,source=2,operator=uplink-auto)✅ + Redis SISMEMBER=1 ✅ + 接收端收到 `/uplink {…,"content":"R"}` ✅(**拉黑不影响推送实证**)
|
||||
3. **死信→重派**:坏 URL 任务 attempt=9 → 一轮失败 attempt=10 → **status=2 dead**(last_err=connection refused)✅ → `POST /ops/callback/3/resend` ISC:000 → URL 修复后重投 → status=1/sent/200 ✅
|
||||
4. **管理面**:GET /ops/callback?status=2 → list 含死信行 + status_counts={dead:1,sent:2} ✅(非侵入 JWT,走查脚本同法)
|
||||
5. **metrics**::8082 go_sms_callback_push_total{result="sent",type="report"}=1 / go_sms_callback_blacklist_total=1 ✅
|
||||
6. 冒烟数据全清(callback_task/policy_blacklist/detail/seg 删除;account_config URL 置空);一次性探针 cmd/cbprobe 用后即删。
|
||||
|
||||
## 三、门禁(七件套)
|
||||
|
||||
- 后端:build ✓ / vet ✓ / **-race 40 包全绿** / golangci-lint 0 issues / 迁移测试末七窗口 v15..v21 两遍幂等+v21 断言;dev 库实跑 v21(callback_task 表+casbin 两策略核验)。
|
||||
- 前端:eslint 0 警 / vue-tsc 0 / **vitest 148 全绿**(role-menu-matrix +ops-callback;guard 菜单 32→33 显式更)/ build ✓。
|
||||
- 单测增量:callback 6 件(入队门控/载荷/状态机全链 500→退避→死信→重派→送达/并发批)+ uplink 5 件(词表/SystemConfig 覆盖/幂等+缓存/消费链挂点)+ report 2 件(首锁入队一次/nil 零开销)。
|
||||
|
||||
## 四、口径与假设(诚实标注)
|
||||
|
||||
1. **载荷无签名**:POST JSON 不带签名头(需求未定义);客户侧回调地址建议内网/HTTPS。后续可加 `X-Gosms-Signature=md5(api_secret+body)`(挂账)。
|
||||
2. **拦截终态(受理期 ISC:xxx 拒收)暂不入 HTTP 回调**:CMPP 侧经 blockedDeliverAdapter 已推;HTTP 侧待用户反馈需求后再挂(范围控制)。
|
||||
3. **多实例部署**:PushWorker 单实例语义;多实例重复推送容忍(at-least-once,CAS 保证状态机,客户端幂等兜底)——与 CMPP Deliver 同语义。
|
||||
4. **退避参数**:2s 扫描/2^n 秒退避(封顶 10min)/10 次(累计 ~17min)为保守起步;S5 压测期重标。
|
||||
5. 浏览器页面走查(回调管理页)留待用户本人(:5173,admin/ops 可见「系统管理→回调管理」)。
|
||||
@@ -0,0 +1,55 @@
|
||||
# S4 验收单 D3:明细按天分区热 5 天 + ClickHouse 冷热切换(2026-11-16)
|
||||
|
||||
> 泳道:S4 第 3 日;验收项「冷查 <1s」✅(16ms,索引冷缓存口径)。S4 三项验收全过,泳道收官。
|
||||
|
||||
## 一、交付范围
|
||||
|
||||
### 明细按天 RANGE(id) 分区(热 5 天)
|
||||
|
||||
| 组件 | 说明 |
|
||||
|---|---|
|
||||
| 迁移 v22 | `ALTER TABLE message_detail PARTITION BY RANGE (id)`——分区键=雪花 id(时间位单调,当天零点边界=`(dayStart-Epoch)<<22` 精确计算;**主键即分区键,唯一键零改造**);分区集=热 5 天+未来 3 天缓冲+pmax 兜底;MySQL only(sqlite 方言守卫) |
|
||||
| `archive/partition.go` | DayFloorSnowflake/PartName/HotFloor 纯函数 + EnsurePartitions 幂等滚动(缺补 ADD/REORGANIZE pmax 拆分;**过期分区不在此删**——归档校验通过才 DROP)+ DropPartition |
|
||||
| 真机分区状态 | p20261112..p20261119 + pmax 共 9 分区;存量 3 行无损迁入 |
|
||||
|
||||
### CH 冷热归档(`internal/service/archive`)
|
||||
|
||||
| 组件 | 说明 |
|
||||
|---|---|
|
||||
| CH 表 | sms_detail_archive(MergeTree,PARTITION BY toDate(created_at),ORDER BY id,18 列与 message_detail 对齐;EnsureCHSchema 幂等) |
|
||||
| ArchiveJob(consumer 进程,1h) | 热窗外逐日:CH DROP 当日分区(重写幂等)→MySQL 分批 2000 读→CH BatchInsert→**count 校验相等才 DROP MySQL 分区/DELETE 兜底**(零丢失方向:不等保留重试) |
|
||||
| 冷查 | GET /record/history(CH 查询;task_no 经 MySQL message_task 转译+批量回填;契约=web record.ts D26);metrics archive_rows/errors |
|
||||
|
||||
### settle-check 热窗感知
|
||||
|
||||
对账窗起点早于热窗下限(月中对账常态)→ `DetailPartial=true`:明细↔汇总比对退化为**两方基线(日汇总↔结算单)**,明细侧不判差异(归档清退≠丢失,历史明细核对走 CH 冷查)——reconcile.go 头注预声明口径落地;settle.Service 时钟注入(nowFn)供测试。
|
||||
|
||||
## 二、真机全链(dev MySQL/CH,2026-11-16 11:44-11:58)
|
||||
|
||||
1. **分区**:v22 迁移→9 分区落地,存量 3 行保留 ✓
|
||||
2. **归档**:造 6 天前(11-10)2 行+今天 1 行→consumer 重启即首轮归档→`archive_day_done rows=2`(DELETE 兜底分支)→MySQL 冷数据 0 行/热数据保留,CH 2 行(content/mobile 完整),metrics archive_rows_total=2 ✓
|
||||
3. **冷查**:`GET /record/history?start_time=2026-11-01&end_time=2026-11-12` → **16ms**(**验收线 <1s 达标**,两次测 7.5/16ms),total=2 行数据全对;mobile 精筛 ✓
|
||||
4. **settle-check**:月中 2026-11 → detail 侧=0(热窗感知生效)+BillMissing 正常列行;all_zero=false 仅因 S2 走查种子残留(account=2 账单 1955 vs 消耗 16050 不齐——用户走查在途数据,不清理)
|
||||
5. 冒烟数据全清(MySQL 3 行+当日消耗行;CH 20261110 分区 DROP→count=0)
|
||||
|
||||
## 三、门禁(七件套)
|
||||
|
||||
- 后端:build ✓ / vet ✓ / golangci-lint **0** / **-race 全包绿**(后台终跑) / 迁移测试 v16..v22 两遍幂等(v22 sqlite 守卫跳过;v15 断言随窗口右移移除)。
|
||||
- 单测:archive 5 件(边界单调/归档全链含幂等/校验不匹配保留/冷查条件)+ settle +1(热窗外两方基线,时钟注入月 20 号稳定复现)。
|
||||
- 前端:无改动(契约 D26 已在;history 页零改动直连新端点)。
|
||||
|
||||
## 四、真机调试教训留痕(5 连坑,全部已修)
|
||||
|
||||
1. **MySQL 8.0 information_schema.PARTITIONS 对非分区表也返回一行**(PARTITION_NAME=NULL)——已分区判据必须 `AND PARTITION_NAME IS NOT NULL`(迁移 v22+existingParts 双处)。
|
||||
2. **go-sql-driver parseTime=True 下 DATE() 结果 Scan 到 []string 带 RFC3339 尾巴**('2026-11-10T00:00:00+08:00')→ Parse 失败静默跳天;改为 DISTINCT 时刻级 created_at Scan []time.Time+Go 侧归一化去重(MySQL/glebarez 双方言兼容)。
|
||||
3. **CH 24.3 无 `DROP PARTITION IF EXISTS`**(语法错)→ 先查 system.parts 存在才 DROP。
|
||||
4. **CH count() 返回 UInt64,Scan *int64 不支持**——dropCHPartition/chCountDay 用 *uint64;QueryHistory 的 count Scan 错误曾被 `_=` 吞掉致 total 恒 0(错误不可吞)。
|
||||
5. **CH 绑定 time.Time 的 UTC 化歧义**——查询时间窗参数预格式化墙钟字符串(与归档写入同口径)。
|
||||
|
||||
## 五、口径与假设(诚实标注)
|
||||
|
||||
1. **归档 job 单实例**(consumer 进程);多实例重复迁出由「CH 分区重写+count 校验」幂等吸收。
|
||||
2. **CH 无备份/TTL**(dev 口径);生产建议 CH 副本+冷分区 S3 挂载(S5 运维项)。
|
||||
3. **history 导出(/record/history/export 与下载中心)挂账 D29/P2**——D26 契约先查后导,异步导出走 MinIO/RustFS 下载中心(S5+ 择机)。
|
||||
4. **pmax 兜底分区语义**:EnsurePartitions 挂掉期间数据落入 pmax,恢复后 REORGANIZE 拆分(运维 runbook 项)。
|
||||
5. 明细写入侧零改动(雪花 id 天然落在当日分区);watchdog/明细页查询语义不变(分区对 SQL 透明)。
|
||||
Executable
+238
@@ -0,0 +1,238 @@
|
||||
#!/usr/bin/env bash
|
||||
# s2-walkthrough.sh S2 用户人工走查脚本(D2/D3 2026-11-14;D4/D5+收官 2026-11-15)
|
||||
#
|
||||
# 用途:把 S2 各日验收从「AI 自证」补正为「用户本人走查」(七件套·本人走查项)。
|
||||
# D2 消耗日汇总 / D3 充值审核流 / D4 出账+阶梯+结账+导出 / D5 利润+三方对账
|
||||
# (D4/D5 用 2026-10 空月,与真实当月数据隔离;全程可还原,跑完自动清理)。
|
||||
# 前置:① gateway 在跑(:8080,make gw-start,须含 D5 端点的二进制)
|
||||
# ② sg docker 可用(dev MySQL 13306) ③ go 工具链(构建 settle-check)
|
||||
# 用法:bash docs/acceptance/scripts/s2-walkthrough.sh 2>&1 | tee /tmp/s2-walk.log
|
||||
# 走查后将 /tmp/s2-walk.log 全文交回,回填 s2-d2~d5.md/s2-closeout.md。
|
||||
set -u
|
||||
GW=http://127.0.0.1:8080/api/v1
|
||||
ROOT="$(cd "$(dirname "$0")/../../.." && pwd)"
|
||||
step() { echo; echo "===== $1 ====="; }
|
||||
pass() { echo "[PASS] $1"; }
|
||||
fail() { echo "[FAIL] $1 (预期 ${2:-?},见上行输出)"; }
|
||||
# mysql_pipe:stdin SQL → dev MySQL(多条语句稳态管道;-- 注释行自动忽略)。
|
||||
mysql_pipe() { sg docker -c 'docker exec -i go-sms-mysql mysql -uroot -proot123 gosms' "$@"; }
|
||||
|
||||
# ---------- 步骤 0:签临时 JWT(dev secret,账号 1=admin) ----------
|
||||
step "步骤0 签发临时 token(非侵入)"
|
||||
TOK=$(python3 - <<'EOF'
|
||||
import hmac, hashlib, base64, json, time, uuid
|
||||
def b64(d): return base64.urlsafe_b64encode(d).rstrip(b'=')
|
||||
now = int(time.time())
|
||||
h = b64(json.dumps({'alg':'HS256','typ':'JWT'}).encode())
|
||||
p = b64(json.dumps({'account_id':1,'username':'admin','roles':['admin'],
|
||||
'permissions':[],'jti':uuid.uuid4().hex,'iss':'go-sms','sub':'1',
|
||||
'exp':now+3600,'iat':now}).encode())
|
||||
s = b64(hmac.new(b'dev-go-sms-secret', h+b'.'+p, hashlib.sha256).digest())
|
||||
print((h+b'.'+p+b'.'+s).decode())
|
||||
EOF
|
||||
)
|
||||
AUTH="Authorization: Bearer $TOK"
|
||||
curl -s -m 3 -H "$AUTH" "$GW/billing/consume" | grep -q ISC:000 \
|
||||
&& pass "token 有效(列表 200)" || fail "token 无效"
|
||||
|
||||
# ---------- D2 走查:消耗日汇总 ----------
|
||||
step "D2-1 种两条测试明细(账号1 成功+未知,通道1)"
|
||||
sg docker -c 'docker exec go-sms-mysql mysql -uroot -proot123 gosms -e "\
|
||||
INSERT INTO message_detail (id,task_id,account_id,channel_id,mobile,content,status,business,fee,created_at) VALUES \
|
||||
(7302401110000000001,810001,1,1,\"13000000001\",\"s2-walk\",2,\"sms\",1.5,NOW()),\
|
||||
(7302401110000000002,810002,1,1,\"13000000002\",\"s2-walk\",4,\"sms\",2.5,NOW());"' >/dev/null 2>&1 \
|
||||
&& pass "明细 2 条已插入" || fail "明细插入"
|
||||
|
||||
step "D2-2 手动触发今日汇总(应 account_rows=1/channel_rows=1)"
|
||||
R=$(curl -s -m 5 -X POST -H "$AUTH" -H 'Content-Type: application/json' \
|
||||
-d "{\"date\":\"$(date +%F)\"}" "$GW/ops/settle/aggregate")
|
||||
echo "$R"
|
||||
echo "$R" | grep -q '"account_rows":1' && echo "$R" | grep -q '"channel_rows":1' \
|
||||
&& pass "落账行数 1/1" || fail "落账行数"
|
||||
|
||||
step "D2-3 查询消耗(admin 应见账号1:submit2/succ1/unk1/fee4.0)"
|
||||
R=$(curl -s -m 5 -H "$AUTH" "$GW/billing/consume?account_id=1")
|
||||
echo "$R"
|
||||
echo "$R" | grep -q '"success_count":1' && echo "$R" | grep -q '"unknown_count":1' \
|
||||
&& echo "$R" | grep -q '"fee_total":4' && pass "计数口径正确" || fail "计数口径"
|
||||
|
||||
step "D2-4 customer 视角收窄(以账号1 冒充 customer 应仍只见自己)"
|
||||
TOKC=$(python3 - <<'EOF'
|
||||
import hmac, hashlib, base64, json, time, uuid
|
||||
def b64(d): return base64.urlsafe_b64encode(d).rstrip(b'=')
|
||||
now = int(time.time())
|
||||
h = b64(json.dumps({'alg':'HS256','typ':'JWT'}).encode())
|
||||
p = b64(json.dumps({'account_id':1,'username':'admin','roles':['customer'],
|
||||
'permissions':[],'jti':uuid.uuid4().hex,'iss':'go-sms','sub':'1',
|
||||
'exp':now+3600,'iat':now}).encode())
|
||||
s = b64(hmac.new(b'dev-go-sms-secret', h+b'.'+p, hashlib.sha256).digest())
|
||||
print((h+b'.'+p+b'.'+s).decode())
|
||||
EOF
|
||||
)
|
||||
R=$(curl -s -m 5 -H "Authorization: Bearer $TOKC" "$GW/billing/consume?account_id=999")
|
||||
echo "$R"
|
||||
echo "$R" | grep -q '"total":1' && pass "customer 越权带参被覆盖收窄(total=1)" || fail "customer 收窄"
|
||||
|
||||
step "D2-5 状态迁移收敛(未知→成功,重刷后 unknown 0/succ 2)"
|
||||
sg docker -c 'docker exec go-sms-mysql mysql -uroot -proot123 gosms -e "\
|
||||
UPDATE message_detail SET status=2 WHERE id=7302401110000000002;"' >/dev/null 2>&1
|
||||
curl -s -m 5 -X POST -H "$AUTH" -H 'Content-Type: application/json' \
|
||||
-d "{\"date\":\"$(date +%F)\"}" "$GW/ops/settle/aggregate" >/dev/null
|
||||
R=$(curl -s -m 5 -H "$AUTH" "$GW/billing/consume?account_id=1")
|
||||
echo "$R"
|
||||
echo "$R" | grep -q '"success_count":2' && echo "$R" | grep -q '"unknown_count":0' \
|
||||
&& pass "滚动重刷收敛(72h 回执窗语义)" || fail "收敛"
|
||||
|
||||
# ---------- D3 走查:充值订单 ----------
|
||||
step "D3-1 创建订单(count=300,不动余额)"
|
||||
B0=$(sg docker -c 'docker exec go-sms-mysql mysql -uroot -proot123 gosms -N -e "SELECT balance FROM account_user WHERE id=1;"' 2>/dev/null)
|
||||
R=$(curl -s -m 5 -H "$AUTH" -H 'Content-Type: application/json' \
|
||||
-d '{"account_id":1,"count":300,"amount":30.0,"pay_type":"bank","remark":"s2-walk"}' "$GW/billing/orders")
|
||||
echo "$R"
|
||||
ON=$(echo "$R" | grep -o '"order_no":"[^"]*"' | cut -d'"' -f4)
|
||||
OID=$(echo "$R" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)
|
||||
B1=$(sg docker -c 'docker exec go-sms-mysql mysql -uroot -proot123 gosms -N -e "SELECT balance FROM account_user WHERE id=1;"' 2>/dev/null)
|
||||
[ "$B0" = "$B1" ] && echo "$R" | grep -q '"pay_status":0' \
|
||||
&& pass "创建后 pay_status=0 且余额未动($B0)" || fail "创建不动余额"
|
||||
|
||||
step "D3-2 confirm(余额应 +300)"
|
||||
R=$(curl -s -m 5 -X POST -H "$AUTH" "$GW/billing/orders/$OID/confirm")
|
||||
echo "$R"
|
||||
echo "$R" | grep -q "\"balance\":$((B0+300))" && pass "confirm 余额 $B0→$((B0+300))" || fail "confirm 余额"
|
||||
|
||||
step "D3-3 重复 confirm(应 409 ISC:039)"
|
||||
R=$(curl -s -w '\nHTTP=%{http_code}' -m 5 -X POST -H "$AUTH" "$GW/billing/orders/$OID/confirm")
|
||||
echo "$R"
|
||||
echo "$R" | grep -q 'HTTP=409' && echo "$R" | grep -q 'ISC:039' && pass "409+ISC:039" || fail "重复 confirm"
|
||||
|
||||
step "D3-4 真库核验流水(ref_id=order_no 双通道锚点)"
|
||||
sg docker -c "docker exec go-sms-mysql mysql -uroot -proot123 gosms -e \
|
||||
\"SELECT biz_type,amount,balance_after,ref_id FROM account_balance_log WHERE ref_id='$ON';\"" 2>/dev/null
|
||||
|
||||
step "D3-5 审计留痕(module=billing)"
|
||||
sg docker -c 'docker exec go-sms-mysql mysql -uroot -proot123 gosms -e \
|
||||
"SELECT module,action FROM system_operate_log WHERE module=\"billing\" ORDER BY id DESC LIMIT 3;"' 2>/dev/null
|
||||
|
||||
# ---------- D4 走查:出账+阶梯+结账+导出(2026-10 空月) ----------
|
||||
step "D4-1 种出账链数据(通道/上家 9101,账号1 价0.10,通道价0.05,明细2条 2026-10-15)"
|
||||
printf '%s\n' \
|
||||
"INSERT INTO channel_upstream (id,name,contact,status,created_at,updated_at) VALUES (9101,'s2-walk-up','walk',1,NOW(),NOW());" \
|
||||
"INSERT INTO channel_channel (id,name,code,protocol,upstream_id,status,billing_mode,created_at,updated_at) VALUES (9101,'s2-walk-ch','S2WALK','http',9101,1,'success',NOW(),NOW());" \
|
||||
"INSERT INTO account_price (account_id,business,price,effective_at,created_at) VALUES (1,'sms',0.10,'2026-10-01 00:00:00',NOW());" \
|
||||
"INSERT INTO channel_price (channel_id,price,effective_at,created_at) VALUES (9101,0.05,'2026-10-01 00:00:00',NOW());" \
|
||||
"INSERT INTO message_detail (id,task_id,account_id,channel_id,mobile,content,status,business,fee,created_at) VALUES" \
|
||||
"(7302411150000000001,820101,1,9101,'13000000001','s2walk',2,'sms',1,'2026-10-15 12:00:00')," \
|
||||
"(7302411150000000002,820102,1,9101,'13000000002','s2walk',2,'sms',1,'2026-10-15 12:00:00');" \
|
||||
| mysql_pipe >/dev/null 2>&1 && pass "种子数据已插入" || fail "种子插入"
|
||||
|
||||
step "D4-2 回补 2026-10-15 汇总(应 account_rows=1/channel_rows=1)"
|
||||
R=$(curl -s -m 5 -X POST -H "$AUTH" -H 'Content-Type: application/json' \
|
||||
-d '{"date":"2026-10-15"}' "$GW/ops/settle/aggregate")
|
||||
echo "$R"
|
||||
echo "$R" | grep -q '"account_rows":1' && echo "$R" | grep -q '"channel_rows":1' \
|
||||
&& pass "落账 1/1" || fail "落账行数"
|
||||
|
||||
step "D4-3 生成下家账单(时间线价 2×0.10=0.2)"
|
||||
R=$(curl -s -m 5 -X POST -H "$AUTH" -H 'Content-Type: application/json' \
|
||||
-d '{"period":"2026-10"}' "$GW/settle/customer/generate")
|
||||
echo "$R"
|
||||
echo "$R" | grep -q '"generated":1' && pass "生成 1 张" || fail "生成"
|
||||
R=$(curl -s -m 5 -H "$AUTH" "$GW/settle/customer?period=2026-10")
|
||||
echo "$R"
|
||||
BID=$(echo "$R" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)
|
||||
echo "$R" | grep -q '"amount":0.2' \
|
||||
&& pass "时间线价金额 0.2(bill id=$BID)" || fail "金额 0.2"
|
||||
|
||||
step "D4-4 配阶梯(0 档 0.05)重算 → 整月覆写 0.1(Q2)"
|
||||
R=$(curl -s -m 5 -X POST -H "$AUTH" -H 'Content-Type: application/json' \
|
||||
-d '{"owner_type":"account","owner_id":1,"tier_from":0,"price":0.05}' "$GW/settle/ladder")
|
||||
echo "$R"
|
||||
R=$(curl -s -m 5 -X POST -H "$AUTH" -H 'Content-Type: application/json' \
|
||||
-d '{"period":"2026-10"}' "$GW/settle/customer/generate")
|
||||
echo "$R"
|
||||
echo "$R" | grep -q '"recalculated":1' && pass "重算 1 张" || fail "重算"
|
||||
curl -s -m 5 -H "$AUTH" "$GW/settle/customer?period=2026-10" | grep -q '"amount":0.1' \
|
||||
&& pass "阶梯覆写金额 0.1" || fail "覆写金额"
|
||||
|
||||
step "D4-5 确认→结账(SettleRecord direction=1/amount=0.1)"
|
||||
curl -s -m 5 -X POST -H "$AUTH" "$GW/settle/customer/$BID/confirm" >/dev/null
|
||||
R=$(curl -s -m 5 -X POST -H "$AUTH" -H 'Content-Type: application/json' \
|
||||
-d '{"pay_type":"bank"}' "$GW/settle/customer/$BID/settle")
|
||||
echo "$R"
|
||||
echo "$R" | grep -q '"direction":1' && echo "$R" | grep -q '"amount":0.1' \
|
||||
&& pass "结账 SettleRecord 0.1" || fail "结账"
|
||||
R=$(curl -s -m 5 -X POST -H "$AUTH" -H 'Content-Type: application/json' \
|
||||
-d '{"period":"2026-10"}' "$GW/settle/customer/generate")
|
||||
echo "$R" | grep -q '"skipped":1' && pass "已结拒重算 skipped=1" || fail "终态幂等"
|
||||
|
||||
step "D4-6 上家出账(US202610-9101,count=2,amount=2×0.05=0.1)"
|
||||
R=$(curl -s -m 5 -X POST -H "$AUTH" -H 'Content-Type: application/json' \
|
||||
-d '{"period":"2026-10"}' "$GW/settle/upstream/generate")
|
||||
echo "$R"
|
||||
R=$(curl -s -m 5 -H "$AUTH" "$GW/settle/upstream?period=2026-10")
|
||||
echo "$R"
|
||||
echo "$R" | grep -q '"success_count":2' && echo "$R" | grep -q '"amount":0.1' \
|
||||
&& pass "上家账单 2/0.1" || fail "上家账单"
|
||||
|
||||
step "D4-7 导出下家账单 XLSX(PK 魔数,>1000 字节)"
|
||||
curl -s -m 10 -H "$AUTH" -o /tmp/s2-walk-bill.xlsx "$GW/settle/customer/$BID/export"
|
||||
SZ=$(wc -c < /tmp/s2-walk-bill.xlsx 2>/dev/null || echo 0)
|
||||
head -c 2 /tmp/s2-walk-bill.xlsx | grep -q 'PK' && [ "$SZ" -gt 1000 ] \
|
||||
&& pass "真 XLSX ${SZ}B" || fail "导出($SZ B)"
|
||||
|
||||
# ---------- D5 走查:利润分析+三方对账 ----------
|
||||
step "D5-1 用户利润(阶梯不进分析:revenue=0.2 时间线价 vs 账单 0.1=口径差异)"
|
||||
R=$(curl -s -m 5 -H "$AUTH" "$GW/stats/profit/users?date_from=2026-10-01&date_to=2026-10-31")
|
||||
echo "$R"
|
||||
echo "$R" | grep -q '"revenue":0.2' && echo "$R" | grep -q '"cost":0.1' \
|
||||
&& echo "$R" | grep -q '"profit":0.1' && pass "rev0.2/cost0.1/profit0.1" || fail "用户利润"
|
||||
|
||||
step "D5-2 通道利润(cost=2×0.05;revenue 分摊=全平台收入×2/2)"
|
||||
R=$(curl -s -m 5 -H "$AUTH" "$GW/stats/profit/channels?date_from=2026-10-01&date_to=2026-10-31")
|
||||
echo "$R"
|
||||
echo "$R" | grep -q '"channel_id":9101' && echo "$R" | grep -q '"profit":0.1' \
|
||||
&& pass "通道利润 0.1" || fail "通道利润"
|
||||
|
||||
step "D5-3 customer 访问利润端点应 403"
|
||||
R=$(curl -s -m 5 -H "Authorization: Bearer $TOKC" "$GW/stats/profit/users")
|
||||
echo "$R"
|
||||
echo "$R" | grep -q 'ISC:403' && pass "customer 403" || fail "利润权限门"
|
||||
|
||||
step "D5-4 settle-check 三方对账(all_zero=true,退出码 0)"
|
||||
(cd "$ROOT" && go build -o /tmp/settle-check-walk ./cmd/reconcile/settle-check) \
|
||||
&& pass "settle-check 已构建" || fail "构建 settle-check"
|
||||
(cd "$ROOT" && /tmp/settle-check-walk -period 2026-10 2>/dev/null; echo "EXIT=$?") | tee /tmp/s2-walk-reconcile.log
|
||||
grep -q 'all_zero=true' /tmp/s2-walk-reconcile.log && grep -q 'EXIT=0' /tmp/s2-walk-reconcile.log \
|
||||
&& pass "三方对账 diff=0(S2 出口判据)" || fail "三方对账"
|
||||
|
||||
# ---------- 清理还原 ----------
|
||||
step "清理(明细/汇总行/订单/流水还原;审计行保留为走查证据)"
|
||||
# printf 双引号=外层展开 $ON/$B0(原 -e 单引号写法 $B0 内层展开为空致 SQL 语法错,
|
||||
# 2026-11-15 D5 收官全脚本实跑暴露并修复);空值兜底防半程重跑。
|
||||
printf '%s\n' \
|
||||
"DELETE FROM message_detail WHERE id IN (7302401110000000001,7302401110000000002);" \
|
||||
"DELETE FROM billing_consume_log WHERE stat_date=CURDATE();" \
|
||||
"DELETE FROM stats_channel_daily WHERE stat_date=CURDATE();" \
|
||||
"DELETE FROM billing_order WHERE order_no='${ON:-}';" \
|
||||
"DELETE FROM account_balance_log WHERE ref_id='${ON:-}';" \
|
||||
"UPDATE account_user SET balance=${B0:-0} WHERE id=1;" \
|
||||
| mysql_pipe >/dev/null 2>&1 \
|
||||
&& pass "数据已还原" || fail "清理"
|
||||
|
||||
step "清理 D4/D5(2026-10 走查数据全量还原)"
|
||||
printf '%s\n' \
|
||||
"DELETE FROM message_detail WHERE id IN (7302411150000000001,7302411150000000002);" \
|
||||
"DELETE FROM billing_consume_log WHERE stat_date='2026-10-15';" \
|
||||
"DELETE FROM stats_channel_daily WHERE stat_date='2026-10-15';" \
|
||||
"DELETE FROM account_price WHERE account_id=1 AND effective_at='2026-10-01';" \
|
||||
"DELETE FROM channel_price WHERE channel_id=9101;" \
|
||||
"DELETE FROM settle_ladder WHERE owner_type='account' AND owner_id=1 AND tier_from=0;" \
|
||||
"DELETE FROM settle_customer WHERE period='2026-10';" \
|
||||
"DELETE FROM settle_upstream WHERE period='2026-10';" \
|
||||
"DELETE FROM settle_record WHERE settle_no IN ('SC202610-1','US202610-9101');" \
|
||||
"DELETE FROM channel_channel WHERE id=9101 AND code='S2WALK';" \
|
||||
"DELETE FROM channel_upstream WHERE id=9101 AND name='s2-walk-up';" \
|
||||
| mysql_pipe >/dev/null 2>&1 && pass "D4/D5 数据已还原" || fail "D4/D5 清理"
|
||||
|
||||
echo
|
||||
echo "===== 走查完成:以上 [PASS] 全绿即 S2 全域(D2~D5)用户走查通过;任一 [FAIL] 请连同整份日志交回 ====="
|
||||
@@ -0,0 +1,52 @@
|
||||
# S2 结算域·表设计评审纪要(D1,2026-11-14)
|
||||
|
||||
> 评审输入:《Go-sms-重建计划.md》S2 行(5 工作日,D 层结算域后端全量,出口=结算单测+对账差异=0)/
|
||||
> 《Go-sms-需求分析.md》§2.6 计费与结算(下家结算·上家结算·消耗日志·充值日志·利润分析,P1)/
|
||||
> 《Go-sms-重建设计规范.md》性能三律与幂等三锚 / 代码实证(结算 5 表实体自 v0 已建且业务代码零使用、
|
||||
> StatsUserDaily/StatsChannelDaily 同为零写入方空壳、明细 Fee=片数非金额、v16 已建 idx_detail_channel_created)。
|
||||
|
||||
## 一、六项裁决
|
||||
|
||||
| # | 决策点 | 裁决 | 依据 |
|
||||
|---|---|---|---|
|
||||
| **R1** | 计价模型 | **出账时刻计价**:明细 Fee 保持片数语义(send 域零改动);结算金额=Σ(日成功条数×生效单价),按 AccountPrice/ChannelPrice 时间线(EffectiveAt≤当日取最近)+阶梯档推导 | 用户 2026-11-14 拍板。与需求「分时单价与结算联动」一致;不触碰已收官域;调价可追溯;Fee≡片数口径(Day70)不动摇 |
|
||||
| **R2** | 消耗事实源 | **billing_consume_log(账号×业务×天)+ stats_channel_daily(通道×天)为事实源;stats_user_daily 废弃**(v20 DROP,实体删除,All() 移除) | 两表同粒度双空壳;单一事实源纪律,双写必漂移;表名即域归属 |
|
||||
| **R3** | 充值双通道 | **保留** `/accounts/:id/recharge` 即时到账(语义=已确认付款快捷入账)+ **新增** BillingOrder 审核流(0待→1到账/2失败;confirm=状态 CAS+余额自增 count+流水 ref_id=order_no 同事务) | 兼容存量 API 与权限测试;「付款审核确认」为需求 §2.6 明示项 |
|
||||
| **R4** | 返点模型 | account_user.rebate_rate decimal(5,4)(v20 加列;小数表示 0.1200=12%,0=无返点);返点额=**直线下级**当月结算金额×rate;**历史不重算**(换上级只影响未来) | 树+Path 基座已备;先落地单级分成,层级分成留扩展 |
|
||||
| **R5** | 阶梯载体 | 新表 **settle_ladder**(owner_type=account/upstream × owner_id × tier_from 唯一;tier_from=档位下限含,0=首档;出账按月成功总量落档覆写时间线价) | 独立表可审计可单测(「阶梯结算单测」为显式验收项);不塞 JSON |
|
||||
| **R6** | 单位口径 | 余额/预扣/明细 Fee/fee_total=**条**;BillingOrder.Count=条、Amount=元;结算单/利润/单价=**元**;条→元换算**只发生在出账引擎**(settle/price.go)一处 | 消除 balance(条)与 decimal(元)的隐性错位;单点换算单点审计 |
|
||||
|
||||
## 二、v20_settle_domain 迁移范围(已实现)
|
||||
|
||||
1. billing_consume_log 补 `unknown_count INT`(上家 success_unknown 口径依赖)+ `updated_at DATETIME`(UPSERT 重跑痕迹);
|
||||
2. account_user 补 `rebate_rate DECIMAL(5,4)`;
|
||||
3. DROP stats_user_daily(空库路径 v0 已不再建它,DROP IF EXISTS 双方言幂等);
|
||||
4. 新表 settle_ladder;
|
||||
5. casbin 补策略:ops settle:write / customer settle:read(逐条 NOT EXISTS 守卫;单块重放路径 HasTable 守卫)。
|
||||
|
||||
加列走显式 SQL+HasColumn 守卫、空库走 HasTable 守卫 AutoMigrate 整表(v16/v17 教训:glebarez 下 entity 加字段触发表 diff 重建抹索引)。
|
||||
**不需要**新聚合索引:通道维度日汇总复用 v16 `idx_detail_channel_created(channel_id, created_at)`。
|
||||
**不改** init_schema.sql(Day11 基线参考、权威=migrate 链,v16/v17 演进均未回写,维持惯例)。
|
||||
`stats_channel_daily` 零 schema 变更(unknown_count/cost_total 原有;grain=通道×天,不加 business——通道价格体系本身不分业务,视频走独立通道)。
|
||||
|
||||
## 三、状态机与语义定稿
|
||||
|
||||
- **充值订单**:pay_status 0待确认→1已到账/2失败;confirm 事务=UPDATE ... WHERE pay_status=0(行数=0 即 409)+balance+=count 原子自增+流水(ref_id=order_no);驳回不涉余额。
|
||||
- **结算单(下家/上家同构)**:status 0草稿→1已确认(人工锁账,拒静默重算)→2已结账(写 settle_record,终态);generate 幂等=uk(account|upstream, period)+OnConflict 更新重算,status=2 拒。
|
||||
- **上家计费口径**:按通道 `billing_mode`——success(仅成功)/success_unknown(成功+未知)/all(全部提交);UNKNOWN 72h 窗语义(部分段事实送达)与 success_unknown 口径天然对齐。
|
||||
- **日汇总 job**:T+1 01:05 核算昨日+启动回补 7 天,幂等 UPSERT(明细热窗 5 天→月结必须走日汇总,job 必须在明细出窗前落账——整域地基,D2 最先做)。
|
||||
|
||||
## 四、任务映射(D2-D5)
|
||||
|
||||
| 任务 | 落点 |
|
||||
|---|---|
|
||||
| D2 消耗日汇总引擎 | settle/consume.go+job.go(照 StartStatsRefresher 幂等模式)+/billing/consume+/ops/settle/aggregate+Settle 配置段+job 指标 |
|
||||
| D3 充值审核流 | settle/recharge.go+/billing/orders 族(R3 事务语义) |
|
||||
| D4 出账引擎+阶梯 | settle/price.go(PriceAt+阶梯落档,R1/R6 唯一换算点)+bill.go+/settle 族(生成/确认/结账/导出) |
|
||||
| D5 利润/返点/对账 | profit.go(树聚合,R4)+reconcile.go+cmd/reconcile settle-check(三方 diff=0=S2 出口) |
|
||||
|
||||
## 五、遗留与显式不承诺
|
||||
|
||||
- est_balance(通道估算余额 2h 刷新,需求 §5.3)语义依赖「校正余额」录入通道,本期字段保留不实现,验收单如实标注;
|
||||
- 层级返点(多级分成)、月结自动生成调度(手动 generate 先行)留后续;
|
||||
- 阶梯与时间线价并存时:**有阶梯档则整月单价按落档价覆写**(不做分档混合计价)——单测覆盖边界(恰好落档/跨档/无阶梯回退)。
|
||||
@@ -22,13 +22,17 @@ import (
|
||||
"go-sms/internal/pkg/response"
|
||||
"go-sms/internal/repository/mysql"
|
||||
"go-sms/internal/service/account"
|
||||
"go-sms/internal/service/archive"
|
||||
"go-sms/internal/service/audit"
|
||||
"go-sms/internal/service/auth"
|
||||
"go-sms/internal/service/callback"
|
||||
"go-sms/internal/service/channel"
|
||||
"go-sms/internal/service/interceptor"
|
||||
"go-sms/internal/service/report"
|
||||
"go-sms/internal/service/router"
|
||||
"go-sms/internal/service/send"
|
||||
"go-sms/internal/service/settle"
|
||||
"go-sms/internal/service/sysconfig"
|
||||
"go-sms/internal/service/template"
|
||||
)
|
||||
|
||||
@@ -64,6 +68,20 @@ type Deps struct {
|
||||
// CMPPCfg Day 62:CMPP 服务端配置(系统默认配额+端口;GET api-config 的
|
||||
// cmpp_access 块数据源;nil=CMPP 未启用,响应不带该块)。
|
||||
CMPPCfg *config.CMPPServer
|
||||
// Settle S2:结算域(消耗日汇总查询/手动回补;充值订单/月结/阶梯/利润
|
||||
// 随 D3~D5 追加;nil=不注册,前端 404 优于半可用)。
|
||||
Settle *settle.Service
|
||||
// Callback S4·C1:回调任务管理(死信查询/重派;PushWorker 在 consumer
|
||||
// 进程,gateway 侧仅管理面;nil=不注册)。
|
||||
Callback *callback.Service
|
||||
// Archive S4·D3:明细冷热归档(/record/history 冷查走 ClickHouse;
|
||||
// nil=不注册 history 端点,前端 404 优于半可用)。
|
||||
Archive *archive.Service
|
||||
// SysConfig Issue#3 方案B:系统参数配置(/system/config GET/PUT;
|
||||
// access 系统默认接入码回退的数据源;nil=不注册)。
|
||||
SysConfig *sysconfig.Service
|
||||
// Health Issue#6·P2:系统健康总览(/ops/system/overview;nil=不注册)。
|
||||
Health *HealthDeps
|
||||
}
|
||||
|
||||
// Register 注册 v1 路由组。
|
||||
@@ -140,6 +158,8 @@ func Register(rg *gin.RouterGroup, deps Deps) {
|
||||
protected.POST("/accounts/:id/realname", h.submitRealname)
|
||||
protected.POST("/accounts/:id/realname/review",
|
||||
middleware.RequirePermission(deps.Perm, "account", "write"), h.reviewRealname)
|
||||
// Issue#5·P0-A:账号分时单价时间线(结算/出账售价事实源的配置面;account:write)
|
||||
registerAccountPriceRoutes(protected, deps.Perm, deps.Account)
|
||||
}
|
||||
// Day 27:余额预警配置(本人或 admin)
|
||||
if deps.Warning != nil {
|
||||
@@ -175,6 +195,10 @@ func Register(rg *gin.RouterGroup, deps Deps) {
|
||||
if deps.ErrorCode != nil {
|
||||
registerErrorCodeRoutes(protected, deps.Perm, deps.ErrorCode)
|
||||
}
|
||||
// Issue#3 方案B:系统参数配置(system_config KV 管理页数据源)。
|
||||
if deps.SysConfig != nil {
|
||||
registerSysConfigRoutes(protected, deps.Perm, deps.SysConfig)
|
||||
}
|
||||
// Day 53(runbook §6.4/§6.2 P0):前端错误上报(POST public/GET 排障)+
|
||||
// Alertmanager webhook 接收(AM 推送→日志留痕,告警通道验收抓手)。
|
||||
registerFeErrorRoutes(rg, protected, deps.DB, deps.Perm)
|
||||
@@ -182,13 +206,24 @@ func Register(rg *gin.RouterGroup, deps Deps) {
|
||||
// Day 60(D24-25):record 域查询端点(批次/明细/拦截 CSV/通道日志/明细 v2;
|
||||
// 裸 DB 查询,无服务层;查询类不挂 Casbin,JWT 保护即可)。
|
||||
if deps.DB != nil {
|
||||
registerRecordRoutes(protected, deps.DB)
|
||||
registerRecordRoutes(protected, deps.DB, deps.Archive)
|
||||
}
|
||||
// Day68:运营总览(/home)真实数据——summary 四指标卡+trend 近 N 日趋势
|
||||
// (查询类,JWT 保护即可;口径/角色范围见 stats_handler.go 头注)。
|
||||
if deps.DB != nil {
|
||||
registerStatsRoutes(protected, deps.DB)
|
||||
}
|
||||
// S2:结算域(D2 消耗日汇总两端点;D3~D5 端点随任务追加,见 settle_handler.go)。
|
||||
if deps.Settle != nil {
|
||||
registerSettleRoutes(protected, deps)
|
||||
}
|
||||
if deps.Callback != nil {
|
||||
registerCallbackRoutes(protected, deps)
|
||||
}
|
||||
// Issue#6·P2:系统健康总览(Dashboard 系统健康卡)。
|
||||
if deps.Health != nil {
|
||||
registerOpsRoutes(protected, deps)
|
||||
}
|
||||
// 阶段5收尾(D28):回执重推域四端点(回执分页/重推受理/挂起明细/手动配对;
|
||||
// 服务依赖 Billing/PushSink,装配见 cmd/gateway)。
|
||||
if deps.Pair != nil {
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// account_day29_test.go Day 29:审计查询 API(system:read 门禁)+ 拦截器联动。
|
||||
// account_audit_query_test.go(原 Day29):审计查询 API(system:read 门禁)+ 拦截器联动。
|
||||
package v1
|
||||
|
||||
import (
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// account_day25_test.go Day 25 验收:普通账号访问 /accounts/export 返回 403,
|
||||
// account_export_permission_test.go(原 Day25)验收:普通账号访问 /accounts/export 返回 403,
|
||||
// admin 可正常导出;二级权限授予后即时放行、撤销后收回。
|
||||
package v1
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// account_day28_test.go Day 28:上级切换/标星/团队/实名的 API 权限与闭环。
|
||||
// account_hierarchy_realname_test.go(原 Day28):上级切换/标星/团队/实名的 API 权限与闭环。
|
||||
package v1
|
||||
|
||||
import (
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// account_day30_test.go Day 30:阶段 2 验收全量回归(A-2.1~A-2.13)。
|
||||
// account_phase2_regression_test.go(原 Day30):阶段 2 验收全量回归(A-2.1~A-2.13)。
|
||||
// 以真实 HTTP 请求串联全部端点,等价 Postman 集合;证据映射见 docs/phase2-review.md。
|
||||
package v1
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// account_day26_test.go Day 26 验收:充值二级权限门禁 / 批量 CSV / 余额越权。
|
||||
// account_recharge_permission_test.go(原 Day26)验收:充值二级权限门禁 / 批量 CSV / 余额越权。
|
||||
// 复用 newD25Router(admin=1 通配,customer=2;Register 已含 Day 26 路由)。
|
||||
package v1
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// account_day27_test.go Day 27:预警配置端点(本人/admin 读写、越权 403)。
|
||||
// account_warning_config_test.go(原 Day27):预警配置端点(本人/admin 读写、越权 403)。
|
||||
package v1
|
||||
|
||||
import (
|
||||
@@ -0,0 +1,113 @@
|
||||
// callback_handler.go S4·C1:回调任务管理端点(死信可查/重派)。
|
||||
// 投递引擎(PushWorker)在 consumer 进程,gateway 侧仅管理面;
|
||||
// 端点=GET /ops/callback(列表,状态/类型/账号筛选)+ POST /ops/callback/:id/resend
|
||||
// (死信重派);casbin 策略 v21 所种(ops callback:read/write)。
|
||||
package v1
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go-sms/internal/middleware"
|
||||
"go-sms/internal/model/entity"
|
||||
"go-sms/internal/pkg/errors"
|
||||
"go-sms/internal/pkg/response"
|
||||
"go-sms/internal/service/callback"
|
||||
)
|
||||
|
||||
func registerCallbackRoutes(protected *gin.RouterGroup, deps Deps) {
|
||||
h := &callbackHandler{svc: deps.Callback, db: deps.DB}
|
||||
// 任务列表(含死信;缺省不过滤状态,传 status=0/1/2 精筛)。
|
||||
protected.GET("/ops/callback",
|
||||
middleware.RequirePermission(deps.Perm, "callback", "read"), h.list)
|
||||
// 死信重派(status 2→0;非死信 0 行影响=幂等拒绝)。
|
||||
protected.POST("/ops/callback/:id/resend",
|
||||
middleware.RequirePermission(deps.Perm, "callback", "write"), h.resend)
|
||||
}
|
||||
|
||||
type callbackHandler struct {
|
||||
svc *callback.Service
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// list GET /ops/callback?page=&page_size=&status=&type=&account_id=。
|
||||
func (h *callbackHandler) list(c *gin.Context) {
|
||||
q := h.db.WithContext(c.Request.Context()).Model(&entity.CallbackTask{})
|
||||
if s := c.Query("status"); s != "" {
|
||||
v, err := strconv.Atoi(s)
|
||||
if err != nil || v < 0 || v > 2 {
|
||||
response.ErrorWithMsg(c, errors.InvalidParam, "status 须为 0/1/2")
|
||||
return
|
||||
}
|
||||
q = q.Where("status = ?", int8(v))
|
||||
}
|
||||
if s := c.Query("type"); s != "" {
|
||||
v, err := strconv.Atoi(s)
|
||||
if err != nil || v < 1 || v > 2 {
|
||||
response.ErrorWithMsg(c, errors.InvalidParam, "type 须为 1/2")
|
||||
return
|
||||
}
|
||||
q = q.Where("type = ?", int8(v))
|
||||
}
|
||||
if s := c.Query("account_id"); s != "" {
|
||||
v, err := strconv.ParseInt(s, 10, 64)
|
||||
if err != nil || v <= 0 {
|
||||
response.ErrorWithMsg(c, errors.InvalidParam, "account_id 非法")
|
||||
return
|
||||
}
|
||||
q = q.Where("account_id = ?", v)
|
||||
}
|
||||
page, size := 1, 20
|
||||
if v, err := strconv.Atoi(c.Query("page")); err == nil && v > 0 {
|
||||
page = v
|
||||
}
|
||||
if v, err := strconv.Atoi(c.Query("page_size")); err == nil && v > 0 && v <= 200 {
|
||||
size = v
|
||||
}
|
||||
var total int64
|
||||
if err := q.Count(&total).Error; err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
var rows []entity.CallbackTask
|
||||
if err := q.Order("id DESC").Limit(size).Offset((page - 1) * size).Find(&rows).Error; err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
// 状态计数面板(死信可查验收项:一眼看死信存量)。
|
||||
var statusAgg []struct {
|
||||
Status int8
|
||||
N int64
|
||||
}
|
||||
_ = h.db.WithContext(c.Request.Context()).Model(&entity.CallbackTask{}).
|
||||
Select("status, COUNT(*) AS n").Group("status").Scan(&statusAgg).Error
|
||||
agg := gin.H{}
|
||||
for _, r := range statusAgg {
|
||||
key := map[int8]string{0: "pending", 1: "sent", 2: "dead"}[r.Status]
|
||||
if key != "" {
|
||||
agg[key] = r.N
|
||||
}
|
||||
}
|
||||
response.Success(c, gin.H{"total": total, "list": rows, "status_counts": agg})
|
||||
}
|
||||
|
||||
// resend POST /ops/callback/:id/resend:死信重派(attempt 清零/立即到期)。
|
||||
func (h *callbackHandler) resend(c *gin.Context) {
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
response.ErrorWithMsg(c, errors.InvalidParam, "id 非法")
|
||||
return
|
||||
}
|
||||
n, err := h.svc.Resend(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
if n == 0 {
|
||||
response.Error(c, errors.NotFound.WithMsg("任务不存在或非死信状态"))
|
||||
return
|
||||
}
|
||||
response.Success(c, gin.H{"id": id, "resent": true})
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// channel_day35_test.go Day 35:通道路由——CRUD/测试工具/拦截(channel:write 门禁)。
|
||||
// channel_crud_route_test.go(原 Day35):通道路由——CRUD/测试工具/拦截(channel:write 门禁)。
|
||||
package v1
|
||||
|
||||
import (
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// channel_day39_test.go Day 39:链路状态/回执复核端点(发送真实性 WEB 验收)。
|
||||
// channel_link_receipts_test.go(原 Day39):链路状态/回执复核端点(发送真实性 WEB 验收)。
|
||||
package v1
|
||||
|
||||
import (
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// channel_day40_test.go Day 40:监控快照/WS 升级/通道日志端点(A-3.13/A-3.14 WEB 验收)。
|
||||
// channel_monitor_logs_test.go(原 Day40):监控快照/WS 升级/通道日志端点(A-3.13/A-3.14 WEB 验收)。
|
||||
package v1
|
||||
|
||||
import (
|
||||
@@ -0,0 +1,181 @@
|
||||
// ops_handler.go Issue#6·P2:系统健康总览(GET /ops/system/overview)——Dashboard
|
||||
// 系统健康卡数据源(D10 元信息层,gateway 视角探测):
|
||||
// - gateway 本进程 uptime(装配时刻起算);
|
||||
// - consumer 独立进程无直接探针,以 submit 消费组 lag 间接观测(<10k=up);
|
||||
// - mysql sql.DB.Ping 计时;
|
||||
// - redis go-redis Ping 计时(nil=未启用降级 → down,诚实标注);
|
||||
// - kafka LagReader.Total(与 consumer 同源 lag)。
|
||||
//
|
||||
// 角色门:admin/ops(基础设施信息不放大,group/customer/service 403——与
|
||||
// /billing/orders 平台门同口径;service 属业务角色不见运维面)。
|
||||
package v1
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go-sms/internal/pkg/cache"
|
||||
"go-sms/internal/pkg/response"
|
||||
)
|
||||
|
||||
// HealthDeps 系统健康探测依赖(nil 字段=该子系统探针缺失,状态 unknown)。
|
||||
// Lag 为函数字段便于测试注入(生产闭包包 LagReader;ctx 来自请求)。
|
||||
type HealthDeps struct {
|
||||
DB *gorm.DB
|
||||
Redis *cache.Client
|
||||
Lag func(ctx context.Context) (int64, error)
|
||||
StartTime time.Time
|
||||
HTTPAddr string
|
||||
RedisMode string
|
||||
}
|
||||
|
||||
// subsystemDTO 子系统状态(up/degraded/down/unknown 三态+unknown,拒绝全绿作弊)。
|
||||
type subsystemDTO struct {
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
Detail string `json:"detail"`
|
||||
LatencyMs float64 `json:"latency_ms"`
|
||||
}
|
||||
|
||||
type opsHandler struct{ hd *HealthDeps }
|
||||
|
||||
func registerOpsRoutes(protected *gin.RouterGroup, deps Deps) {
|
||||
if deps.Health == nil {
|
||||
return
|
||||
}
|
||||
h := &opsHandler{hd: deps.Health}
|
||||
protected.GET("/ops/system/overview", h.overview)
|
||||
}
|
||||
|
||||
// opsAdminGate admin/ops 角色门(与 /billing/orders 平台门同口径)。
|
||||
func opsAdminGate(c *gin.Context) bool {
|
||||
claims := statsClaims(c)
|
||||
if claims == nil {
|
||||
return true // 无 claims 仅测试场景,放行
|
||||
}
|
||||
for _, r := range claims.Roles {
|
||||
if r == "admin" || r == "ops" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// overview GET /ops/system/overview:五子系统快照(单次请求全量探测,
|
||||
// 上位机 10s 轮询;各探针失败不互相影响,缺失=unknown)。
|
||||
func (h *opsHandler) overview(c *gin.Context) {
|
||||
if !opsAdminGate(c) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"code": "ISC-403", "msg": "仅管理员可查看系统健康"})
|
||||
return
|
||||
}
|
||||
hd := h.hd
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), 3*time.Second)
|
||||
defer cancel()
|
||||
subs := make([]subsystemDTO, 0, 5)
|
||||
|
||||
// gateway:本进程
|
||||
up := time.Since(hd.StartTime).Round(time.Second)
|
||||
subs = append(subs, subsystemDTO{
|
||||
Name: "gateway", Status: "up",
|
||||
Detail: fmt.Sprintf("UP %s · %s", opsHumane(up), opsOr(hd.HTTPAddr, ":8080")),
|
||||
})
|
||||
|
||||
// lag 采样(consumer/kafka 共源;失败=双 unknown,链路抖动不判死)。
|
||||
var lag int64 = -1
|
||||
var lagErr error
|
||||
if hd.Lag != nil {
|
||||
lag, lagErr = hd.Lag(ctx)
|
||||
}
|
||||
lagStatus := func() string {
|
||||
switch {
|
||||
case hd.Lag == nil || lagErr != nil:
|
||||
return "unknown"
|
||||
case lag >= 10000:
|
||||
return "degraded"
|
||||
default:
|
||||
return "up"
|
||||
}
|
||||
}
|
||||
lagDetail := func(prefix string) string {
|
||||
if hd.Lag == nil {
|
||||
return prefix + "探针未装配"
|
||||
}
|
||||
if lagErr != nil {
|
||||
return prefix + "采样失败"
|
||||
}
|
||||
return fmt.Sprintf("%slag %s", prefix, opsThousand(lag))
|
||||
}
|
||||
subs = append(subs, subsystemDTO{Name: "consumer", Status: lagStatus(), Detail: lagDetail("kafka ")})
|
||||
subs = append(subs, subsystemDTO{Name: "kafka", Status: lagStatus(), Detail: lagDetail("")})
|
||||
|
||||
// mysql
|
||||
if hd.DB != nil {
|
||||
t0 := time.Now()
|
||||
if sdb, err := hd.DB.DB(); err == nil {
|
||||
if perr := sdb.PingContext(ctx); perr != nil {
|
||||
subs = append(subs, subsystemDTO{Name: "mysql", Status: "down", Detail: perr.Error()})
|
||||
} else {
|
||||
subs = append(subs, subsystemDTO{Name: "mysql", Status: "up",
|
||||
Detail: "OK", LatencyMs: opsMs(time.Since(t0))})
|
||||
}
|
||||
} else {
|
||||
subs = append(subs, subsystemDTO{Name: "mysql", Status: "unknown", Detail: "句柄不可用"})
|
||||
}
|
||||
} else {
|
||||
subs = append(subs, subsystemDTO{Name: "mysql", Status: "unknown", Detail: "未装配"})
|
||||
}
|
||||
|
||||
// redis(nil=降级未启用,诚实标 down 而非假绿)
|
||||
if hd.Redis == nil {
|
||||
subs = append(subs, subsystemDTO{Name: "redis", Status: "down", Detail: "未启用(降级态)"})
|
||||
} else {
|
||||
t0 := time.Now()
|
||||
if perr := hd.Redis.Ping(ctx).Err(); perr != nil {
|
||||
subs = append(subs, subsystemDTO{Name: "redis", Status: "down", Detail: perr.Error()})
|
||||
} else {
|
||||
subs = append(subs, subsystemDTO{Name: "redis", Status: "up",
|
||||
Detail: opsOr(hd.RedisMode, "single"), LatencyMs: opsMs(time.Since(t0))})
|
||||
}
|
||||
}
|
||||
|
||||
response.Success(c, gin.H{"uptime_s": int64(time.Since(hd.StartTime).Seconds()), "subsystems": subs})
|
||||
}
|
||||
|
||||
func opsOr(s, def string) string {
|
||||
if strings.TrimSpace(s) == "" {
|
||||
return def
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func opsMs(d time.Duration) float64 { return math.Round(float64(d.Microseconds())/10) / 100 }
|
||||
|
||||
func opsThousand(v int64) string {
|
||||
s := fmt.Sprintf("%d", v)
|
||||
if len(s) <= 3 {
|
||||
return s
|
||||
}
|
||||
var b []byte
|
||||
for i, r := range []byte(s) {
|
||||
if i > 0 && (len(s)-i)%3 == 0 {
|
||||
b = append(b, ',')
|
||||
}
|
||||
b = append(b, r)
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func opsHumane(d time.Duration) string {
|
||||
if d < time.Hour {
|
||||
return d.Round(time.Minute).String()
|
||||
}
|
||||
days := int(d.Hours()) / 24
|
||||
return fmt.Sprintf("%dd%dh", days, int(d.Hours())%24)
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
package v1
|
||||
|
||||
// ops_handler_test.go Issue#6·P2:/stats/biz(两维分布+双口径)+/stats/realtime
|
||||
// (60s 窗口 Top5+名字)+/ops/system/overview(角色门+三态诚实)。sqlite 单测;
|
||||
// LagReader 以函数注入,Kafka/Redis 真链路在 dev 实机冒烟。
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go-sms/internal/middleware"
|
||||
"go-sms/internal/model/entity"
|
||||
)
|
||||
|
||||
// newOpsRouter 种子:今日 task(账号1:sms×2[1 条 business=video]+template×1;
|
||||
// 账号100:sms×1);60s 内 detail 账号1×7+账号100×3(60s 外 2 条不计);
|
||||
// 账号名 1=admin/100=cust100。同时挂 stats 与 ops(Health) 路由。
|
||||
func newOpsRouter(t *testing.T, hd *HealthDeps) (*gin.Engine, *gorm.DB, func(*middleware.Claims)) {
|
||||
t.Helper()
|
||||
gin.SetMode(gin.TestMode)
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s, e := db.DB(); e == nil {
|
||||
s.SetMaxOpenConns(1)
|
||||
}
|
||||
if err := db.AutoMigrate(&entity.MessageTask{}, &entity.MessageDetail{}, &entity.AccountUser{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
now := time.Now()
|
||||
tasks := []entity.MessageTask{
|
||||
{ID: 1, TaskNo: "T1", AccountID: 1, Business: "sms", SendType: 1, CreatedAt: now},
|
||||
{ID: 2, TaskNo: "T2", AccountID: 1, Business: "sms", SendType: 1, CreatedAt: now},
|
||||
{ID: 3, TaskNo: "T3", AccountID: 1, Business: "video", SendType: 1, CreatedAt: now},
|
||||
{ID: 4, TaskNo: "T4", AccountID: 1, Business: "", SendType: 2, CreatedAt: now}, // 空 business→sms 兼容
|
||||
{ID: 5, TaskNo: "T5", AccountID: 100, Business: "sms", SendType: 1, CreatedAt: now},
|
||||
}
|
||||
if err := db.Create(&tasks).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mk := func(id, acc int64, created time.Time) entity.MessageDetail {
|
||||
return entity.MessageDetail{SnowflakeModel: entity.SnowflakeModel{ID: id, CreatedAt: created},
|
||||
TaskID: 1, AccountID: acc, ChannelID: 1, Mobile: "13000001111", Content: "t", Status: 2}
|
||||
}
|
||||
details := []entity.MessageDetail{
|
||||
mk(11, 1, now.Add(-10*time.Second)), mk(12, 1, now.Add(-20*time.Second)),
|
||||
mk(13, 1, now.Add(-30*time.Second)), mk(14, 1, now.Add(-40*time.Second)),
|
||||
mk(15, 1, now.Add(-50*time.Second)), mk(16, 1, now.Add(-55*time.Second)),
|
||||
mk(17, 1, now.Add(-58*time.Second)),
|
||||
mk(21, 100, now.Add(-10*time.Second)), mk(22, 100, now.Add(-20*time.Second)),
|
||||
mk(23, 100, now.Add(-30*time.Second)),
|
||||
mk(31, 1, now.Add(-5*time.Minute)), mk(32, 100, now.Add(-5*time.Minute)), // 窗外不计
|
||||
}
|
||||
if err := db.Create(&details).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
accts := []entity.AccountUser{
|
||||
{Model: entity.Model{ID: 1}, Username: "admin", Status: 1},
|
||||
{Model: entity.Model{ID: 100}, Username: "cust100", Status: 1},
|
||||
}
|
||||
if err := db.Create(&accts).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rg := gin.New()
|
||||
api := rg.Group("/api/v1")
|
||||
var claims *middleware.Claims
|
||||
api.Use(func(c *gin.Context) {
|
||||
if claims != nil {
|
||||
c.Set(middleware.ContextKeyClaims, claims)
|
||||
}
|
||||
})
|
||||
registerStatsRoutes(api, db)
|
||||
if hd != nil {
|
||||
hd.DB = db
|
||||
registerOpsRoutes(api, Deps{Health: hd})
|
||||
}
|
||||
setClaims := func(cl *middleware.Claims) { claims = cl }
|
||||
return rg, db, setClaims
|
||||
}
|
||||
|
||||
func TestStatsBiz_PlatformAndCustomer(t *testing.T) {
|
||||
rg, _, setClaims := newOpsRouter(t, nil)
|
||||
setClaims(&middleware.Claims{AccountID: 1, Roles: []string{"admin"}})
|
||||
w := httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1/stats/biz?days=7", nil))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("code=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
var res struct {
|
||||
Data statsBizDTO `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := map[string]int64{}
|
||||
for _, r := range res.Data.ByBusiness {
|
||||
got[r.Key] = r.Count
|
||||
}
|
||||
// 平台:sms=3(账号1×2+账号100)+video=1;空 business 归一为 sms。
|
||||
if got["sms"] != 4 || got["video"] != 1 {
|
||||
t.Errorf("by_business = %v, 期望 sms=3 video=1", got)
|
||||
}
|
||||
gotT := map[string]int64{}
|
||||
for _, r := range res.Data.BySendType {
|
||||
gotT[r.Key] = r.Count
|
||||
}
|
||||
if gotT["batch"] != 4 || gotT["personal"] != 1 {
|
||||
t.Errorf("by_send_type = %v, 期望 batch=4 personal=1", gotT)
|
||||
}
|
||||
// 客户口径仅自己(账号100:sms×1)。
|
||||
setClaims(&middleware.Claims{AccountID: 100, Roles: []string{"customer"}})
|
||||
w = httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1/stats/biz", nil))
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(res.Data.ByBusiness) != 1 || res.Data.ByBusiness[0].Key != "sms" || res.Data.ByBusiness[0].Count != 1 {
|
||||
t.Errorf("customer by_business = %+v, 期望仅 sms=1", res.Data.ByBusiness)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatsRealtime_TopAccounts(t *testing.T) {
|
||||
rg, _, setClaims := newOpsRouter(t, nil)
|
||||
setClaims(&middleware.Claims{AccountID: 1, Roles: []string{"ops"}})
|
||||
w := httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1/stats/realtime", nil))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("code=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
var res struct {
|
||||
Data statsRealtimeDTO `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 窗内 10 条(账号1×7+账号100×3)/60s ≈ 0.17;Top1=账号1 名 admin。
|
||||
if res.Data.GlobalSubmitPs < 0.1 || res.Data.GlobalSubmitPs > 0.25 {
|
||||
t.Errorf("global_submit_ps = %v, 期望 ≈0.17", res.Data.GlobalSubmitPs)
|
||||
}
|
||||
if len(res.Data.TopAccounts) != 2 || res.Data.TopAccounts[0].AccountID != 1 ||
|
||||
res.Data.TopAccounts[0].Name != "admin" {
|
||||
t.Errorf("top_accounts = %+v, 期望首名 admin(账号1)", res.Data.TopAccounts)
|
||||
}
|
||||
// 客户口径:仅自己(账号100 3 条)。
|
||||
setClaims(&middleware.Claims{AccountID: 100, Roles: []string{"customer"}})
|
||||
w = httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1/stats/realtime", nil))
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if res.Data.GlobalSubmitPs < 0.03 || res.Data.GlobalSubmitPs > 0.07 {
|
||||
t.Errorf("customer global = %v, 期望 ≈0.05", res.Data.GlobalSubmitPs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpsOverview_GatesAndSubsystems(t *testing.T) {
|
||||
hd := &HealthDeps{
|
||||
Redis: nil, // 降级未启用 → down
|
||||
Lag: func(ctx context.Context) (int64, error) { return 1204, nil },
|
||||
StartTime: time.Now().Add(-3 * time.Hour),
|
||||
HTTPAddr: ":8080",
|
||||
}
|
||||
rg, _, setClaims := newOpsRouter(t, hd)
|
||||
setClaims(&middleware.Claims{AccountID: 1, Roles: []string{"admin"}})
|
||||
w := httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1/ops/system/overview", nil))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("code=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
var res struct {
|
||||
Data struct {
|
||||
UptimeS int64 `json:"uptime_s"`
|
||||
Subsystems []subsystemDTO `json:"subsystems"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(res.Data.Subsystems) != 5 {
|
||||
t.Fatalf("subsystems = %d, 期望 5", len(res.Data.Subsystems))
|
||||
}
|
||||
st := map[string]subsystemDTO{}
|
||||
for _, s := range res.Data.Subsystems {
|
||||
st[s.Name] = s
|
||||
}
|
||||
if st["gateway"].Status != "up" || st["mysql"].Status != "up" {
|
||||
t.Errorf("gateway/mysql = %s/%s, 期望 up/up", st["gateway"].Status, st["mysql"].Status)
|
||||
}
|
||||
if st["consumer"].Status != "up" || st["kafka"].Detail != "lag 1,204" {
|
||||
t.Errorf("consumer/kafka = %+v/%+v, 期望 up 且 lag 千分位", st["consumer"], st["kafka"])
|
||||
}
|
||||
if st["redis"].Status != "down" {
|
||||
t.Errorf("redis(nil 降级) = %s, 期望 down(诚实标注)", st["redis"].Status)
|
||||
}
|
||||
// lag 超阈 → degraded。
|
||||
hd.Lag = func(ctx context.Context) (int64, error) { return 15000, nil }
|
||||
w = httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1/ops/system/overview", nil))
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, s := range res.Data.Subsystems {
|
||||
if s.Name == "kafka" && s.Status != "degraded" {
|
||||
t.Errorf("kafka lag=15000 → %s, 期望 degraded", s.Status)
|
||||
}
|
||||
}
|
||||
// 角色门:customer 403。
|
||||
setClaims(&middleware.Claims{AccountID: 100, Roles: []string{"customer"}})
|
||||
w = httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1/ops/system/overview", nil))
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Errorf("customer code=%d, 期望 403", w.Code)
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
"go-sms/internal/model/entity"
|
||||
"go-sms/internal/pkg/errors"
|
||||
"go-sms/internal/pkg/response"
|
||||
"go-sms/internal/service/archive"
|
||||
)
|
||||
|
||||
// fmtTime JSON 时间("2006-01-02 15:04:05",与 mock/前端 ts() 解析口径一致;
|
||||
@@ -54,16 +55,23 @@ func qTime(c *gin.Context, key string) (*time.Time, error) {
|
||||
}
|
||||
|
||||
// registerRecordRoutes 注册 record 域端点(查询类,JWT 保护即可,不挂 Casbin)。
|
||||
func registerRecordRoutes(protected *gin.RouterGroup, db *gorm.DB) {
|
||||
h := &recordHandler{db: db}
|
||||
func registerRecordRoutes(protected *gin.RouterGroup, db *gorm.DB, arc *archive.Service) {
|
||||
h := &recordHandler{db: db, arc: arc}
|
||||
protected.GET("/record/down", h.downList)
|
||||
protected.GET("/record/down/:id/intercepted", h.downIntercepted)
|
||||
protected.GET("/record/down/details", h.downDetails)
|
||||
protected.GET("/record/down/details/:id/channel-logs", h.detailChannelLogs)
|
||||
protected.GET("/sms/downlink-details", h.downlinkDetails)
|
||||
// S4·D3:历史明细冷查(CH 归档域;契约 web/src/api/record.ts D26)。
|
||||
if arc != nil {
|
||||
protected.GET("/record/history", h.history)
|
||||
}
|
||||
}
|
||||
|
||||
type recordHandler struct{ db *gorm.DB }
|
||||
type recordHandler struct {
|
||||
db *gorm.DB
|
||||
arc *archive.Service // S4·D3 冷查(nil=不注册 history 端点)
|
||||
}
|
||||
|
||||
// ---- D24:下行批次列表(GET /record/down) ----
|
||||
|
||||
@@ -650,3 +658,97 @@ func (h *recordHandler) downlinkDetails(c *gin.Context) {
|
||||
}
|
||||
response.Success(c, gin.H{"list": list, "total": total})
|
||||
}
|
||||
|
||||
// ---- S4·D3:历史明细冷查(GET /record/history;CH 归档域) ----
|
||||
|
||||
// historyRecordDTO 冷查行(契约 record.ts HistoryRecord)。
|
||||
type historyRecordDTO struct {
|
||||
ID int64 `json:"id"`
|
||||
TaskID int64 `json:"task_id"`
|
||||
TaskNo string `json:"task_no"`
|
||||
Mobile string `json:"mobile"`
|
||||
Province string `json:"province"`
|
||||
Carrier string `json:"carrier"`
|
||||
Status int8 `json:"status"`
|
||||
ErrorCode string `json:"error_code"`
|
||||
Fee float64 `json:"fee"`
|
||||
CreatedAt fmtTime `json:"created_at"`
|
||||
}
|
||||
|
||||
// history GET /record/history?page=&page_size=&mobile=&task_no=&status=&start_time=&end_time=
|
||||
// 冷数据走 ClickHouse(分区裁剪,验收=冷查<1s);task_no 筛选/回填经 MySQL
|
||||
// message_task 转译(归档表仅存 task_id)。
|
||||
func (h *recordHandler) history(c *gin.Context) {
|
||||
if h.arc == nil {
|
||||
response.Error(c, errors.NotFound.WithMsg("冷查未配置 ClickHouse"))
|
||||
return
|
||||
}
|
||||
q := archive.HistoryQuery{
|
||||
Page: intQuery(c, "page", 1),
|
||||
PageSize: intQuery(c, "page_size", 50),
|
||||
Mobile: strings.TrimSpace(c.Query("mobile")),
|
||||
}
|
||||
if q.PageSize > downlinkPageSizeMax {
|
||||
q.PageSize = downlinkPageSizeMax
|
||||
}
|
||||
if v := intQuery(c, "status", -1); v >= 0 {
|
||||
st := int8(v)
|
||||
q.Status = &st
|
||||
}
|
||||
lo, err := qTime(c, "start_time")
|
||||
if err != nil {
|
||||
response.ErrorWithMsg(c, errors.InvalidParam, "start_time "+err.Error())
|
||||
return
|
||||
}
|
||||
q.Start = lo
|
||||
hi, err := qTime(c, "end_time")
|
||||
if err != nil {
|
||||
response.ErrorWithMsg(c, errors.InvalidParam, "end_time "+err.Error())
|
||||
return
|
||||
}
|
||||
q.End = hi
|
||||
// task_no → task_id 转译(MySQL 反查;归档行回填同理)。
|
||||
if v := strings.TrimSpace(c.Query("task_no")); v != "" {
|
||||
var taskID int64
|
||||
if err := h.db.WithContext(c.Request.Context()).Table("message_task").
|
||||
Select("id").Where("task_no = ?", v).Scan(&taskID).Error; err != nil || taskID == 0 {
|
||||
response.Success(c, gin.H{"list": []historyRecordDTO{}, "total": 0})
|
||||
return
|
||||
}
|
||||
q.ChannelID = 0
|
||||
q.AccountID = 0
|
||||
q.TaskID = taskID
|
||||
}
|
||||
rows, total, err := h.arc.QueryHistory(c.Request.Context(), q)
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
// 批量回填 task_no(page_size≤1000,IN 单查)。
|
||||
taskNos := map[int64]string{}
|
||||
if len(rows) > 0 {
|
||||
ids := make([]int64, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
ids = append(ids, r.TaskID)
|
||||
}
|
||||
var tr []struct {
|
||||
ID int64
|
||||
TaskNo string
|
||||
}
|
||||
_ = h.db.WithContext(c.Request.Context()).Table("message_task").
|
||||
Select("id, task_no").Where("id IN ?", ids).Scan(&tr).Error
|
||||
for _, t := range tr {
|
||||
taskNos[t.ID] = t.TaskNo
|
||||
}
|
||||
}
|
||||
list := make([]historyRecordDTO, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
list = append(list, historyRecordDTO{
|
||||
ID: r.ID, TaskID: r.TaskID, TaskNo: taskNos[r.TaskID],
|
||||
Mobile: r.Mobile, Province: r.Province, Carrier: r.Carrier,
|
||||
Status: r.Status, ErrorCode: r.ErrorCode, Fee: r.Fee,
|
||||
CreatedAt: fmtTime(r.CreatedAt),
|
||||
})
|
||||
}
|
||||
response.Success(c, gin.H{"list": list, "total": total})
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// send_day42_test.go Day 42:/send/precheck 预检端点——拦截器链真入口(D18 提交前预检)。
|
||||
// send_precheck_test.go(原 Day42):/send/precheck 预检端点——拦截器链真入口(D18 提交前预检)。
|
||||
package v1
|
||||
|
||||
import (
|
||||
@@ -0,0 +1,608 @@
|
||||
// settle_handler.go S2:结算域端点。D2 先挂两件——消耗日汇总查询(/billing/
|
||||
// consume)与手动回补(/ops/settle/aggregate);D3(充值订单审核族)/D4(月结
|
||||
// 单/阶梯/导出)/D5(利润/对账)随任务推进在本文件追加,契约见 openapi v1.3.0。
|
||||
package v1
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go-sms/internal/config"
|
||||
"go-sms/internal/middleware"
|
||||
"go-sms/internal/model/entity"
|
||||
errs "go-sms/internal/pkg/errors"
|
||||
"go-sms/internal/pkg/response"
|
||||
"go-sms/internal/service/settle"
|
||||
)
|
||||
|
||||
// settleConsumeDefaultDays 消耗查询缺省窗口(近 7 天,对齐 stats trend 口径)。
|
||||
const settleConsumeDefaultDays = 7
|
||||
|
||||
func registerSettleRoutes(protected *gin.RouterGroup, deps Deps) {
|
||||
h := &settleHandler{svc: deps.Settle}
|
||||
// 消耗日汇总查询:JWT 即可(admin/ops/service 全量;group/customer 仅自己,
|
||||
// scope 收窄在 handler 内做——与 stats 域同口径,casbin customer settle:read
|
||||
// 策略为 v20 所种,细分资源门按需后挂)。
|
||||
protected.GET("/billing/consume", h.consumeList)
|
||||
// 手动触发回补(运维口径;settle:write)。
|
||||
protected.POST("/ops/settle/aggregate",
|
||||
middleware.RequirePermission(deps.Perm, "settle", "write"), h.aggregate)
|
||||
// S2-D3:充值订单审核族(R3 双通道——即时充值保留,订单流承载付款审核确认;
|
||||
// 创建/确认/驳回挂 account:recharge 二级权限与即时充值同门;列表=平台角色
|
||||
// admin/ops/service(group/customer 403——订单含全平台资金信息不放大)。
|
||||
protected.GET("/billing/orders", h.ordersList)
|
||||
protected.POST("/billing/orders",
|
||||
middleware.RequirePermission(deps.Perm, "account", middleware.PermRecharge), h.orderCreate)
|
||||
protected.POST("/billing/orders/:id/confirm",
|
||||
middleware.RequirePermission(deps.Perm, "account", middleware.PermRecharge), h.orderConfirm)
|
||||
protected.POST("/billing/orders/:id/reject",
|
||||
middleware.RequirePermission(deps.Perm, "account", middleware.PermRecharge), h.orderReject)
|
||||
// S2-D4:出账+阶梯(settle:write 门=D1 契约;查询类平台角色/客户仅自己)。
|
||||
protected.GET("/settle/customer", h.customerBills)
|
||||
protected.POST("/settle/customer/generate",
|
||||
middleware.RequirePermission(deps.Perm, "settle", "write"), h.customerGenerate)
|
||||
protected.GET("/settle/customer/:id", h.customerBillDetail)
|
||||
protected.POST("/settle/customer/:id/confirm",
|
||||
middleware.RequirePermission(deps.Perm, "settle", "write"), h.customerBillConfirm)
|
||||
protected.POST("/settle/customer/:id/settle",
|
||||
middleware.RequirePermission(deps.Perm, "settle", "write"), h.customerBillSettle)
|
||||
protected.GET("/settle/customer/:id/export", h.customerBillExport)
|
||||
protected.GET("/settle/upstream", h.upstreamBills)
|
||||
protected.POST("/settle/upstream/generate",
|
||||
middleware.RequirePermission(deps.Perm, "settle", "write"), h.upstreamGenerate)
|
||||
protected.GET("/settle/upstream/:id", h.upstreamBillDetail)
|
||||
protected.POST("/settle/upstream/:id/settle",
|
||||
middleware.RequirePermission(deps.Perm, "settle", "write"), h.upstreamBillSettle)
|
||||
protected.GET("/settle/upstream/:id/export", h.upstreamBillExport)
|
||||
protected.GET("/settle/ladder", h.ladderList)
|
||||
protected.POST("/settle/ladder",
|
||||
middleware.RequirePermission(deps.Perm, "settle", "write"), h.ladderCreate)
|
||||
protected.PUT("/settle/ladder/:id",
|
||||
middleware.RequirePermission(deps.Perm, "settle", "write"), h.ladderUpdate)
|
||||
protected.DELETE("/settle/ladder/:id",
|
||||
middleware.RequirePermission(deps.Perm, "settle", "write"), h.ladderDelete)
|
||||
// S2-D5:利润分析双端点(平台角色门——利润含全平台资金信息,group/customer
|
||||
// 403,与充值订单列表同口径;查询类不挂 settle:write)。
|
||||
protected.GET("/stats/profit/users", h.profitUsers)
|
||||
protected.GET("/stats/profit/channels", h.profitChannels)
|
||||
}
|
||||
|
||||
// StartSettleAggregateJob v1 层装配入口(照 StartStatsRefresher 模式,仅
|
||||
// gateway 调用;测试/迁移工具不启)。
|
||||
func StartSettleAggregateJob(db *gorm.DB, cfg config.Settle, log *zap.Logger) {
|
||||
settle.StartAggregateJob(db, cfg.RefreshEvery, cfg.RefreshDays, cfg.BackfillDays, log)
|
||||
}
|
||||
|
||||
type settleHandler struct{ svc *settle.Service }
|
||||
|
||||
// consumeList GET /billing/consume:账号×业务×天 消耗事实源查询。
|
||||
func (h *settleHandler) consumeList(c *gin.Context) {
|
||||
claims := statsClaims(c) // protected 组内恒在;复用 stats 域提取器
|
||||
q := settle.ConsumeQuery{Page: 1, PageSize: 20}
|
||||
if v, err := strconv.ParseInt(c.Query("account_id"), 10, 64); err == nil && v > 0 {
|
||||
q.AccountID = v
|
||||
}
|
||||
q.Business = c.Query("business")
|
||||
now := time.Now()
|
||||
q.To = now
|
||||
q.From = now.AddDate(0, 0, -(settleConsumeDefaultDays - 1))
|
||||
if s := c.Query("date_from"); s != "" {
|
||||
d, err := time.ParseInLocation("2006-01-02", s, time.Local)
|
||||
if err != nil {
|
||||
response.Error(c, errs.InvalidParam.WithMsg("date_from 须为 YYYY-MM-DD"))
|
||||
return
|
||||
}
|
||||
q.From = d
|
||||
}
|
||||
if s := c.Query("date_to"); s != "" {
|
||||
d, err := time.ParseInLocation("2006-01-02", s, time.Local)
|
||||
if err != nil {
|
||||
response.Error(c, errs.InvalidParam.WithMsg("date_to 须为 YYYY-MM-DD"))
|
||||
return
|
||||
}
|
||||
q.To = d
|
||||
}
|
||||
if v, err := strconv.Atoi(c.Query("page")); err == nil && v > 0 {
|
||||
q.Page = v
|
||||
}
|
||||
if v, err := strconv.Atoi(c.Query("page_size")); err == nil && v > 0 {
|
||||
q.PageSize = v
|
||||
}
|
||||
// 非平台角色强制收窄到自身账号(带参越权请求直接覆盖,不报错——stats 同口径)。
|
||||
if claims != nil && !statsIsPlatform(c) {
|
||||
q.AccountID = claims.AccountID
|
||||
}
|
||||
list, total, err := h.svc.ListConsume(c.Request.Context(), q)
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, gin.H{"total": total, "list": list})
|
||||
}
|
||||
|
||||
// aggregate POST /ops/settle/aggregate:date 缺省=昨日;幂等 UPSERT 可重入,
|
||||
// 返回两表落账行数(只回补单日——历史区间回补走重启 job 的 backfill 或逐日调用)。
|
||||
func (h *settleHandler) aggregate(c *gin.Context) {
|
||||
var req struct {
|
||||
Date string `json:"date"`
|
||||
}
|
||||
_ = c.ShouldBindJSON(&req)
|
||||
day := time.Now().AddDate(0, 0, -1)
|
||||
if req.Date != "" {
|
||||
d, err := time.ParseInLocation("2006-01-02", req.Date, time.Local)
|
||||
if err != nil {
|
||||
response.Error(c, errs.InvalidParam.WithMsg("date 须为 YYYY-MM-DD"))
|
||||
return
|
||||
}
|
||||
day = d
|
||||
}
|
||||
accountRows, channelRows, err := h.svc.AggregateDay(c.Request.Context(), day)
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, gin.H{
|
||||
"day": day.Format("2006-01-02"),
|
||||
"account_rows": accountRows,
|
||||
"channel_rows": channelRows,
|
||||
})
|
||||
}
|
||||
|
||||
// ordersList GET /billing/orders:订单分页(account_id/pay_status 过滤;平台角色)。
|
||||
func (h *settleHandler) ordersList(c *gin.Context) {
|
||||
if !statsIsPlatform(c) {
|
||||
response.Error(c, errs.Forbidden.WithMsg("仅平台角色可查看充值订单"))
|
||||
return
|
||||
}
|
||||
q := settle.OrderQuery{Page: 1, PageSize: 20}
|
||||
if v, err := strconv.ParseInt(c.Query("account_id"), 10, 64); err == nil && v > 0 {
|
||||
q.AccountID = v
|
||||
}
|
||||
if s := c.Query("pay_status"); s != "" {
|
||||
v, err := strconv.Atoi(s)
|
||||
if err != nil || v < 0 || v > 2 {
|
||||
response.Error(c, errs.InvalidParam.WithMsg("pay_status 须为 0/1/2"))
|
||||
return
|
||||
}
|
||||
ps := int8(v)
|
||||
q.PayStatus = &ps
|
||||
}
|
||||
if v, err := strconv.Atoi(c.Query("page")); err == nil && v > 0 {
|
||||
q.Page = v
|
||||
}
|
||||
if v, err := strconv.Atoi(c.Query("page_size")); err == nil && v > 0 {
|
||||
q.PageSize = v
|
||||
}
|
||||
list, total, err := h.svc.ListOrders(c.Request.Context(), q)
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, gin.H{"total": total, "list": list})
|
||||
}
|
||||
|
||||
// orderCreate POST /billing/orders:创建待确认订单(不动余额;到账只发生在 confirm)。
|
||||
func (h *settleHandler) orderCreate(c *gin.Context) {
|
||||
var req struct {
|
||||
AccountID int64 `json:"account_id" binding:"required"`
|
||||
Count int64 `json:"count" binding:"required"`
|
||||
Amount float64 `json:"amount"`
|
||||
PayType string `json:"pay_type"`
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.Error(c, errs.InvalidParam.WithMsg("account_id/count 必填"))
|
||||
return
|
||||
}
|
||||
claims := statsClaims(c)
|
||||
operator := ""
|
||||
if claims != nil {
|
||||
operator = claims.Username
|
||||
}
|
||||
order, err := h.svc.CreateOrder(c.Request.Context(), &settle.CreateOrderRequest{
|
||||
AccountID: req.AccountID, Count: req.Count, Amount: req.Amount,
|
||||
PayType: req.PayType, Operator: operator, Remark: req.Remark,
|
||||
})
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, order)
|
||||
}
|
||||
|
||||
// orderConfirm POST /billing/orders/:id/confirm:付款审核确认(0→1,余额+count)。
|
||||
func (h *settleHandler) orderConfirm(c *gin.Context) {
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
response.Error(c, errs.InvalidParam.WithMsg("订单 ID 非法"))
|
||||
return
|
||||
}
|
||||
claims := statsClaims(c)
|
||||
operator := ""
|
||||
if claims != nil {
|
||||
operator = claims.Username
|
||||
}
|
||||
res, err := h.svc.ConfirmOrder(c.Request.Context(), id, operator)
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, gin.H{
|
||||
"order_no": res.Order.OrderNo, "pay_status": res.Order.PayStatus,
|
||||
"count": res.Order.Count, "balance": res.Balance, "balance_log_id": res.BalanceLogID,
|
||||
})
|
||||
}
|
||||
|
||||
// orderReject POST /billing/orders/:id/reject:驳回(0→2,不涉余额)。
|
||||
func (h *settleHandler) orderReject(c *gin.Context) {
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
response.Error(c, errs.InvalidParam.WithMsg("订单 ID 非法"))
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
_ = c.ShouldBindJSON(&req)
|
||||
claims := statsClaims(c)
|
||||
operator := ""
|
||||
if claims != nil {
|
||||
operator = claims.Username
|
||||
}
|
||||
order, err := h.svc.RejectOrder(c.Request.Context(), id, operator, req.Remark)
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, order)
|
||||
}
|
||||
|
||||
// ---- S2-D4:出账 handler ----
|
||||
|
||||
// billQueryFrom 通用账单列表参数解析。
|
||||
func billQueryFrom(c *gin.Context) (settle.BillQuery, error) {
|
||||
q := settle.BillQuery{Period: c.Query("period"), Page: 1, PageSize: 20}
|
||||
if v, err := strconv.ParseInt(c.Query("account_id"), 10, 64); err == nil && v > 0 {
|
||||
q.OwnerID = v
|
||||
}
|
||||
if v, err := strconv.ParseInt(c.Query("upstream_id"), 10, 64); err == nil && v > 0 {
|
||||
q.OwnerID = v
|
||||
}
|
||||
if s := c.Query("status"); s != "" {
|
||||
v, err := strconv.Atoi(s)
|
||||
if err != nil || v < 0 || v > 2 {
|
||||
return q, errs.InvalidParam.WithMsg("status 须为 0/1/2")
|
||||
}
|
||||
st := int8(v)
|
||||
q.Status = &st
|
||||
}
|
||||
if v, err := strconv.Atoi(c.Query("page")); err == nil && v > 0 {
|
||||
q.Page = v
|
||||
}
|
||||
if v, err := strconv.Atoi(c.Query("page_size")); err == nil && v > 0 {
|
||||
q.PageSize = v
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
|
||||
// customerBills GET /settle/customer:customer 仅自己;平台角色全量。
|
||||
func (h *settleHandler) customerBills(c *gin.Context) {
|
||||
q, err := billQueryFrom(c)
|
||||
if err != nil {
|
||||
response.Error(c, err.(*errs.Error))
|
||||
return
|
||||
}
|
||||
if claims := statsClaims(c); claims != nil && !statsIsPlatform(c) {
|
||||
q.OwnerID = claims.AccountID
|
||||
}
|
||||
list, total, err := h.svc.ListCustomerBills(c.Request.Context(), q)
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, gin.H{"total": total, "list": list})
|
||||
}
|
||||
|
||||
// customerGenerate POST /settle/customer/generate {period}。
|
||||
func (h *settleHandler) customerGenerate(c *gin.Context) {
|
||||
var req struct {
|
||||
Period string `json:"period" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.Error(c, errs.InvalidParam.WithMsg("period 必填(YYYY-MM)"))
|
||||
return
|
||||
}
|
||||
res, err := h.svc.GenerateCustomerBills(c.Request.Context(), req.Period)
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, res)
|
||||
}
|
||||
|
||||
// customerBillDetail GET /settle/customer/:id:单头+按天明细;customer 仅自己。
|
||||
func (h *settleHandler) customerBillDetail(c *gin.Context) {
|
||||
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
bill, err := h.svc.GetCustomerBill(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
if claims := statsClaims(c); claims != nil && !statsIsPlatform(c) && bill.AccountID != claims.AccountID {
|
||||
response.Error(c, errs.Forbidden.WithMsg("非本人账单"))
|
||||
return
|
||||
}
|
||||
days, err := h.svc.CustomerBillDays(c.Request.Context(), bill)
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, gin.H{"bill": bill, "days": days})
|
||||
}
|
||||
|
||||
// customerBillConfirm POST /settle/customer/:id/confirm 0→1。
|
||||
func (h *settleHandler) customerBillConfirm(c *gin.Context) {
|
||||
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err := h.svc.ConfirmCustomerBill(c.Request.Context(), id); err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
// customerBillSettle POST /settle/customer/:id/settle 1→2+settle_record。
|
||||
func (h *settleHandler) customerBillSettle(c *gin.Context) {
|
||||
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
var req struct {
|
||||
PayType string `json:"pay_type"`
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
_ = c.ShouldBindJSON(&req)
|
||||
operator := ""
|
||||
if claims := statsClaims(c); claims != nil {
|
||||
operator = claims.Username
|
||||
}
|
||||
rec, err := h.svc.SettleCustomerBill(c.Request.Context(), id, req.PayType, operator, req.Remark)
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, rec)
|
||||
}
|
||||
|
||||
// customerBillExport GET /settle/customer/:id/export XLSX。
|
||||
func (h *settleHandler) customerBillExport(c *gin.Context) {
|
||||
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
data, name, err := h.svc.ExportCustomerBillXLSX(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
c.Header("Content-Disposition", `attachment; filename="`+name+`"`)
|
||||
c.Data(200, "application/octet-stream", data)
|
||||
}
|
||||
|
||||
// upstreamBills GET /settle/upstream(平台角色)。
|
||||
func (h *settleHandler) upstreamBills(c *gin.Context) {
|
||||
if !statsIsPlatform(c) {
|
||||
response.Error(c, errs.Forbidden.WithMsg("仅平台角色可查看上家账单"))
|
||||
return
|
||||
}
|
||||
q, err := billQueryFrom(c)
|
||||
if err != nil {
|
||||
response.Error(c, err.(*errs.Error))
|
||||
return
|
||||
}
|
||||
list, total, err := h.svc.ListUpstreamBills(c.Request.Context(), q)
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, gin.H{"total": total, "list": list})
|
||||
}
|
||||
|
||||
// upstreamGenerate POST /settle/upstream/generate {period}。
|
||||
func (h *settleHandler) upstreamGenerate(c *gin.Context) {
|
||||
var req struct {
|
||||
Period string `json:"period" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.Error(c, errs.InvalidParam.WithMsg("period 必填(YYYY-MM)"))
|
||||
return
|
||||
}
|
||||
res, err := h.svc.GenerateUpstreamBills(c.Request.Context(), req.Period)
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, res)
|
||||
}
|
||||
|
||||
// upstreamBillDetail GET /settle/upstream/:id:单头+通道×天明细。
|
||||
func (h *settleHandler) upstreamBillDetail(c *gin.Context) {
|
||||
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
bill, err := h.svc.GetUpstreamBill(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
rows, err := h.svc.UpstreamBillRows(c.Request.Context(), bill)
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, gin.H{"bill": bill, "rows": rows})
|
||||
}
|
||||
|
||||
// upstreamBillSettle POST /settle/upstream/:id/settle 1→2+settle_record(direction=2)。
|
||||
func (h *settleHandler) upstreamBillSettle(c *gin.Context) {
|
||||
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
var req struct {
|
||||
PayType string `json:"pay_type"`
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
_ = c.ShouldBindJSON(&req)
|
||||
operator := ""
|
||||
if claims := statsClaims(c); claims != nil {
|
||||
operator = claims.Username
|
||||
}
|
||||
rec, err := h.svc.SettleUpstreamBill(c.Request.Context(), id, req.PayType, operator, req.Remark)
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, rec)
|
||||
}
|
||||
|
||||
// upstreamBillExport GET /settle/upstream/:id/export XLSX。
|
||||
func (h *settleHandler) upstreamBillExport(c *gin.Context) {
|
||||
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
data, name, err := h.svc.ExportUpstreamBillXLSX(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
c.Header("Content-Disposition", `attachment; filename="`+name+`"`)
|
||||
c.Data(200, "application/octet-stream", data)
|
||||
}
|
||||
|
||||
// ---- 阶梯档位 ----
|
||||
|
||||
// ladderList GET /settle/ladder?owner_type&owner_id。
|
||||
func (h *settleHandler) ladderList(c *gin.Context) {
|
||||
ownerID, _ := strconv.ParseInt(c.Query("owner_id"), 10, 64)
|
||||
list, err := h.svc.ListLadders(c.Request.Context(), c.Query("owner_type"), ownerID)
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, list)
|
||||
}
|
||||
|
||||
// ladderCreate POST /settle/ladder。
|
||||
func (h *settleHandler) ladderCreate(c *gin.Context) {
|
||||
var req entity.SettleLadder
|
||||
if err := c.ShouldBindJSON(&req); err != nil || req.OwnerID <= 0 {
|
||||
response.Error(c, errs.InvalidParam.WithMsg("owner_type/owner_id/tier_from/price 必填"))
|
||||
return
|
||||
}
|
||||
if err := h.svc.CreateLadder(c.Request.Context(), &req); err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, req)
|
||||
}
|
||||
|
||||
// ladderUpdate PUT /settle/ladder/:id {tier_from?,price?}。
|
||||
func (h *settleHandler) ladderUpdate(c *gin.Context) {
|
||||
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
var req struct {
|
||||
TierFrom *int64 `json:"tier_from"`
|
||||
Price *float64 `json:"price"`
|
||||
}
|
||||
_ = c.ShouldBindJSON(&req)
|
||||
if err := h.svc.UpdateLadder(c.Request.Context(), id, req.TierFrom, req.Price); err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
// ladderDelete DELETE /settle/ladder/:id。
|
||||
func (h *settleHandler) ladderDelete(c *gin.Context) {
|
||||
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err := h.svc.DeleteLadder(c.Request.Context(), id); err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
// ---- S2-D5:利润分析 ----
|
||||
|
||||
// parseProfitDates date_from/date_to 解析(YYYY-MM-DD;零值兜底=本月 1 号~今天,
|
||||
// 月度利润视角)。返回 ok=false 时已写响应。
|
||||
func parseProfitDates(c *gin.Context) (from, to time.Time, ok bool) {
|
||||
now := time.Now()
|
||||
from = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
|
||||
to = now
|
||||
if s := c.Query("date_from"); s != "" {
|
||||
d, err := time.ParseInLocation("2006-01-02", s, time.Local)
|
||||
if err != nil {
|
||||
response.Error(c, errs.InvalidParam.WithMsg("date_from 须为 YYYY-MM-DD"))
|
||||
return time.Time{}, time.Time{}, false
|
||||
}
|
||||
from = d
|
||||
}
|
||||
if s := c.Query("date_to"); s != "" {
|
||||
d, err := time.ParseInLocation("2006-01-02", s, time.Local)
|
||||
if err != nil {
|
||||
response.Error(c, errs.InvalidParam.WithMsg("date_to 须为 YYYY-MM-DD"))
|
||||
return time.Time{}, time.Time{}, false
|
||||
}
|
||||
to = d
|
||||
}
|
||||
return from, to, true
|
||||
}
|
||||
|
||||
// profitUsers GET /stats/profit/users:用户利润(时间线价收入/成本分摊/直线下级
|
||||
// 返点 R4/子树过滤;平台角色)。口径详见 settle/profit.go 头注。
|
||||
func (h *settleHandler) profitUsers(c *gin.Context) {
|
||||
if !statsIsPlatform(c) {
|
||||
response.Error(c, errs.Forbidden.WithMsg("仅平台角色可查看利润分析"))
|
||||
return
|
||||
}
|
||||
from, to, ok := parseProfitDates(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
q := settle.UserProfitQuery{From: from, To: to, Page: 1, PageSize: 20}
|
||||
if v, err := strconv.ParseInt(c.Query("parent_id"), 10, 64); err == nil && v > 0 {
|
||||
q.ParentID = v
|
||||
}
|
||||
if v, err := strconv.Atoi(c.Query("page")); err == nil && v > 0 {
|
||||
q.Page = v
|
||||
}
|
||||
if v, err := strconv.Atoi(c.Query("page_size")); err == nil && v > 0 {
|
||||
q.PageSize = v
|
||||
}
|
||||
res, err := h.svc.UserProfit(c.Request.Context(), q)
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, res)
|
||||
}
|
||||
|
||||
// profitChannels GET /stats/profit/channels:通道利润(通道价成本直算/收入占比
|
||||
// 分摊;平台角色)。
|
||||
func (h *settleHandler) profitChannels(c *gin.Context) {
|
||||
if !statsIsPlatform(c) {
|
||||
response.Error(c, errs.Forbidden.WithMsg("仅平台角色可查看利润分析"))
|
||||
return
|
||||
}
|
||||
from, to, ok := parseProfitDates(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
q := settle.ChannelProfitQuery{From: from, To: to, Page: 1, PageSize: 20}
|
||||
if v, err := strconv.ParseInt(c.Query("channel_id"), 10, 64); err == nil && v > 0 {
|
||||
q.ChannelID = v
|
||||
}
|
||||
if v, err := strconv.Atoi(c.Query("page")); err == nil && v > 0 {
|
||||
q.Page = v
|
||||
}
|
||||
if v, err := strconv.Atoi(c.Query("page_size")); err == nil && v > 0 {
|
||||
q.PageSize = v
|
||||
}
|
||||
res, err := h.svc.ChannelProfit(c.Request.Context(), q)
|
||||
if err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, res)
|
||||
}
|
||||
@@ -0,0 +1,472 @@
|
||||
package v1
|
||||
|
||||
// settle_handler_test.go S2-D2:消耗日汇总两端点——customer scope 强制收窄
|
||||
// (带参越权请求覆盖不报错,stats 同口径)+手动回补落账断言。
|
||||
// gin TestMode + sqlite;claims 由中间件注入模拟(stats_handler_test 同款)。
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go-sms/internal/middleware"
|
||||
"go-sms/internal/model/entity"
|
||||
"go-sms/internal/service/settle"
|
||||
)
|
||||
|
||||
// newSettleRouter 种子:账号 1/100 今日各 1 条成功明细(通道 1);返回注入器。
|
||||
func newSettleRouter(t *testing.T) (*gin.Engine, *gorm.DB, func(*middleware.Claims)) {
|
||||
return newSettleRouterWithPerm(t, nil)
|
||||
}
|
||||
|
||||
// newSettleRouterWithPerm 可注入权限 checker(测试 403 路径);nil=仅要求已认证。
|
||||
func newSettleRouterWithPerm(t *testing.T, perm middleware.PermissionChecker) (*gin.Engine, *gorm.DB, func(*middleware.Claims)) {
|
||||
t.Helper()
|
||||
gin.SetMode(gin.TestMode)
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s, e := db.DB(); e == nil {
|
||||
s.SetMaxOpenConns(1)
|
||||
}
|
||||
if err := db.AutoMigrate(&entity.MessageDetail{}, &entity.BillingConsumeLog{},
|
||||
&entity.StatsChannelDaily{}, &entity.AccountUser{}, &entity.BillingOrder{},
|
||||
&entity.AccountPrice{}, &entity.ChannelPrice{}, &entity.ChannelChannel{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 流水表手动建:idx_account_time 与 BillingOrder 同名,glebarez 索引名
|
||||
// 全库唯一不查重(settle 包同款方言坑);无索引版本读写语义零影响。
|
||||
if err := db.Exec(`CREATE TABLE IF NOT EXISTS account_balance_log (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
account_id INTEGER NOT NULL, biz_type TEXT, amount INTEGER,
|
||||
balance_after INTEGER, ref_id TEXT, operator TEXT, remark TEXT,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP)`).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
now := time.Now()
|
||||
details := []entity.MessageDetail{
|
||||
{SnowflakeModel: entity.SnowflakeModel{ID: 9100000001, CreatedAt: now},
|
||||
TaskID: 800001, AccountID: 1, ChannelID: 1, Mobile: "13000000001",
|
||||
Content: "t", Status: 2, Business: "sms", Fee: 1.5},
|
||||
{SnowflakeModel: entity.SnowflakeModel{ID: 9100000002, CreatedAt: now},
|
||||
TaskID: 800002, AccountID: 100, ChannelID: 1, Mobile: "13000000002",
|
||||
Content: "t", Status: 2, Business: "sms", Fee: 2.5},
|
||||
}
|
||||
if err := db.Create(&details).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&entity.AccountUser{
|
||||
Model: entity.Model{ID: 1}, Username: "admin", Role: "admin", Balance: 1000,
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rg := gin.New()
|
||||
api := rg.Group("/api/v1")
|
||||
var claims *middleware.Claims
|
||||
api.Use(func(c *gin.Context) {
|
||||
if claims != nil {
|
||||
c.Set(middleware.ContextKeyClaims, claims)
|
||||
}
|
||||
})
|
||||
registerSettleRoutes(api, Deps{Settle: settle.NewService(db), Perm: perm})
|
||||
return rg, db, func(cl *middleware.Claims) { claims = cl }
|
||||
}
|
||||
|
||||
// TestSettleConsume_AdminSeesAll admin 全量;customer 强制收窄(带参越权覆盖)。
|
||||
func TestSettleConsume_Scope(t *testing.T) {
|
||||
rg, db, setClaims := newSettleRouter(t)
|
||||
// 预落账(查询读事实源,不直查明细)。
|
||||
svc := settle.NewService(db)
|
||||
if _, _, err := svc.AggregateDay(t.Context(), time.Now()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
setClaims(&middleware.Claims{AccountID: 1, Username: "admin", Roles: []string{"admin"}})
|
||||
w := httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1/billing/consume", nil))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("admin status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
var res struct {
|
||||
Data struct {
|
||||
Total int64 `json:"total"`
|
||||
List []entity.BillingConsumeLog `json:"list"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if res.Data.Total != 2 {
|
||||
t.Fatalf("admin 应见全量 2 行,got %d", res.Data.Total)
|
||||
}
|
||||
|
||||
// customer 带参越权(account_id=1):应被覆盖为自身 100。
|
||||
setClaims(&middleware.Claims{AccountID: 100, Username: "cust", Roles: []string{"customer"}})
|
||||
w = httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/billing/consume?account_id=1", nil)
|
||||
rg.ServeHTTP(w, req)
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("customer status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if res.Data.Total != 1 || len(res.Data.List) != 1 || res.Data.List[0].AccountID != 100 {
|
||||
t.Fatalf("customer 应强制收窄到自身 100: total=%d list=%v", res.Data.Total, res.Data.List)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSettleAggregate_Manual 手动回补:返回行数+落库断言;坏日期 4xx。
|
||||
func TestSettleAggregate_Manual(t *testing.T) {
|
||||
rg, db, setClaims := newSettleRouter(t)
|
||||
setClaims(&middleware.Claims{AccountID: 1, Username: "admin", Roles: []string{"admin"}})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodPost, "/api/v1/ops/settle/aggregate", nil))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
var res struct {
|
||||
Data struct {
|
||||
Day string `json:"day"`
|
||||
AccountRows int64 `json:"account_rows"`
|
||||
ChannelRows int64 `json:"channel_rows"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 缺省=昨日:种子明细是今天 → 0 行也合法;显式补今天断言 2/1。
|
||||
w = httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/ops/settle/aggregate",
|
||||
strings.NewReader(`{"date":"`+time.Now().Format("2006-01-02")+`"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rg.ServeHTTP(w, req)
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if res.Data.AccountRows != 2 || res.Data.ChannelRows != 1 {
|
||||
t.Fatalf("今日回补应 account_rows=2/channel_rows=1: %+v", res.Data)
|
||||
}
|
||||
var n int64
|
||||
db.Model(&entity.BillingConsumeLog{}).Count(&n)
|
||||
if n != 2 {
|
||||
t.Fatalf("billing_consume_log 落账=%d 期望 2", n)
|
||||
}
|
||||
|
||||
// 坏日期参数。
|
||||
w = httptest.NewRecorder()
|
||||
req = httptest.NewRequest(http.MethodPost, "/api/v1/ops/settle/aggregate",
|
||||
strings.NewReader(`{"date":"2026/11/14"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rg.ServeHTTP(w, req)
|
||||
if w.Code == 200 {
|
||||
t.Fatalf("坏日期应 4xx,got %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestSettleOrders_FullFlow 订单族全流程:创建(不动余额)→列表→confirm(余额+count)
|
||||
// →重复 confirm HTTP 409+ISC:039 →驳回路径;二级权限 403。
|
||||
func TestSettleOrders_FullFlow(t *testing.T) {
|
||||
rg, db, setClaims := newSettleRouter(t)
|
||||
setClaims(&middleware.Claims{AccountID: 1, Username: "admin", Roles: []string{"admin"}})
|
||||
|
||||
// 创建(perm=nil:已认证即过,功能路径)。
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/billing/orders",
|
||||
strings.NewReader(`{"account_id":1,"count":500,"amount":50.0,"pay_type":"bank"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rg.ServeHTTP(w, req)
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("创建 status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
var created struct {
|
||||
Data entity.BillingOrder `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &created); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if created.Data.PayStatus != 0 || created.Data.ID == 0 {
|
||||
t.Fatalf("订单字段错: %+v", created.Data)
|
||||
}
|
||||
var bal int64
|
||||
db.Model(&entity.AccountUser{}).Where("id = ?", int64(1)).Pluck("balance", &bal)
|
||||
if bal != 1000 {
|
||||
t.Fatalf("创建不应动余额: %d", bal)
|
||||
}
|
||||
|
||||
// 列表(admin 平台角色)。
|
||||
w = httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1/billing/orders?pay_status=0", nil))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("列表 status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
// confirm:余额+count。
|
||||
w = httptest.NewRecorder()
|
||||
req = httptest.NewRequest(http.MethodPost, "/api/v1/billing/orders/1/confirm", nil)
|
||||
rg.ServeHTTP(w, req)
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("confirm status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
var res struct {
|
||||
Data struct {
|
||||
Balance int64 `json:"balance"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if res.Data.Balance != 1500 {
|
||||
t.Fatalf("confirm 后余额=%d 期望 1500", res.Data.Balance)
|
||||
}
|
||||
|
||||
// 重复 confirm → HTTP 409 + ISC:039。
|
||||
w = httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodPost, "/api/v1/billing/orders/1/confirm", nil))
|
||||
if w.Code != http.StatusConflict {
|
||||
t.Fatalf("重复 confirm 应 409,got %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), "ISC:039") {
|
||||
t.Fatalf("409 响应应带 ISC:039: %s", w.Body.String())
|
||||
}
|
||||
db.Model(&entity.AccountUser{}).Where("id = ?", int64(1)).Pluck("balance", &bal)
|
||||
if bal != 1500 {
|
||||
t.Fatalf("重复 confirm 后余额=%d 应保持 1500", bal)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSettleOrders_PermissionAndScope 二级权限 403 与 customer 列表 403。
|
||||
func TestSettleOrders_PermissionAndScope(t *testing.T) {
|
||||
// 恒拒 checker:创建/confirm 应 403。
|
||||
denyAll := middleware.PermissionChecker(func(*middleware.Claims, string, string) bool { return false })
|
||||
rg, _, setClaims := newSettleRouterWithPerm(t, denyAll)
|
||||
setClaims(&middleware.Claims{AccountID: 1, Username: "ops", Roles: []string{"ops"}})
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/billing/orders",
|
||||
strings.NewReader(`{"account_id":1,"count":100}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rg.ServeHTTP(w, req)
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Fatalf("无二级权限创建应 403,got %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
// customer 列表 → 403(perm 不参与,角色判定)。
|
||||
rg2, _, setClaims2 := newSettleRouter(t)
|
||||
setClaims2(&middleware.Claims{AccountID: 100, Username: "cust", Roles: []string{"customer"}})
|
||||
w = httptest.NewRecorder()
|
||||
rg2.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1/billing/orders", nil))
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Fatalf("customer 列表应 403,got %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestSettleBill_FullFlow D4 出账全流程:消耗落账→生成(时间线价)→阶梯配档重算
|
||||
// →确认→结账(SettleRecord)→导出;已结拒重算;settle:write 权限门。
|
||||
func TestSettleBill_FullFlow(t *testing.T) {
|
||||
rg, db, setClaims := newSettleRouter(t)
|
||||
// 种价格+消耗(账号1:0.10 价,11-10 succ=100)。
|
||||
d, _ := time.ParseInLocation("2006-01-02", "2026-11-10", time.Local)
|
||||
if err := db.AutoMigrate(&entity.AccountPrice{}, &entity.SettleLadder{},
|
||||
&entity.SettleCustomer{}, &entity.SettleRecord{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&[]entity.AccountPrice{
|
||||
{AccountID: 1, Business: "sms", Price: 0.10, EffectiveAt: d},
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&entity.BillingConsumeLog{
|
||||
AccountID: 1, Business: "sms", StatDate: d, SuccessCount: 100,
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
setClaims(&middleware.Claims{AccountID: 1, Username: "admin", Roles: []string{"admin"}})
|
||||
|
||||
// 生成:金额=100×0.10=10。
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/settle/customer/generate",
|
||||
strings.NewReader(`{"period":"2026-11"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rg.ServeHTTP(w, req)
|
||||
if w.Code != 200 || !strings.Contains(w.Body.String(), `"generated":1`) {
|
||||
t.Fatalf("生成失败: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
// 配阶梯(0→0.05)重算:100×0.05=5。
|
||||
w = httptest.NewRecorder()
|
||||
req = httptest.NewRequest(http.MethodPost, "/api/v1/settle/ladder",
|
||||
strings.NewReader(`{"owner_type":"account","owner_id":1,"tier_from":0,"price":0.05}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rg.ServeHTTP(w, req)
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("配档失败: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
w = httptest.NewRecorder()
|
||||
req = httptest.NewRequest(http.MethodPost, "/api/v1/settle/customer/generate",
|
||||
strings.NewReader(`{"period":"2026-11"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rg.ServeHTTP(w, req)
|
||||
if !strings.Contains(w.Body.String(), `"recalculated":1`) {
|
||||
t.Fatalf("重算失败: %s", w.Body.String())
|
||||
}
|
||||
// 列表校验金额 5。
|
||||
w = httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1/settle/customer?account_id=1", nil))
|
||||
if !strings.Contains(w.Body.String(), `"amount":5`) {
|
||||
t.Fatalf("阶梯金额错: %s", w.Body.String())
|
||||
}
|
||||
// 确认→结账(SettleRecord direction=1)。
|
||||
w = httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodPost, "/api/v1/settle/customer/1/confirm", nil))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("确认失败: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
w = httptest.NewRecorder()
|
||||
req = httptest.NewRequest(http.MethodPost, "/api/v1/settle/customer/1/settle",
|
||||
strings.NewReader(`{"pay_type":"bank"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rg.ServeHTTP(w, req)
|
||||
if w.Code != 200 || !strings.Contains(w.Body.String(), `"settle_no":"SC202611-1"`) {
|
||||
t.Fatalf("结账失败: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
// 已结拒重算。
|
||||
w = httptest.NewRecorder()
|
||||
req = httptest.NewRequest(http.MethodPost, "/api/v1/settle/customer/generate",
|
||||
strings.NewReader(`{"period":"2026-11"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rg.ServeHTTP(w, req)
|
||||
if !strings.Contains(w.Body.String(), `"skipped":1`) {
|
||||
t.Fatalf("已结应 skipped: %s", w.Body.String())
|
||||
}
|
||||
// 导出 XLSX(PK 魔数)。
|
||||
w = httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1/settle/customer/1/export", nil))
|
||||
if w.Code != 200 || !bytes.HasPrefix(w.Body.Bytes(), []byte("PK")) {
|
||||
t.Fatalf("导出失败: %d", w.Code)
|
||||
}
|
||||
// 权限门:settle:write 恒拒 → 生成 403。
|
||||
denyAll := middleware.PermissionChecker(func(*middleware.Claims, string, string) bool { return false })
|
||||
rg2, _, setClaims2 := newSettleRouterWithPerm(t, denyAll)
|
||||
setClaims2(&middleware.Claims{AccountID: 1, Username: "x", Roles: []string{"ops"}})
|
||||
w = httptest.NewRecorder()
|
||||
req = httptest.NewRequest(http.MethodPost, "/api/v1/settle/customer/generate",
|
||||
strings.NewReader(`{"period":"2026-11"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rg2.ServeHTTP(w, req)
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Fatalf("无 settle:write 应 403: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestSettleProfit_Endpoints S2-D5:利润双端点——平台角色可查(数值口径),
|
||||
// customer 403;坏日期 400。种子=helper 明细(账号1/100 今日各 1 成功,通道1)
|
||||
// + 价(账号1=1.0,通道1=0.5):acc1 rev1.0/cost0.5(分摊 1.0×1/2)/profit0.5;
|
||||
// ch1 succ2/cost1.0/rev1.0/profit0(账号100 无账号行,消耗行不入结果)。
|
||||
func TestSettleProfit_Endpoints(t *testing.T) {
|
||||
rg, db, setClaims := newSettleRouter(t)
|
||||
today := time.Now().Format("2006-01-02")
|
||||
yday := time.Now().AddDate(0, 0, -1)
|
||||
if err := db.Create(&[]entity.AccountPrice{
|
||||
{AccountID: 1, Business: "sms", Price: 1.0, EffectiveAt: yday},
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&[]entity.ChannelPrice{
|
||||
{ChannelID: 1, Price: 0.5, EffectiveAt: yday},
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, _, err := settle.NewService(db).AggregateDay(t.Context(), time.Now()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
setClaims(&middleware.Claims{AccountID: 1, Username: "admin", Roles: []string{"admin"}})
|
||||
w := httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet,
|
||||
"/api/v1/stats/profit/users?date_from="+today+"&date_to="+today, nil))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("users status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
var users struct {
|
||||
Data struct {
|
||||
Total int64 `json:"total"`
|
||||
List []struct {
|
||||
AccountID int64 `json:"account_id"`
|
||||
Revenue float64 `json:"revenue"`
|
||||
Cost float64 `json:"cost"`
|
||||
Profit float64 `json:"profit"`
|
||||
} `json:"list"`
|
||||
Summary struct {
|
||||
TotalRevenue float64 `json:"total_revenue"`
|
||||
} `json:"summary"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &users); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if users.Data.Total != 1 || len(users.Data.List) != 1 {
|
||||
t.Fatalf("users 应 1 行(账号100 无账号实体): %+v", users.Data)
|
||||
}
|
||||
r := users.Data.List[0]
|
||||
if r.AccountID != 1 || r.Revenue != 1.0 || r.Cost != 0.5 || r.Profit != 0.5 {
|
||||
t.Fatalf("acc1 利润行错: %+v", r)
|
||||
}
|
||||
|
||||
w = httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet,
|
||||
"/api/v1/stats/profit/channels?date_from="+today+"&date_to="+today, nil))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("channels status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
var channels struct {
|
||||
Data struct {
|
||||
Total int64 `json:"total"`
|
||||
List []struct {
|
||||
ChannelID int64 `json:"channel_id"`
|
||||
SuccessCount int64 `json:"success_count"`
|
||||
Revenue float64 `json:"revenue"`
|
||||
Cost float64 `json:"cost"`
|
||||
Profit float64 `json:"profit"`
|
||||
} `json:"list"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &channels); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if channels.Data.Total != 1 || len(channels.Data.List) != 1 {
|
||||
t.Fatalf("channels 应 1 行: %+v", channels.Data)
|
||||
}
|
||||
c := channels.Data.List[0]
|
||||
if c.ChannelID != 1 || c.SuccessCount != 2 || c.Revenue != 1.0 || c.Cost != 1.0 || c.Profit != 0 {
|
||||
t.Fatalf("ch1 利润行错: %+v", c)
|
||||
}
|
||||
|
||||
// customer → 403;坏日期 → 400。
|
||||
setClaims(&middleware.Claims{AccountID: 100, Username: "cust", Roles: []string{"customer"}})
|
||||
for _, path := range []string{"/stats/profit/users", "/stats/profit/channels"} {
|
||||
w = httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/v1"+path, nil))
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Fatalf("customer %s 应 403: %d %s", path, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
setClaims(&middleware.Claims{AccountID: 1, Username: "admin", Roles: []string{"admin"}})
|
||||
w = httptest.NewRecorder()
|
||||
rg.ServeHTTP(w, httptest.NewRequest(http.MethodGet,
|
||||
"/api/v1/stats/profit/users?date_from=2026/11/14", nil))
|
||||
if w.Code == 200 {
|
||||
t.Fatalf("坏日期应 4xx: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,8 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -46,6 +48,9 @@ func registerStatsRoutes(protected *gin.RouterGroup, db *gorm.DB) {
|
||||
h := &statsHandler{db: db}
|
||||
protected.GET("/stats/summary", h.summary)
|
||||
protected.GET("/stats/trend", h.trend)
|
||||
// Issue#6·P2:Dashboard 业务分布与实时速率(契约 v3.2.1)。
|
||||
protected.GET("/stats/biz", h.biz)
|
||||
protected.GET("/stats/realtime", h.realtime)
|
||||
}
|
||||
|
||||
// statsSummaryDTO 四指标卡(mock 契约逐字段)。
|
||||
@@ -303,3 +308,153 @@ func StartStatsRefresher(db *gorm.DB, log *zap.Logger) {
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// ---- Issue#6·P2:业务类型分布(/stats/biz)与实时速率(/stats/realtime) ----
|
||||
// 契约=docs/design/issue6/v3-2-tailwind.html(v3.2.1)业务分布 donut 与 KPI
|
||||
// 实时速率卡;两维分布均聚合 message_task(表量级远小于 detail,HDD 可控),
|
||||
// 实时速率走 message_detail 的 idx_detail_created 覆盖索引 60s 窗口 COUNT。
|
||||
|
||||
type bizCountDTO struct {
|
||||
Key string `json:"key"`
|
||||
Count int64 `json:"count"`
|
||||
}
|
||||
|
||||
// statsBizDTO 业务分布(business=sms/video 业务线;send_type=发送方式)。
|
||||
// 注:biz_type(sms/template/sign/group)属审核域 audit_record 非 task 维度,
|
||||
// 契约 donut 的第二维以 send_type 呈现(群发/个性/定时/文件)更贴运营语义。
|
||||
type statsBizDTO struct {
|
||||
Days int `json:"days"`
|
||||
ByBusiness []bizCountDTO `json:"by_business"` // message_task.business(空=sms 兼容 Day48 旧载荷)
|
||||
BySendType []bizCountDTO `json:"by_send_type"` // message_task.send_type:batch/personal/timer/file/other
|
||||
}
|
||||
|
||||
// biz GET /stats/biz?days=7——近 N 自然日两维分布(平台全量/客户仅自己)。
|
||||
func (h *statsHandler) biz(c *gin.Context) {
|
||||
days, _ := strconv.Atoi(c.DefaultQuery("days", "7"))
|
||||
if days < 1 || days > statsTrendMaxDays {
|
||||
days = 7
|
||||
}
|
||||
now := time.Now()
|
||||
start := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).
|
||||
AddDate(0, 0, -(days - 1))
|
||||
type row struct {
|
||||
Key string
|
||||
Count int64
|
||||
}
|
||||
var rows []row
|
||||
if err := h.db.Model(&entity.MessageTask{}).
|
||||
Where("created_at >= ?", start).Scopes(statsTaskScope(c)).
|
||||
Select("COALESCE(NULLIF(business,''),'sms') AS `key`, COUNT(*) AS count").
|
||||
Group("`key`").Order("count DESC").Scan(&rows).Error; err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
byBiz := make([]bizCountDTO, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
byBiz = append(byBiz, bizCountDTO(r))
|
||||
}
|
||||
rows = nil
|
||||
if err := h.db.Model(&entity.MessageTask{}).
|
||||
Where("created_at >= ?", start).Scopes(statsTaskScope(c)).
|
||||
Select("send_type AS `key`, COUNT(*) AS count").
|
||||
Group("`key`").Order("count DESC").Scan(&rows).Error; err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
sendNames := map[string]string{"1": "batch", "2": "personal", "3": "timer", "4": "file"}
|
||||
bySend := make([]bizCountDTO, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
k, ok := sendNames[r.Key]
|
||||
if !ok {
|
||||
k = "other"
|
||||
}
|
||||
bySend = append(bySend, bizCountDTO{Key: k, Count: r.Count})
|
||||
}
|
||||
response.Success(c, statsBizDTO{Days: days, ByBusiness: byBiz, BySendType: bySend})
|
||||
}
|
||||
|
||||
// statsTaskScope 平台/客户口径收敛(admin/ops/service 全量;其余仅自己)。
|
||||
func statsTaskScope(c *gin.Context) func(*gorm.DB) *gorm.DB {
|
||||
return func(db *gorm.DB) *gorm.DB {
|
||||
if !statsIsPlatform(c) {
|
||||
return db.Where("account_id = ?", statsClaims(c).AccountID)
|
||||
}
|
||||
return db
|
||||
}
|
||||
}
|
||||
|
||||
type rtAccountDTO struct {
|
||||
AccountID int64 `json:"account_id"`
|
||||
Name string `json:"name"`
|
||||
SubmitPs float64 `json:"submit_ps"`
|
||||
}
|
||||
|
||||
// statsRealtimeDTO 实时速率(60s 窗口均值;平台=全局+Top5 账号,客户=仅自己)。
|
||||
type statsRealtimeDTO struct {
|
||||
Ts int64 `json:"ts"`
|
||||
WindowS int `json:"window_s"`
|
||||
GlobalSubmitPs float64 `json:"global_submit_ps"`
|
||||
TopAccounts []rtAccountDTO `json:"top_accounts"`
|
||||
}
|
||||
|
||||
// realtime GET /stats/realtime——60s 窗口提交速率(idx_detail_created 覆盖
|
||||
// COUNT 零回表;账号名小表 IN 补齐,缺失=空串不阻塞)。
|
||||
func (h *statsHandler) realtime(c *gin.Context) {
|
||||
const win = 60
|
||||
now := time.Now()
|
||||
since := now.Add(-win * time.Second)
|
||||
var global int64
|
||||
type topRow struct {
|
||||
AccountID int64
|
||||
Count int64
|
||||
}
|
||||
var tops []topRow
|
||||
if statsIsPlatform(c) {
|
||||
if err := h.db.Model(&entity.MessageDetail{}).Where("created_at >= ?", since).
|
||||
Count(&global).Error; err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
if err := h.db.Model(&entity.MessageDetail{}).Where("created_at >= ?", since).
|
||||
Select("account_id, COUNT(*) AS count").
|
||||
Group("account_id").Order("count DESC").Limit(5).Scan(&tops).Error; err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
claims := statsClaims(c)
|
||||
if err := h.db.Model(&entity.MessageDetail{}).
|
||||
Where("created_at >= ? AND account_id = ?", since, claims.AccountID).
|
||||
Count(&global).Error; err != nil {
|
||||
bizResp(c, err)
|
||||
return
|
||||
}
|
||||
tops = []topRow{{AccountID: claims.AccountID, Count: global}}
|
||||
}
|
||||
names := make(map[int64]string, len(tops))
|
||||
if len(tops) > 0 {
|
||||
ids := make([]int64, 0, len(tops))
|
||||
for _, t := range tops {
|
||||
ids = append(ids, t.AccountID)
|
||||
}
|
||||
var accs []entity.AccountUser
|
||||
_ = h.db.Select("id", "username").Where("id IN ?", ids).Find(&accs).Error
|
||||
for _, a := range accs {
|
||||
names[a.ID] = a.Username
|
||||
}
|
||||
}
|
||||
list := make([]rtAccountDTO, 0, len(tops))
|
||||
for _, t := range tops {
|
||||
list = append(list, rtAccountDTO{
|
||||
AccountID: t.AccountID, Name: names[t.AccountID],
|
||||
SubmitPs: statsRound2(float64(t.Count) / float64(win)),
|
||||
})
|
||||
}
|
||||
response.Success(c, statsRealtimeDTO{
|
||||
Ts: now.Unix(), WindowS: win,
|
||||
GlobalSubmitPs: statsRound2(float64(global) / float64(win)),
|
||||
TopAccounts: list,
|
||||
})
|
||||
}
|
||||
|
||||
func statsRound2(f float64) float64 { return math.Round(f*100) / 100 }
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
// sysconfig_handler.go Issue#3 方案B:系统参数配置端点(system_config KV 表)。
|
||||
// - GET /system/config 全量列表(登录即可,管理页数据源)
|
||||
// - PUT /system/config 新增/更新(system:write——admin 直通;UPSERT+缓存即刻失效)
|
||||
// 已知键:cmpp.default_ext_code(系统默认接入码前缀,账号 ext_code 空时回退,
|
||||
// 受理热路径 30s 读缓存)/uplink.blacklist.keywords(上行拉黑词表,uplink 域
|
||||
// 自刷新 60s 生效)。表结构无白名单约束,新键=新能力按需扩展。
|
||||
package v1
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"go-sms/internal/middleware"
|
||||
"go-sms/internal/pkg/errors"
|
||||
"go-sms/internal/pkg/response"
|
||||
"go-sms/internal/service/sysconfig"
|
||||
)
|
||||
|
||||
type sysConfigHandler struct{ svc *sysconfig.Service }
|
||||
|
||||
func registerSysConfigRoutes(protected *gin.RouterGroup, perm middleware.PermissionChecker, svc *sysconfig.Service) {
|
||||
h := &sysConfigHandler{svc: svc}
|
||||
protected.GET("/system/config", h.list)
|
||||
protected.PUT("/system/config", middleware.RequirePermission(perm, "system", "write"), h.set)
|
||||
}
|
||||
|
||||
// list GET /system/config(全量,cfg_key 升序)。
|
||||
func (h *sysConfigHandler) list(c *gin.Context) {
|
||||
rows, err := h.svc.List(c.Request.Context())
|
||||
if err != nil {
|
||||
response.Error(c, errors.InternalError)
|
||||
return
|
||||
}
|
||||
response.Success(c, gin.H{"list": rows})
|
||||
}
|
||||
|
||||
// sysConfigReq PUT 请求体。
|
||||
type sysConfigReq struct {
|
||||
CfgKey string `json:"cfg_key" binding:"required"`
|
||||
CfgValue string `json:"cfg_value"` // 空串=合法值(清空语义)
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
|
||||
// set PUT /system/config(cfg_key 唯一 UPSERT;审计由 AuditTrail 写方法自动留痕)。
|
||||
func (h *sysConfigHandler) set(c *gin.Context) {
|
||||
var req sysConfigReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
|
||||
return
|
||||
}
|
||||
claims := operatorClaims(c)
|
||||
operator := ""
|
||||
if claims != nil {
|
||||
operator = claims.Username
|
||||
}
|
||||
row, err := h.svc.Set(c.Request.Context(), req.CfgKey, req.CfgValue, req.Remark, operator)
|
||||
if err != nil {
|
||||
if bizErr, ok := err.(*errors.Error); ok {
|
||||
response.Error(c, bizErr)
|
||||
return
|
||||
}
|
||||
c.Set("biz_error", err.Error())
|
||||
response.Error(c, errors.InternalError)
|
||||
return
|
||||
}
|
||||
response.Success(c, row)
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// template_day41_test.go Day 41:模板 CRUD/审核状态机/发送预估端点(阶段 4 开篇)。
|
||||
// template_lifecycle_test.go(原 Day41):模板 CRUD/审核状态机/发送预估端点(阶段 4 开篇)。
|
||||
package v1
|
||||
|
||||
import (
|
||||
@@ -25,6 +25,16 @@ type Config struct {
|
||||
RateLimit RateLimit `mapstructure:"rate_limit"`
|
||||
HotReload HotReload `mapstructure:"hot_reload"`
|
||||
Balance Balance `mapstructure:"balance"`
|
||||
Settle Settle `mapstructure:"settle"`
|
||||
}
|
||||
|
||||
// Settle 结算域配置(S2):消耗日汇总滚动重刷模型——周期重刷近 N 天
|
||||
// (默认 4 天:72h 回执窗内明细状态会迁移 UNKNOWN→成功/失败,昨日行非终值,
|
||||
// 须滚动收敛;月结建议 T+4 后生成)。零值兜底见 service/settle/job.go。
|
||||
type Settle struct {
|
||||
RefreshEvery time.Duration `mapstructure:"refresh_every"` // 重刷周期(默认 1h)
|
||||
RefreshDays int `mapstructure:"refresh_days"` // 每轮重刷天数窗(默认 4=today-3..today)
|
||||
BackfillDays int `mapstructure:"backfill_days"` // 启动回补天数(默认 7,明细热窗 5 天内余量)
|
||||
}
|
||||
|
||||
// Balance 资金预扣模式(Day54 R1,评审 docs/review/r1-balance.md):
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// auth_day24_test.go Day 24:黑名单鉴权中间件测试。
|
||||
// auth_jwt_revocation_test.go(原 Day24):黑名单鉴权中间件测试。
|
||||
package middleware
|
||||
|
||||
import (
|
||||
@@ -18,6 +18,7 @@ type AccountUser struct {
|
||||
IPBind string `gorm:"size:256" json:"ip_bind"`
|
||||
Balance int64 `json:"balance"`
|
||||
CreditBalance int64 `json:"credit_balance"`
|
||||
RebateRate float64 `gorm:"type:decimal(5,4);default:0" json:"rebate_rate"` // v20:代理返点率(小数,0.1200=12%;0=无返点;上级对直线下级结算额的分成)
|
||||
SvcTeam string `gorm:"size:64" json:"svc_team"`
|
||||
ExpiredAt *time.Time `json:"expired_at"`
|
||||
LastLoginAt *time.Time `json:"last_login_at"`
|
||||
|
||||
@@ -46,8 +46,10 @@ func All() []any {
|
||||
// 计费与结算(5)
|
||||
&BillingOrder{}, &BillingConsumeLog{}, &SettleCustomer{}, &SettleUpstream{},
|
||||
&SettleRecord{},
|
||||
// 统计与运维(8)
|
||||
&StatsUserDaily{}, &StatsChannelDaily{}, &SystemConfig{}, &SystemAlertRule{},
|
||||
// 统计与运维(7)
|
||||
&StatsChannelDaily{}, &SystemConfig{}, &SystemAlertRule{},
|
||||
&SystemLoginLog{}, &SystemOperateLog{}, &SystemErrorCode{}, &FeErrorLog{},
|
||||
// 回调与通知(S4·C1)
|
||||
&CallbackTask{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,9 @@ type BillingOrder struct {
|
||||
|
||||
func (BillingOrder) TableName() string { return "billing_order" }
|
||||
|
||||
// BillingConsumeLog 消耗日志(按账号/业务/天)。
|
||||
// BillingConsumeLog 消耗日志(按账号/业务/天)——S2 起为结算域消耗事实源
|
||||
// (R2 裁决:stats_user_daily 同粒度双空壳废弃,单一事实源归本表);
|
||||
// 日汇总 job T+1 凌晨幂等 UPSERT(明细热窗仅 5 天,月结必须走本表)。
|
||||
type BillingConsumeLog struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
AccountID int64 `gorm:"uniqueIndex:uk_account_date,priority:1" json:"account_id"`
|
||||
@@ -28,21 +30,24 @@ type BillingConsumeLog struct {
|
||||
SubmitCount int `json:"submit_count"`
|
||||
SuccessCount int `json:"success_count"`
|
||||
FailCount int `json:"fail_count"`
|
||||
FeeTotal float64 `gorm:"type:decimal(12,4)" json:"fee_total"`
|
||||
UnknownCount int `json:"unknown_count"` // v20:未知(72h 窗后仍 UNKNOWN)——上家 success_unknown 口径依赖
|
||||
FeeTotal float64 `gorm:"type:decimal(12,4)" json:"fee_total"` // 单位=条(=Σ明细 fee,片数口径 Day70)
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"` // v20:UPSERT 重跑痕迹
|
||||
}
|
||||
|
||||
func (BillingConsumeLog) TableName() string { return "billing_consume_log" }
|
||||
|
||||
// SettleCustomer 下家(客户)月度结算单。
|
||||
// SettleCustomer 下家(客户)月度结算单——金额=Σ(日成功条数×生效单价),
|
||||
// 出账时刻按价格时间线推导(R1 裁决:明细 Fee 保持片数,不在受理侧金额化)。
|
||||
type SettleCustomer struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
SettleNo string `gorm:"size:32" json:"settle_no"`
|
||||
AccountID int64 `gorm:"uniqueIndex:uk_account_period,priority:1" json:"account_id"`
|
||||
Period string `gorm:"size:7;uniqueIndex:uk_account_period,priority:2;index:idx_period" json:"period"`
|
||||
SuccessCount int64 `json:"success_count"`
|
||||
Amount float64 `gorm:"type:decimal(12,2)" json:"amount"`
|
||||
Status int8 `gorm:"default:0" json:"status"`
|
||||
Amount float64 `gorm:"type:decimal(12,2)" json:"amount"` // 应收(元)
|
||||
Status int8 `gorm:"default:0" json:"status"` // 0草稿 1已确认 2已结账(已结拒重算)
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
}
|
||||
|
||||
@@ -76,3 +81,17 @@ type SettleRecord struct {
|
||||
}
|
||||
|
||||
func (SettleRecord) TableName() string { return "settle_record" }
|
||||
|
||||
// SettleLadder 阶梯价档位(S2·R5:出账时按月成功总量落档,档内单价覆写
|
||||
// 时间线价;tier_from=档位下限(含),0=首档,上一档与下一档的 from 界定区间;
|
||||
// owner_type=account(下家应收)/upstream(上家成本)两类锚点各自配档)。
|
||||
type SettleLadder struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
OwnerType string `gorm:"size:8;uniqueIndex:uk_owner_tier,priority:1" json:"owner_type"` // account/upstream
|
||||
OwnerID int64 `gorm:"uniqueIndex:uk_owner_tier,priority:2" json:"owner_id"`
|
||||
TierFrom int64 `gorm:"uniqueIndex:uk_owner_tier,priority:3" json:"tier_from"` // 档位下限(含,条)
|
||||
Price float64 `gorm:"type:decimal(10,4)" json:"price"` // 该档单价(元/条)
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
}
|
||||
|
||||
func (SettleLadder) TableName() string { return "settle_ladder" }
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package entity
|
||||
|
||||
import "time"
|
||||
|
||||
// Callback 回调任务类型(S4·C1)。
|
||||
const (
|
||||
CallbackTypeReport int8 = 1 // 回执终态推送(账号 report_url)
|
||||
CallbackTypeUplink int8 = 2 // 上行消息推送(账号 uplink_url)
|
||||
)
|
||||
|
||||
// Callback 回调任务状态机(S4·C1)。
|
||||
const (
|
||||
CallbackPending int8 = 0 // 待推送(含退避等待)
|
||||
CallbackSent int8 = 1 // 已送达(2xx)
|
||||
CallbackDead int8 = 2 // 死信(重试耗尽,可重派)
|
||||
)
|
||||
|
||||
// CallbackTask HTTP 回调推送任务(S4·C1:指数退避+死信可查)。
|
||||
// 入队源=report 终态首锁/report 手动配对(EnqueueReport)与 uplink 消费
|
||||
// (EnqueueUplink);PushWorker 扫描投递。幂等锚=无(每终态一条;report 侧
|
||||
// 由 finalizeDetail 首锁语义天然至多一次,重放由 detail 终态锁定挡住)。
|
||||
type CallbackTask struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
AccountID int64 `gorm:"index:idx_cb_account,priority:1" json:"account_id"`
|
||||
Type int8 `gorm:"default:1" json:"type"` // 1 回执 2 上行(CallbackType*)
|
||||
URL string `gorm:"size:512" json:"url"`
|
||||
Payload string `gorm:"type:text" json:"payload"` // 客户回调 JSON 载荷
|
||||
Status int8 `gorm:"index:idx_cb_status_retry,priority:1;default:0" json:"status"`
|
||||
Attempt int `gorm:"default:0" json:"attempt"`
|
||||
NextRetryAt time.Time `gorm:"index:idx_cb_status_retry,priority:2" json:"next_retry_at"`
|
||||
LastCode int `json:"last_code"` // 最近一次 HTTP 状态码(0=连接层错误)
|
||||
LastErr string `gorm:"size:255" json:"last_err"`
|
||||
SentAt *time.Time `json:"sent_at"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime;index:idx_cb_account,priority:2" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
||||
}
|
||||
|
||||
func (CallbackTask) TableName() string { return "callback_task" }
|
||||
@@ -2,20 +2,9 @@ package entity
|
||||
|
||||
import "time"
|
||||
|
||||
// StatsUserDaily 用户日统计(偶数整点刷新)。
|
||||
type StatsUserDaily struct {
|
||||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
AccountID int64 `gorm:"uniqueIndex:uk_account_date,priority:1" json:"account_id"`
|
||||
StatDate time.Time `gorm:"type:date;uniqueIndex:uk_account_date,priority:2;index:idx_date" json:"stat_date"`
|
||||
SubmitCount int `json:"submit_count"`
|
||||
SuccessCount int `json:"success_count"`
|
||||
FailCount int `json:"fail_count"`
|
||||
UnknownCount int `json:"unknown_count"`
|
||||
FeeTotal float64 `gorm:"type:decimal(12,4)" json:"fee_total"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
||||
}
|
||||
|
||||
func (StatsUserDaily) TableName() string { return "stats_user_daily" }
|
||||
// (S2·R2 裁决)StatsUserDaily 用户日统计已废弃:与 billing_consume_log
|
||||
// 同粒度双空壳,消耗事实源统一归 billing_consume_log(账号×业务×天),
|
||||
// 表由迁移 v20 DROP;用户侧统计查询走 billing_consume_log API。
|
||||
|
||||
// StatsChannelDaily 通道日统计(含估算余额)。
|
||||
type StatsChannelDaily struct {
|
||||
|
||||
@@ -80,6 +80,7 @@ var (
|
||||
BlockedRegion = &Error{"ISC:036", "地区屏蔽", "region blocked", http.StatusForbidden}
|
||||
AuditStatusConflict = &Error{"ISC:037", "审核状态冲突(非待审)", "audit status conflict", http.StatusConflict}
|
||||
TaskStatusConflict = &Error{"ISC:038", "任务状态冲突(不可取消/改期)", "task status conflict", http.StatusConflict}
|
||||
OrderStatusConflict = &Error{"ISC:039", "订单状态冲突(非待确认)", "order status conflict", http.StatusConflict}
|
||||
|
||||
// ---- 通用请求错误(400 段,段值与 HTTP 语义对齐)----
|
||||
InvalidParam = &Error{"ISC:400", "参数错误", "invalid parameter", http.StatusBadRequest}
|
||||
@@ -115,8 +116,8 @@ var registry = func() map[string]*Error {
|
||||
NoChannelRoute, ChannelRejected, ContentNonCompliant, SendLimited,
|
||||
TextTooLong, DailyLimitExceeded, CreditLimitExceeded, NightRateLimited, FiveGBlocked,
|
||||
BlockedRegion,
|
||||
// 审核与任务(Day 43)
|
||||
AuditStatusConflict, TaskStatusConflict,
|
||||
// 审核与任务(Day 43;S2 增订单冲突)
|
||||
AuditStatusConflict, TaskStatusConflict, OrderStatusConflict,
|
||||
// 通用
|
||||
InvalidParam, Unauthorized, TokenExpired, Forbidden, NotFound,
|
||||
MethodNotAllowed, RateLimited,
|
||||
|
||||
@@ -84,8 +84,8 @@ func TestAuthoritativeCatalog(t *testing.T) {
|
||||
t.Errorf("ByCode(%s) = {%s %s}, 期望 {%s %s}", code, got.Code, got.Msg, code, msg)
|
||||
}
|
||||
}
|
||||
// 全量 51 条 = 成功码 ISC:000 + 错误码 50 条(权威清单;Day42 +ISC:036;Day43 +037/038)。
|
||||
if n := len(All()); n != 51 {
|
||||
t.Errorf("预定义码数量 = %d, 期望 51(含成功码)", n)
|
||||
// 全量 52 条 = 成功码 ISC:000 + 错误码 51 条(权威清单;Day42 +ISC:036;Day43 +037/038;S2-D3 +039)。
|
||||
if n := len(All()); n != 52 {
|
||||
t.Errorf("预定义码数量 = %d, 期望 52(含成功码)", n)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,6 +75,27 @@ var (
|
||||
// ReportRecheckTotal pending 后新事务复查结果({result}: finalized=补刀
|
||||
// 终态/still_pending=真未到齐;双盲竞态健康度=finalized 占比)。
|
||||
ReportRecheckTotal *prometheus.CounterVec
|
||||
|
||||
// SettleAggregateRows 结算域消耗日汇总落账行数(scope: account=billing_
|
||||
// consume_log / channel=stats_channel_daily;幂等 UPSERT 重刷亦计入,
|
||||
// 单调累计供观测 job 活性,S5 基线期再评估是否细化)。
|
||||
SettleAggregateRows *prometheus.CounterVec
|
||||
// SettleAggregateErrors 消耗日汇总执行失败次数(失败 Warn 不中断,
|
||||
// 幂等下轮自愈;持续增长=真故障信号)。
|
||||
SettleAggregateErrors prometheus.Counter
|
||||
|
||||
// ---- 回调推送域(S4·C1) ----
|
||||
// CallbackPushTotal 推送结果计数({type}: report/uplink × {result}:
|
||||
// sent=2xx 送达/retry=失败待重试/dead=死信)。
|
||||
CallbackPushTotal *prometheus.CounterVec
|
||||
// CallbackBlacklistTotal 上行自动拉黑新增数(S4·C2;幂等冲突不计)。
|
||||
CallbackBlacklistTotal prometheus.Counter
|
||||
|
||||
// ArchiveRows 归档迁出 MySQL→CH 的明细行数(单调累计,S4·D3)。
|
||||
ArchiveRows prometheus.Counter
|
||||
// ArchiveErrors 归档链路失败计数(校验不匹配/CH/MySQL 故障;下轮自愈,
|
||||
// 持续增长=真故障信号)。
|
||||
ArchiveErrors prometheus.Counter
|
||||
)
|
||||
|
||||
var registry = prometheus.NewRegistry()
|
||||
@@ -171,6 +192,36 @@ func init() {
|
||||
Help: "pending 后新事务复查结果(result: finalized=补刀终态/still_pending=真未到齐)",
|
||||
}, []string{LabelKind})
|
||||
|
||||
SettleAggregateRows = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Name: "go_sms_settle_aggregate_rows_total",
|
||||
Help: "结算域消耗日汇总落账行数(scope: account/channel;UPSERT 重刷亦计入,S2)",
|
||||
}, []string{LabelKind})
|
||||
|
||||
SettleAggregateErrors = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "go_sms_settle_aggregate_errors_total",
|
||||
Help: "消耗日汇总执行失败次数(幂等下轮自愈,持续增长=真故障,S2)",
|
||||
})
|
||||
|
||||
CallbackPushTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Name: "go_sms_callback_push_total",
|
||||
Help: "回调推送结果(type: report/uplink × result: sent/retry/dead,S4·C1)",
|
||||
}, []string{"type", "result"})
|
||||
|
||||
CallbackBlacklistTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "go_sms_callback_blacklist_total",
|
||||
Help: "上行自动拉黑新增数(幂等冲突不计,S4·C2)",
|
||||
})
|
||||
|
||||
ArchiveRows = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "go_sms_archive_rows_total",
|
||||
Help: "明细归档迁出 MySQL→ClickHouse 行数(按日校验通过后计,S4·D3)",
|
||||
})
|
||||
|
||||
ArchiveErrors = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "go_sms_archive_errors_total",
|
||||
Help: "归档链路失败计数(下轮自愈,持续增长=真故障,S4·D3)",
|
||||
})
|
||||
|
||||
registry.MustRegister(
|
||||
HTTPRequestsTotal, HTTPRequestDuration, HTTPRequestsInFlight,
|
||||
SMSSubmitTotal, SMSReportTotal, KafkaProducerMessages,
|
||||
@@ -179,6 +230,9 @@ func init() {
|
||||
LongSmsMergedTotal, LongSmsDegradeTotal, LongSmsDupFragTotal,
|
||||
LongSmsExpireFailTotal,
|
||||
ReportPendingTotal, ReportRecheckTotal, ReportRequeueTotal,
|
||||
SettleAggregateRows, SettleAggregateErrors,
|
||||
CallbackPushTotal, CallbackBlacklistTotal,
|
||||
ArchiveRows, ArchiveErrors,
|
||||
)
|
||||
// Go 运行时与进程指标
|
||||
registry.MustRegister(
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Package storage Day 44:对象存储(MinIO,S3 API)——号码文件/报表/检测结果落桶。
|
||||
// Package storage Day 44:对象存储(S3 API;2026-11-13 起后端=RustFS,客户端=minio-go 通用 S3 SDK)——号码文件/报表/检测结果落桶。
|
||||
// 最小接口 ObjectStore 供服务层依赖倒置(测试用内存实现,见 fileimport_test)。
|
||||
package storage
|
||||
|
||||
@@ -17,14 +17,14 @@ type ObjectStore interface {
|
||||
Put(ctx context.Context, key string, r io.Reader, size int64, contentType string) error
|
||||
}
|
||||
|
||||
// MinIOStore minio-go 实现(启动时 EnsureBucket)。
|
||||
type MinIOStore struct {
|
||||
// S3Store minio-go 实现(启动时 EnsureBucket)。
|
||||
type S3Store struct {
|
||||
cli *minio.Client
|
||||
bucket string
|
||||
}
|
||||
|
||||
// NewMinIO 按配置建客户端并确保桶存在(幂等)。
|
||||
func NewMinIO(ctx context.Context, endpoint, accessKey, secretKey, bucket string, useSSL bool) (*MinIOStore, error) {
|
||||
// NewS3 按配置建客户端并确保桶存在(幂等)。
|
||||
func NewS3(ctx context.Context, endpoint, accessKey, secretKey, bucket string, useSSL bool) (*S3Store, error) {
|
||||
cli, err := minio.New(endpoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(accessKey, secretKey, ""),
|
||||
Secure: useSSL,
|
||||
@@ -41,11 +41,11 @@ func NewMinIO(ctx context.Context, endpoint, accessKey, secretKey, bucket string
|
||||
return nil, fmt.Errorf("storage: make bucket %s: %w", bucket, err)
|
||||
}
|
||||
}
|
||||
return &MinIOStore{cli: cli, bucket: bucket}, nil
|
||||
return &S3Store{cli: cli, bucket: bucket}, nil
|
||||
}
|
||||
|
||||
// Put 实现 ObjectStore。
|
||||
func (s *MinIOStore) Put(ctx context.Context, key string, r io.Reader, size int64, contentType string) error {
|
||||
func (s *S3Store) Put(ctx context.Context, key string, r io.Reader, size int64, contentType string) error {
|
||||
if contentType == "" {
|
||||
contentType = "application/octet-stream"
|
||||
}
|
||||
@@ -92,37 +92,74 @@ func VerifyAuth(sourceAddr, secret string, ts uint32, authenticator []byte) bool
|
||||
return true
|
||||
}
|
||||
|
||||
// ConnectResp 包体(status 1B + authenticator_ISMG 16B + version 1B)。
|
||||
// ConnectResp 包体(CMPP 2.0 规范:Status 4B 大端 + authenticator_ISMG 16B + version 1B = 21B)。
|
||||
type ConnectResp struct {
|
||||
Status byte
|
||||
Secret string // 计算服务端鉴权串
|
||||
Secret string // 计算服务端鉴权串(shared_secret;server 侧应填账号密钥)
|
||||
AuthenticatorClient []byte // 客户端鉴权串(参与计算)
|
||||
// ClientVersion 客户端 Connect.Version 声明(Issue#3 加固):≥0x30 按 3.0
|
||||
// 布局应答(1B Status,18B 包体,ISMG 串 Status 以 1B 参与)——防严格 3.0
|
||||
// 客户端按 1B 校验 ISMG 串错位报 -1;0/0x20 按 2.0(4B Status,21B 包体)。
|
||||
ClientVersion byte
|
||||
}
|
||||
|
||||
// AuthenticatorISMG 计算服务端鉴权串:MD5(status + authenticatorSource + shared)。
|
||||
// Issue#3 修正:CMPP 2.0 规范中 Status 以 4 字节大端参与拼接(3.0 为 1 字节)。
|
||||
func (r *ConnectResp) AuthenticatorISMG() []byte {
|
||||
sum := md5.Sum(append(append([]byte{r.Status}, r.AuthenticatorClient...), []byte(r.Secret)...))
|
||||
buf := make([]byte, 0, 4+len(r.AuthenticatorClient)+len(r.Secret))
|
||||
if r.ClientVersion >= 0x30 { // 3.0:Status 1B 参与
|
||||
buf = append(buf, r.Status)
|
||||
} else { // 2.0:Status 4B 大端参与
|
||||
var st [4]byte
|
||||
binary.BigEndian.PutUint32(st[:], uint32(r.Status))
|
||||
buf = append(buf, st[:]...)
|
||||
}
|
||||
buf = append(buf, r.AuthenticatorClient...)
|
||||
buf = append(buf, r.Secret...)
|
||||
sum := md5.Sum(buf)
|
||||
return sum[:]
|
||||
}
|
||||
|
||||
// Marshal 编码 ConnectResp 包体。
|
||||
// Issue#3 根因修复:CMPP 2.0 规范 CMPP_CONNECT_RESP 的 Status 为 4 字节整数
|
||||
// (与 3.0 的 1 字节不同——而 2.0 的 SUBMIT_RESP Result 恰为 1 字节,两版本在该
|
||||
// 两字段上互为镜像,极易混淆)。此前按 1B 编码(18B 包体),标准 2.0 客户端按
|
||||
// 4B 大端解析 Status 时读到 [status,MD5[0..2]] 恒非 0,客户连接被误判失败
|
||||
// (客户端工具报 -1);自家客户端同按 1B 解析,自测格式对称掩盖了缺陷。
|
||||
// Issue#3 加固:按客户端 Connect.Version 协商——0x30 回 3.0 布局(18B)。
|
||||
func (r *ConnectResp) Marshal() []byte {
|
||||
b := make([]byte, 1+16+1)
|
||||
b[0] = r.Status
|
||||
copy(b[1:17], r.AuthenticatorISMG())
|
||||
b[17] = 0x20
|
||||
if r.ClientVersion >= 0x30 {
|
||||
b := make([]byte, 1+16+1)
|
||||
b[0] = r.Status
|
||||
copy(b[1:17], r.AuthenticatorISMG())
|
||||
b[17] = 0x30
|
||||
return b
|
||||
}
|
||||
b := make([]byte, 4+16+1)
|
||||
binary.BigEndian.PutUint32(b[0:4], uint32(r.Status))
|
||||
copy(b[4:20], r.AuthenticatorISMG())
|
||||
b[20] = 0x20
|
||||
return b
|
||||
}
|
||||
|
||||
// ParseConnectResp 解析 ConnectResp(客户端校验服务端身份用)。
|
||||
// 自适应两种规范布局(Issue#3):
|
||||
// - CMPP 2.0(≥20B):Status 4B 大端 + AuthenticatorISMG 16B(+Version 1B=21B)
|
||||
// - CMPP 3.0/兼容(17~19B):Status 1B + AuthenticatorISMG 16B(+Version 1B)
|
||||
// - 其余短包:取首字节(历史行为;主流网关均回完整包)。
|
||||
func ParseConnectResp(body []byte) (status byte, authenticatorISMG []byte, err error) {
|
||||
if len(body) < 1 {
|
||||
return 0, nil, errors.New("cmpp: connect_resp 包体为空")
|
||||
}
|
||||
if len(body) >= 17 {
|
||||
authenticatorISMG = body[1:17]
|
||||
if len(body) >= 20 { // CMPP 2.0 布局(4B Status)
|
||||
status = byte(binary.BigEndian.Uint32(body[0:4]))
|
||||
authenticatorISMG = body[4:20]
|
||||
return status, authenticatorISMG, nil
|
||||
}
|
||||
return body[0], authenticatorISMG, nil
|
||||
if len(body) >= 17 { // 3.0/兼容布局(1B Status)
|
||||
return body[0], body[1:17], nil
|
||||
}
|
||||
return body[0], nil, nil
|
||||
}
|
||||
|
||||
// ActiveTest 心跳包体(空)。
|
||||
|
||||
@@ -169,6 +169,12 @@ func TestClient_LoginOK(t *testing.T) {
|
||||
t.Fatalf("握手应成功: %v", err)
|
||||
}
|
||||
defer func() { _ = c.Close() }()
|
||||
// 网关侧 logins.Add 在 WritePacket 之后:客户端握手完成与计数落账存在
|
||||
// 调度窗口,全量 -race 并行下曾偶发 0(轮询吸收,免 flaky 阻塞门禁)。
|
||||
deadline := time.Now().Add(time.Second)
|
||||
for g.logins.Load() != 1 && time.Now().Before(deadline) {
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
if g.logins.Load() != 1 {
|
||||
t.Fatalf("网关成功登录数 = %d", g.logins.Load())
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package cmpp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"encoding/binary"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -66,6 +68,9 @@ func TestConnectRespAuth(t *testing.T) {
|
||||
clientAuth := c.Authenticator(ts)
|
||||
resp := &ConnectResp{Status: 0, Secret: "pw", AuthenticatorClient: clientAuth}
|
||||
body := resp.Marshal()
|
||||
if len(body) != 21 { // Issue#3:CMPP 2.0 布局 4B+16B+1B
|
||||
t.Fatalf("connect_resp 包体应为 21 字节: %d", len(body))
|
||||
}
|
||||
status, ismg, err := ParseConnectResp(body)
|
||||
if err != nil || status != 0 || len(ismg) != 16 {
|
||||
t.Fatalf("解析: status=%d err=%v", status, err)
|
||||
@@ -76,6 +81,71 @@ func TestConnectRespAuth(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestConnectRespCmpp20StatusLayout Issue#3 回归:服务端 Status 必须按 CMPP 2.0
|
||||
// 规范编为 4 字节大端——此前 1B 编码(3.0 布局)致标准 2.0 客户端按 4B 解析读到
|
||||
// [status,MD5[0..2]] 恒非 0,客户连接被误判失败(客户端工具报 -1)。
|
||||
func TestConnectRespCmpp20StatusLayout(t *testing.T) {
|
||||
resp := &ConnectResp{Status: ConnectBadAuth, Secret: "pw",
|
||||
AuthenticatorClient: bytes.Repeat([]byte{0xAB}, 16)}
|
||||
body := resp.Marshal()
|
||||
if len(body) != 21 {
|
||||
t.Fatalf("CMPP 2.0 connect_resp 包体应为 21 字节(4+16+1): %d", len(body))
|
||||
}
|
||||
// 第三方标准 2.0 客户端视角:前 4 字节大端 uint32 即 Status
|
||||
if got := binary.BigEndian.Uint32(body[0:4]); got != uint32(ConnectBadAuth) {
|
||||
t.Fatalf("2.0 客户端按 4B 解析 Status=%d, 期望 %d", got, ConnectBadAuth)
|
||||
}
|
||||
// 成功路径:前 4 字节全零,任何布局客户端均读到 0
|
||||
ok := (&ConnectResp{Status: ConnectOK, Secret: "pw",
|
||||
AuthenticatorClient: bytes.Repeat([]byte{0x01}, 16)}).Marshal()
|
||||
if got := binary.BigEndian.Uint32(ok[0:4]); got != 0 {
|
||||
t.Fatalf("成功状态 4B 解析应全零: %d", got)
|
||||
}
|
||||
// 自家客户端自适应解析(21B → 2.0 布局)拒绝码同样正确
|
||||
status, ismg, err := ParseConnectResp(body)
|
||||
if err != nil || status != ConnectBadAuth || len(ismg) != 16 {
|
||||
t.Fatalf("自适应解析: status=%d ismg=%dB err=%v", status, len(ismg), err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseConnectRespAdaptive Issue#3:客户端自适应两种规范布局。
|
||||
// 2.0(21B/4B Status)与 3.0(18B/1B Status)网关均可正确读出状态码。
|
||||
func TestParseConnectRespAdaptive(t *testing.T) {
|
||||
// 3.0/兼容布局(1B status=3 + 16B auth + 1B ver)
|
||||
b18 := append([]byte{3}, bytes.Repeat([]byte{0x11}, 17)...)
|
||||
if st, ismg, err := ParseConnectResp(b18); err != nil || st != 3 || len(ismg) != 16 {
|
||||
t.Fatalf("18B 布局解析: st=%d ismg=%dB err=%v", st, len(ismg), err)
|
||||
}
|
||||
// 2.0 无 Version 变体(4B status + 16B = 20B)
|
||||
b20 := append([]byte{0, 0, 0, 4}, bytes.Repeat([]byte{0x22}, 16)...)
|
||||
if st, ismg, err := ParseConnectResp(b20); err != nil || st != 4 || len(ismg) != 16 {
|
||||
t.Fatalf("20B 布局解析: st=%d ismg=%dB err=%v", st, len(ismg), err)
|
||||
}
|
||||
// 空包拒绝
|
||||
if _, _, err := ParseConnectResp(nil); err == nil {
|
||||
t.Fatal("空包应拒绝")
|
||||
}
|
||||
}
|
||||
|
||||
// TestConnectRespVersion30Layout Issue#3 加固:3.0 布局(1B Status)往返 +
|
||||
// ISMG 串按 1B status 计算(严格 3.0 客户端校验口径)。
|
||||
func TestConnectRespVersion30Layout(t *testing.T) {
|
||||
clientAuth := bytes.Repeat([]byte{0x33}, 16)
|
||||
r := &ConnectResp{Status: 0, Secret: "pw", AuthenticatorClient: clientAuth, ClientVersion: 0x30}
|
||||
body := r.Marshal()
|
||||
if len(body) != 18 {
|
||||
t.Fatalf("3.0 包体应 18B: %d", len(body))
|
||||
}
|
||||
st, ismg, err := ParseConnectResp(body)
|
||||
if err != nil || st != 0 || len(ismg) != 16 {
|
||||
t.Fatalf("解析: st=%d err=%v", st, err)
|
||||
}
|
||||
want := md5.Sum(append(append([]byte{0}, clientAuth...), []byte("pw")...))
|
||||
if !bytes.Equal(want[:], ismg) {
|
||||
t.Fatal("3.0 ISMG 串应按 1B status 计算")
|
||||
}
|
||||
}
|
||||
|
||||
// TestPacketReadWrite 包读写往返(WritePacket 自动补 TotalLength)。
|
||||
func TestPacketReadWrite(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
@@ -68,6 +68,13 @@ type ConnLimiter interface {
|
||||
ConnLimit(sourceAddr string) int
|
||||
}
|
||||
|
||||
// ISMGSecretSource 可选接口(Issue#3 加固):提供账号共享密钥——ConnectResp 的
|
||||
// AuthenticatorISMG=MD5(Status+AuthenticatorSource+shared_secret) 按规范掺入;
|
||||
// 不实现=空串(旧口径,严格校验 ISMG 的客户端会判失败)。
|
||||
type ISMGSecretSource interface {
|
||||
ISMGSecret(sourceAddr string) string
|
||||
}
|
||||
|
||||
// SubmitRateLimiter 可选接口(Day 62):账号级单链提交限速 条/s(≤0=不限)。
|
||||
type SubmitRateLimiter interface {
|
||||
SubmitRateLimit(sourceAddr string) int
|
||||
@@ -272,6 +279,9 @@ func (s *Server) handleConn(ctx context.Context, conn net.Conn) {
|
||||
ss := &session{conn: conn}
|
||||
defer s.dropConn(conn, ss)
|
||||
ip := remoteIP(conn)
|
||||
// Issue#3 联调补盲:TCP 到达即留痕(此前 Connect 成功零日志,客户「连没连上」不可追溯;
|
||||
// 配合 scripts/cmpp-capture.sh 抓包分析双通道定位)。
|
||||
logger.L().Info("cmpp_conn_accepted", zap.String("client_ip", ip))
|
||||
|
||||
// ---- Connect 握手 ----
|
||||
_ = conn.SetReadDeadline(time.Now().Add(s.cfg.LoginTimeout))
|
||||
@@ -284,6 +294,17 @@ func (s *Server) handleConn(ctx context.Context, conn net.Conn) {
|
||||
if perr == nil {
|
||||
status = s.h.Connect(sourceAddr, ts, authenticator, ip)
|
||||
}
|
||||
// Issue#3 加固:按客户端 Connect.Version 协商应答布局(≥0x30 回 3.0 的
|
||||
// 1B Status/18B;否则 2.0 的 4B Status/21B)——版本字节在包体 offset 22。
|
||||
ver := byte(0x20)
|
||||
if len(body) >= 23 && body[22] >= 0x30 {
|
||||
ver = body[22]
|
||||
}
|
||||
// Issue#3 加固:ISMG 鉴权串按规范掺 shared_secret(可选接口;不实现=空串)。
|
||||
smsSecret := ""
|
||||
if ss, ok := s.h.(ISMGSecretSource); ok {
|
||||
smsSecret = ss.ISMGSecret(sourceAddr)
|
||||
}
|
||||
// Day 62:鉴权通过后先占会话名额(账号级连接数上限,可选接口;持锁原子
|
||||
// 检查+登记防并发超卖),超限回保留码 0x8 后断链。
|
||||
if status == ConnectOK {
|
||||
@@ -297,7 +318,8 @@ func (s *Server) handleConn(ctx context.Context, conn net.Conn) {
|
||||
status = ConnectTooMany
|
||||
}
|
||||
}
|
||||
resp := &ConnectResp{Status: status, Secret: "", AuthenticatorClient: authenticator}
|
||||
resp := &ConnectResp{Status: status, Secret: smsSecret,
|
||||
AuthenticatorClient: authenticator, ClientVersion: ver}
|
||||
if werr := WritePacket(conn, &Header{CommandID: CmdConnectResp, SequenceID: h.SequenceID}, resp.Marshal()); werr != nil {
|
||||
return
|
||||
}
|
||||
@@ -308,6 +330,10 @@ func (s *Server) handleConn(ctx context.Context, conn net.Conn) {
|
||||
if rl, ok := s.h.(SubmitRateLimiter); ok {
|
||||
ss.rate.limit = rl.SubmitRateLimit(sourceAddr)
|
||||
}
|
||||
// Issue#3 联调补盲:Connect 成功留痕(账号/IP/客户端声明版本——与抓包时间线互证)。
|
||||
logger.L().Info("cmpp_conn_ok",
|
||||
zap.String("source_addr", sourceAddr), zap.String("client_ip", ip),
|
||||
zap.Uint8("ver", ver))
|
||||
|
||||
// ---- 会话循环 ----
|
||||
for {
|
||||
|
||||
@@ -5,6 +5,8 @@ package cmpp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"net"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -36,6 +38,9 @@ func (h *fakeHandler) Connect(sourceAddr string, ts uint32, authenticator []byte
|
||||
return ConnectOK
|
||||
}
|
||||
|
||||
// ISMGSecret Issue#3 加固:ConnectResp 鉴权串掺入 shared_secret(fake=密钥本身)。
|
||||
func (h *fakeHandler) ISMGSecret(sourceAddr string) string { return h.secret }
|
||||
|
||||
func (h *fakeHandler) Submit(sourceAddr, srcID string, dest []string,
|
||||
content string, udh *UDHInfo, registeredDelivery uint8, msgFmt uint8) (uint64, byte) {
|
||||
if h.rejectAll {
|
||||
@@ -113,6 +118,50 @@ func TestServer_ConnectOKAndSubmit(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestServer_ConnectVersion30 Issue#3 加固:按客户端 Connect.Version 协商应答——
|
||||
// 0x30 回 3.0 布局(18B/1B Status/version 回显 0x30);0x20 回 2.0(21B/4B)。
|
||||
// 裸 TCP 第三方视角(自家 client 固定发 0x20 测不到该分支)。
|
||||
func TestServer_ConnectVersion30(t *testing.T) {
|
||||
h := &fakeHandler{secret: "abc123", submits: make(chan *submitRecord, 1)}
|
||||
srv := startTestServer(t, h)
|
||||
|
||||
send := func(ver byte) []byte {
|
||||
t.Helper()
|
||||
conn, err := net.DialTimeout("tcp", srv.Addr(), 2*time.Second)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() { _ = conn.Close() }()
|
||||
_ = conn.SetDeadline(time.Now().Add(3 * time.Second))
|
||||
req := &Connect{SourceAddr: "user1", Secret: "abc123", Version: ver}
|
||||
if err := WritePacket(conn, &Header{CommandID: CmdConnect, SequenceID: 7},
|
||||
req.Marshal(authTimestamp(time.Now()))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, body, err := ReadPacket(conn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
b30 := send(0x30)
|
||||
if len(b30) != 18 {
|
||||
t.Fatalf("3.0 客户端应答应 18B(1B Status): %d", len(b30))
|
||||
}
|
||||
if b30[0] != 0 {
|
||||
t.Fatalf("3.0 Status(1B)=%d 期望 0", b30[0])
|
||||
}
|
||||
if b30[17] != 0x30 {
|
||||
t.Fatalf("3.0 应答回显 version=0x30, got 0x%02x", b30[17])
|
||||
}
|
||||
|
||||
b20 := send(0x20)
|
||||
if len(b20) != 21 || binary.BigEndian.Uint32(b20[0:4]) != 0 {
|
||||
t.Fatalf("2.0 应答应 21B/Status=0: len=%d", len(b20))
|
||||
}
|
||||
}
|
||||
|
||||
// TestServer_PushDeliver Day 61(P0.5):Connect 注册会话→PushDeliver 下发状态报告
|
||||
// →客户端 OnDeliver 收到(含 MsgID/Stat 口径)→服务端吸收 Deliver_Resp。
|
||||
func TestServer_PushDeliver(t *testing.T) {
|
||||
|
||||
@@ -25,6 +25,17 @@ type Conn interface {
|
||||
// BatchInsert 批量插入:INSERT INTO table (columns...) VALUES 每行一行。
|
||||
// 空行返回 nil;类型由调用方保证与表结构一致。
|
||||
BatchInsert(ctx context.Context, table string, columns []string, rows [][]any) error
|
||||
// Query 只读查询(S4·D3 冷查;游标语义 Next/Scan/Close,args 走原生 bind)。
|
||||
Query(ctx context.Context, query string, args ...any) (Rows, error)
|
||||
// Exec DDL/管理语句(建表/DROP PARTITION;args 走原生 bind)。
|
||||
Exec(ctx context.Context, query string, args ...any) error
|
||||
Close() error
|
||||
}
|
||||
|
||||
// Rows 查询游标抽象(驱动行集满足该接口即可)。
|
||||
type Rows interface {
|
||||
Next() bool
|
||||
Scan(dest ...any) error
|
||||
Close() error
|
||||
}
|
||||
|
||||
@@ -55,6 +66,23 @@ func (c *nativeConn) Ping(ctx context.Context) error { return c.conn.Ping(ctx) }
|
||||
|
||||
func (c *nativeConn) Close() error { return c.conn.Close() }
|
||||
|
||||
// Query 只读查询透传(*ch.Rows 实现 Rows 接口)。
|
||||
func (c *nativeConn) Query(ctx context.Context, query string, args ...any) (Rows, error) {
|
||||
rows, err := c.conn.Query(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("clickhouse: 查询失败: %w", err)
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
// Exec DDL/管理语句透传。
|
||||
func (c *nativeConn) Exec(ctx context.Context, query string, args ...any) error {
|
||||
if err := c.conn.Exec(ctx, query, args...); err != nil {
|
||||
return fmt.Errorf("clickhouse: 执行失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *nativeConn) BatchInsert(ctx context.Context, table string, columns []string, rows [][]any) error {
|
||||
if table == "" || len(columns) == 0 {
|
||||
return fmt.Errorf("clickhouse: table/columns 不能为空")
|
||||
|
||||
@@ -4,9 +4,9 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestInsertSQL 验证语句构造。
|
||||
// TestInsertSQL 验证语句构造(S1-B:标识符反引号化,兼容含连字符表名)。
|
||||
func TestInsertSQL(t *testing.T) {
|
||||
if got := insertSQL("mt", []string{"id", "mobile"}); got != "INSERT INTO mt (id,mobile)" {
|
||||
if got := insertSQL("mt", []string{"id", "mobile"}); got != "INSERT INTO `mt` (`id`,`mobile`)" {
|
||||
t.Errorf("insertSQL = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,8 +29,8 @@ func TestMigrations_IdempotentAndVersioned(t *testing.T) {
|
||||
seen[mig.ID] = true
|
||||
last = mig
|
||||
}
|
||||
if last == nil || last.ID != "v19_longsms_pending" {
|
||||
t.Fatalf("终版迁移应为 v19_longsms_pending,实际 %v", last)
|
||||
if last == nil || last.ID != "v22_detail_partitions" {
|
||||
t.Fatalf("终版迁移应为 v22_detail_partitions,实际 %v", last)
|
||||
}
|
||||
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
@@ -42,36 +42,28 @@ func TestMigrations_IdempotentAndVersioned(t *testing.T) {
|
||||
}
|
||||
opts := gormigrate.DefaultOptions
|
||||
opts.TableName = "schema_migrations"
|
||||
// 注册末七迁移(现 v13..v19;历史 v10/V11 含 MessageTask AutoMigrate,gleberez
|
||||
// sqlite 索引不查重,双块连跑撞 idx_account_time——同 v1+v6 已知方言缺口;v13/v14
|
||||
// 各建各表、v15 AutoMigrate 加列、v16/v17 空库守卫补建 detail 后加查询索引、
|
||||
// v18/v19 新表 AutoMigrate,单块两遍覆盖幂等)。
|
||||
// 注册末七迁移(现 v16..v22;历史 v10/V11 含 MessageTask AutoMigrate,gleberez
|
||||
// sqlite 索引不查重,双块连跑撞 idx_account_time——同 v1+v6 已知方言缺口;
|
||||
// v15 AutoMigrate 加列、v16/v17/v20 空库守卫补建表后加列/索引、v22 分区 MySQL 守卫(sqlite 跳过)、
|
||||
// v18/v19/v21 新表 AutoMigrate,v20 另含 DROP IF EXISTS 与 casbin HasTable 守卫,
|
||||
// 单块两遍覆盖幂等)。
|
||||
migs := Migrations()
|
||||
gm := gormigrate.New(db.Session(&gorm.Session{Logger: logger.Discard}), opts, migs[len(migs)-7:])
|
||||
if err := gm.Migrate(); err != nil {
|
||||
t.Fatalf("v13..v19 首遍失败: %v", err)
|
||||
t.Fatalf("v16..v22 首遍失败: %v", err)
|
||||
}
|
||||
if err := gm.Migrate(); err != nil {
|
||||
t.Fatalf("v13..v19 二遍不幂等: %v", err)
|
||||
t.Fatalf("v16..v22 二遍不幂等: %v", err)
|
||||
}
|
||||
v, err := CurrentMigrationVersion(db)
|
||||
if err != nil {
|
||||
t.Fatalf("查版本失败: %v", err)
|
||||
}
|
||||
if v == nil || *v != "v19_longsms_pending" {
|
||||
t.Fatalf("版本应为 v19_longsms_pending,实际 %v", v)
|
||||
}
|
||||
if !db.Migrator().HasTable(&entity.MessageReportSeg{}) {
|
||||
t.Fatal("v13 后应存在 message_report_seg 表")
|
||||
}
|
||||
if !db.Migrator().HasColumn(&entity.MessageReportSeg{}, "PkTotal") {
|
||||
t.Fatal("v13 后应存在 pk_total 列(段级配对键)")
|
||||
}
|
||||
// Day 62:v15 后 account_config 应有账号级 CMPP 配额两列。
|
||||
if !db.Migrator().HasColumn(&entity.AccountConfig{}, "CmppMaxConns") ||
|
||||
!db.Migrator().HasColumn(&entity.AccountConfig{}, "CmppSpeed") {
|
||||
t.Fatal("v15 后应存在 cmpp_max_conns/cmpp_speed 列")
|
||||
if v == nil || *v != "v22_detail_partitions" {
|
||||
t.Fatalf("版本应为 v22_detail_partitions,实际 %v", v)
|
||||
}
|
||||
// (v13/v14/v15 断言随窗口右移移除:各表/列由 v13~v15 建,
|
||||
// 现窗口 v16..v22 不含;全量链路覆盖仍由 dev MySQL 实库验证。)
|
||||
// Day 64:v16 后 message_detail 应有明细查询两索引(空库守卫路径建表+显式加索引)。
|
||||
if !db.Migrator().HasIndex(&entity.MessageDetail{}, "idx_detail_created") ||
|
||||
!db.Migrator().HasIndex(&entity.MessageDetail{}, "idx_detail_channel_created") {
|
||||
@@ -85,6 +77,25 @@ func TestMigrations_IdempotentAndVersioned(t *testing.T) {
|
||||
if !db.Migrator().HasIndex(&entity.MessageDetail{}, "idx_business_created") {
|
||||
t.Fatal("v17 后应存在 idx_business_created 索引")
|
||||
}
|
||||
// S2:v20 后 billing_consume_log 应有 unknown_count/updated_at 列,
|
||||
// account_user 应有 rebate_rate 列,settle_ladder 建表且 stats_user_daily 废弃。
|
||||
if !db.Migrator().HasColumn(&entity.BillingConsumeLog{}, "UnknownCount") ||
|
||||
!db.Migrator().HasColumn(&entity.BillingConsumeLog{}, "UpdatedAt") {
|
||||
t.Fatal("v20 后 billing_consume_log 应有 unknown_count/updated_at 列")
|
||||
}
|
||||
if !db.Migrator().HasColumn(&entity.AccountUser{}, "RebateRate") {
|
||||
t.Fatal("v20 后 account_user 应有 rebate_rate 列")
|
||||
}
|
||||
if !db.Migrator().HasTable(&entity.SettleLadder{}) {
|
||||
t.Fatal("v20 后应存在 settle_ladder 表")
|
||||
}
|
||||
if db.Migrator().HasTable("stats_user_daily") {
|
||||
t.Fatal("v20 后 stats_user_daily 应已 DROP(R2 单一事实源裁决)")
|
||||
}
|
||||
// S4·C1:v21 后 callback_task 建表(空库守卫 AutoMigrate 路径)。
|
||||
if !db.Migrator().HasTable(&entity.CallbackTask{}) {
|
||||
t.Fatal("v21 后应存在 callback_task 表")
|
||||
}
|
||||
}
|
||||
|
||||
// newTestDB 内存 SQLite + 自动建表。
|
||||
|
||||
@@ -2,6 +2,8 @@ package mysql
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
@@ -10,6 +12,7 @@ import (
|
||||
|
||||
"go-sms/internal/model/entity"
|
||||
errscodes "go-sms/internal/pkg/errors"
|
||||
"go-sms/internal/util"
|
||||
)
|
||||
|
||||
// Migrations 迁移清单(顺序执行,ID 单调递增;禁止修改已发布 ID)。
|
||||
@@ -393,6 +396,155 @@ func Migrations() []*gormigrate.Migration {
|
||||
return nil // 表可手工 DROP,回滚为空操作
|
||||
},
|
||||
},
|
||||
{
|
||||
// S2(结算域 D 层)表基线,详见 docs/review/s2-settle-table-review.md:
|
||||
// ①billing_consume_log 补 unknown_count/updated_at——消耗事实源
|
||||
// (R2:stats_user_daily 废弃),幂等 UPSERT 重跑需更新痕迹;
|
||||
// ②account_user 补 rebate_rate——代理返点率(R4,小数表示);
|
||||
// ③DROP stats_user_daily——同粒度双空壳,单一事实源归 billing_
|
||||
// consume_log;空库路径 v0 已不再建它,DROP IF EXISTS 双方言幂等;
|
||||
// ④新表 settle_ladder——阶梯价档位(R5,出账时按月成功总量落档);
|
||||
// ⑤casbin 补结算域策略(ops settle:write / customer settle:read,
|
||||
// 逐条 NOT EXISTS 守卫;末段迁移单块重放路径无 casbin_rule 表,
|
||||
// HasTable 守卫跳过,全量链路种入)。
|
||||
// 加列走显式 SQL 不走 AutoMigrate——glebarez SQLite 下 entity 加
|
||||
// 字段触发表 diff 重建会抹既有索引(v16/v17 教训);空库守卫分支
|
||||
// AutoMigrate 整表无此风险(表本不存在)。
|
||||
ID: "v20_settle_domain",
|
||||
Migrate: func(tx *gorm.DB) error {
|
||||
if !tx.Migrator().HasTable(&entity.BillingConsumeLog{}) { // 空库守卫(同 v16/v17)
|
||||
if err := tx.AutoMigrate(&entity.BillingConsumeLog{}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if !tx.Migrator().HasColumn(&entity.BillingConsumeLog{}, "UnknownCount") {
|
||||
if err := tx.Exec("ALTER TABLE billing_consume_log ADD COLUMN unknown_count INT NOT NULL DEFAULT 0").Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if !tx.Migrator().HasColumn(&entity.BillingConsumeLog{}, "UpdatedAt") {
|
||||
if err := tx.Exec("ALTER TABLE billing_consume_log ADD COLUMN updated_at DATETIME NULL DEFAULT NULL").Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if !tx.Migrator().HasTable(&entity.AccountUser{}) { // 空库守卫
|
||||
if err := tx.AutoMigrate(&entity.AccountUser{}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if !tx.Migrator().HasColumn(&entity.AccountUser{}, "RebateRate") {
|
||||
if err := tx.Exec("ALTER TABLE account_user ADD COLUMN rebate_rate DECIMAL(5,4) NOT NULL DEFAULT 0").Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := tx.Exec("DROP TABLE IF EXISTS stats_user_daily").Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if !tx.Migrator().HasTable(&entity.SettleLadder{}) { // 新表,AutoMigrate 安全(同 v18/v19)
|
||||
if err := tx.AutoMigrate(&entity.SettleLadder{}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if tx.Migrator().HasTable(&casbinRule{}) { // v5 才建 casbin_rule
|
||||
policies := []casbinRule{
|
||||
{Ptype: "p", V0: "ops", V1: "settle", V2: "write"},
|
||||
{Ptype: "p", V0: "customer", V1: "settle", V2: "read"}, // 客户查自己账单,scope 在 handler 侧过滤
|
||||
}
|
||||
for _, p := range policies {
|
||||
var n int64
|
||||
if err := tx.Model(&casbinRule{}).
|
||||
Where("ptype = ? AND v0 = ? AND v1 = ? AND v2 = ?", p.Ptype, p.V0, p.V1, p.V2).
|
||||
Count(&n).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
if err := tx.Create(&p).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
Rollback: func(tx *gorm.DB) error {
|
||||
return nil // 列/表/策略可手工重建或清除,回滚为空操作(v16~v19 惯例)
|
||||
},
|
||||
},
|
||||
// v21(S4·C1)回调推送域:callback_task 队列表(指数退避+死信可查)
|
||||
// + ops 角色回调管理策略。表仅 AutoMigrate 空库守卫建(同 v18~v20 惯例;
|
||||
// 存量库首跑由 migrate 链自动补建,无列变更)。
|
||||
{
|
||||
ID: "v21_callback_domain",
|
||||
Migrate: func(tx *gorm.DB) error {
|
||||
if !tx.Migrator().HasTable(&entity.CallbackTask{}) {
|
||||
if err := tx.AutoMigrate(&entity.CallbackTask{}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if tx.Migrator().HasTable(&casbinRule{}) { // v5 才建 casbin_rule
|
||||
policies := []casbinRule{
|
||||
{Ptype: "p", V0: "ops", V1: "callback", V2: "read"}, // 死信/任务查询
|
||||
{Ptype: "p", V0: "ops", V1: "callback", V2: "write"}, // 死信重派
|
||||
}
|
||||
for _, p := range policies {
|
||||
var n int64
|
||||
if err := tx.Model(&casbinRule{}).
|
||||
Where("ptype = ? AND v0 = ? AND v1 = ? AND v2 = ?", p.Ptype, p.V0, p.V1, p.V2).
|
||||
Count(&n).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
if err := tx.Create(&p).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
Rollback: func(tx *gorm.DB) error {
|
||||
return nil // 新表可手工清除,回滚为空操作(v16~v20 惯例)
|
||||
},
|
||||
},
|
||||
// v22(S4·D3)明细按天分区:RANGE(id)(雪花高位=时间戳,当天零点边界
|
||||
// 可精确计算;主键即分区键无需改造唯一键)。分区集=热 5 天+未来 3 天
|
||||
// 缓冲+pmax 兜底;滚动维护与过期 DROP 由 archive.Service 承担(归档校验
|
||||
// 通过才删,见 internal/service/archive/partition.go)。MySQL only——
|
||||
// sqlite 单测无分区(物理优化,方言守卫跳过)。
|
||||
{
|
||||
ID: "v22_detail_partitions",
|
||||
Migrate: func(tx *gorm.DB) error {
|
||||
if tx.Name() != "mysql" {
|
||||
return nil
|
||||
}
|
||||
var n int64
|
||||
if err := tx.Raw(
|
||||
"SELECT COUNT(*) FROM information_schema.PARTITIONS " +
|
||||
"WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'message_detail' " +
|
||||
"AND PARTITION_NAME IS NOT NULL"). // 非分区表也占一行(NULL),须排除
|
||||
Scan(&n).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if n > 0 {
|
||||
return nil // 已分区(链路重放/EnsurePartitions 已先行)
|
||||
}
|
||||
now := time.Now()
|
||||
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
||||
var parts []string
|
||||
for i := -4; i <= 3; i++ { // 热 5 天(今日-4..今日)+未来 3 天
|
||||
day := today.AddDate(0, 0, i)
|
||||
boundary := (day.AddDate(0, 0, 1).UnixMilli() - util.Epoch) << (util.WorkerIDBits + util.SequenceBits)
|
||||
parts = append(parts, fmt.Sprintf("PARTITION p%s VALUES LESS THAN (%d)",
|
||||
day.Format("20060102"), boundary))
|
||||
}
|
||||
parts = append(parts, "PARTITION pmax VALUES LESS THAN MAXVALUE")
|
||||
return tx.Exec("ALTER TABLE message_detail PARTITION BY RANGE (id) (" +
|
||||
strings.Join(parts, ", ") + ")").Error
|
||||
},
|
||||
Rollback: func(tx *gorm.DB) error {
|
||||
return nil // REMOVE PARTITIONING 可手工执行,回滚为空操作(v16~v21 惯例)
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
"go-sms/internal/service/openapi"
|
||||
"go-sms/internal/service/report"
|
||||
"go-sms/internal/service/send"
|
||||
"go-sms/internal/service/sysconfig"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -80,8 +81,19 @@ type Service struct {
|
||||
// 长短信片聚合器(批次 2278141 修复):入向 UDH 拼接多片按组聚合,
|
||||
// 到齐合并为一条逻辑 task;超时逐片退化直发。见 longsms.go。
|
||||
grouper *longSmsGrouper
|
||||
// sysCfg Issue#3 方案B:系统配置源(账号 ext_code 空时回退系统默认接入码
|
||||
// cmpp.default_ext_code);nil=无系统回退(行为不变,测试/降级场景)。
|
||||
sysCfg SystemConfigGetter
|
||||
}
|
||||
|
||||
// SystemConfigGetter 系统配置读取接口(由 sysconfig.Service 实现;解耦包依赖)。
|
||||
type SystemConfigGetter interface {
|
||||
Get(ctx context.Context, key string) string
|
||||
}
|
||||
|
||||
// SetSystemConfig 注入系统配置源(可选;gateway 装配调用,nil=无系统回退)。
|
||||
func (s *Service) SetSystemConfig(g SystemConfigGetter) { s.sysCfg = g }
|
||||
|
||||
// accountCacheEntry 账号缓存条目(u/cfg nil=查无,负缓存防穿透)。
|
||||
type accountCacheEntry struct {
|
||||
u, cfg any // *entity.AccountUser | *entity.AccountConfig | nil
|
||||
@@ -263,6 +275,17 @@ func (s *Service) Connect(sourceAddr string, ts uint32, authenticator []byte, cl
|
||||
// Submit 实现 cmpp.ServerHandler:账号/通道号前缀校验→SendMass 受理。
|
||||
// 长短信片(udh 非 nil 且单目的码)改走聚合器:按组拼接合并为一条逻辑
|
||||
// 短信单次受理(批次 2278141 修复,决策①A/②A/③——详见 longsms.go);其余
|
||||
// ISMGSecret 实现 cmpp.ISMGSecretSource:ConnectResp 鉴权串的 shared_secret
|
||||
// (账号 ApiSecret;查无账号=空串——该场景应答本就 status≠0,客户端按状态码判)。
|
||||
// 与 Connect 同走 loadAccountDB 直查(握手期非热路径,口径一致)。
|
||||
func (s *Service) ISMGSecret(sourceAddr string) string {
|
||||
_, cfg := s.loadAccountDB(context.Background(), sourceAddr)
|
||||
if cfg == nil {
|
||||
return ""
|
||||
}
|
||||
return cfg.ApiSecret
|
||||
}
|
||||
|
||||
// (普通提交/多目的码片)走原直发路径不变。
|
||||
func (s *Service) Submit(sourceAddr, srcID string, dest []string,
|
||||
content string, udh *cmpp.UDHInfo, registeredDelivery uint8, msgFmt uint8) (uint64, byte) {
|
||||
@@ -271,31 +294,47 @@ func (s *Service) Submit(sourceAddr, srcID string, dest []string,
|
||||
defer func() { submitStats.record(time.Since(t0)) }()
|
||||
ctx := context.Background()
|
||||
if udh != nil && udh.Total > 1 && len(dest) == 1 {
|
||||
cfg, result := s.precheck(ctx, sourceAddr, srcID)
|
||||
cfg, ext, result := s.precheck(ctx, sourceAddr, srcID)
|
||||
if cfg == nil {
|
||||
return 0, result
|
||||
}
|
||||
return s.grouper.submitFragment(s.api, cfg, sourceAddr, srcID,
|
||||
return s.grouper.submitFragment(s.api, cfg, ext, sourceAddr, srcID,
|
||||
dest[0], content, udh, msgFmt == cmpp.MsgFmtUCS2)
|
||||
}
|
||||
return s.submitDirect(ctx, sourceAddr, srcID, dest, content, msgFmt)
|
||||
}
|
||||
|
||||
// precheck 账号加载+通道号前缀校验(cfg=nil 时返回值为 CMPP 拒绝码)。
|
||||
// precheck 账号加载+通道号校验(cfg=nil 时返回值为 CMPP 拒绝码)。
|
||||
// 直发与长短信聚合共用(聚合组以首片账号快照为准)。
|
||||
func (s *Service) precheck(ctx context.Context, sourceAddr, srcID string) (*entity.AccountConfig, byte) {
|
||||
// Issue#3 方案B:返回生效校验前缀 ext——账号 ext_code 优先,空则回退系统默认
|
||||
// cmpp.default_ext_code(账号级定制>系统级统一;两层均空=不校验)。
|
||||
func (s *Service) precheck(ctx context.Context, sourceAddr, srcID string) (*entity.AccountConfig, string, byte) {
|
||||
_, cfg := s.loadAccount(ctx, sourceAddr)
|
||||
if cfg == nil {
|
||||
return nil, cmpp.SubmitRejected
|
||||
return nil, "", cmpp.SubmitRejected
|
||||
}
|
||||
// 通道号前缀:账号 ext_code 非空时 SrcID 必须以其为前缀(UT:0012 语义)。
|
||||
if cfg.ExtCode != "" && !strings.HasPrefix(srcID, cfg.ExtCode) {
|
||||
ext := s.effectiveExtCode(ctx, cfg)
|
||||
// 通道号前缀:ext 非空时 SrcID 必须以其为前缀(UT:0012 语义;系统默认同)。
|
||||
if ext != "" && !strings.HasPrefix(srcID, ext) {
|
||||
logger.L().Warn("cmpp_submit_src_prefix_mismatch",
|
||||
zap.String("source_addr", sourceAddr), zap.String("src_id", srcID),
|
||||
zap.String("ext_code", cfg.ExtCode))
|
||||
return nil, cmpp.SubmitBadCode
|
||||
zap.String("ext_code", ext),
|
||||
zap.Bool("from_system_default", cfg.ExtCode == ""))
|
||||
return nil, "", cmpp.SubmitBadCode
|
||||
}
|
||||
return cfg, cmpp.SubmitOK
|
||||
return cfg, ext, cmpp.SubmitOK
|
||||
}
|
||||
|
||||
// effectiveExtCode 生效校验前缀:账号 ext_code 优先;空则回退系统默认
|
||||
// (Issue#3 方案B;sysCfg 未注入/系统键未配置=空=不校验)。
|
||||
func (s *Service) effectiveExtCode(ctx context.Context, cfg *entity.AccountConfig) string {
|
||||
if cfg.ExtCode != "" {
|
||||
return cfg.ExtCode
|
||||
}
|
||||
if s.sysCfg != nil {
|
||||
return s.sysCfg.Get(ctx, sysconfig.KeyDefaultExtCode)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// submitDirect 普通提交(原 Submit 主体):P0-1(Day 63)快路径受理。
|
||||
@@ -303,7 +342,7 @@ func (s *Service) precheck(ctx context.Context, sourceAddr, srcID string) (*enti
|
||||
func (s *Service) submitDirect(ctx context.Context, sourceAddr, srcID string,
|
||||
dest []string, content string, msgFmt uint8) (uint64, byte) {
|
||||
tAcc := time.Now()
|
||||
cfg, result := s.precheck(ctx, sourceAddr, srcID)
|
||||
cfg, ext, result := s.precheck(ctx, sourceAddr, srcID)
|
||||
tAccCost := time.Since(tAcc)
|
||||
if cfg == nil {
|
||||
return 0, result
|
||||
@@ -316,7 +355,7 @@ func (s *Service) submitDirect(ctx context.Context, sourceAddr, srcID string,
|
||||
PhoneList: dest,
|
||||
Content: content,
|
||||
SignName: "", // CMPP 内容自带【签名】,与 Day41 验收二变口径一致
|
||||
ExtCode: strings.TrimPrefix(srcID, cfg.ExtCode),
|
||||
ExtCode: strings.TrimPrefix(srcID, ext),
|
||||
// Day 70:按 MsgFmt 分流计费口径(UCS2=8:每码元 2B;fee≡上游真实片数)。
|
||||
ContentEncoding: ucs2Encoding(msgFmt == cmpp.MsgFmtUCS2),
|
||||
}, remoteIPPlaceholder)
|
||||
@@ -356,7 +395,8 @@ func (s *Service) Bind(systemID, password, clientIP string) smpp.Status {
|
||||
return smpp.StatusOK
|
||||
}
|
||||
|
||||
// SubmitSM 实现 smpp.ServerHandler:源地址前缀校验→SendMass 受理。
|
||||
// SubmitSM 实现 smpp.ServerHandler:源地址校验(账号 ext_code 优先,空回退系统
|
||||
// 默认接入码——Issue#3 方案B,与 CMPP precheck 同口径)→SendMass 受理。
|
||||
func (s *Service) SubmitSM(systemID, srcAddr, destAddr, content string,
|
||||
registeredDelivery uint8) (string, smpp.Status) {
|
||||
ctx := context.Background()
|
||||
@@ -364,7 +404,8 @@ func (s *Service) SubmitSM(systemID, srcAddr, destAddr, content string,
|
||||
if cfg == nil {
|
||||
return "", smpp.StatusSysErr
|
||||
}
|
||||
if cfg.ExtCode != "" && !strings.HasPrefix(srcAddr, cfg.ExtCode) {
|
||||
ext := s.effectiveExtCode(ctx, cfg)
|
||||
if ext != "" && !strings.HasPrefix(srcAddr, ext) {
|
||||
return "", smpp.StatusSysErr
|
||||
}
|
||||
resp := s.api.SendMass(ctx, &openapi.AuthContext{
|
||||
@@ -373,7 +414,7 @@ func (s *Service) SubmitSM(systemID, srcAddr, destAddr, content string,
|
||||
}, &openapi.MassReq{
|
||||
PhoneList: []string{destAddr},
|
||||
Content: content,
|
||||
ExtCode: strings.TrimPrefix(srcAddr, cfg.ExtCode),
|
||||
ExtCode: strings.TrimPrefix(srcAddr, ext),
|
||||
}, remoteIPPlaceholder)
|
||||
if resp.Code != openapi.CodeOK {
|
||||
return "", smpp.StatusSysErr
|
||||
|
||||
@@ -90,14 +90,16 @@ func longSmsKey(accountID int64, srcID, mobile string, ref uint8) string {
|
||||
}
|
||||
|
||||
// submitFragment 片提交:入组缓存(未齐回组ID 不发)/到齐合并单次受理。
|
||||
// precheck(账号+通道号前缀)已由调用方 Submit 完成,cfg 为验过快照。
|
||||
// precheck(账号+通道号校验)已由调用方 Submit 完成,cfg 为验过快照;
|
||||
// ext=生效校验前缀(账号 ext_code 或系统默认,Issue#3 方案B)——扩展码
|
||||
// 剥离口径与直发一致(TrimPrefix(srcID, ext))。
|
||||
func (g *longSmsGrouper) submitFragment(api longSmsAPI, cfg *entity.AccountConfig,
|
||||
sourceAddr, srcID, mobile, content string, udh *cmpp.UDHInfo, ucs2 bool) (uint64, byte) {
|
||||
ext, sourceAddr, srcID, mobile, content string, udh *cmpp.UDHInfo, ucs2 bool) (uint64, byte) {
|
||||
auth := &openapi.AuthContext{
|
||||
AccountID: cfg.AccountID, Username: sourceAddr, Config: cfg,
|
||||
Source: send.TaskSourceCMPP, // Day 60:受理来源打标(CMPP 接入)
|
||||
}
|
||||
ext := strings.TrimPrefix(srcID, cfg.ExtCode)
|
||||
extCode := strings.TrimPrefix(srcID, ext)
|
||||
key := longSmsKey(cfg.AccountID, srcID, mobile, udh.Ref)
|
||||
|
||||
isNew := false
|
||||
@@ -112,7 +114,7 @@ func (g *longSmsGrouper) submitFragment(api longSmsAPI, cfg *entity.AccountConfi
|
||||
zap.String("source_addr", sourceAddr), zap.String("mobile", mobile),
|
||||
zap.Uint8("ref", udh.Ref), zap.Uint8("group_total", grp.total),
|
||||
zap.Uint8("frag_total", udh.Total))
|
||||
return directSubmit(api, auth, mobile, ext, content, ucs2)
|
||||
return directSubmit(api, auth, mobile, extCode, content, ucs2)
|
||||
}
|
||||
if !ok {
|
||||
if len(g.groups) >= g.maxGroup {
|
||||
@@ -127,7 +129,7 @@ func (g *longSmsGrouper) submitFragment(api longSmsAPI, cfg *entity.AccountConfi
|
||||
}
|
||||
grp = &pendingLong{
|
||||
groupID: uint64(api.NextMsgID()),
|
||||
auth: auth, srcID: srcID, mobile: mobile, extCode: ext,
|
||||
auth: auth, srcID: srcID, mobile: mobile, extCode: extCode,
|
||||
total: udh.Total, ref: udh.Ref, ucs2: ucs2, frags: make(map[uint8]string),
|
||||
}
|
||||
g.groups[key] = grp
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
// sysconfig_fallback_test.go Issue#3 方案B:SrcID 校验两级回退链——
|
||||
// 账号 ext_code 优先 > 系统默认 cmpp.default_ext_code > 均空放行。
|
||||
package access
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"go-sms/internal/model/entity"
|
||||
"go-sms/internal/protocol/cmpp"
|
||||
)
|
||||
|
||||
// sysCfgStub 固定值系统配置桩。
|
||||
type sysCfgStub struct{ v string }
|
||||
|
||||
func (s sysCfgStub) Get(_ context.Context, _ string) string { return s.v }
|
||||
|
||||
// TestSubmit_SystemDefaultFallback 账号 ext_code 空 → 回退系统默认:
|
||||
// 匹配系统前缀受理、不匹配回 9、命中日志可辨识。
|
||||
func TestSubmit_SystemDefaultFallback(t *testing.T) {
|
||||
db := newAccessDB(t)
|
||||
u := entity.AccountUser{Username: "noext_user", Status: 1, Balance: 10000}
|
||||
if err := db.Create(&u).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := entity.AccountConfig{AccountID: u.ID, Business: "sms",
|
||||
ApiSecret: "shared-secret-32hex", AuditMode: 0} // ExtCode 留空
|
||||
if err := db.Create(&cfg).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s, _ := newSvc(t, db)
|
||||
s.SetSystemConfig(sysCfgStub{v: "SYS:1069"})
|
||||
|
||||
// 系统前缀+扩展 → 过(方案C 动态扩展同口径)。
|
||||
if _, result := s.Submit("noext_user", "SYS:1069001",
|
||||
[]string{"13800138000"}, "【签名】你好", nil, 1, cmpp.MsgFmtGBK); result != cmpp.SubmitOK {
|
||||
t.Fatalf("系统默认前缀应放行: result=%d", result)
|
||||
}
|
||||
// 不匹配系统前缀 → 9。
|
||||
if _, result := s.Submit("noext_user", "XX:9999",
|
||||
[]string{"13800138000"}, "【签名】你好", nil, 1, cmpp.MsgFmtGBK); result != cmpp.SubmitBadCode {
|
||||
t.Fatalf("不匹配系统默认应拒: result=%d", result)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSubmit_AccountExtTakesPriority 账号 ext_code 非空时账号优先——系统默认
|
||||
// 不参与(系统前缀的 SrcID 也拒,证明两级不叠加)。
|
||||
func TestSubmit_AccountExtTakesPriority(t *testing.T) {
|
||||
db := newAccessDB(t)
|
||||
seedAccount(t, db) // ExtCode=UT:0012
|
||||
s, _ := newSvc(t, db)
|
||||
s.SetSystemConfig(sysCfgStub{v: "SYS:1069"})
|
||||
|
||||
if _, result := s.Submit("cmpp_user", "SYS:1069001",
|
||||
[]string{"13800138000"}, "【签名】你好", nil, 1, cmpp.MsgFmtGBK); result != cmpp.SubmitBadCode {
|
||||
t.Fatalf("账号 ext 优先:系统前缀应拒(不叠加放行), result=%d", result)
|
||||
}
|
||||
if _, result := s.Submit("cmpp_user", "UT:001299",
|
||||
[]string{"13800138000"}, "【签名】你好", nil, 1, cmpp.MsgFmtGBK); result != cmpp.SubmitOK {
|
||||
t.Fatalf("账号前缀应放行: result=%d", result)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSubmit_NoSysCfgNoExt 双空放行(getter 未注入/ext 空/系统键空=回归原行为)。
|
||||
func TestSubmit_NoSysCfgNoExt(t *testing.T) {
|
||||
db := newAccessDB(t)
|
||||
u := entity.AccountUser{Username: "noext_user", Status: 1, Balance: 10000}
|
||||
if err := db.Create(&u).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := entity.AccountConfig{AccountID: u.ID, Business: "sms",
|
||||
ApiSecret: "shared-secret-32hex", AuditMode: 0}
|
||||
if err := db.Create(&cfg).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s, _ := newSvc(t, db) // 未注入 sysCfg
|
||||
if _, result := s.Submit("noext_user", "ANY:THING",
|
||||
[]string{"13800138000"}, "【签名】你好", nil, 1, cmpp.MsgFmtGBK); result != cmpp.SubmitOK {
|
||||
t.Fatalf("双空应放行(原行为): result=%d", result)
|
||||
}
|
||||
// 注入但系统键空值=同语义。
|
||||
s.SetSystemConfig(sysCfgStub{v: ""})
|
||||
if _, result := s.Submit("noext_user", "ANY:THING",
|
||||
[]string{"13800138000"}, "【签名】你好", nil, 1, cmpp.MsgFmtGBK); result != cmpp.SubmitOK {
|
||||
t.Fatalf("系统键空应放行: result=%d", result)
|
||||
}
|
||||
}
|
||||
@@ -85,10 +85,10 @@ func IPAllowed(ipBindJSON, clientIP string) bool {
|
||||
|
||||
// ---- 审核模式 ----
|
||||
|
||||
// SetAuditMode 设置审核模式(0免审/1手动/2自动拒绝)。
|
||||
// SetAuditMode 设置审核模式(0免审/1手动/2自动审核——黑词命中拒/未命中过,对齐 audit.ModeAuto 口径)。
|
||||
func (s *Service) SetAuditMode(ctx context.Context, accountID int64, business string, mode int) error {
|
||||
if mode < 0 || mode > 2 {
|
||||
return errs.InvalidParam.WithMsg(fmt.Sprintf("非法审核模式 %d(0免审/1手动/2自动拒绝)", mode))
|
||||
return errs.InvalidParam.WithMsg(fmt.Sprintf("非法审核模式 %d(0免审/1手动/2自动审核·黑词)", mode))
|
||||
}
|
||||
result := s.config.DB().WithContext(ctx).Model(&entity.AccountConfig{}).
|
||||
Where("account_id = ? AND business = ?", accountID, business).
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// service_day25_test.go Day 25:Casbin 策略同步(开户绑定/改角重绑/删除解绑)+ 二级权限。
|
||||
// service_casbin_sync_test.go(原 Day25):Casbin 策略同步(开户绑定/改角重绑/删除解绑)+ 二级权限。
|
||||
package account
|
||||
|
||||
import (
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// service_day29_test.go Day 29:覆盖率补齐——存量路径重建/nil 引擎降级/默认构造分支。
|
||||
// service_coverage_legacy_test.go(原 Day29):覆盖率补齐——存量路径重建/nil 引擎降级/默认构造分支。
|
||||
package account
|
||||
|
||||
import (
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// service_day28_test.go Day 28:上级切换(级联路径+防环)/标星置顶/服务团队/实名状态机。
|
||||
// service_hierarchy_test.go(原 Day28):上级切换(级联路径+防环)/标星置顶/服务团队/实名状态机。
|
||||
package account
|
||||
|
||||
import (
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// service_day26_test.go Day 26:充值(单笔/批量)+ 余额查询。
|
||||
// service_recharge_test.go(原 Day26):充值(单笔/批量)+ 余额查询。
|
||||
// 验收锚点:充值即余额+流水一致(事务);批量行级报错定位。
|
||||
package account
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// service_day27_test.go Day 27:余额预警——阈值触发/冷却去重/可用额度/停用跳过/配置 upsert。
|
||||
// service_warning_test.go(原 Day27):余额预警——阈值触发/冷却去重/可用额度/停用跳过/配置 upsert。
|
||||
package account
|
||||
|
||||
import (
|
||||
@@ -0,0 +1,180 @@
|
||||
// archive_test.go S4·D3:分区边界纯函数+归档全链(sqlite+mock CH)+冷查条件。
|
||||
package archive
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go-sms/internal/model/entity"
|
||||
"go-sms/internal/repository/clickhouse"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
func newArchiveDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{Logger: logger.Discard})
|
||||
if err != nil {
|
||||
t.Fatalf("打开内存库失败: %v", err)
|
||||
}
|
||||
if err := db.AutoMigrate(&entity.MessageDetail{}); err != nil {
|
||||
t.Fatalf("AutoMigrate 失败: %v", err)
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
// mockRows 查询游标打桩(仅支撑 count() 单列场景)。
|
||||
type mockRows struct {
|
||||
vals []int64
|
||||
idx int
|
||||
}
|
||||
|
||||
func (r *mockRows) Next() bool { r.idx++; return r.idx-1 < len(r.vals) }
|
||||
func (r *mockRows) Scan(dest ...any) error {
|
||||
if r.idx-1 < len(r.vals) {
|
||||
v := r.vals[r.idx-1]
|
||||
switch p := dest[0].(type) {
|
||||
case *int64:
|
||||
*p = v
|
||||
case *uint64: // CH count() UInt64 口径
|
||||
*p = uint64(v)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (r *mockRows) Close() error { return nil }
|
||||
|
||||
// mockCH ClickHouse Conn 打桩:记录写入/DDL;count 应答按调用序注入。
|
||||
type mockCH struct {
|
||||
inserted [][]any
|
||||
execs []string
|
||||
countReplies []int64
|
||||
}
|
||||
|
||||
func (m *mockCH) Ping(context.Context) error { return nil }
|
||||
func (m *mockCH) Close() error { return nil }
|
||||
func (m *mockCH) Exec(_ context.Context, q string, _ ...any) error {
|
||||
m.execs = append(m.execs, q)
|
||||
return nil
|
||||
}
|
||||
func (m *mockCH) BatchInsert(_ context.Context, _ string, _ []string, rows [][]any) error {
|
||||
m.inserted = append(m.inserted, rows...)
|
||||
return nil
|
||||
}
|
||||
func (m *mockCH) Query(_ context.Context, q string, _ ...any) (clickhouse.Rows, error) {
|
||||
if strings.Contains(q, "system.parts") {
|
||||
return &mockRows{vals: []int64{1}}, nil // dropCHPartition 探测:分区存在
|
||||
}
|
||||
v := int64(0)
|
||||
if strings.HasPrefix(q, "SELECT count()") && len(m.countReplies) > 0 {
|
||||
v = m.countReplies[0]
|
||||
m.countReplies = m.countReplies[1:]
|
||||
}
|
||||
return &mockRows{vals: []int64{v}}, nil
|
||||
}
|
||||
|
||||
var _ clickhouse.Conn = (*mockCH)(nil)
|
||||
|
||||
// TestDayFloorSnowflakeMonotonic 边界单调+PartName/HotFloor 口径。
|
||||
func TestDayFloorSnowflakeMonotonic(t *testing.T) {
|
||||
base := time.Date(2026, 11, 16, 15, 30, 0, 0, time.Local)
|
||||
if PartName(base) != "p20261116" {
|
||||
t.Fatalf("PartName=%s 期望 p20261116", PartName(base))
|
||||
}
|
||||
prev := DayFloorSnowflake(base)
|
||||
for i := 1; i <= 3; i++ {
|
||||
next := DayFloorSnowflake(base.AddDate(0, 0, i))
|
||||
if next <= prev {
|
||||
t.Fatalf("边界非单调: day+%d", i)
|
||||
}
|
||||
prev = next
|
||||
}
|
||||
if got := HotFloor(base); !got.Equal(time.Date(2026, 11, 12, 0, 0, 0, 0, time.Local)) {
|
||||
t.Fatalf("HotFloor=%v 期望 2026-11-12 零点", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestArchiveOnceFullCycle 归档全链:7 天前 2 行迁出(CH 收到+MySQL 清退,
|
||||
// sqlite 无分区=DELETE 兜底分支),今天行保留;再跑幂等零动作。
|
||||
func TestArchiveOnceFullCycle(t *testing.T) {
|
||||
db := newArchiveDB(t)
|
||||
old := time.Now().AddDate(0, 0, -7)
|
||||
mk := func(id int64, at time.Time, mobile string) {
|
||||
d := entity.MessageDetail{TaskID: 1, AccountID: 5, ChannelID: 7,
|
||||
Mobile: mobile, Status: 2, Fee: 1}
|
||||
d.ID = id
|
||||
d.CreatedAt = at
|
||||
if err := db.Create(&d).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
mk(73021000001, old, "13800001111")
|
||||
mk(73021000002, old.Add(time.Hour), "13800002222")
|
||||
mk(73021000003, time.Now(), "13800003333") // 热窗内:不迁
|
||||
|
||||
ch := &mockCH{countReplies: []int64{2}}
|
||||
s := NewService(db, ch)
|
||||
if done := s.ArchiveOnce(context.Background()); done != 1 {
|
||||
t.Fatalf("应归档 1 天, got %d", done)
|
||||
}
|
||||
if len(ch.inserted) != 2 {
|
||||
t.Fatalf("CH 应收到 2 行, got %d", len(ch.inserted))
|
||||
}
|
||||
var remainOld, remainNew int64
|
||||
db.Model(&entity.MessageDetail{}).Where("mobile = ?", "13800001111").Count(&remainOld)
|
||||
db.Model(&entity.MessageDetail{}).Where("mobile = ?", "13800003333").Count(&remainNew)
|
||||
if remainOld != 0 || remainNew != 1 {
|
||||
t.Fatalf("清退异常: old=%d new=%d 期望 0/1", remainOld, remainNew)
|
||||
}
|
||||
if done := s.ArchiveOnce(context.Background()); done != 0 {
|
||||
t.Fatalf("再跑应 0 天(幂等), got %d", done)
|
||||
}
|
||||
}
|
||||
|
||||
// TestArchiveVerifyMismatchKeepsMySQL 校验不匹配:CH count≠MySQL count →
|
||||
// MySQL 保留(零丢失方向),不计完成。
|
||||
func TestArchiveVerifyMismatchKeepsMySQL(t *testing.T) {
|
||||
db := newArchiveDB(t)
|
||||
old := time.Now().AddDate(0, 0, -7)
|
||||
d := entity.MessageDetail{TaskID: 1, AccountID: 5, Mobile: "13800009999", Status: 2}
|
||||
d.ID = 73021000009
|
||||
d.CreatedAt = old
|
||||
if err := db.Create(&d).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ch := &mockCH{countReplies: []int64{0}}
|
||||
s := NewService(db, ch)
|
||||
if done := s.ArchiveOnce(context.Background()); done != 0 {
|
||||
t.Fatalf("校验不匹配不应计完成, got %d", done)
|
||||
}
|
||||
var n int64
|
||||
db.Model(&entity.MessageDetail{}).Count(&n)
|
||||
if n != 1 {
|
||||
t.Fatalf("MySQL 应保留(零丢失方向), got %d", n)
|
||||
}
|
||||
}
|
||||
|
||||
// TestQueryHistoryWhere 冷查条件构造(六筛选→where 子句与参数序)。
|
||||
func TestQueryHistoryWhere(t *testing.T) {
|
||||
st := int8(2)
|
||||
lo := time.Date(2026, 11, 1, 0, 0, 0, 0, time.Local)
|
||||
hi := lo.AddDate(0, 0, 5)
|
||||
w, args := historyWhere(HistoryQuery{
|
||||
AccountID: 5, Mobile: "138", Status: &st, Start: &lo, End: &hi, TaskID: 9,
|
||||
})
|
||||
for _, want := range []string{
|
||||
"account_id = ?", "mobile = ?", "status = ?",
|
||||
"created_at >= ?", "created_at < ?", "task_id = ?",
|
||||
} {
|
||||
if !strings.Contains(w, want) {
|
||||
t.Fatalf("where 缺 %q: %s", want, w)
|
||||
}
|
||||
}
|
||||
if len(args) != 6 {
|
||||
t.Fatalf("参数数 %d 期望 6", len(args))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
// job.go S4·D3:归档 job——热窗外明细逐日迁出 MySQL→ClickHouse,校验通过才
|
||||
// DROP MySQL 分区(数据零丢失方向:先 CH 后 MySQL;CH 侧重写=DROP PARTITION
|
||||
// +INSERT 当日全量,幂等可重入)。周期 1h(启动先 EnsureSchema+Partitions)。
|
||||
package archive
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go-sms/internal/model/entity"
|
||||
"go-sms/internal/pkg/logger"
|
||||
"go-sms/internal/pkg/metrics"
|
||||
"go-sms/internal/repository/clickhouse"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 默认参数(SSD 基线保守起步)。
|
||||
const (
|
||||
DefaultArchiveEvery = time.Hour // 归档周期
|
||||
archiveBatch = 2000 // MySQL 分批读取行数
|
||||
archiveMaxDays = 62 // 单轮最多处理天数(两月保护,防首轮长事务风暴)
|
||||
)
|
||||
|
||||
// Service 归档服务(db=MySQL;ch 可 nil=禁用归档链路,仅分区维护可用)。
|
||||
type Service struct {
|
||||
db *gorm.DB
|
||||
ch clickhouse.Conn
|
||||
}
|
||||
|
||||
// NewService 构造。
|
||||
func NewService(db *gorm.DB, ch clickhouse.Conn) *Service { return &Service{db: db, ch: ch} }
|
||||
|
||||
// Run 常驻循环(caller 起 goroutine;每轮 Ensure→归档;失败 Warn 下轮自愈)。
|
||||
func (s *Service) Run(ctx context.Context, every time.Duration) {
|
||||
if every <= 0 {
|
||||
every = DefaultArchiveEvery
|
||||
}
|
||||
t := time.NewTicker(every)
|
||||
defer t.Stop()
|
||||
s.EnsureOnce(ctx) // 幂等前置:CH 建表+分区维护
|
||||
s.ArchiveOnce(ctx) // 启动先归档一轮(重启即清退热窗外积压)
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-t.C:
|
||||
s.ArchiveOnce(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// EnsureOnce 幂等前置(CH 建表+分区维护)。
|
||||
func (s *Service) EnsureOnce(ctx context.Context) {
|
||||
if err := s.EnsureCHSchema(ctx); err != nil {
|
||||
logger.L().Warn("archive_ch_schema_failed", zap.Error(err))
|
||||
}
|
||||
if _, err := s.EnsurePartitions(ctx); err != nil {
|
||||
logger.L().Warn("archive_partition_ensure_failed", zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
// ArchiveOnce 单轮归档:找热窗外 distinct 日期→逐日迁出。
|
||||
func (s *Service) ArchiveOnce(ctx context.Context) int {
|
||||
if s.ch == nil {
|
||||
return 0 // CH 未装配:链路禁用(分区维护仍由 EnsureOnce 承担)
|
||||
}
|
||||
floor := HotFloor(time.Now())
|
||||
// DISTINCT 时刻级 created_at(MySQL/glebarez 双方言直 Scan time.Time;
|
||||
// DATE() 在 sqlite 返回 TEXT 难以统一转换——D3 真机+单测双教训),
|
||||
// Go 侧归一化当日零点去重。
|
||||
var times []time.Time
|
||||
if err := s.db.WithContext(ctx).Raw(
|
||||
"SELECT DISTINCT created_at FROM message_detail "+
|
||||
"WHERE created_at < ? ORDER BY created_at LIMIT ?", floor, archiveMaxDays*4).
|
||||
Scan(×).Error; err != nil {
|
||||
logger.L().Warn("archive_scan_days_failed", zap.Error(err))
|
||||
metrics.ArchiveErrors.Inc()
|
||||
return 0
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
var dayTimes []time.Time
|
||||
for _, t := range times {
|
||||
if t.IsZero() {
|
||||
continue
|
||||
}
|
||||
day := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
|
||||
key := day.Format("2006-01-02")
|
||||
if !seen[key] {
|
||||
seen[key] = true
|
||||
dayTimes = append(dayTimes, day)
|
||||
}
|
||||
}
|
||||
done := 0
|
||||
for _, day := range dayTimes {
|
||||
logger.L().Info("archive_day_begin", zap.String("day", day.Format("2006-01-02")))
|
||||
if s.archiveDay(ctx, day) {
|
||||
done++
|
||||
}
|
||||
}
|
||||
if len(dayTimes) > 0 {
|
||||
logger.L().Info("archive_round_done",
|
||||
zap.Int("pending_days", len(dayTimes)), zap.Int("archived_days", done))
|
||||
}
|
||||
return done
|
||||
}
|
||||
|
||||
// archiveDay 单日归档(MySQL count → CH 重写 → CH count 校验 → DROP 分区/兜底 DELETE)。
|
||||
func (s *Service) archiveDay(ctx context.Context, day time.Time) bool {
|
||||
lo := day
|
||||
hi := day.AddDate(0, 0, 1)
|
||||
var mysqlCount int64
|
||||
if err := s.db.WithContext(ctx).Table("message_detail").
|
||||
Where("created_at >= ? AND created_at < ?", lo, hi).Count(&mysqlCount).Error; err != nil {
|
||||
logger.L().Warn("archive_mysql_count_failed", zap.String("day", day.Format("2006-01-02")), zap.Error(err))
|
||||
metrics.ArchiveErrors.Inc()
|
||||
return false
|
||||
}
|
||||
if mysqlCount == 0 {
|
||||
// 无数据但分区存在(历史残留):直接清分区。
|
||||
if dropped, err := s.DropPartition(ctx, day); err == nil && dropped {
|
||||
logger.L().Info("archive_empty_partition_dropped", zap.String("day", day.Format("2006-01-02")))
|
||||
}
|
||||
return false
|
||||
}
|
||||
logger.L().Info("archive_day_counted", zap.String("day", day.Format("2006-01-02")), zap.Int64("rows", mysqlCount))
|
||||
// CH 侧重写(幂等):先 DROP 当日分区,再分批 INSERT 当日全量。
|
||||
if err := s.dropCHPartition(ctx, day); err != nil {
|
||||
logger.L().Warn("archive_ch_drop_failed", zap.String("day", day.Format("2006-01-02")), zap.Error(err))
|
||||
metrics.ArchiveErrors.Inc()
|
||||
return false
|
||||
}
|
||||
var lastID int64
|
||||
batches := 0
|
||||
for {
|
||||
var rows []entity.MessageDetail
|
||||
if err := s.db.WithContext(ctx).
|
||||
Where("created_at >= ? AND created_at < ? AND id > ?", lo, hi, lastID).
|
||||
Order("id ASC").Limit(archiveBatch).Find(&rows).Error; err != nil {
|
||||
logger.L().Warn("archive_mysql_read_failed", zap.String("day", day.Format("2006-01-02")), zap.Error(err))
|
||||
metrics.ArchiveErrors.Inc()
|
||||
return false
|
||||
}
|
||||
if len(rows) == 0 {
|
||||
break
|
||||
}
|
||||
if err := s.ch.BatchInsert(ctx, ArchiveTable, archiveColumns, detailToRows(rows)); err != nil {
|
||||
logger.L().Warn("archive_ch_insert_failed", zap.String("day", day.Format("2006-01-02")), zap.Error(err))
|
||||
metrics.ArchiveErrors.Inc()
|
||||
return false
|
||||
}
|
||||
lastID = rows[len(rows)-1].ID
|
||||
batches++
|
||||
if len(rows) < archiveBatch {
|
||||
break
|
||||
}
|
||||
}
|
||||
// 校验:CH count == MySQL count 才清 MySQL(不等=保留重试,零丢失方向)。
|
||||
chCount, err := s.chCountDay(ctx, day)
|
||||
if err != nil || chCount != mysqlCount {
|
||||
logger.L().Warn("archive_verify_mismatch_keep_mysql",
|
||||
zap.String("day", day.Format("2006-01-02")),
|
||||
zap.Int64("mysql", mysqlCount), zap.Int64("ch", chCount), zap.Error(err))
|
||||
metrics.ArchiveErrors.Inc()
|
||||
return false
|
||||
}
|
||||
dropped, err := s.DropPartition(ctx, day)
|
||||
if err != nil {
|
||||
logger.L().Warn("archive_mysql_drop_failed", zap.String("day", day.Format("2006-01-02")), zap.Error(err))
|
||||
metrics.ArchiveErrors.Inc()
|
||||
return false
|
||||
}
|
||||
if !dropped {
|
||||
// 非分区残留(历史数据无对应分区):DELETE 兜底(限当日窗)。
|
||||
if err := s.db.WithContext(ctx).
|
||||
Where("created_at >= ? AND created_at < ?", lo, hi).
|
||||
Delete(&entity.MessageDetail{}).Error; err != nil {
|
||||
logger.L().Warn("archive_mysql_delete_fallback_failed", zap.String("day", day.Format("2006-01-02")), zap.Error(err))
|
||||
metrics.ArchiveErrors.Inc()
|
||||
return false
|
||||
}
|
||||
}
|
||||
metrics.ArchiveRows.Add(float64(mysqlCount))
|
||||
logger.L().Info("archive_day_done",
|
||||
zap.String("day", day.Format("2006-01-02")), zap.Int64("rows", mysqlCount),
|
||||
zap.Int("batches", batches), zap.Bool("partition_dropped", dropped))
|
||||
return true
|
||||
}
|
||||
|
||||
// detailToRows 实体→CH 行(列序=archiveColumns)。
|
||||
func detailToRows(rows []entity.MessageDetail) [][]any {
|
||||
out := make([][]any, 0, len(rows))
|
||||
for i := range rows {
|
||||
d := &rows[i]
|
||||
out = append(out, []any{
|
||||
d.ID, d.CreatedAt, d.TaskID, d.AccountID, d.ChannelID, d.Mobile,
|
||||
d.Content, d.Province, d.Carrier, d.Status, d.ErrorCode, d.ExtCode,
|
||||
d.CallData, d.Fee, d.SendAt, d.ReportAt, d.Business, d.Source,
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
// Package archive S4·D3:明细热 5 天(MySQL 按天 RANGE(id) 分区)+ 冷数据
|
||||
// ClickHouse 归档。分区键=雪花 id(时间位单调,RANGE(id) 边界=当天零点雪花
|
||||
// 值,无需改主键);分区集=热 5 天(今日-4..今日)+ 未来 3 天缓冲 + pmax 兜底
|
||||
// (Manager 挂掉数据仍可写,恢复后 REORGANIZE 拆分——运维口径)。过期分区
|
||||
// DROP 只发生在归档 job 校验通过后(先 CH 后 MySQL,数据零丢失方向)。
|
||||
package archive
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go-sms/internal/pkg/logger"
|
||||
"go-sms/internal/util"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// HotDays 热窗天数(含今日;需求 2.5「近 5 天热数据」)。
|
||||
const HotDays = 5
|
||||
|
||||
// BufferDays 未来预建分区缓冲天数(写入永不停摆的余量)。
|
||||
const BufferDays = 3
|
||||
|
||||
// partPrefix MySQL 分区名前缀(pYYYYMMDD)。
|
||||
const partPrefix = "p"
|
||||
|
||||
// DayFloorSnowflake 某日 00:00(本地时区)对应的雪花 ID 边界值:
|
||||
// id 高 41 位=相对 Epoch 毫秒 → 边界 = (dayStartMilli - Epoch) << 22。
|
||||
func DayFloorSnowflake(day time.Time) int64 {
|
||||
dayStart := time.Date(day.Year(), day.Month(), day.Day(), 0, 0, 0, 0, day.Location())
|
||||
return (dayStart.UnixMilli() - util.Epoch) << (util.WorkerIDBits + util.SequenceBits)
|
||||
}
|
||||
|
||||
// PartName 分区名(本地时区日期)。
|
||||
func PartName(day time.Time) string { return partPrefix + day.Format("20060102") }
|
||||
|
||||
// hotRange 当前应存在的按天分区日期集(今日-4 .. 今日+3)。
|
||||
func hotRange(now time.Time) []time.Time {
|
||||
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
||||
out := make([]time.Time, 0, HotDays-1+BufferDays+1)
|
||||
for i := -(HotDays - 1); i <= BufferDays; i++ {
|
||||
out = append(out, today.AddDate(0, 0, i))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// existingParts 查询 message_detail 现有分区(名称→边界描述)。
|
||||
// 空 map+isPartitioned=false 表示表未分区。
|
||||
func existingParts(ctx context.Context, db *gorm.DB) (map[string]string, bool, error) {
|
||||
var rows []struct {
|
||||
Name string
|
||||
Desc string
|
||||
}
|
||||
err := db.WithContext(ctx).Raw(
|
||||
"SELECT PARTITION_NAME AS name, PARTITION_DESCRIPTION AS desc_ FROM information_schema.PARTITIONS " +
|
||||
"WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'message_detail' AND PARTITION_NAME IS NOT NULL").
|
||||
Scan(&rows).Error
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
out := map[string]string{}
|
||||
for _, r := range rows {
|
||||
if r.Name == "" {
|
||||
continue // NULL 行(非分区表在 MySQL 8.0 也占一行;方言护栏双保险)
|
||||
}
|
||||
out[r.Name] = r.Desc
|
||||
}
|
||||
return out, len(out) > 0, nil // 存在非空分区名=已分区
|
||||
}
|
||||
|
||||
// partDefs 按天分区子句列表(每天一个,VALUES LESS THAN 次日边界)。
|
||||
func partDefs(days []time.Time) []string {
|
||||
out := make([]string, 0, len(days)+1)
|
||||
for _, d := range days {
|
||||
out = append(out, fmt.Sprintf("PARTITION %s VALUES LESS THAN (%d)",
|
||||
PartName(d), DayFloorSnowflake(d.AddDate(0, 0, 1))))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// EnsurePartitions 幂等维护(MySQL only;sqlite 单测跳过):未分区→一次性
|
||||
// RANGE(id) 建(热集+pmax);已分区→补缺失的按天分区(过期分区不在此删,
|
||||
// 归档 job 校验后 DROP)。每轮返回是否发生了 DDL(观测用)。
|
||||
func (s *Service) EnsurePartitions(ctx context.Context) (bool, error) {
|
||||
if s.db.Name() != "mysql" {
|
||||
return false, nil // sqlite 内存库无分区(物理优化,MySQL 语义等价)
|
||||
}
|
||||
existing, partitioned, err := existingParts(ctx, s.db)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
days := hotRange(time.Now())
|
||||
if !partitioned {
|
||||
ddl := "ALTER TABLE message_detail PARTITION BY RANGE (id) (" +
|
||||
strings.Join(partDefs(days), ", ") + ", PARTITION pmax VALUES LESS THAN MAXVALUE)"
|
||||
if err := s.db.WithContext(ctx).Exec(ddl).Error; err != nil {
|
||||
return true, fmt.Errorf("archive: 首次分区失败: %w", err)
|
||||
}
|
||||
logger.L().Info("archive_partition_initialized", zap.Int("parts", len(days)+1))
|
||||
return true, nil
|
||||
}
|
||||
changed := false
|
||||
for _, d := range days {
|
||||
if _, ok := existing[PartName(d)]; ok {
|
||||
continue
|
||||
}
|
||||
ddl := fmt.Sprintf("ALTER TABLE message_detail ADD PARTITION (%s VALUES LESS THAN (%d))",
|
||||
PartName(d), DayFloorSnowflake(d.AddDate(0, 0, 1)))
|
||||
// pmax 已存在时不能 ADD(区间必须单调追加)——pmax 场景走 REORGANIZE 拆分。
|
||||
if _, hasMax := existing["pmax"]; hasMax {
|
||||
ddl = fmt.Sprintf("ALTER TABLE message_detail REORGANIZE PARTITION pmax INTO (%s VALUES LESS THAN (%d), PARTITION pmax VALUES LESS THAN MAXVALUE)",
|
||||
PartName(d), DayFloorSnowflake(d.AddDate(0, 0, 1)))
|
||||
}
|
||||
if err := s.db.WithContext(ctx).Exec(ddl).Error; err != nil {
|
||||
return changed, fmt.Errorf("archive: 补分区 %s 失败: %w", PartName(d), err)
|
||||
}
|
||||
existing[PartName(d)] = ""
|
||||
changed = true
|
||||
}
|
||||
return changed, nil
|
||||
}
|
||||
|
||||
// DropPartition 删除指定日分区(归档校验后调用;分区不存在返回 false 不报错)。
|
||||
func (s *Service) DropPartition(ctx context.Context, day time.Time) (bool, error) {
|
||||
if s.db.Name() != "mysql" {
|
||||
return false, nil
|
||||
}
|
||||
existing, _, err := existingParts(ctx, s.db)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, ok := existing[PartName(day)]; !ok {
|
||||
return false, nil
|
||||
}
|
||||
if err := s.db.WithContext(ctx).
|
||||
Exec("ALTER TABLE message_detail DROP PARTITION " + PartName(day)).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// HotFloor 热窗下限(今日-4 零点;此前明细属归档对象)。
|
||||
func HotFloor(now time.Time) time.Time {
|
||||
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
||||
return today.AddDate(0, 0, -(HotDays - 1))
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
// schema.go S4·D3:ClickHouse 归档表 DDL(幂等)+ 冷查行扫描。
|
||||
package archive
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"go-sms/internal/repository/clickhouse"
|
||||
)
|
||||
|
||||
// ArchiveTable CH 归档表名(字段与 message_detail 对齐;fee Decimal→Float64,
|
||||
// 可空时间列 Nullable;按天分区 toDate(created_at),ORDER BY id)。
|
||||
const ArchiveTable = "sms_detail_archive"
|
||||
|
||||
// archiveColumns 归档列(与 DDL/INSERT/SELECT 三方对齐,顺序稳定)。
|
||||
var archiveColumns = []string{
|
||||
"id", "created_at", "task_id", "account_id", "channel_id", "mobile",
|
||||
"content", "province", "carrier", "status", "error_code", "ext_code",
|
||||
"call_data", "fee", "send_at", "report_at", "business", "source",
|
||||
}
|
||||
|
||||
// ArchiveTableDDL 建表语句(幂等;PARTITION BY toDate(created_at) 的分区名
|
||||
// =YYYYMMDD,与 ArchiveDayPartition 一致,归档重写=先 DROP PARTITION 再 INSERT)。
|
||||
const ArchiveTableDDL = `
|
||||
CREATE TABLE IF NOT EXISTS sms_detail_archive (
|
||||
id Int64,
|
||||
created_at DateTime64(3),
|
||||
task_id Int64,
|
||||
account_id Int64,
|
||||
channel_id Int64,
|
||||
mobile String,
|
||||
content String,
|
||||
province LowCardinality(String),
|
||||
carrier LowCardinality(String),
|
||||
status Int8,
|
||||
error_code LowCardinality(String),
|
||||
ext_code LowCardinality(String),
|
||||
call_data String,
|
||||
fee Float64,
|
||||
send_at Nullable(DateTime64(3)),
|
||||
report_at Nullable(DateTime64(3)),
|
||||
business LowCardinality(String),
|
||||
source Int8
|
||||
) ENGINE = MergeTree
|
||||
PARTITION BY toDate(created_at)
|
||||
ORDER BY id
|
||||
SETTINGS index_granularity = 8192`
|
||||
|
||||
// EnsureCHSchema 幂等建归档表。
|
||||
func (s *Service) EnsureCHSchema(ctx context.Context) error {
|
||||
if s.ch == nil {
|
||||
return nil // CH 未装配:归档链路禁用(单测/最小部署)
|
||||
}
|
||||
return s.ch.Exec(ctx, ArchiveTableDDL)
|
||||
}
|
||||
|
||||
// ArchiveDayPartition 某日在 CH 的分区名(toDate 表达式分区名=YYYYMMDD)。
|
||||
func ArchiveDayPartition(day time.Time) string { return day.Format("20060102") }
|
||||
|
||||
// dropCHPartition 删除 CH 当日分区(CH 24.3 无 DROP PARTITION IF EXISTS——
|
||||
// 先查 system.parts 存在才 DROP;不存在静默通过)。
|
||||
func (s *Service) dropCHPartition(ctx context.Context, day time.Time) error {
|
||||
rows, err := s.ch.Query(ctx,
|
||||
"SELECT count() FROM system.parts WHERE database = currentDatabase() "+
|
||||
"AND table = ? AND partition = ?", ArchiveTable, ArchiveDayPartition(day))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n := uint64(0) // CH count() 返回 UInt64(Scan *int64 不支持,真机教训)
|
||||
if rows.Next() {
|
||||
if err := rows.Scan(&n); err != nil {
|
||||
_ = rows.Close()
|
||||
return err
|
||||
}
|
||||
}
|
||||
_ = rows.Close()
|
||||
if n == 0 {
|
||||
return nil
|
||||
}
|
||||
return s.ch.Exec(ctx,
|
||||
fmt.Sprintf("ALTER TABLE `%s` DROP PARTITION '%s'", ArchiveTable, ArchiveDayPartition(day)))
|
||||
}
|
||||
|
||||
// chCountDay CH 当日行数(归档校验锚)。
|
||||
func (s *Service) chCountDay(ctx context.Context, day time.Time) (int64, error) {
|
||||
rows, err := s.ch.Query(ctx,
|
||||
fmt.Sprintf("SELECT count() FROM `%s` WHERE toDate(created_at) = ?", ArchiveTable),
|
||||
ArchiveDayPartition(day))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
if !rows.Next() {
|
||||
return 0, fmt.Errorf("archive: CH count 无行返回")
|
||||
}
|
||||
var n uint64 // CH count() 返回 UInt64
|
||||
if err := rows.Scan(&n); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int64(n), nil
|
||||
}
|
||||
|
||||
// ArchiveRow 冷查明细行(CH 扫描 DTO;时间字段 CH 驱动返回 time.Time)。
|
||||
type ArchiveRow struct {
|
||||
ID int64 `json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
TaskID int64 `json:"task_id"`
|
||||
AccountID int64 `json:"account_id"`
|
||||
ChannelID int64 `json:"channel_id"`
|
||||
Mobile string `json:"mobile"`
|
||||
Content string `json:"content"`
|
||||
Province string `json:"province"`
|
||||
Carrier string `json:"carrier"`
|
||||
Status int8 `json:"status"`
|
||||
ErrorCode string `json:"error_code"`
|
||||
ExtCode string `json:"ext_code"`
|
||||
CallData string `json:"call_data"`
|
||||
Fee float64 `json:"fee"`
|
||||
SendAt *time.Time `json:"send_at"`
|
||||
ReportAt *time.Time `json:"report_at"`
|
||||
Business string `json:"business"`
|
||||
Source int8 `json:"source"`
|
||||
}
|
||||
|
||||
// QueryHistory 冷查(CH;分页+账号/手机号/时间窗筛选;验收=冷查<1s)。
|
||||
// 返回行列表与总数(总数另查 count();行窗 id 排序稳定)。
|
||||
func (s *Service) QueryHistory(ctx context.Context, q HistoryQuery) ([]ArchiveRow, int64, error) {
|
||||
if s.ch == nil {
|
||||
return nil, 0, fmt.Errorf("archive: ClickHouse 未配置")
|
||||
}
|
||||
page, size := q.Page, q.PageSize
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if size < 1 || size > 1000 {
|
||||
size = 50
|
||||
}
|
||||
where, args := historyWhere(q)
|
||||
var total int64
|
||||
crows, err := s.ch.Query(ctx,
|
||||
fmt.Sprintf("SELECT count() FROM `%s`%s", ArchiveTable, where), args...)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
{ // CH count() 返回 UInt64(*int64 Scan 不支持——错误不可吞,D3 真机教训)
|
||||
var n uint64
|
||||
if crows.Next() {
|
||||
if err := crows.Scan(&n); err != nil {
|
||||
_ = crows.Close()
|
||||
return nil, 0, err
|
||||
}
|
||||
}
|
||||
_ = crows.Close()
|
||||
total = int64(n)
|
||||
}
|
||||
if total == 0 {
|
||||
return []ArchiveRow{}, 0, nil
|
||||
}
|
||||
lrows, err := s.ch.Query(ctx,
|
||||
fmt.Sprintf("SELECT %s FROM `%s`%s ORDER BY id DESC LIMIT %d OFFSET %d",
|
||||
joinColumns(), ArchiveTable, where, size, (page-1)*size), args...)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer func() { _ = lrows.Close() }()
|
||||
out := make([]ArchiveRow, 0, size)
|
||||
for lrows.Next() {
|
||||
var r ArchiveRow
|
||||
if err := lrows.Scan(&r.ID, &r.CreatedAt, &r.TaskID, &r.AccountID, &r.ChannelID,
|
||||
&r.Mobile, &r.Content, &r.Province, &r.Carrier, &r.Status, &r.ErrorCode,
|
||||
&r.ExtCode, &r.CallData, &r.Fee, &r.SendAt, &r.ReportAt, &r.Business, &r.Source); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
out = append(out, r)
|
||||
}
|
||||
return out, total, nil
|
||||
}
|
||||
|
||||
// HistoryQuery 冷查入参(全部可选;时间窗起闭止开)。
|
||||
type HistoryQuery struct {
|
||||
AccountID int64
|
||||
ChannelID int64
|
||||
TaskID int64
|
||||
Mobile string
|
||||
Status *int8
|
||||
Start *time.Time
|
||||
End *time.Time
|
||||
Page int
|
||||
PageSize int
|
||||
}
|
||||
|
||||
func historyWhere(q HistoryQuery) (string, []any) {
|
||||
conds := []string{"1=1"}
|
||||
var args []any
|
||||
if q.AccountID > 0 {
|
||||
conds = append(conds, "account_id = ?")
|
||||
args = append(args, q.AccountID)
|
||||
}
|
||||
if q.ChannelID > 0 {
|
||||
conds = append(conds, "channel_id = ?")
|
||||
args = append(args, q.ChannelID)
|
||||
}
|
||||
if q.TaskID > 0 {
|
||||
conds = append(conds, "task_id = ?")
|
||||
args = append(args, q.TaskID)
|
||||
}
|
||||
if q.Mobile != "" {
|
||||
conds = append(conds, "mobile = ?")
|
||||
args = append(args, q.Mobile)
|
||||
}
|
||||
if q.Status != nil {
|
||||
conds = append(conds, "status = ?")
|
||||
args = append(args, *q.Status)
|
||||
}
|
||||
if q.Start != nil {
|
||||
conds = append(conds, "created_at >= ?")
|
||||
// CH 绑定 time.Time 存在 UTC 化歧义;归档写入=墙钟语义,查询同口径
|
||||
// 预格式化字符串(D3 真机教训)。
|
||||
args = append(args, q.Start.Format("2006-01-02 15:04:05"))
|
||||
}
|
||||
if q.End != nil {
|
||||
conds = append(conds, "created_at < ?")
|
||||
args = append(args, q.End.Format("2006-01-02 15:04:05"))
|
||||
}
|
||||
return " WHERE " + joinStrings(conds, " AND "), args
|
||||
}
|
||||
|
||||
func joinStrings(ss []string, sep string) string {
|
||||
out := ""
|
||||
for i, s := range ss {
|
||||
if i > 0 {
|
||||
out += sep
|
||||
}
|
||||
out += s
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func joinColumns() string { return joinStrings(archiveColumns, ", ") }
|
||||
|
||||
var _ clickhouse.Conn = (clickhouse.Conn)(nil) // 引用守卫(接口扩展与本包解耦)
|
||||
@@ -107,7 +107,7 @@ func TestRouteModes(t *testing.T) {
|
||||
t4 := mkTask(t, db, 3, "低价代开发票")
|
||||
task, rec, err = s.Route(ctx, t4.ID)
|
||||
if err != nil || task.AuditStatus != AuditRejected || rec == nil {
|
||||
t.Fatalf("自动拒绝 got status=%d err=%v", task.AuditStatus, err)
|
||||
t.Fatalf("自动档黑词拒绝 got status=%d err=%v", task.AuditStatus, err)
|
||||
}
|
||||
if rec.Reason != "命中敏感词: 发票" {
|
||||
t.Fatalf("拒绝留痕应含命中词, got %q", rec.Reason)
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
// Package callback S4·C1:HTTP 回调推送引擎(指数退避+死信可查)。
|
||||
// 入队源:report 终态首锁(EnqueueReport,与 CMPP Deliver 同挂点)/uplink
|
||||
// 消费(EnqueueUplink);投递=PushWorker 周期扫描 callback_task(需求 2.5
|
||||
// 回执/上行绑定地址;实施计划 5.8 重试队列+指数退避)。
|
||||
// 客户通知双通道并存:CMPP 活跃会话走 cmpp-deliver(deliver_push.go),
|
||||
// 配置 report_url/uplink_url 的账号走本引擎(验收=死信可查/重派可恢复)。
|
||||
package callback
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"go-sms/internal/model/entity"
|
||||
"go-sms/internal/pkg/logger"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// urlCacheTTL 账号回调地址缓存 TTL(策略变更最迟生效窗口)。
|
||||
const urlCacheTTL = 60 * time.Second
|
||||
|
||||
// statForStatus 明细终态 int8 → 回调载荷状态词(与 statForDeliver 同口径;
|
||||
// 成功=DELIVRD,失败/未知=errCode 透传截 16B,空兜底 UNKNOWN)。
|
||||
func statForStatus(status int8, errCode string) string {
|
||||
if status == 2 {
|
||||
return "DELIVRD"
|
||||
}
|
||||
if errCode == "" {
|
||||
return "UNKNOWN"
|
||||
}
|
||||
if r := []rune(errCode); len(r) > 16 {
|
||||
return string(r[:16])
|
||||
}
|
||||
return errCode
|
||||
}
|
||||
|
||||
// ReportPayload 回执终态回调载荷(标准 JSON;detail_id 与 Submit_Resp MsgID
|
||||
// 同口径,客户据此对账)。
|
||||
type ReportPayload struct {
|
||||
Type string `json:"type"` // "report"
|
||||
DetailID int64 `json:"detail_id"`
|
||||
AccountID int64 `json:"account_id"`
|
||||
Mobile string `json:"mobile"`
|
||||
Status int8 `json:"status"` // 2 成功 3 失败 4 未知
|
||||
Stat string `json:"stat"` // DELIVRD/UNDELIV/上游原始码
|
||||
ErrorCode string `json:"error_code"`
|
||||
ReportAt time.Time `json:"report_at"`
|
||||
}
|
||||
|
||||
// UplinkPayload 上行消息回调载荷(需求 2.5:拉黑不影响上行推送)。
|
||||
type UplinkPayload struct {
|
||||
Type string `json:"type"` // "uplink"
|
||||
UplinkID int64 `json:"uplink_id"`
|
||||
AccountID int64 `json:"account_id"`
|
||||
DestCode string `json:"dest_code"`
|
||||
SrcMobile string `json:"src_mobile"`
|
||||
Content string `json:"content"`
|
||||
RecvAt string `json:"recv_at"` // RFC3339(透传事件原值)
|
||||
}
|
||||
|
||||
type urlEntry struct {
|
||||
report, uplink string
|
||||
exp time.Time
|
||||
}
|
||||
|
||||
// Service 回调入队器:URL 缓存(60s TTL)+ 载荷构造 + callback_task 写入。
|
||||
// 实现 report.ReportEnqueuer / uplink.UplinkEnqueuer(装配层注入)。
|
||||
type Service struct {
|
||||
db *gorm.DB
|
||||
|
||||
mu sync.RWMutex
|
||||
urls map[int64]urlEntry
|
||||
}
|
||||
|
||||
// NewService 构造。
|
||||
func NewService(db *gorm.DB) *Service {
|
||||
return &Service{db: db, urls: map[int64]urlEntry{}}
|
||||
}
|
||||
|
||||
// urlsFor 账号回调地址(缓存 miss 回源 account_config;DB 故障返回空串=
|
||||
// 该账号本轮跳过,下轮回源自愈)。
|
||||
func (s *Service) urlsFor(ctx context.Context, accountID int64) (report, uplink string) {
|
||||
now := time.Now()
|
||||
s.mu.RLock()
|
||||
e, ok := s.urls[accountID]
|
||||
s.mu.RUnlock()
|
||||
if ok && now.Before(e.exp) {
|
||||
return e.report, e.uplink
|
||||
}
|
||||
var cfg entity.AccountConfig
|
||||
if err := s.db.WithContext(ctx).
|
||||
Where("account_id = ? AND business = ?", accountID, "sms").
|
||||
First(&cfg).Error; err != nil {
|
||||
if err != gorm.ErrRecordNotFound {
|
||||
logger.L().Warn("callback_url_lookup_failed",
|
||||
zap.Int64("account_id", accountID), zap.Error(err))
|
||||
}
|
||||
return "", "" // 不缓存失败:下轮重查
|
||||
}
|
||||
s.mu.Lock()
|
||||
s.urls[accountID] = urlEntry{report: cfg.ReportURL, uplink: cfg.UplinkURL, exp: now.Add(urlCacheTTL)}
|
||||
s.mu.Unlock()
|
||||
return cfg.ReportURL, cfg.UplinkURL
|
||||
}
|
||||
|
||||
// EnqueueReport 回执终态入队(report 域调用;失败仅返回错误由调用方 Warn,
|
||||
// 终态不回滚——与 CMPP Deliver 下发同口径)。
|
||||
func (s *Service) EnqueueReport(ctx context.Context, detailID int64,
|
||||
status int8, st, errCode, mobile string, now time.Time) error {
|
||||
var accountID int64
|
||||
if err := s.db.WithContext(ctx).Table("message_detail").
|
||||
Select("account_id").Where("id = ?", detailID).Scan(&accountID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
reportURL, _ := s.urlsFor(ctx, accountID)
|
||||
if reportURL == "" {
|
||||
return nil // 未配置回调地址:零任务(多数账号)
|
||||
}
|
||||
p := ReportPayload{
|
||||
Type: "report", DetailID: detailID, AccountID: accountID, Mobile: mobile,
|
||||
Status: status, Stat: statForStatus(status, errCode), ErrorCode: errCode,
|
||||
ReportAt: now,
|
||||
}
|
||||
if st != "" { // report 域三态词优先(DELIVRD/FAILURE/UNKNOWN)
|
||||
p.Stat = st
|
||||
}
|
||||
return s.insert(ctx, accountID, entity.CallbackTypeReport, reportURL, p)
|
||||
}
|
||||
|
||||
// EnqueueUplink 上行消息入队(uplink 域调用;拉黑判定在前不影响推送)。
|
||||
func (s *Service) EnqueueUplink(ctx context.Context, uplinkID, accountID int64,
|
||||
destCode, srcMobile, content, recvAt string) error {
|
||||
_, uplinkURL := s.urlsFor(ctx, accountID)
|
||||
if uplinkURL == "" {
|
||||
return nil
|
||||
}
|
||||
return s.insert(ctx, accountID, entity.CallbackTypeUplink, uplinkURL, UplinkPayload{
|
||||
Type: "uplink", UplinkID: uplinkID, AccountID: accountID,
|
||||
DestCode: destCode, SrcMobile: srcMobile, Content: content, RecvAt: recvAt,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Service) insert(ctx context.Context, accountID int64, typ int8, url string, payload any) error {
|
||||
body, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.db.WithContext(ctx).Create(&entity.CallbackTask{
|
||||
AccountID: accountID, Type: typ, URL: url,
|
||||
Payload: string(body), Status: entity.CallbackPending,
|
||||
NextRetryAt: time.Now(),
|
||||
}).Error
|
||||
}
|
||||
|
||||
// Resend 死信重派(status 2→0/attempt 清零/立即到期;CAS 仅死信可派)。
|
||||
// 返回影响行数(0=非死信或不存在)。
|
||||
func (s *Service) Resend(ctx context.Context, id int64) (int64, error) {
|
||||
res := s.db.WithContext(ctx).Model(&entity.CallbackTask{}).
|
||||
Where("id = ? AND status = ?", id, entity.CallbackDead).
|
||||
Updates(map[string]any{
|
||||
"status": entity.CallbackPending, "attempt": 0,
|
||||
"next_retry_at": time.Now(), "last_err": "resend by ops",
|
||||
})
|
||||
return res.RowsAffected, res.Error
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
// callback_test.go S4·C1:入队(URL 配置门控/载荷)+ 推送状态机
|
||||
// (sent/退避/死信/重派)——httptest 模拟客户回调地址。
|
||||
package callback
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go-sms/internal/model/entity"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
func newCallbackDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{Logger: logger.Discard})
|
||||
if err != nil {
|
||||
t.Fatalf("打开内存库失败: %v", err)
|
||||
}
|
||||
if err := db.AutoMigrate(&entity.CallbackTask{}, &entity.AccountConfig{}, &entity.MessageDetail{}); err != nil {
|
||||
t.Fatalf("AutoMigrate 失败: %v", err)
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
func mkDetailRow(t *testing.T, db *gorm.DB, accountID int64) int64 {
|
||||
t.Helper()
|
||||
d := entity.MessageDetail{TaskID: 1, AccountID: accountID, Mobile: "13800001111", Status: 2}
|
||||
d.ID = time.Now().UnixNano()
|
||||
if err := db.Create(&d).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return d.ID
|
||||
}
|
||||
|
||||
// TestEnqueueReport_NoURL 未配置 report_url:零任务(多数账号零开销)。
|
||||
func TestEnqueueReport_NoURL(t *testing.T) {
|
||||
db := newCallbackDB(t)
|
||||
s := NewService(db)
|
||||
detailID := mkDetailRow(t, db, 5)
|
||||
if err := s.EnqueueReport(context.Background(), detailID, 2, "SUCCESS", "", "13800001111", time.Now()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var n int64
|
||||
db.Model(&entity.CallbackTask{}).Count(&n)
|
||||
if n != 0 {
|
||||
t.Fatalf("未配置 URL 应零任务, got %d", n)
|
||||
}
|
||||
}
|
||||
|
||||
// TestEnqueueReport_WithURL 配置 report_url:入队 type=report,载荷字段完整。
|
||||
func TestEnqueueReport_WithURL(t *testing.T) {
|
||||
db := newCallbackDB(t)
|
||||
if err := db.Create(&entity.AccountConfig{
|
||||
AccountID: 5, Business: "sms", ReportURL: "http://cb.example/hook",
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := NewService(db)
|
||||
detailID := mkDetailRow(t, db, 5)
|
||||
if err := s.EnqueueReport(context.Background(), detailID, 3, "FAILURE", "UNDELIV", "13800001111", time.Now()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var task entity.CallbackTask
|
||||
if err := db.First(&task).Error; err != nil {
|
||||
t.Fatalf("任务未入队: %v", err)
|
||||
}
|
||||
if task.Type != entity.CallbackTypeReport || task.URL != "http://cb.example/hook" || task.Status != entity.CallbackPending {
|
||||
t.Fatalf("type/url/status 异常: %+v", task)
|
||||
}
|
||||
var p ReportPayload
|
||||
if err := json.Unmarshal([]byte(task.Payload), &p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if p.DetailID != detailID || p.AccountID != 5 || p.Stat != "FAILURE" || p.Mobile != "13800001111" {
|
||||
t.Fatalf("载荷字段异常: %+v", p)
|
||||
}
|
||||
}
|
||||
|
||||
// TestEnqueueUplink 配置 uplink_url 才入队(type=uplink)。
|
||||
func TestEnqueueUplink(t *testing.T) {
|
||||
db := newCallbackDB(t)
|
||||
s := NewService(db)
|
||||
if err := s.EnqueueUplink(context.Background(), 9, 5, "10690", "13811112222", "R", "2026-11-16T10:00:00+08:00"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var n int64
|
||||
db.Model(&entity.CallbackTask{}).Count(&n)
|
||||
if n != 0 {
|
||||
t.Fatalf("未配置 uplink_url 应零任务, got %d", n)
|
||||
}
|
||||
db.Create(&entity.AccountConfig{AccountID: 5, Business: "sms", UplinkURL: "http://cb.example/mo"})
|
||||
if err := s.EnqueueUplink(context.Background(), 9, 5, "10690", "13811112222", "R", "2026-11-16T10:00:00+08:00"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var task entity.CallbackTask
|
||||
if err := db.First(&task).Error; err != nil {
|
||||
t.Fatalf("任务未入队: %v", err)
|
||||
}
|
||||
if task.Type != entity.CallbackTypeUplink {
|
||||
t.Fatalf("type 期望 uplink(2), got %d", task.Type)
|
||||
}
|
||||
var p UplinkPayload
|
||||
if err := json.Unmarshal([]byte(task.Payload), &p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if p.UplinkID != 9 || p.SrcMobile != "13811112222" || p.Content != "R" {
|
||||
t.Fatalf("上行载荷异常: %+v", p)
|
||||
}
|
||||
}
|
||||
|
||||
// TestWorkerSentToDeadLifecycle 状态机全链:500→退避重排→耗尽死信→Resend→200 送达。
|
||||
func TestWorkerSentToDeadLifecycle(t *testing.T) {
|
||||
db := newCallbackDB(t)
|
||||
code := 500
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(code)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
task := entity.CallbackTask{
|
||||
AccountID: 5, Type: entity.CallbackTypeReport, URL: srv.URL,
|
||||
Payload: `{"type":"report"}`, Status: entity.CallbackPending,
|
||||
NextRetryAt: time.Now(),
|
||||
}
|
||||
if err := db.Create(&task).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w := NewPushWorker(db)
|
||||
w.maxAttempt = 2 // 测试压缩(生产 10)
|
||||
|
||||
// 第一轮:500 → attempt=1 重排(退避后到期)。
|
||||
if n := w.Tick(context.Background()); n != 1 {
|
||||
t.Fatalf("第一轮应处理 1 条, got %d", n)
|
||||
}
|
||||
var got entity.CallbackTask
|
||||
db.First(&got, task.ID)
|
||||
if got.Status != entity.CallbackPending || got.Attempt != 1 || !got.NextRetryAt.After(time.Now()) {
|
||||
t.Fatalf("失败后应 pending+attempt=1+未来重试: %+v", got)
|
||||
}
|
||||
// 未到期不重扫。
|
||||
if n := w.Tick(context.Background()); n != 0 {
|
||||
t.Fatalf("退避窗口内不应重扫, got %d", n)
|
||||
}
|
||||
// 到期重扫 → 耗尽 → 死信。
|
||||
db.Model(&entity.CallbackTask{}).Where("id = ?", task.ID).
|
||||
Update("next_retry_at", time.Now().Add(-time.Second))
|
||||
w.Tick(context.Background())
|
||||
db.First(&got, task.ID)
|
||||
if got.Status != entity.CallbackDead || got.Attempt != 2 {
|
||||
t.Fatalf("重试耗尽应死信: %+v", got)
|
||||
}
|
||||
// 死信不参与扫描。
|
||||
if n := w.Tick(context.Background()); n != 0 {
|
||||
t.Fatalf("死信不应被扫描, got %d", n)
|
||||
}
|
||||
// 重派 → 服务恢复 → 送达。
|
||||
if n, err := NewService(db).Resend(context.Background(), task.ID); err != nil || n != 1 {
|
||||
t.Fatalf("Resend: n=%d err=%v", n, err)
|
||||
}
|
||||
code = http.StatusOK
|
||||
w.Tick(context.Background())
|
||||
db.First(&got, task.ID)
|
||||
if got.Status != entity.CallbackSent || got.SentAt == nil || got.LastCode != 200 {
|
||||
t.Fatalf("重派后应送达: %+v", got)
|
||||
}
|
||||
// 非死信重派=幂等拒绝(0 行)。
|
||||
if n, _ := NewService(db).Resend(context.Background(), task.ID); n != 0 {
|
||||
t.Fatalf("非死信 Resend 应 0 行, got %d", n)
|
||||
}
|
||||
}
|
||||
|
||||
// TestWorkerConcurrentBatch 并发批次全部推进(信号量不丢任务)。
|
||||
func TestWorkerConcurrentBatch(t *testing.T) {
|
||||
db := newCallbackDB(t)
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(200)
|
||||
}))
|
||||
defer srv.Close()
|
||||
for i := 0; i < 20; i++ {
|
||||
db.Create(&entity.CallbackTask{
|
||||
AccountID: 5, Type: entity.CallbackTypeUplink, URL: srv.URL,
|
||||
Payload: "{}", Status: entity.CallbackPending, NextRetryAt: time.Now(),
|
||||
})
|
||||
}
|
||||
w := NewPushWorker(db)
|
||||
w.batch = 20
|
||||
w.concurrency = 1 // sqlite 内存库并发写锁:单测串行(批次不丢任务语义不变)
|
||||
if n := w.Tick(context.Background()); n != 20 {
|
||||
t.Fatalf("应处理 20 条, got %d", n)
|
||||
}
|
||||
var sent int64
|
||||
db.Model(&entity.CallbackTask{}).Where("status = ?", entity.CallbackSent).Count(&sent)
|
||||
if sent != 20 {
|
||||
t.Fatalf("应全部送达, got %d", sent)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
// worker.go S4·C1:回调推送 Worker——周期扫描 callback_task(status=0 且到期)
|
||||
// →HTTP POST→2xx 置 sent/失败指数退避/耗尽置 dead。单实例部署(与 consumer
|
||||
// 同进程);多实例时批次 SELECT 无锁会重复推送(at-least-once,UPDATE CAS
|
||||
// 保证状态机不乱,重复送达客户侧幂等兜底——与 CMPP Deliver 同语义)。
|
||||
package callback
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"go-sms/internal/model/entity"
|
||||
"go-sms/internal/pkg/logger"
|
||||
"go-sms/internal/pkg/metrics"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 推送参数默认值(HDD→SSD 后保守起步;S5 基线期按压测重标)。
|
||||
const (
|
||||
DefaultScanEvery = 2 * time.Second // 扫描周期
|
||||
DefaultBatch = 100 // 单轮上限
|
||||
DefaultConcurrency = 8 // 并发推送信号量
|
||||
DefaultTimeout = 5 * time.Second // 单次 HTTP 超时
|
||||
DefaultMaxAttempt = 10 // 重试上限(超过=死信)
|
||||
DefaultMaxBackoff = 10 * time.Minute // 退避封顶
|
||||
)
|
||||
|
||||
// PushWorker 回调推送器。
|
||||
type PushWorker struct {
|
||||
db *gorm.DB
|
||||
client *http.Client
|
||||
batch int
|
||||
concurrency int
|
||||
maxAttempt int
|
||||
maxBackoff time.Duration
|
||||
}
|
||||
|
||||
// NewPushWorker 构造(零值参数取默认)。
|
||||
func NewPushWorker(db *gorm.DB) *PushWorker {
|
||||
return &PushWorker{
|
||||
db: db,
|
||||
client: &http.Client{
|
||||
Timeout: DefaultTimeout,
|
||||
},
|
||||
batch: DefaultBatch,
|
||||
concurrency: DefaultConcurrency,
|
||||
maxAttempt: DefaultMaxAttempt,
|
||||
maxBackoff: DefaultMaxBackoff,
|
||||
}
|
||||
}
|
||||
|
||||
// Run 常驻循环(caller 起 goroutine;ctx 取消即退出,每轮独立 tick)。
|
||||
func (w *PushWorker) Run(ctx context.Context, every time.Duration) {
|
||||
if every <= 0 {
|
||||
every = DefaultScanEvery
|
||||
}
|
||||
t := time.NewTicker(every)
|
||||
defer t.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-t.C:
|
||||
w.Tick(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Tick 单轮扫描推送(导出供测试/手动触发);返回本轮处理条数。
|
||||
func (w *PushWorker) Tick(ctx context.Context) int {
|
||||
var rows []entity.CallbackTask
|
||||
if err := w.db.WithContext(ctx).
|
||||
Where("status = ? AND next_retry_at <= ?", entity.CallbackPending, time.Now()).
|
||||
Order("id ASC").Limit(w.batch).Find(&rows).Error; err != nil {
|
||||
logger.L().Warn("callback_scan_failed", zap.Error(err))
|
||||
return 0
|
||||
}
|
||||
sem := make(chan struct{}, w.concurrency)
|
||||
done := make(chan struct{}, len(rows))
|
||||
for i := range rows {
|
||||
sem <- struct{}{}
|
||||
go func(task entity.CallbackTask) {
|
||||
defer func() { <-sem; done <- struct{}{} }()
|
||||
w.pushOne(ctx, task)
|
||||
}(rows[i])
|
||||
}
|
||||
for range rows {
|
||||
<-done
|
||||
}
|
||||
return len(rows)
|
||||
}
|
||||
|
||||
// pushOne 单条推送+状态机推进(CAS WHERE status=0:与 Resend/并发实例至多一次生效)。
|
||||
func (w *PushWorker) pushOne(ctx context.Context, task entity.CallbackTask) {
|
||||
typ := "report"
|
||||
if task.Type == entity.CallbackTypeUplink {
|
||||
typ = "uplink"
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, task.URL, bytes.NewReader([]byte(task.Payload)))
|
||||
if err != nil {
|
||||
w.markRetry(ctx, &task, 0, err.Error(), typ)
|
||||
return
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := w.client.Do(req)
|
||||
if err != nil {
|
||||
w.markRetry(ctx, &task, 0, truncate(err.Error(), 255), typ)
|
||||
return
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
_, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, 4<<10))
|
||||
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
|
||||
res := w.db.WithContext(ctx).Model(&entity.CallbackTask{}).
|
||||
Where("id = ? AND status = ?", task.ID, entity.CallbackPending).
|
||||
Updates(map[string]any{
|
||||
"status": entity.CallbackSent, "last_code": resp.StatusCode,
|
||||
"last_err": "", "sent_at": time.Now(),
|
||||
})
|
||||
if res.Error != nil {
|
||||
logger.L().Warn("callback_mark_sent_failed", zap.Int64("id", task.ID), zap.Error(res.Error))
|
||||
}
|
||||
if res.RowsAffected > 0 {
|
||||
metrics.CallbackPushTotal.WithLabelValues(typ, "sent").Inc()
|
||||
}
|
||||
return
|
||||
}
|
||||
w.markRetry(ctx, &task, resp.StatusCode, fmt.Sprintf("http %d", resp.StatusCode), typ)
|
||||
}
|
||||
|
||||
// markRetry 失败推进:attempt+1 → 耗尽置 dead,否则退避重排。
|
||||
func (w *PushWorker) markRetry(ctx context.Context, task *entity.CallbackTask, httpCode int, msg, typ string) {
|
||||
attempt := task.Attempt + 1
|
||||
updates := map[string]any{
|
||||
"attempt": attempt, "last_code": httpCode, "last_err": truncate(msg, 255),
|
||||
}
|
||||
if attempt >= w.maxAttempt {
|
||||
updates["status"] = entity.CallbackDead
|
||||
updates["next_retry_at"] = time.Now() // MySQL 不收 zero time;死信不扫靠 status
|
||||
} else {
|
||||
backoff := time.Duration(1<<uint(attempt)) * time.Second // 2^attempt s
|
||||
if backoff > w.maxBackoff {
|
||||
backoff = w.maxBackoff
|
||||
}
|
||||
updates["next_retry_at"] = time.Now().Add(backoff)
|
||||
}
|
||||
res := w.db.WithContext(ctx).Model(&entity.CallbackTask{}).
|
||||
Where("id = ? AND status = ?", task.ID, entity.CallbackPending).
|
||||
Updates(updates)
|
||||
if res.Error != nil {
|
||||
logger.L().Warn("callback_mark_retry_failed", zap.Int64("id", task.ID), zap.Error(res.Error))
|
||||
return
|
||||
}
|
||||
if res.RowsAffected > 0 {
|
||||
if attempt >= w.maxAttempt {
|
||||
metrics.CallbackPushTotal.WithLabelValues(typ, "dead").Inc()
|
||||
logger.L().Warn("callback_task_dead",
|
||||
zap.Int64("id", task.ID), zap.Int64("account_id", task.AccountID),
|
||||
zap.Int("attempt", attempt), zap.String("url", task.URL))
|
||||
} else {
|
||||
metrics.CallbackPushTotal.WithLabelValues(typ, "retry").Inc()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func truncate(s string, n int) string {
|
||||
if r := []rune(s); len(r) > n {
|
||||
return string(r[:n])
|
||||
}
|
||||
return s
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// link_day40_test.go Day 40(A-3.5):真网断线重连演练——网关进程消失→心跳断链→
|
||||
// link_reconnect_drill_test.go(原 Day40 A-3.5):真网断线重连演练——网关进程消失→心跳断链→
|
||||
// 同端口复活→指数退避重连→提交恢复。断言全程远小于 30s 重连上限,并留痕 channel_log。
|
||||
package pool_test
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
// Package service 业务服务实现层(账号/通道/发送/路由/审核/计费/统计/结算等)。
|
||||
// 阶段 2 起逐步填充。
|
||||
// 阶段 2 起逐步填充;S2 起新增 settle 域(消耗日汇总/充值审核/月结/阶梯/利润/对账)。
|
||||
package service
|
||||
|
||||
@@ -26,9 +26,18 @@ type Consumer struct {
|
||||
db *gorm.DB
|
||||
bill *Billing // Day 57:计费确认(nil=跳过,Day56 零破坏)
|
||||
push PushSink // Day 61:CMPP Deliver 下发出口(nil=跳过)
|
||||
cb ReportEnqueuer // S4·C1:HTTP 回调入队出口(nil=跳过)
|
||||
requeue func(ev *Event) error // Day 69 三轮:unpaired 短重投出口(nil=直接按杂音丢弃)
|
||||
}
|
||||
|
||||
// ReportEnqueuer 回执终态 HTTP 回调入队出口(S4·C1;装配层注入
|
||||
// *callback.Service。终态首锁后调用,与 bill.Confirm 同为事务提交后动作;
|
||||
// 内部失败由实现方/调用方 Warn 吞错——终态不回滚)。
|
||||
type ReportEnqueuer interface {
|
||||
EnqueueReport(ctx context.Context, detailID int64,
|
||||
status int8, st, errCode, mobile string, now time.Time) error
|
||||
}
|
||||
|
||||
// NewConsumer 构造。
|
||||
func NewConsumer(db *gorm.DB) *Consumer { return &Consumer{db: db} }
|
||||
|
||||
@@ -45,6 +54,25 @@ func (c *Consumer) WithPushSink(p PushSink) *Consumer {
|
||||
return c
|
||||
}
|
||||
|
||||
// WithCallback 注入 HTTP 回调入队出口(S4·C1;链式,nil=跳过)。
|
||||
func (c *Consumer) WithCallback(e ReportEnqueuer) *Consumer {
|
||||
c.cb = e
|
||||
return c
|
||||
}
|
||||
|
||||
// enqueueReportLocked 终态首锁后的回调入队(吞错 Warn:推送失败不影响
|
||||
// 已锁定的对账事实源——与 CMPP Deliver 下发同口径)。
|
||||
func (c *Consumer) enqueueReportLocked(ctx context.Context, act *finAction, now time.Time) {
|
||||
if c.cb == nil || act == nil {
|
||||
return
|
||||
}
|
||||
if err := c.cb.EnqueueReport(ctx, act.detailID, act.status,
|
||||
string(act.st), act.errCode, act.mobile, now); err != nil {
|
||||
logger.L().Warn("callback_report_enqueue_failed",
|
||||
zap.Int64("detail_id", act.detailID), zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
// WithRequeue 注入 unpaired 短重投出口(Day 69 三轮;链式,nil=不重投)。
|
||||
// 场景:秒回模拟器回执(push→kafka→消费,~10ms)可跑赢发送侧 seg 落库
|
||||
// (submit 往返+批量写,~20ms+)——合法回执 unpaired 若直接丢弃=detail
|
||||
@@ -121,6 +149,9 @@ func (c *Consumer) Process(ctx context.Context, ev *Event) error {
|
||||
if locked && c.bill != nil { // 首到终态锁定且事务已提交:计费确认(内部吞错,方向=少退不少扣)
|
||||
c.bill.Confirm(ctx, act.detailID, act.status)
|
||||
}
|
||||
if locked { // S4·C1:HTTP 回调入队(账号配置 report_url 才产生任务)
|
||||
c.enqueueReportLocked(ctx, act, now)
|
||||
}
|
||||
if !paired {
|
||||
return c.handleUnpaired(ev) // Day 69 三轮:秒回竞态短重投,耗尽才按杂音丢弃
|
||||
}
|
||||
@@ -188,6 +219,9 @@ func (c *Consumer) recheckPending(ctx context.Context, ev *Event, st Status, now
|
||||
if locked && c.bill != nil {
|
||||
c.bill.Confirm(ctx, act.detailID, act.status)
|
||||
}
|
||||
if locked { // S4·C1:HTTP 回调入队(补刀终态同推)
|
||||
c.enqueueReportLocked(ctx, act, now)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
// consumer_callback_test.go S4·C1:回执终态首锁后的 HTTP 回调入队挂点
|
||||
// (重复回执/已锁定不重复入队;挂点语义=与 bill.Confirm 同为事务后动作)。
|
||||
package report
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type capEnqueuer struct {
|
||||
calls []capEnqCall
|
||||
}
|
||||
|
||||
type capEnqCall struct {
|
||||
detailID int64
|
||||
status int8
|
||||
}
|
||||
|
||||
func (e *capEnqueuer) EnqueueReport(_ context.Context, detailID int64,
|
||||
status int8, st, errCode, mobile string, _ time.Time) error {
|
||||
e.calls = append(e.calls, capEnqCall{detailID: detailID, status: status})
|
||||
return nil
|
||||
}
|
||||
|
||||
// TestConsumer_CallbackEnqueuedOnce 单段成功终态=入队一次;重复回执(终态
|
||||
// 已锁定)=零重复——首锁幂等语义延伸到回调面。
|
||||
func TestConsumer_CallbackEnqueuedOnce(t *testing.T) {
|
||||
db := newConsumerDB(t)
|
||||
enq := &capEnqueuer{}
|
||||
c := NewConsumer(db).WithCallback(enq)
|
||||
d := mkDetail(t, db, 1, "13800005555", 1)
|
||||
mid := mkSeg(t, db, d.ID, 7, "13800005555", 1, 1)
|
||||
|
||||
if err := c.Process(testCtx, ev(7, mid, "DELIVRD", "13800005555")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(enq.calls) != 1 || enq.calls[0].detailID != d.ID || enq.calls[0].status != 2 {
|
||||
t.Fatalf("首锁应入队一次: %+v", enq.calls)
|
||||
}
|
||||
// 同一回执重放:终态已锁定,跳过(零重复入队)。
|
||||
if err := c.Process(testCtx, ev(7, mid, "DELIVRD", "13800005555")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(enq.calls) != 1 {
|
||||
t.Fatalf("重复回执不应重复入队: %+v", enq.calls)
|
||||
}
|
||||
}
|
||||
|
||||
// TestConsumer_CallbackNilSink 注入 nil=零开销跳过(旧装配兼容)。
|
||||
func TestConsumer_CallbackNilSink(t *testing.T) {
|
||||
db := newConsumerDB(t)
|
||||
c := NewConsumer(db) // 无 WithCallback
|
||||
d := mkDetail(t, db, 1, "13800006666", 1)
|
||||
mid := mkSeg(t, db, d.ID, 7, "13800006666", 1, 1)
|
||||
if err := c.Process(testCtx, ev(7, mid, "UNDELIV", "13800006666")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := getDetail(t, db, d.ID); got.Status != 3 {
|
||||
t.Fatalf("status=%d 期望 3", got.Status)
|
||||
}
|
||||
}
|
||||
@@ -82,6 +82,7 @@ type PairService struct {
|
||||
db *gorm.DB
|
||||
bill *Billing
|
||||
push PushSink
|
||||
cb ReportEnqueuer // S4·C1:HTTP 回调入队(nil=跳过)
|
||||
sf *util.Snowflake // 重推受理号(雪花;D29 异步任务表落地后即任务主键)
|
||||
}
|
||||
|
||||
@@ -96,6 +97,12 @@ func (p *PairService) WithPushSink(s PushSink) *PairService {
|
||||
return p
|
||||
}
|
||||
|
||||
// WithCallback 注入 HTTP 回调入队出口(S4·C1;链式,nil=跳过)。
|
||||
func (p *PairService) WithCallback(e ReportEnqueuer) *PairService {
|
||||
p.cb = e
|
||||
return p
|
||||
}
|
||||
|
||||
// DB 裸查询出口(handler 回执分页 JOIN 查询复用同一连接)。
|
||||
func (p *PairService) DB() *gorm.DB { return p.db }
|
||||
|
||||
@@ -282,6 +289,14 @@ func (p *PairService) Pair(ctx context.Context, req PairRequest) (PairResult, er
|
||||
if p.bill != nil {
|
||||
p.bill.Confirm(ctx, id, req.TargetStatus)
|
||||
}
|
||||
// S4·C1:HTTP 回调入队(与 Consumer 回执链同挂点;吞错 Warn)。
|
||||
if p.cb != nil {
|
||||
if err := p.cb.EnqueueReport(ctx, id, req.TargetStatus,
|
||||
string(st), "MANUAL", d.Mobile, now); err != nil {
|
||||
logger.L().Warn("callback_report_enqueue_failed",
|
||||
zap.Int64("detail_id", id), zap.Error(err))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
res.Skipped++ // 圈中窗口与执行窗口间被回执/并发配对锁定
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// router_day40_test.go Day 40:千条/秒路由压测(验收:1000 条/秒稳定,单次决策 <10ms)。
|
||||
// router_1000qps_test.go(原 Day40):千条/秒路由压测(验收:1000 条/秒稳定,单次决策 <10ms)。
|
||||
// 8 并发 × 125 次 = 1000 次 Pick(权重+过滤全链路),统计 p50/p99/吞吐。
|
||||
package router
|
||||
|
||||
@@ -6,13 +6,15 @@
|
||||
// 顺带修复"手动审核通过后任务停滞"缺陷);
|
||||
// - 分发复用 ChannelDispatcher(router 选路→聚合/直发链路→明细终态);
|
||||
// 系统性错误回滚 status 1→0 留待重扫(claim 原子性保证重试幂等)。
|
||||
package send
|
||||
package consume
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"go-sms/internal/service/send/dispatch"
|
||||
"go-sms/internal/service/send/submit"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -26,64 +28,15 @@ import (
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// SubmitEvent sms-submit 事件载荷(Day46 openapi 五字段向后兼容+Day48 扩展+Day52 R2 明细全量)。
|
||||
type SubmitEvent struct {
|
||||
TaskID int64 `json:"task_id"`
|
||||
TaskNo string `json:"task_no"`
|
||||
AccountID int64 `json:"account_id"`
|
||||
SendType int8 `json:"send_type"`
|
||||
PassCount int `json:"pass_count"` // 过拦截条数(0=全拦,消费者跳过;Day52 起由 details 推导)
|
||||
Source string `json:"source"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
Business string `json:"business,omitempty"` // Day 49:sms/video(biz_type 路由;空=sms 兼容 Day48 旧载荷)
|
||||
// Day52 R2:明细全量(入口预生成雪花 ID+拦截终态;消费侧批量落库,入口事务零明细写)。
|
||||
// 空=Day48 旧格式/兜底扫描场景,消费侧不落(明细已由降级路径或旧版本落库)。
|
||||
Details []EventDetail `json:"details,omitempty"`
|
||||
// P0-1(Day 63)接入异步化:Deferred=true 表示受理侧走 SubmitFast 快路径
|
||||
// (Validate+预扣+DetailID 预生成+投递,零 DB 写),task 落库/拦截判定/退费/
|
||||
// 审核分流全部由消费侧补全(handleDeferred);TaskID=0(自增主键消费侧生成)。
|
||||
Deferred bool `json:"deferred,omitempty"`
|
||||
// TaskMeta Deferred 专用:任务行补建字段(与 entity.MessageTask 对齐;
|
||||
// 旧事件零携带,消费侧按非 Deferred 老路径处理)。
|
||||
TaskMeta *SubmitTaskMeta `json:"task_meta,omitempty"`
|
||||
}
|
||||
|
||||
// SubmitTaskMeta 快路径任务补建载荷(受理侧未落库,消费侧 INSERT 重建)。
|
||||
type SubmitTaskMeta struct {
|
||||
Content string `json:"content"`
|
||||
Sign string `json:"sign"`
|
||||
MobileCount int `json:"mobile_count"`
|
||||
FeeTotal float64 `json:"fee_total"`
|
||||
SubmitIP string `json:"submit_ip"`
|
||||
CallData string `json:"call_data"`
|
||||
Source int8 `json:"source"` // 任务来源 int8(WEB=4/CMPP=2/SMPP=3/API=1)
|
||||
ScheduleAt string `json:"schedule_at,omitempty"` // RFC3339,空=非定时
|
||||
}
|
||||
|
||||
// EventDetail 明细行载荷(task_id/account_id 公共字段在事件级;字段与 entity.MessageDetail 对齐)。
|
||||
type EventDetail struct {
|
||||
ID int64 `json:"id"` // 雪花主键(入口预生成,回执配对键)
|
||||
Mobile string `json:"mobile"`
|
||||
Content string `json:"content"`
|
||||
Status int8 `json:"status"` // 0=存活 5=拦截终态(受理期已退费)
|
||||
Fee float64 `json:"fee"` // 计费条数
|
||||
ErrorCode string `json:"error_code,omitempty"`
|
||||
ExtCode string `json:"ext_code,omitempty"`
|
||||
CallData string `json:"call_data,omitempty"`
|
||||
// Day66:明细单表化冗余(空=旧载荷,persistDetailsDB 兜底 ev.Business/'sms')。
|
||||
Business string `json:"business,omitempty"`
|
||||
Source int8 `json:"source,omitempty"` // 0=旧载荷→兜底 API
|
||||
}
|
||||
|
||||
// SubmitConsumer 事件消费者。
|
||||
type SubmitConsumer struct {
|
||||
db *gorm.DB
|
||||
dp TaskDispatcher
|
||||
dp dispatch.TaskDispatcher
|
||||
log *zap.Logger
|
||||
blocked BlockedDeliverSink // Day62c:拦截终态 CMPP 下发出口(nil=静默)
|
||||
// P0-1(Day 63)Deferred 补全依赖(受理快路径的同步链后移;nil=对应环节降级)。
|
||||
chain *interceptor.Chain // 拦截器链(nil=全放行,兼容最小装配)
|
||||
rt TaskRouter // 审核分流(nil=停留待审,48h Scanner 兜底)
|
||||
rt submit.TaskRouter // 审核分流(nil=停留待审,48h Scanner 兜底)
|
||||
bal balance.Store // 拦截/审核拒退费(nil=不退,对账 Reconciler 兜底)
|
||||
}
|
||||
|
||||
@@ -109,7 +62,7 @@ func (c *SubmitConsumer) WithConsumerChain(ch *interceptor.Chain) *SubmitConsume
|
||||
}
|
||||
|
||||
// WithConsumerRouter 注入审核分流(P0-1 Deferred 补全;链式)。
|
||||
func (c *SubmitConsumer) WithConsumerRouter(rt TaskRouter) *SubmitConsumer {
|
||||
func (c *SubmitConsumer) WithConsumerRouter(rt submit.TaskRouter) *SubmitConsumer {
|
||||
c.rt = rt
|
||||
return c
|
||||
}
|
||||
@@ -121,7 +74,7 @@ func (c *SubmitConsumer) WithConsumerBalance(b balance.Store) *SubmitConsumer {
|
||||
}
|
||||
|
||||
// NewSubmitConsumer 构造。
|
||||
func NewSubmitConsumer(db *gorm.DB, dp TaskDispatcher, log *zap.Logger) *SubmitConsumer {
|
||||
func NewSubmitConsumer(db *gorm.DB, dp dispatch.TaskDispatcher, log *zap.Logger) *SubmitConsumer {
|
||||
if log == nil {
|
||||
log = zap.NewNop()
|
||||
}
|
||||
@@ -133,7 +86,7 @@ func NewSubmitConsumer(db *gorm.DB, dp TaskDispatcher, log *zap.Logger) *SubmitC
|
||||
// Day52 R2:明细批量落库先于认领/下发(回执配对键先于 submit 存在;
|
||||
// 雪花主键冲突 DoNothing=重投幂等;失败返回错误进重试——明细为对账事实源不能丢)。
|
||||
func (c *SubmitConsumer) HandlePayload(ctx context.Context, payload []byte) error {
|
||||
var ev SubmitEvent
|
||||
var ev submit.SubmitEvent
|
||||
if err := json.Unmarshal(payload, &ev); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -161,7 +114,7 @@ func (c *SubmitConsumer) HandlePayload(ctx context.Context, payload []byte) erro
|
||||
// ①拦截器链判定(命中→明细终态 5+拦截费退)②task INSERT 补建(uk_task_no
|
||||
// 冲突=重投幂等:查回既有 ID,退费仅首次执行)③明细落库+拦截回执桥(Day62c)
|
||||
// ④全拦→任务终态 ⑤审核分流(拒绝→存活费退)⑥claim/dispatch(待审/定时自然跳过)。
|
||||
func (c *SubmitConsumer) handleDeferred(ctx context.Context, ev *SubmitEvent) error {
|
||||
func (c *SubmitConsumer) handleDeferred(ctx context.Context, ev *submit.SubmitEvent) error {
|
||||
if len(ev.Details) == 0 || ev.TaskMeta == nil {
|
||||
return nil // 防御:Deferred 事件必带明细与任务载荷(缺失静默,防毒丸循环重试)
|
||||
}
|
||||
@@ -248,7 +201,7 @@ func (c *SubmitConsumer) handleDeferred(ctx context.Context, ev *SubmitEvent) er
|
||||
_ = c.db.WithContext(ctx).Model(&entity.MessageTask{}).
|
||||
Where("id = ? AND status = ?", task.ID, 0).
|
||||
Updates(map[string]any{
|
||||
"status": taskStDone, "fail_count": len(ev.Details), "finished_at": time.Now(),
|
||||
"status": submit.TaskStDone, "fail_count": len(ev.Details), "finished_at": time.Now(),
|
||||
}).Error
|
||||
return nil
|
||||
}
|
||||
@@ -282,7 +235,7 @@ func isDupTaskNo(err error) bool {
|
||||
// (status=5)且 task.source=CMPP 逐条推送下发事件——拦截明细无上游回执可等,
|
||||
// 不推则 CMPP 客户永远收不到状态(真机报障实证)。
|
||||
// Day65:内核拆出 persistDetailsDB(exec)——deferred 路径与任务补建同事务执行。
|
||||
func (c *SubmitConsumer) persistDetails(ctx context.Context, ev *SubmitEvent) error {
|
||||
func (c *SubmitConsumer) persistDetails(ctx context.Context, ev *submit.SubmitEvent) error {
|
||||
inserted, err := persistDetailsDB(c.db.WithContext(ctx), ctx, ev)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -293,7 +246,7 @@ func (c *SubmitConsumer) persistDetails(ctx context.Context, ev *SubmitEvent) er
|
||||
|
||||
// persistDetailsDB 明细批量落库(exec=连接或事务;返回本轮实际插入行数——
|
||||
// 重投幂等锚=雪花主键 OnConflict DoNothing)。
|
||||
func persistDetailsDB(exec *gorm.DB, ctx context.Context, ev *SubmitEvent) (int64, error) {
|
||||
func persistDetailsDB(exec *gorm.DB, ctx context.Context, ev *submit.SubmitEvent) (int64, error) {
|
||||
if len(ev.Details) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
@@ -310,7 +263,7 @@ func persistDetailsDB(exec *gorm.DB, ctx context.Context, ev *SubmitEvent) (int6
|
||||
}
|
||||
}
|
||||
if src == 0 {
|
||||
src = TaskSourceAPI
|
||||
src = submit.TaskSourceAPI
|
||||
}
|
||||
rows = append(rows, entity.MessageDetail{
|
||||
SnowflakeModel: entity.SnowflakeModel{ID: d.ID},
|
||||
@@ -338,7 +291,7 @@ func persistDetailsDB(exec *gorm.DB, ctx context.Context, ev *SubmitEvent) (int6
|
||||
|
||||
// pushBlocked 拦截终态下发(Day62c):本轮新插入才推(RowsAffected=0=纯重投跳过;
|
||||
// 批内混合插入的极端重投重复可容忍,客户端按 Stat 幂等处理)。
|
||||
func (c *SubmitConsumer) pushBlocked(ctx context.Context, ev *SubmitEvent, inserted int64) {
|
||||
func (c *SubmitConsumer) pushBlocked(ctx context.Context, ev *submit.SubmitEvent, inserted int64) {
|
||||
if c.blocked == nil || inserted == 0 {
|
||||
return
|
||||
}
|
||||
@@ -361,7 +314,7 @@ func (c *SubmitConsumer) pushBlocked(ctx context.Context, ev *SubmitEvent, inser
|
||||
zap.Int64("task_id", ev.TaskID), zap.Error(err))
|
||||
return
|
||||
}
|
||||
if source != TaskSourceCMPP {
|
||||
if source != submit.TaskSourceCMPP {
|
||||
return // HTTP/控制台/SMPP 来源无 CMPP 会话诉求(SMPP 下发另立项)
|
||||
}
|
||||
submitAt := time.Now().Format("0102150405") // CMPP MMDDHHMMSS
|
||||
@@ -378,7 +331,7 @@ func (c *SubmitConsumer) pushBlocked(ctx context.Context, ev *SubmitEvent, inser
|
||||
func (c *SubmitConsumer) claim(ctx context.Context, id int64) bool {
|
||||
res := c.db.WithContext(ctx).Model(&entity.MessageTask{}).
|
||||
Where("id = ? AND status = 0 AND audit_status IN (1, 3) AND send_type IN (1, 2)", id).
|
||||
Update("status", taskStSending)
|
||||
Update("status", submit.TaskStSending)
|
||||
return res.RowsAffected == 1
|
||||
}
|
||||
|
||||
@@ -393,12 +346,12 @@ func (c *SubmitConsumer) dispatch(ctx context.Context, id int64) error {
|
||||
// Day62d:陈年孤儿任务(旧格式无明细,事件永不再来)一次性终态结案——
|
||||
// 否则 ReclaimScanner 5s/轮死循环重试(dev 实证 task 343 刷 warn 两日)。
|
||||
// 1h 宽限覆盖 Day52 事件在途窗口;正常任务明细早已落库。
|
||||
if errors.Is(derr, ErrDetailsNotReady) && time.Since(task.CreatedAt) > time.Hour {
|
||||
if errors.Is(derr, dispatch.ErrDetailsNotReady) && time.Since(task.CreatedAt) > time.Hour {
|
||||
c.log.Warn("orphan_task_finalized",
|
||||
zap.Int64("task_id", id), zap.Time("created_at", task.CreatedAt))
|
||||
res := c.db.WithContext(ctx).Model(&entity.MessageTask{}).
|
||||
Where("id = ? AND status = ?", id, taskStSending).
|
||||
Updates(map[string]any{"status": taskStDone, "finished_at": time.Now()})
|
||||
Where("id = ? AND status = ?", id, submit.TaskStSending).
|
||||
Updates(map[string]any{"status": submit.TaskStDone, "finished_at": time.Now()})
|
||||
if res.Error == nil && res.RowsAffected > 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -407,9 +360,9 @@ func (c *SubmitConsumer) dispatch(ctx context.Context, id int64) error {
|
||||
return c.rollback(ctx, id)
|
||||
}
|
||||
res := c.db.WithContext(ctx).Model(&entity.MessageTask{}).
|
||||
Where("id = ? AND status = ?", id, taskStSending).
|
||||
Where("id = ? AND status = ?", id, submit.TaskStSending).
|
||||
Updates(map[string]any{
|
||||
"status": taskStDone, "success_count": sent, "fail_count": failed,
|
||||
"status": submit.TaskStDone, "success_count": sent, "fail_count": failed,
|
||||
"finished_at": time.Now(),
|
||||
})
|
||||
if res.Error != nil || res.RowsAffected == 0 {
|
||||
@@ -421,7 +374,7 @@ func (c *SubmitConsumer) dispatch(ctx context.Context, id int64) error {
|
||||
// rollback 认领回退(status 1→0,下轮重扫;幂等由 claim 保证)。
|
||||
func (c *SubmitConsumer) rollback(ctx context.Context, id int64) error {
|
||||
return c.db.WithContext(ctx).Model(&entity.MessageTask{}).
|
||||
Where("id = ? AND status = ?", id, taskStSending).
|
||||
Where("id = ? AND status = ?", id, submit.TaskStSending).
|
||||
Update("status", 0).Error
|
||||
}
|
||||
|
||||
@@ -433,7 +386,7 @@ type ReclaimScanner struct {
|
||||
}
|
||||
|
||||
// NewReclaimScanner 构造。
|
||||
func NewReclaimScanner(db *gorm.DB, dp TaskDispatcher, log *zap.Logger) *ReclaimScanner {
|
||||
func NewReclaimScanner(db *gorm.DB, dp dispatch.TaskDispatcher, log *zap.Logger) *ReclaimScanner {
|
||||
return &ReclaimScanner{db: db, sc: NewSubmitConsumer(db, dp, log), batch: 100}
|
||||
}
|
||||
|
||||
+18
-16
@@ -1,6 +1,6 @@
|
||||
// consumer_test.go Day 48:submit 消费者单测——事件认领分发终态/幂等重投/
|
||||
// 定时域不认领/补偿扫描(审核通过即发)/毒丸载荷回死信口径。
|
||||
package send
|
||||
package consume
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -11,6 +11,8 @@ import (
|
||||
"time"
|
||||
|
||||
"go-sms/internal/model/entity"
|
||||
"go-sms/internal/service/send/dispatch"
|
||||
"go-sms/internal/service/send/submit"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
@@ -40,7 +42,7 @@ func mkImmediateTask(t *testing.T, db *gorm.DB, audit int8, sendType int8) *enti
|
||||
|
||||
func evPayload(t *testing.T, task *entity.MessageTask, pass int) []byte {
|
||||
t.Helper()
|
||||
b, err := json.Marshal(SubmitEvent{TaskID: task.ID, TaskNo: task.TaskNo,
|
||||
b, err := json.Marshal(submit.SubmitEvent{TaskID: task.ID, TaskNo: task.TaskNo,
|
||||
AccountID: task.AccountID, SendType: task.SendType, PassCount: pass})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -86,7 +88,7 @@ func TestConsumerIdempotentReplay(t *testing.T) {
|
||||
// TestConsumerSkipDomains 域过滤:定时(3)/待审(audit=0)不认领。
|
||||
func TestConsumerSkipDomains(t *testing.T) {
|
||||
db := newTimerDB(t)
|
||||
timerTask := mkImmediateTask(t, db, 3, SendTypeTimer)
|
||||
timerTask := mkImmediateTask(t, db, 3, submit.SendTypeTimer)
|
||||
pending := mkImmediateTask(t, db, 0, 1)
|
||||
dp := &fakeDispatcher{}
|
||||
sc := NewSubmitConsumer(db, dp, zap.NewNop())
|
||||
@@ -178,11 +180,11 @@ func TestConsumerPersistDetailsIdempotent(t *testing.T) {
|
||||
if err := db.Create(task).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dets := []EventDetail{
|
||||
dets := []submit.EventDetail{
|
||||
{ID: 9200000001, Mobile: "13800138000", Content: "【店】测试", Fee: 1},
|
||||
{ID: 9200000002, Mobile: "13800138001", Content: "【店】测试", Fee: 1},
|
||||
}
|
||||
b, err := json.Marshal(SubmitEvent{TaskID: task.ID, TaskNo: task.TaskNo,
|
||||
b, err := json.Marshal(submit.SubmitEvent{TaskID: task.ID, TaskNo: task.TaskNo,
|
||||
AccountID: task.AccountID, SendType: 1, PassCount: 2, Details: dets})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -227,9 +229,9 @@ func TestConsumerBlockedDeliver(t *testing.T) {
|
||||
source int8
|
||||
want int
|
||||
}{
|
||||
{TaskSourceCMPP, 2}, // CMPP:2 条拦截行各 1 次
|
||||
{TaskSourceAPI, 0}, // API 来源:不下发
|
||||
{TaskSourceWeb, 0}, // 控制台来源:不下发
|
||||
{submit.TaskSourceCMPP, 2}, // CMPP:2 条拦截行各 1 次
|
||||
{submit.TaskSourceAPI, 0}, // API 来源:不下发
|
||||
{submit.TaskSourceWeb, 0}, // 控制台来源:不下发
|
||||
} {
|
||||
db := newTimerDB(t)
|
||||
task := &entity.MessageTask{
|
||||
@@ -240,14 +242,14 @@ func TestConsumerBlockedDeliver(t *testing.T) {
|
||||
if err := db.Create(task).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dets := []EventDetail{
|
||||
dets := []submit.EventDetail{
|
||||
{ID: 9300000001, Mobile: "13800138000", Content: task.Content, Fee: 1}, // 存活
|
||||
{ID: 9300000002, Mobile: "13800138001", Content: task.Content, Status: 5,
|
||||
ErrorCode: "ISC:004"}, // 拦截(签名)
|
||||
{ID: 9300000003, Mobile: "13800138002", Content: task.Content, Status: 5,
|
||||
ErrorCode: "ISC:009"}, // 拦截(其他码)
|
||||
}
|
||||
b, err := json.Marshal(SubmitEvent{TaskID: task.ID, TaskNo: task.TaskNo,
|
||||
b, err := json.Marshal(submit.SubmitEvent{TaskID: task.ID, TaskNo: task.TaskNo,
|
||||
AccountID: task.AccountID, SendType: 1, PassCount: 1, Details: dets})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -292,7 +294,7 @@ func TestOrphanTaskFinalized(t *testing.T) {
|
||||
return task
|
||||
}
|
||||
old, fresh := mk(-2*time.Hour), mk(-time.Minute)
|
||||
dp := &fakeDispatcher{err: ErrDetailsNotReady} // 明细缺失(事件在途/孤儿)
|
||||
dp := &fakeDispatcher{err: dispatch.ErrDetailsNotReady} // 明细缺失(事件在途/孤儿)
|
||||
rs := NewReclaimScanner(db, dp, zap.NewNop()).WithBatch(10)
|
||||
for i := 0; i < 2; i++ { // 两轮扫描:孤儿首轮结案;新任务始终回滚
|
||||
if _, _, err := rs.ScanOnce(context.Background()); err != nil {
|
||||
@@ -303,7 +305,7 @@ func TestOrphanTaskFinalized(t *testing.T) {
|
||||
if err := db.First(&got, old.ID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.Status != taskStDone || got.FinishedAt.IsZero() {
|
||||
if got.Status != submit.TaskStDone || got.FinishedAt.IsZero() {
|
||||
t.Fatalf("孤儿任务应终态结案: status=%d finished_at=%v", got.Status, got.FinishedAt)
|
||||
}
|
||||
var freshGot entity.MessageTask
|
||||
@@ -321,15 +323,15 @@ func TestConsumerTimerEventPersistOnly(t *testing.T) {
|
||||
db := newTimerDB(t)
|
||||
task := &entity.MessageTask{
|
||||
TaskNo: "CS" + time.Now().Format("150405.000000001"), AccountID: 1001,
|
||||
SendType: SendTypeTimer, Content: "【店】定时", Sign: "店",
|
||||
SendType: submit.SendTypeTimer, Content: "【店】定时", Sign: "店",
|
||||
MobileCount: 1, AuditStatus: 3, Status: 0,
|
||||
}
|
||||
if err := db.Create(task).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b, err := json.Marshal(SubmitEvent{TaskID: task.ID, TaskNo: task.TaskNo,
|
||||
AccountID: task.AccountID, SendType: SendTypeTimer, PassCount: 1,
|
||||
Details: []EventDetail{{ID: 9200000003, Mobile: "13800138000", Content: "【店】定时", Fee: 1}}})
|
||||
b, err := json.Marshal(submit.SubmitEvent{TaskID: task.ID, TaskNo: task.TaskNo,
|
||||
AccountID: task.AccountID, SendType: submit.SendTypeTimer, PassCount: 1,
|
||||
Details: []submit.EventDetail{{ID: 9200000003, Mobile: "13800138000", Content: "【店】定时", Fee: 1}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
+17
-16
@@ -1,11 +1,12 @@
|
||||
// deferred_test.go P0-1(Day 63)接入异步化——SubmitFast 快路径(零 DB 写+
|
||||
// deferred_test.go P0-1(Day 63)接入异步化——submit.SubmitFast 快路径(零 DB 写+
|
||||
// MsgID=DetailID 口径)+handleDeferred 消费侧补全(建任务/拦截终态/退费/
|
||||
// 全拦终态/claim 幂等)。
|
||||
package send
|
||||
package consume
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"go-sms/internal/service/send/submit"
|
||||
"testing"
|
||||
|
||||
"go-sms/internal/model/entity"
|
||||
@@ -52,13 +53,13 @@ func TestSubmitFastEmit(t *testing.T) {
|
||||
db := newTimerDB(t)
|
||||
sf, _ := util.NewSnowflake(9)
|
||||
fb, sink := &fakeBal{}, &captureSink{}
|
||||
svc := NewSubmitService(db, sf, nil, WithBalanceStore(fb), WithEventSink(sink))
|
||||
svc := submit.NewSubmitService(db, sf, nil, submit.WithBalanceStore(fb), submit.WithEventSink(sink))
|
||||
|
||||
req := &SubmitReq{AccountID: 1001, SendType: SendTypeBatch,
|
||||
Mobiles: []string{"13800138000"}, Content: "【梦网】验证码", Source: TaskSourceCMPP}
|
||||
req := &submit.SubmitReq{AccountID: 1001, SendType: submit.SendTypeBatch,
|
||||
Mobiles: []string{"13800138000"}, Content: "【梦网】验证码", Source: submit.TaskSourceCMPP}
|
||||
res, err := svc.SubmitFast(context.Background(), req, req.Content, "1.2.3.4")
|
||||
if err != nil {
|
||||
t.Fatalf("SubmitFast err=%v", err)
|
||||
t.Fatalf("submit.SubmitFast err=%v", err)
|
||||
}
|
||||
if len(fb.holds) != 1 {
|
||||
t.Fatalf("Hold 调用 %d 次,期望 1", len(fb.holds))
|
||||
@@ -84,17 +85,17 @@ func TestSubmitFastEmit(t *testing.T) {
|
||||
func TestSubmitFastFail(t *testing.T) {
|
||||
db := newTimerDB(t)
|
||||
sf, _ := util.NewSnowflake(9)
|
||||
req := &SubmitReq{AccountID: 1001, SendType: SendTypeBatch,
|
||||
req := &submit.SubmitReq{AccountID: 1001, SendType: submit.SendTypeBatch,
|
||||
Mobiles: []string{"13800138000"}, Content: "【梦网】x"}
|
||||
|
||||
fb := &fakeBal{failHold: true}
|
||||
svc := NewSubmitService(db, sf, nil, WithBalanceStore(fb), WithEventSink(&captureSink{}))
|
||||
svc := submit.NewSubmitService(db, sf, nil, submit.WithBalanceStore(fb), submit.WithEventSink(&captureSink{}))
|
||||
if _, err := svc.SubmitFast(context.Background(), req, req.Content, ""); err == nil {
|
||||
t.Fatal("欠费应同步拒绝")
|
||||
}
|
||||
|
||||
fb2 := &fakeBal{}
|
||||
svc2 := NewSubmitService(db, sf, nil, WithBalanceStore(fb2), WithEventSink(failSink{}))
|
||||
svc2 := submit.NewSubmitService(db, sf, nil, submit.WithBalanceStore(fb2), submit.WithEventSink(failSink{}))
|
||||
if _, err := svc2.SubmitFast(context.Background(), req, req.Content, ""); err == nil {
|
||||
t.Fatal("投递失败应拒绝")
|
||||
}
|
||||
@@ -106,11 +107,11 @@ func TestSubmitFastFail(t *testing.T) {
|
||||
// mkDeferredEv 构造快路径事件载荷。
|
||||
func mkDeferredEv(t *testing.T, taskNo, content string, detailID int64, mobile string) []byte {
|
||||
t.Helper()
|
||||
b, err := json.Marshal(SubmitEvent{
|
||||
TaskNo: taskNo, AccountID: 1001, SendType: SendTypeBatch, Deferred: true,
|
||||
TaskMeta: &SubmitTaskMeta{Content: content, Sign: "梦网", MobileCount: 1,
|
||||
FeeTotal: 1, SubmitIP: "protocol", Source: TaskSourceCMPP},
|
||||
Details: []EventDetail{{ID: detailID, Mobile: mobile, Content: content,
|
||||
b, err := json.Marshal(submit.SubmitEvent{
|
||||
TaskNo: taskNo, AccountID: 1001, SendType: submit.SendTypeBatch, Deferred: true,
|
||||
TaskMeta: &submit.SubmitTaskMeta{Content: content, Sign: "梦网", MobileCount: 1,
|
||||
FeeTotal: 1, SubmitIP: "protocol", Source: submit.TaskSourceCMPP},
|
||||
Details: []submit.EventDetail{{ID: detailID, Mobile: mobile, Content: content,
|
||||
Status: 0, Fee: 1}},
|
||||
})
|
||||
if err != nil {
|
||||
@@ -151,7 +152,7 @@ func TestHandleDeferredPass(t *testing.T) {
|
||||
if err := db.Where("task_no = ?", "DF-PASS-1").First(&task).Error; err != nil {
|
||||
t.Fatalf("任务应补建: %v", err)
|
||||
}
|
||||
if task.Source != TaskSourceCMPP || task.MobileCount != 1 || task.Status != taskStDone {
|
||||
if task.Source != submit.TaskSourceCMPP || task.MobileCount != 1 || task.Status != submit.TaskStDone {
|
||||
t.Fatalf("补建任务字段异常: source=%d mobile=%d status=%d",
|
||||
task.Source, task.MobileCount, task.Status)
|
||||
}
|
||||
@@ -215,7 +216,7 @@ func TestHandleDeferredBlocked(t *testing.T) {
|
||||
if err := db.Where("task_no = ?", "DF-BLK-1").First(&task).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if task.Status != taskStDone {
|
||||
if task.Status != submit.TaskStDone {
|
||||
t.Fatalf("全拦任务应终态,got status=%d", task.Status)
|
||||
}
|
||||
if len(dp.calls) != 0 {
|
||||
@@ -0,0 +1,64 @@
|
||||
// helpers_test.go S1-B 拆包:测试库构造自 dispatch/timer_test.go newTimerDB 同体复制
|
||||
// (跨包测试 helper 不可引,本地化;口径一致:sqlite 内存 + task/detail 两表)。
|
||||
package consume
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go-sms/internal/model/entity"
|
||||
)
|
||||
|
||||
// newTimerDB 消费域测试库。
|
||||
func newTimerDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, m := range []any{&entity.MessageTask{}, &entity.MessageDetail{}} {
|
||||
if err := db.Migrator().CreateTable(m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
// fakeDispatcher 测试分发器(dispatch.TaskDispatcher 替身;自 dispatch/timer_test.go 同体复制)。
|
||||
type fakeDispatcher struct {
|
||||
calls []int64
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *fakeDispatcher) Dispatch(_ context.Context, task *entity.MessageTask) (int64, int64, error) {
|
||||
if f.err != nil {
|
||||
return 0, 0, f.err
|
||||
}
|
||||
f.calls = append(f.calls, task.ID)
|
||||
return int64(task.MobileCount), 0, nil
|
||||
}
|
||||
|
||||
// getTask 取任务断言用(自 dispatch/timer_test.go 同体复制)。
|
||||
func getTask(t *testing.T, db *gorm.DB, id int64) entity.MessageTask {
|
||||
var task entity.MessageTask
|
||||
if err := db.First(&task, id).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return task
|
||||
}
|
||||
|
||||
// captureSink 事件捕获(自 submit/pipeline_test.go 同体复制;桥接测试本地化)。
|
||||
type captureSink struct {
|
||||
events []*entity.MessageTask
|
||||
details [][]entity.MessageDetail
|
||||
}
|
||||
|
||||
func (c *captureSink) EmitSubmit(_ context.Context, t *entity.MessageTask,
|
||||
d []entity.MessageDetail) error {
|
||||
c.events = append(c.events, t)
|
||||
c.details = append(c.details, d)
|
||||
return nil
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user