refactor: 案例项目目录规范化并同步 harness

将 apps/、docs/facts(+business/tech-topic)、.overnight-runner 收敛为统一布局,并刷新安装器默认值与 skill 占位符(DESIGNS_DIR→FACTS_DIR)。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
ning-inspire
2026-11-17 10:34:28 +08:00
parent 26975e5c3a
commit 2fb2fc3361
215 changed files with 740 additions and 426 deletions

View File

@@ -0,0 +1,16 @@
# 事实文档(Facts)
描述系统 **当前真实状态** 的文档,随代码同步更新。
| 位置 | 内容 |
|---|---|
| 本目录根 | 可从代码校准的技术事实:`api.yaml`、`db-schema.md`、`domain-model.md` |
| [`business/`](./business/) | 业务事实(流程等) |
| [`tech-topic/`](./tech-topic/) | 技术专题 / ADR / 实现说明 |
## 现有文档
- [api.yaml](./api.yaml) — API 接口定义
- [db-schema.md](./db-schema.md) — 数据库规格
- [domain-model.md](./domain-model.md) — 领域模型
- [business/booking-flow.md](./business/booking-flow.md) — 预约业务流程

View File

@@ -0,0 +1,406 @@
openapi: "3.0.3"
info:
title: 咨询预约平台 API
version: 0.1.0
paths:
/api/v1/auth/register:
post:
tags: [Auth]
summary: 用户注册
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [email, password, name]
properties:
email: { type: string, format: email }
password: { type: string, minLength: 8 }
name: { type: string }
phone: { type: string }
responses:
"200": { description: 注册成功, content: { application/json: { schema: { $ref: "#/components/schemas/AuthResponse" } } } }
"400": { $ref: "#/components/responses/Error400" }
/api/v1/auth/login:
post:
tags: [Auth]
summary: 用户登录
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [email, password]
properties:
email: { type: string, format: email }
password: { type: string }
responses:
"200": { description: 登录成功, content: { application/json: { schema: { $ref: "#/components/schemas/AuthResponse" } } } }
"400": { $ref: "#/components/responses/Error400" }
/api/v1/home/info:
get:
tags: [Home]
summary: 首页信息
responses:
"200": { description: 首页内容, content: { application/json: { schema: { type: object, properties: { title: { type: string }, description: { type: string } } } } } }
/api/v1/users/me:
get:
tags: [User]
summary: 获取个人资料
security: [{ BearerAuth: [] }]
responses:
"200": { description: 用户信息, content: { application/json: { schema: { $ref: "#/components/schemas/UserResponse" } } } }
"401": { $ref: "#/components/responses/Error401" }
put:
tags: [User]
summary: 更新个人资料
security: [{ BearerAuth: [] }]
requestBody:
content: { application/json: { schema: { type: object, properties: { name: { type: string }, phone: { type: string } } } } }
responses:
"200": { description: 更新后的用户信息, content: { application/json: { schema: { $ref: "#/components/schemas/UserResponse" } } } }
/api/v1/users/me/password:
put:
tags: [User]
summary: 修改密码
security: [{ BearerAuth: [] }]
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [oldPassword, newPassword]
properties:
oldPassword: { type: string }
newPassword: { type: string, minLength: 8 }
responses:
"200": { description: 密码修改成功 }
"400": { $ref: "#/components/responses/Error400" }
/api/v1/consultants:
get:
tags: [Consultant]
summary: 获取咨询师列表
responses:
"200": { description: 咨询师列表, content: { application/json: { schema: { type: array, items: { $ref: "#/components/schemas/ConsultantResponse" } } } } }
/api/v1/consultants/{id}/slots:
get:
tags: [Consultant]
summary: 获取咨询师可预约时间段
parameters:
- name: id
in: path
required: true
schema: { type: string }
- name: date
in: query
required: true
schema: { type: string, format: date }
responses:
"200": { description: 可用时间段列表, content: { application/json: { schema: { type: array, items: { type: object, properties: { startTime: { type: string }, endTime: { type: string } } } } } } }
/api/v1/appointments:
get:
tags: [Appointment]
summary: 获取我的预约列表
security: [{ BearerAuth: [] }]
responses:
"200": { description: 预约列表, content: { application/json: { schema: { type: array, items: { $ref: "#/components/schemas/AppointmentResponse" } } } } }
post:
tags: [Appointment]
summary: 创建预约
security: [{ BearerAuth: [] }]
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [consultantId, date, startTime, endTime]
properties:
consultantId: { type: string }
branchId: { type: string }
date: { type: string, format: date }
startTime: { type: string }
endTime: { type: string }
note: { type: string }
responses:
"200": { description: 创建成功, content: { application/json: { schema: { $ref: "#/components/schemas/AppointmentResponse" } } } }
"409": { $ref: "#/components/responses/Error409" }
/api/v1/appointments/{id}:
get:
tags: [Appointment]
summary: 获取预约详情
security: [{ BearerAuth: [] }]
parameters:
- name: id
in: path
required: true
schema: { type: string }
responses:
"200": { description: 预约详情, content: { application/json: { schema: { $ref: "#/components/schemas/AppointmentResponse" } } } }
/api/v1/appointments/{id}/cancel:
put:
tags: [Appointment]
summary: 取消预约
security: [{ BearerAuth: [] }]
parameters:
- name: id
in: path
required: true
schema: { type: string }
requestBody:
content: { application/json: { schema: { type: object, properties: { reason: { type: string } } } } }
responses:
"200": { description: 取消成功 }
"409": { $ref: "#/components/responses/Error409" }
/admin/dashboard:
get:
tags: [Admin]
summary: 管理仪表盘
security: [{ BearerAuth: [] }]
responses:
"200": { description: 统计数据, content: { application/json: { schema: { type: object, properties: { totalUsers: { type: integer }, totalConsultants: { type: integer }, totalAppointments: { type: integer }, pendingAppointments: { type: integer }, totalBranches: { type: integer } } } } } }
/admin/users:
get:
tags: [Admin]
summary: 用户列表(管理员)
security: [{ BearerAuth: [] }]
responses:
"200": { description: 用户列表, content: { application/json: { schema: { type: array, items: { $ref: "#/components/schemas/UserResponse" } } } } }
/admin/users/{id}/toggle-status:
put:
tags: [Admin]
summary: 启用/禁用用户
security: [{ BearerAuth: [] }]
parameters:
- name: id
in: path
required: true
schema: { type: string }
responses:
"200": { description: 操作成功 }
/admin/consultants:
get:
tags: [Admin]
summary: 咨询师列表(管理员)
security: [{ BearerAuth: [] }]
responses:
"200": { description: 咨询师列表, content: { application/json: { schema: { type: array, items: { $ref: "#/components/schemas/ConsultantResponse" } } } } }
/admin/appointments:
get:
tags: [Admin]
summary: 预约列表(管理员,全量)
security: [{ BearerAuth: [] }]
responses:
"200": { description: 预约列表, content: { application/json: { schema: { type: array, items: { $ref: "#/components/schemas/AppointmentResponse" } } } } }
/admin/branches:
get:
tags: [Admin]
summary: 网点列表
security: [{ BearerAuth: [] }]
responses:
"200": { description: 网点列表, content: { application/json: { schema: { type: array, items: { $ref: "#/components/schemas/BranchResponse" } } } } }
post:
tags: [Admin]
summary: 创建网点
security: [{ BearerAuth: [] }]
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [name]
properties:
name: { type: string }
address: { type: string }
phone: { type: string }
responses:
"200": { description: 创建成功, content: { application/json: { schema: { $ref: "#/components/schemas/BranchResponse" } } } }
/admin/branches/{id}:
put:
tags: [Admin]
summary: 更新网点
security: [{ BearerAuth: [] }]
parameters:
- name: id
in: path
required: true
schema: { type: string }
requestBody:
content:
application/json:
schema:
type: object
properties:
name: { type: string }
address: { type: string }
phone: { type: string }
responses:
"200": { description: 更新成功 }
delete:
tags: [Admin]
summary: 删除网点
security: [{ BearerAuth: [] }]
parameters:
- name: id
in: path
required: true
schema: { type: string }
responses:
"200": { description: 删除成功 }
/api/v1/data-dictionaries:
get:
tags: [DataDictionary]
summary: 获取所有数据字典类型列表
responses:
"200": { description: 字典类型列表, content: { application/json: { schema: { type: array, items: { $ref: "#/components/schemas/DataDictionaryResponse" } } } } }
/api/v1/data-dictionaries/{type}:
get:
tags: [DataDictionary]
summary: 获取指定字典类型的条目列表
parameters:
- name: type
in: path
required: true
schema: { type: string }
responses:
"200": { description: 字典条目列表, content: { application/json: { schema: { type: array, items: { type: object, properties: { code: { type: string }, description: { type: string } } } } } } }
/api/v1/business-rules:
get:
tags: [BusinessRule]
summary: 获取所有业务规则
responses:
"200": { description: 业务规则列表, content: { application/json: { schema: { type: array, items: { $ref: "#/components/schemas/BusinessRuleResponse" } } } } }
components:
securitySchemes:
BearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
schemas:
UserResponse:
type: object
properties:
id: { type: string }
email: { type: string }
name: { type: string }
phone: { type: string }
role: { type: string, enum: [ROLE_USER, ROLE_ADMIN] }
status: { type: string, enum: [ACTIVE, DISABLED] }
AuthResponse:
type: object
properties:
token: { type: string }
user: { $ref: "#/components/schemas/UserResponse" }
ConsultantResponse:
type: object
properties:
id: { type: string }
name: { type: string }
title: { type: string }
bio: { type: string }
specialties: { type: string }
avatar: { type: string }
branchId: { type: string }
status: { type: string, enum: [ACTIVE, DISABLED] }
sortOrder: { type: integer }
AppointmentResponse:
type: object
properties:
id: { type: string }
userId: { type: string }
consultantId: { type: string }
branchId: { type: string }
appointmentDate: { type: string, format: date }
startTime: { type: string }
endTime: { type: string }
durationMinutes: { type: integer }
status: { type: string, enum: [PENDING, CONFIRMED, COMPLETED, CANCELLED] }
note: { type: string }
cancelReason: { type: string }
BranchResponse:
type: object
properties:
id: { type: string }
name: { type: string }
address: { type: string }
phone: { type: string }
status: { type: string, enum: [ACTIVE, DISABLED] }
DataDictionaryResponse:
type: object
properties:
typeName: { type: string }
description: { type: string }
items:
type: array
items:
type: object
properties:
code: { type: string }
description: { type: string }
BusinessRuleResponse:
type: object
properties:
ruleName: { type: string }
description: { type: string }
config: { type: object }
ErrorResponse:
type: object
properties:
errorCode: { type: string }
errorMessage: { type: string }
data: { type: object, nullable: true }
responses:
Error400:
description: 请求参数错误
content:
application/json:
schema: { $ref: "#/components/schemas/ErrorResponse" }
Error401:
description: 未授权
content:
application/json:
schema: { $ref: "#/components/schemas/ErrorResponse" }
Error404:
description: 资源不存在
content:
application/json:
schema: { $ref: "#/components/schemas/ErrorResponse" }
Error409:
description: 业务冲突
content:
application/json:
schema: { $ref: "#/components/schemas/ErrorResponse" }

View File

@@ -0,0 +1,15 @@
# 业务事实(Business Facts)
存放与业务语义直接相关的事实文档:流程、业务侧说明等。
与 `../` 根目录的技术事实(API / 库表 / 领域模型)以及 `../tech-topic/` 技术专题区分开:
| 目录 | 内容 |
|---|---|
| `docs/facts/` | 可从代码校准的技术事实 |
| `docs/facts/tech-topic/` | 技术专题 / ADR / 实现说明 |
| `docs/facts/business/` | 业务事实(本目录) |
## 现有文档
- [booking-flow.md](./booking-flow.md) — 预约业务流程

View File

@@ -0,0 +1,100 @@
# 预约业务流程事实
## 1. 角色定义
| 角色 | 说明 | 系统表示 |
|------|------|----------|
| 普通用户(访客) | 未登录,可浏览首页与咨询师列表 | 无认证 / ROLE_USER |
| 注册用户 | 已登录,可预约、查看/取消自己的预约、编辑资料 | ROLE_USER |
| 管理员 | 已登录,可查看仪表盘统计、管理用户/咨询师/预约/网点 | ROLE_ADMIN |
## 2. 预约流程(4 步)
```
Step 1: 选择咨询师
- 系统展示所有 ACTIVE 状态的咨询师,按 sortOrder 升序
- 用户点击选中一个
Step 2: 选择日期
- 系统展示未来 7 天(含今日)的日期按钮
- 日期连续,无间隔
- 用户点击选中一个
Step 3: 选择时间
- 系统根据选中的咨询师 + 日期,查询可用时段
- 可用时段 = 09:00-18:00 内,每小时一个 slot(09:00, 10:00, ..., 17:00)
- 排除已存在非取消状态的预约
- 用户点击选中一个
Step 4: 确认预约
- 用户可填写备注(选填)
- 提交后系统创建预约,状态为 PENDING(待确认)
- 系统检查同一咨询师、同一日期、同一时段是否已有非取消预约 → 冲突则拒绝(409)
```
## 3. 预约状态机
```
PENDING ──► CONFIRMED ──► COMPLETED
│ │
└──────┬───────┘
▼
CANCELLED
```
| 状态 | 含义 | 可操作 |
|------|------|--------|
| PENDING | 待确认(用户已提交,管理员未确认) | 取消 |
| CONFIRMED | 已确认(管理员已确认) | 取消、完成 |
| COMPLETED | 已完成(终态) | 无 |
| CANCELLED | 已取消(终态) | 无 |
注:从 PENDING 和 CONFIRMED 可取消,取消时需要填写原因。
## 4. 业务规则
### 预约相关
| 规则 | 描述 | 实施方式 |
|------|------|----------|
| 冲突检测 | 同一咨询师同一时段不可重复预约 | `uk_appointment_slot` 唯一索引 + `AppointmentConflictService` |
| 时段间隔 | 预约时段间隔为 60 分钟 | 前端 time slot 按 1h 步进,后端 slot.intervalMinutes = 60 |
| 可用时段 | 可预约时段为 09:00-18:00 | 前端展示 09:00-17:00,后端 slot.available.start/end |
| 取消窗口 | PENDING 和 CONFIRMED 状态可取消 | `Appointment.canCancel()` 守卫方法 |
### 认证相关
| 规则 | 描述 | 实施方式 |
|------|------|----------|
| 密码最小长度 | 密码最少 8 位 | 前端 + 后端校验 |
| JWT 有效期 | JWT 有效期 24 小时 | `jwt.expiration=86400000` |
| 密码算法 | 密码使用 BCrypt 加密 | `PasswordService` → `BCryptPasswordEncoder` |
## 5. 前台 vs 后台功能划分
### 前台(/user/*,需 ROLE_USER)
| 页面 | 功能 |
|------|------|
| 仪表盘 | 展示本人预约统计:总预约数、待确认数、已完成数 |
| 预约管理 | 列出本人预约,可取消 PENDING/CONFIRMED 状态的预约 |
| 预约咨询 | 4 步预约流程 |
| 个人中心 | 编辑姓名、手机号;修改密码 |
### 后台(/admin/*,需 ROLE_ADMIN)
| 页面 | 功能 |
|------|------|
| 仪表盘 | 全局统计:用户数、咨询师数、总预约数、待确认数、网点数 |
| 用户管理 | 列出所有用户,可启用/禁用用户 |
| 咨询师管理 | 列出所有咨询师 |
| 预约管理 | 列出所有预约 |
| 网点管理 | CRUD 网点(新增、编辑、删除) |
## 6. 种子数据
| 类型 | 数据 |
|------|------|
| 管理员账号 | admin@test.com / password |
| 咨询师 | 张医生(资深心理咨询师)、李律师(法律顾问)、王教练(职业规划师) |
| 网点 | 朝阳咨询中心、海淀咨询中心、浦东咨询中心 |

View File

@@ -0,0 +1,77 @@
# 数据库设计
> 所有表使用 InnoDB 引擎,字符集 utf8mb4,排序 utf8mb4_bin。
## 公共字段
| 字段 | 类型 | 说明 |
|------|------|------|
| created_by | VARCHAR(64) | 创建者 ID |
| created_time | DATETIME | 创建时间,默认 CURRENT_TIMESTAMP |
| updated_by | VARCHAR(64) | 最后修改者 ID |
| updated_time | DATETIME | 最后修改时间,默认 CURRENT_TIMESTAMP,ON UPDATE 自动更新 |
## user — 用户表
| 字段 | 类型 | 约束 | 说明 |
|------|------|------|------|
| id | VARCHAR(64) | PK | UUID 主键 |
| email | VARCHAR(100) | UNIQUE, NOT NULL | 邮箱 |
| password | VARCHAR(255) | NOT NULL | BCrypt 加密密码 |
| name | VARCHAR(50) | NOT NULL | 姓名 |
| phone | VARCHAR(20) | | 手机号 |
| role | VARCHAR(20) | NOT NULL, DEFAULT 'ROLE_USER' | 角色 |
| status | VARCHAR(20) | NOT NULL, DEFAULT 'ACTIVE' | 状态 |
**索引**:`uk_user_email` UNIQUE on (email)
## consultant — 咨询师表
| 字段 | 类型 | 约束 | 说明 |
|------|------|------|------|
| id | VARCHAR(64) | PK | UUID 主键 |
| name | VARCHAR(50) | NOT NULL | 姓名 |
| title | VARCHAR(100) | NOT NULL | 职称 |
| bio | TEXT | | 简介 |
| specialties | VARCHAR(255) | | 专长领域 |
| avatar | VARCHAR(255) | | 头像 URL |
| branch_id | VARCHAR(64) | FK → branch(id) | 所属网点 |
| status | VARCHAR(20) | NOT NULL, DEFAULT 'ACTIVE' | 状态 |
| sort_order | INT | NOT NULL, DEFAULT 0 | 排序权重 |
## appointment — 预约表
| 字段 | 类型 | 约束 | 说明 |
|------|------|------|------|
| id | VARCHAR(64) | PK | UUID 主键 |
| user_id | VARCHAR(64) | FK → user(id) | 预约用户 |
| consultant_id | VARCHAR(64) | FK → consultant(id) | 咨询师 |
| branch_id | VARCHAR(64) | FK → branch(id) | 所属网点 |
| appointment_date | DATE | NOT NULL | 预约日期 |
| start_time | VARCHAR(5) | NOT NULL | 开始时间 (HH:mm) |
| end_time | VARCHAR(5) | NOT NULL | 结束时间 (HH:mm) |
| duration_minutes | INT | NOT NULL | 时长(分钟) |
| status | VARCHAR(20) | NOT NULL, DEFAULT 'PENDING' | 状态 |
| note | VARCHAR(500) | | 备注 |
| cancel_reason | VARCHAR(200) | | 取消原因 |
**索引**:
- `uk_appointment_slot` UNIQUE on (consultant_id, appointment_date, start_time)
- `idx_user_id` on (user_id)
- `idx_consultant_date` on (consultant_id, appointment_date)
## branch — 网点表
| 字段 | 类型 | 约束 | 说明 |
|------|------|------|------|
| id | VARCHAR(64) | PK | UUID 主键 |
| name | VARCHAR(100) | NOT NULL | 网点名称 |
| address | VARCHAR(255) | | 地址 |
| phone | VARCHAR(20) | | 电话 |
| status | VARCHAR(20) | NOT NULL, DEFAULT 'ACTIVE' | 状态 |
## 表关系图
```
user (1) ──→ (N) appointment (N) ←── (1) consultant (N) ←── (1) branch
```

View File

@@ -0,0 +1,107 @@
# 领域模型设计
> 本文档描述咨询预约平台的领域模型事实(as-built)。
## 聚合根 (Aggregate Root)
### User
| 字段 | 类型 | 说明 |
|------|------|------|
| id | String | UUID |
| email | String | 邮箱(登录名),唯一 |
| password | String | BCrypt 编码 |
| name | String | 姓名 |
| phone | String | 手机号 |
| role | UserRole | ROLE_USER / ROLE_ADMIN |
| status | UserStatus | ACTIVE / DISABLED |
**方法**:
- `isAdmin()` — role == ROLE_ADMIN
- `isActive()` — status == ACTIVE
### Consultant
| 字段 | 类型 | 说明 |
|------|------|------|
| id | String | UUID |
| name | String | 姓名 |
| title | String | 职称 |
| bio | String | 简介 |
| specialties | String | 专长(逗号分隔) |
| avatar | String | 头像 URL |
| branchId | String | 所属网点 ID |
| status | ConsultantStatus | ACTIVE / DISABLED |
| sortOrder | int | 排序权重 |
**内联枚举**:`ConsultantStatus { ACTIVE, DISABLED }`
**方法**:
- `isAvailable()` — status == ACTIVE
### Appointment
| 字段 | 类型 | 说明 |
|------|------|------|
| id | String | UUID |
| userId | String | 预约用户 ID |
| consultantId | String | 咨询师 ID |
| branchId | String | 网点 ID(可选) |
| appointmentDate | LocalDate | 预约日期 |
| startTime | String | 开始时间 HH:mm |
| endTime | String | 结束时间 HH:mm |
| durationMinutes | int | 时长 |
| status | AppointmentStatus | PENDING / CONFIRMED / COMPLETED / CANCELLED |
| note | String | 用户备注 |
| cancelReason | String | 取消原因 |
**方法**:
- `isOwnedBy(userId)` — 判断是否属于该用户
- `canCancel()` — status 不是 COMPLETED 也不是 CANCELLED
### Branch
| 字段 | 类型 | 说明 |
|------|------|------|
| id | String | UUID |
| name | String | 网点名称 |
| address | String | 地址 |
| phone | String | 电话 |
| status | BranchStatus | ACTIVE / DISABLED |
**内联枚举**:`BranchStatus { ACTIVE, DISABLED }`
## 值对象 (Value Object)
| 类 | 字段 | 说明 |
|----|------|------|
| UserRole | ROLE_USER, ROLE_ADMIN | 用户角色枚举 |
| UserStatus | ACTIVE, DISABLED | 用户状态枚举 |
| AppointmentStatus | PENDING, CONFIRMED, COMPLETED, CANCELLED | 预约状态枚举 |
| TimeSlot | date, startTime, endTime | 时间段 VO,用于冲突检测 |
## 领域服务 (Domain Service)
| 服务 | 方法 | 说明 |
|------|------|------|
| PasswordService | encode(raw), matches(raw, encoded) | 密码编码/校验接口(infrastructure 实现 BCrypt) |
| AppointmentConflictService | validateNoConflict(existing, requested) | 校验同一咨询师同一时段无冲突预约 |
## 仓储接口 (Repository)
| 接口 | 核心方法 | 说明 |
|------|----------|------|
| UserRepository | findById, findByEmail, findAll, save, update, existsByEmail | 用户持久化 |
| ConsultantRepository | findById, findAllActive, findAll, save, update, deleteById, count | 咨询师持久化 |
| AppointmentRepository | findById, findByUserId, findByConsultantIdAndDate, findAll, save, update, count, countByStatus, countByUserId | 预约持久化 |
| BranchRepository | findById, findAll, save, update, deleteById, count | 网点持久化 |
## 聚合关系
```
User (1) ──→ (N) Appointment (N) ←── (1) Consultant (N) ←── (1) Branch
```
- User 创建 Appointment,Appointment 关联一个 User 和一个 Consultant
- Consultant 属于一个 Branch
- Branch 独立创建/停用,Consultant 依赖 Branch 存在

View File

@@ -0,0 +1,48 @@
# ADR-001: 采用四层 DDD 单体架构
- **状态**: 已接受
- **日期**: 2026-06-19
## 上下文
项目需构建一个全栈咨询预约平台,后端需要支撑:
- 清晰的业务逻辑与基础设施分离
- 业务规则与状态机的可维护性
- 适应未来的微服务拆分
## 决策
采用四层 DDD(Domain-Driven Design)单体架构:
```
adapter(HTTP Controller)→ application(Service)
→ domain(Aggregate / VO / DomainService)
← infrastructure(Persistence / Security)
```
依赖规则:
- 依赖方向单向:adapter → application → domain;infrastructure → domain
- domain 层零外部依赖(不依赖 Spring、Web、数据库框架)
## 备选方案
| 方案 | 理由 | 否决原因 |
|------|------|----------|
| 传统三层(Controller/Service/DAO) | 简单直接 | 业务逻辑易泄漏到 Service 层,领域规则分散 |
| 纯微服务 | 独立部署、扩展 | 团队规模小,过早拆分增加运维复杂度 |
## 后果
**正面**:
- 领域逻辑集中在 domain 层,便于测试与推理
- 基础设施可替换(如 MySQL → PostgreSQL 只改 infrastructure 层)
- 为未来拆分保留清晰边界
**负面**:
- 初期开发速度略慢于三层(需维护 converter/assembler)
- 简单 CRUD 需经过四层,代码量较大
## 遵循度
- 新代码必须遵循四层依赖方向
- CI 应通过 `archunit` 或代码审查确保无反向依赖

View File

@@ -0,0 +1,31 @@
# ADR-002: 主键采用 UUID 字符串而非自增整数
- **状态**: 已接受
- **日期**: 2026-06-19
## 上下文
四张核心表(user、consultant、appointment、branch)需要主键策略。选择包括自增整数、UUID 二进制、UUID 字符串。
## 决策
所有表主键采用 UUID 字符串(`VARCHAR(64)`),由应用层 Java 代码生成。
## 备选方案
| 方案 | 理由 | 否决原因 |
|------|------|----------|
| BIGINT AUTO_INCREMENT | 简单、高性能 | 暴露记录数、合并数据库时有冲突风险 |
| UUID BINARY(16) | 存储紧凑 | MyBatis-Plus 处理不方便,调试时不可读 |
| UUID VARCHAR(64) | 可读、无冲突、不暴露信息 | 存储略大(MySQL InnoDB 可接受) |
## 后果
**正面**:
- 主键不暴露记录数,安全
- 跨数据库迁移无冲突
- 日志中可直接查看和关联 ID
**负面**:
- VARCHAR(64) PK 在 InnoDB 聚簇索引中比 BIGINT 占用更多空间
- 需在应用层手动生成 UUID(非数据库自动生成)

View File

@@ -0,0 +1,30 @@
# ADR-003: 持久化框架选择 MyBatis-Plus 而非 JPA/Hibernate
- **状态**: 已接受
- **日期**: 2026-06-19
## 上下文
项目中需要 ORM 框架管理数据库映射。候选方案包括 Spring Data JPA(Hibernate)和 MyBatis-Plus。
## 决策
采用 MyBatis-Plus 3.5.5(Spring Boot 3 Starter)。
## 备选方案
| 方案 | 理由 | 否决原因 |
|------|------|----------|
| Spring Data JPA | 对象映射自动、HQL 灵活 | N+1 问题排查困难、复杂查询难优化、DDD 聚合装配不直接 |
| MyBatis-Plus | SQL 可控、映射直接、分页插件成熟 | 需手写部分 XML/注解 SQL |
## 后果
**正面**:
- SQL 完全可控,可针对 MySQL 优化
- MyBatis-Plus `BaseMapper` 提供基础 CRUD 无需写 SQL
- 分页查询简单(`PaginationInnerInterceptor`)
**负面**:
- 关联查询需手写 SQL(如统计类查询)
- 无 Hibernate 的自动脏检查(需显式调用 update)

View File

@@ -0,0 +1,37 @@
# ADR-004: 采用 JWT HMAC-SHA256 无状态鉴权
- **状态**: 已接受
- **日期**: 2026-06-19
## 上下文
前后端分离架构需要鉴权方案。需要决定使用 Session/Cookie 还是 Token(JWT)。
## 决策
后端自行签发与校验 JWT(HMAC-SHA256,24h 过期),无状态鉴权。
JWT Payload:
- `sub`: userId
- `email`: 用户邮箱
- `role`: ROLE_USER / ROLE_ADMIN
## 备选方案
| 方案 | 理由 | 否决原因 |
|------|------|----------|
| Session + Cookie | Spring Security 默认方案 | 需 Redis 存储 session、跨域配置复杂、不适合前后端分离 |
| OAuth2 / OIDC | 标准协议 | 增加 IDP 依赖,小型项目不需要 |
| JWT HMAC-SHA256 | 无状态、无额外存储 | 无法主动使 token 失效 |
## 后果
**正面**:
- 无状态、无需 Redis
- 前后端分离友好(`Authorization: Bearer <token>`)
- 后端自行签发,无外部依赖
**负面**:
- Token 签发后无法主动吊销(24h 内有效)
- 密钥保存在配置中,泄漏风险需注意
- 载荷明文字段不宜存放敏感信息

View File

@@ -0,0 +1,35 @@
# ADR-005: 当前用户上下文采用 ThreadLocal 传递
- **状态**: 已接受
- **日期**: 2026-06-19
## 上下文
应用层和领域层的方法需要获取当前登录用户信息(userId、email、role)。需要决定传递方式。
## 决策
使用 `UserContextHolder`(ThreadLocal) + `SecurityUtils` 静态工具类获取当前用户上下文。
- `JwtAuthenticationFilter` 从请求头解析 JWT → 写入 `SecurityContextHolder` + `UserContextHolder`
- `UserContextHolder` 在请求结束时(`finally`块)清除 ThreadLocal
## 备选方案
| 方案 | 理由 | 否决原因 |
|------|------|----------|
| 方法参数显式传递 userId | 最干净 | 几乎所有方法都需要 userId,接口签名冗余 |
| Spring Security Authentication | 标准做法 | 领域层不应依赖 Spring Security API |
| 请求作用域 Bean | 自动注入 | 不直观,需要理解 Spring 作用域机制 |
## 后果
**正面**:
- 应用层/领域层无需在方法签名中传递 userId
- 不依赖 Spring Security API(只依赖 `UserContext` 值对象)
- ThreadLocal 在请求结束后自动清理,无内存泄漏
**负面**:
- ThreadLocal 隐式依赖需注意测试时设置上下文
- 异步操作(如 `@Async`)需手动传递上下文
- 需要 `SecurityUtils` 工具类做间接访问

View File

@@ -0,0 +1,44 @@
# 业务规则清单(事实方案)
> 所有业务规则通过 `IBusinessRule` 接口定义,可通过 `GET /api/v1/business-rules` 动态查询。
> 规则配置存储在 `resources/business-rules/*.yml` 中。
## 认证规则
| 规则名 | 描述 | 当前值 | 配置路径 |
|--------|------|--------|----------|
| auth.password.minLength | 密码最小长度 | 8 | `auth.yml` → password.minLength |
| auth.token.expirationMs | JWT Token 有效期(毫秒) | 86400000 (24h) | `auth.yml` → token.expirationMs |
| auth.password.algorithm | 密码加密算法 | BCrypt | `auth.yml` → password.algorithm |
> 认证规则的取值来自 `business-rules/auth.yml`,通过 `GET /api/v1/business-rules`
> 的 `config.auth` 子树对外暴露(结构见 `BusinessRuleConfig.AuthConfig`)。
> 运行时真正生效的 JWT 有效期取 `application.yml` → `jwt.expiration`(`JwtTokenProvider` 读取),
> 密码加密算法为 `PasswordServiceImpl` 里的 `BCryptPasswordEncoder`;
> 这两处当前与 YAML 取值一致,改动时需同步。
## 预约规则
| 规则名 | 描述 | 当前值 | 代码位置 |
|--------|------|--------|----------|
| appointment.conflict.detection | 同一咨询师同一时段不可重复预约 | 唯一索引 + 代码校验 | AppointmentConflictService + uk_appointment_slot |
| appointment.slot.minMinutes | 最短预约时长 | 30 (implicit) | TimeSlot 业务约定 |
| appointment.slot.maxMinutes | 最长预约时长 | 120 (implicit) | TimeSlot 业务约定 |
| appointment.available.start | 每日可预约开始时间 | 09:00 | ConsultantService.getAvailableSlots |
| appointment.available.end | 每日可预约结束时间 | 18:00 | ConsultantService.getAvailableSlots |
| appointment.slot.intervalMinutes | 时间段间隔 | 60 | ConsultantService.getAvailableSlots |
## 管理规则
| 规则名 | 描述 | 当前值 | 代码位置 |
|--------|------|--------|----------|
| admin.access.role | 管理后台权限角色 | ROLE_ADMIN | SecurityConfig + JwtAuthenticationFilter |
| user.status.toggle | 用户启用/停用 | 允许,不可操作自身 | AdminService.toggleUserStatus |
| branch.delete.guard | 删除网点前检查关联咨询师 | 暂未实现 | AdminService (待补充) |
## 字典规则
| 规则名 | 描述 | 当前值 | 代码位置 |
|--------|------|----------|----------|
| data.dictionary.queryable | 数据字典可通过 API 查询 | 所有注册的 DataDictionaryType | DataDictionaryController (Phase 3) |
| enum.self.describing | 所有业务枚举实现 Description 接口 | 提供 getCode() / getDescription() | 各 enum 文件 (Phase 3) |

View File

@@ -0,0 +1,62 @@
# 数据字典清单(事实方案)
> 所有枚举类型均实现 `Description` 接口,可通过 `GET /api/v1/data-dictionaries/{type}` 动态查询。
## UserRole — 用户角色
| Code | 描述 | 说明 |
|------|------|------|
| ROLE_USER | 普通用户 | 默认角色,可预约咨询 |
| ROLE_ADMIN | 管理员 | 可访问管理后台 |
## UserStatus — 用户状态
| Code | 描述 | 说明 |
|------|------|------|
| ACTIVE | 正常 | 可用 |
| DISABLED | 已停用 | 不可登录和操作 |
## AppointmentStatus — 预约状态
| Code | 描述 | 业务含义 |
|------|------|----------|
| PENDING | 待确认 | 初始状态,等待咨询师确认 |
| CONFIRMED | 已确认 | 咨询师已确认,预约生效 |
| COMPLETED | 已完成 | 咨询已结束,终态 |
| CANCELLED | 已取消 | 用户或管理员取消,终态 |
**状态机流转**:
```
PENDING ──→ CONFIRMED ──→ COMPLETED
│ │
└── CANCELLED ←──┘
```
## ConsultantStatus — 咨询师状态
| Code | 描述 | 说明 |
|------|------|------|
| ACTIVE | 可预约 | 可在预约列表展示和选择 |
| DISABLED | 不可预约 | 不展示在预约列表 |
## BranchStatus — 网点状态
| Code | 描述 | 说明 |
|------|------|------|
| ACTIVE | 营业中 | 正常运营 |
| DISABLED | 已关闭 | 停止运营 |
## DataDictionaryType — 字典类型注册表
| Code | 关联枚举 | 描述 | 查询端点 |
|------|----------|------|----------|
| USER_ROLE | UserRole | 用户角色字典 | `/api/v1/data-dictionaries/USER_ROLE` |
| USER_STATUS | UserStatus | 用户状态字典 | `/api/v1/data-dictionaries/USER_STATUS` |
| APPOINTMENT_STATUS | AppointmentStatus | 预约状态字典 | `/api/v1/data-dictionaries/APPOINTMENT_STATUS` |
| CONSULTANT_STATUS | Consultant.ConsultantStatus | 咨询师状态字典 | `/api/v1/data-dictionaries/CONSULTANT_STATUS` |
| BRANCH_STATUS | Branch.BranchStatus | 网点状态字典 | `/api/v1/data-dictionaries/BRANCH_STATUS` |
## 同步策略
修改枚举后,运行 `DataDictionaryUtil.generateMarkdown()` 更新本文档。

View File

@@ -0,0 +1,84 @@
# 错误码清单
> 本文档描述咨询预约平台的错误码体系事实(as-built)。
## 错误码结构
```
{ErrorGroup}_{SubCode}
```
- ErrorGroup(错误组):错误的来源分类
- SubCode(子码):具体错误标识,可读性强
## ErrorGroup 定义
| Group | HTTP Status | 说明 |
|-------|-------------|------|
| COMMON | 400/404 | 通用参数/资源错误 |
| AUTH | 401/400 | 认证/鉴权错误 |
| BUSINESS | 409/400 | 业务逻辑冲突/错误 |
| SYSTEM | 500 | 系统内部错误 |
## 全量错误码
### COMMON 组
| ErrorCode | HTTP Status | 说明 | 触发场景 |
|-----------|-------------|------|----------|
| COMMON_PARAM_ERROR | 400 | 参数校验失败 | @Valid 校验不通过 |
| COMMON_RESOURCE_NOT_FOUND | 404 | 资源不存在 | 根据 ID 查询无结果 |
| COMMON_VALIDATION_ERROR | 400 | 参数格式错误 | 字段级校验失败 |
### AUTH 组
| ErrorCode | HTTP Status | 说明 | 触发场景 |
|-----------|-------------|------|----------|
| AUTH_EMAIL_EXISTS | 400 | 邮箱已注册 | 重复 email 调用 register |
| AUTH_WEAK_PASSWORD | 400 | 密码强度不足 | 密码 < 8 位 |
| AUTH_INVALID_CREDENTIALS | 400 | 邮箱或密码错误 | login 时密码不匹配 |
| AUTH_INVALID_TOKEN | 401 | Token 无效或过期 | JWT 解析失败 |
| AUTH_TOKEN_EXPIRED | 401 | Token 已过期 | JWT 超时 |
| AUTH_FORBIDDEN | 403 | 无权限 | 非 ADMIN 访问 /admin |
| AUTH_USER_DISABLED | 401 | 用户已被停用 | status == DISABLED |
### BUSINESS 组
| ErrorCode | HTTP Status | 说明 | 触发场景 |
|-----------|-------------|------|----------|
| BUSINESS_CONFLICT_SLOT | 409 | 该时段已被预约 | 同一咨询师同一时段重复预约 |
| BUSINESS_CONSULTANT_UNAVAILABLE | 400 | 咨询师不可预约 | consultant.isAvailable() == false |
| BUSINESS_APPOINTMENT_CANNOT_CANCEL | 409 | 当前状态不允许取消 | status 为 COMPLETED 或 CANCELLED |
| BUSINESS_APPOINTMENT_NOT_OWNER | 403 | 非本人预约不可操作 | userId 不匹配 |
| BUSINESS_USER_DISABLED | 400 | 用户已被停用 | 尝试操作已 DISABLED 的用户 |
| BUSINESS_BRANCH_HAS_CONSULTANTS | 409 | 网点有咨询师不可删除 | branch 关联的 consultant > 0 |
### SYSTEM 组
| ErrorCode | HTTP Status | 说明 | 触发场景 |
|-----------|-------------|------|----------|
| SYSTEM_INTERNAL_ERROR | 500 | 系统内部错误 | 未捕获的异常 |
| SYSTEM_DB_ERROR | 500 | 数据库错误 | SQL 执行失败 |
## 返回格式
```json
{
"errorCode": "BUSINESS_CONFLICT_SLOT",
"errorMessage": "该时间段已被预约",
"data": null
}
```
## 当前实现映射
当前 `GlobalExceptionHandler` 使用简化 errorCode(无 ErrorGroup 前缀),映射关系:
| 当前 errorCode | 规范 errorCode | 异常类型 |
|----------------|---------------|----------|
| PARAM_ERROR | COMMON_PARAM_ERROR | IllegalArgumentException |
| VALIDATION_ERROR | COMMON_VALIDATION_ERROR | MethodArgumentNotValidException |
| BUSINESS_CONFLICT | BUSINESS_CONFLICT_SLOT | IllegalStateException |
| SYSTEM_ERROR | SYSTEM_INTERNAL_ERROR | Exception |
**迁移计划**:Phase 2 后统一改用规范 errorCode 格式。

View File

@@ -0,0 +1,90 @@
# 安全设计
> 本文档描述咨询预约平台的安全架构事实(as-built)。
## JWT Token
**算法**:HMAC-SHA256(`jjwt` 库 MAC 算法)
**密钥**:通过 `application.yml` 的 `jwt.secret` 配置,至少 256 位
**有效期**:24 小时(`jwt.expiration` 配置,单位毫秒)
**Payload Claims**:
| Claim | 类型 | 值 | 说明 |
|-------|------|-----|------|
| sub | String | userId | 用户 UUID |
| email | String | email | 用户邮箱 |
| role | String | ROLE_USER / ROLE_ADMIN | 角色(与 Spring Security `hasRole()` 兼容) |
| iat | Date | 签发时间戳 | -
| exp | Date | 过期时间戳 | -
## JWT 处理流程
```
请求 → JwtAuthenticationFilter (OncePerRequestFilter)
1. 从 Authorization: Bearer <token> 提取 Token
2. 解析 JWT → userId + email + role
3. 设置 SecurityContextHolder (Spring Security)
4. 设置 UserContextHolder (自定义 ThreadLocal)
5. 放行请求
6. 请求结束后清除 ThreadLocal
```
## Filter Chain 顺序
```
Spring Security Filter Chain (SecurityConfig):
1. JwtAuthenticationFilter (自定义, 提取 JWT 并设置上下文)
2. UsernamePasswordAuthenticationFilter (内置, 不启用)
3. ExceptionTranslationFilter (内置, 处理 AccessDenied)
4. ...
```
## SecurityConfig 规则矩阵
| 路径模式 | 访问要求 |
|----------|----------|
| POST /api/v1/auth/register | 完全公开 |
| POST /api/v1/auth/login | 完全公开 |
| GET /api/v1/home/info | 完全公开 |
| GET /api/v1/data-dictionaries** | 完全公开 |
| GET /api/v1/business-rules** | 完全公开 |
| GET /api/v1/consultants** | 完全公开 |
| /admin/** | 仅 ROLE_ADMIN |
| /api/v1/** | 需要认证(任何角色) |
## BCrypt 密码策略
- 编码器:`BCryptPasswordEncoder`(Spring Security 默认实现)
- 强度:10(默认 rounds)
- 密码要求:至少 8 位,由前端校验
## UserContext ThreadLocal
**类**:`UserContextHolder` — `ThreadLocal<UserContext>`
**UserContext 字段**:
| 字段 | 类型 | 来源 |
|------|------|------|
| userId | String | JWT sub |
| email | String | JWT email |
| role | String | JWT role |
**获取**:`SecurityUtils.currentUserId()` — 从 UserContextHolder 获取当前用户 ID
## CORS 配置
- 当前:`WebConfig.java` 允许所有 origin(开发阶段)
- 生产:需限制为具体域名
## 密码修改流程
```
1. 用户输入旧密码 + 新密码
2. 从 DB 获取当前 BCrypt hash
3. passwordService.matches(旧密码, 当前 hash) → 校验
4. passwordService.encode(新密码) → 更新 DB
```

View File

@@ -0,0 +1,67 @@
# 状态机设计
> 本文档描述 AppointmentStatus 的完整状态机事实(as-built)。
## 状态定义
| Code | 描述 | 含义 |
|------|------|------|
| PENDING | 待确认 | 初始状态,用户已提交但等待确认 |
| CONFIRMED | 已确认 | 咨询师/管理员已确认,预约生效 |
| COMPLETED | 已完成 | 咨询已结束,**终态** |
| CANCELLED | 已取消 | 用户或管理员取消,**终态** |
## 状态流转图
```
┌──────────┐
│ PENDING │ ← 创建预约后初始状态
└────┬─────┘
│
┌──────┴──────┐
│ │
▼ ▼
┌──────────┐ ┌──────────┐
│CONFIRMED │ │CANCELLED │ ← 用户可取消
└────┬─────┘ └──────────┘
│
▼
┌──────────┐
│COMPLETED │
└──────────┘
```
## 转换表
| 当前状态 | 目标状态 | 触发操作 | 守卫条件 | 代码位置 |
|----------|----------|----------|----------|----------|
| (新创建) | PENDING | 创建预约 (AppointmentService.create) | 咨询师可预约 + 无冲突 | `AppointmentService.java:50-53` |
| PENDING | CONFIRMED | 管理员确认 (预留) | - | 未实现 |
| PENDING | CANCELLED | 取消预约 (AppointmentService.cancel) | `!COMPLETED && !CANCELLED` | `Appointment.java` `canCancel()` |
| CONFIRMED | COMPLETED | 管理员完成 (预留) | - | 未实现 |
| CONFIRMED | CANCELLED | 取消预约 (AppointmentService.cancel) | `!COMPLETED && !CANCELLED` | `Appointment.java` `canCancel()` |
## 守卫条件
```
canCancel() = status != COMPLETED && status != CANCELLED
```
- PENDING 可取消
- CONFIRMED 可取消
- COMPLETED **不可取消**
- CANCELLED **不可再次取消**
## 业务规则集成
**冲突检测**(AppointmentConflictService):
- 创建预约时,排除 status == CANCELLED 的旧预约
- 允许在同一咨询师同一时段重新预约(如果之前的预约已取消)
- 由 `uk_appointment_slot` 唯一索引 + 代码双重校验
## 待实现状态转换
| 动作 | 优先级 | 说明 |
|------|--------|------|
| PENDING → CONFIRMED | 低 | 目前无确认端点,需管理后台功能 |
| CONFIRMED → COMPLETED | 低 | 同上 |