2026-09-04 10:20:40 +08:00

43 lines
1.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 后端架构
后端采用模块化分层单体。HTTP、业务编排、领域计算和持久化彼此隔离,后续可以替换风险模型而不改 API 层。
```text
HTTP Request
-> app/api/v1 路由、参数接收、响应
-> app/services 用例编排
-> app/domain 纯领域规则/模型适配器
-> app/repositories 数据访问
-> app/models SQLAlchemy ORM
-> PostgreSQL
```
- `app/core`:配置、日志和统一异常。
- `app/database`:异步 Engine、Session 和事务边界。
- `app/schemas`:Pydantic 请求/响应契约,外部 JSON 使用 camelCase。
- `app/domain/risk_engine.py`:当前为可替换的基线评分引擎;未来可接 sklearn、XGBoost 或远程模型服务。
- 风险评分遵循 PRD:输入值表示风险程度,先按 `S = 100 - X` 转为安全得分,再按权重求综合分;`R >= 80` 低风险,`60 <= R < 80` 较低风险,`40 <= R < 60` 中等风险,`20 <= R < 40` 高风险,`R < 20` 极高风险。
- `migrations`:Alembic 数据库迁移。
- `tests`:API 和领域服务测试。
认证与审核链路:
```text
密码登录 / 钉钉 authCode
-> UserAccount + 角色映射
-> JWT
-> 每次请求从 JWT 查用户并读取 role_permissions
-> 风险项自动预审
-> 待审核队列
-> 审核通过写入 anomaly_cases / 驳回退回
-> operation_audits 追加式留痕
```
## PRD 核心数据表
首期数据库包含 `anomaly_cases`、`supplier_profiles`、`risk_assessments`、`risk_items`、
`review_records`、`standard_rules`、`user_accounts` 和 `role_permissions` 八张核心表。
目标 PostgreSQL 当前未安装 `pgvector`,因此 `anomaly_cases.embedding` 暂用可空 `JSONB`
保存浮点数组。安装 `vector` 扩展后,应通过独立迁移将该字段改为 `VECTOR(768)` 并补充向量索引。