Files
backend/README.md
T

192 lines
7.1 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.
<div align="center">
<h2>server-java</h2>
<img alt="Java" src="https://img.shields.io/badge/Java-17-brightgreen.svg"/>
<img alt="SpringBoot" src="https://img.shields.io/badge/SpringBoot-3.3.2-green.svg"/>
<img alt="license" src="https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg"/>
</div>
基于 JDK 17、Spring Boot 3.3.2、Spring Security 6、JWT、Redis、MyBatis-Plus、Knife4j 构建的前后端分离单体业务系统。
## 项目概况
- **开发框架**:Spring Boot 3.3.2 + Spring Security 6 + MyBatis-Plus 3.5.8
- **安全认证**:Spring Security + JWT 无状态认证,部分接口白名单免登录
- **权限管理**:基于 RBAC 模型,细粒度控制到接口方法和按钮级别
- **功能模块**:用户管理、角色管理、菜单管理、部门管理、字典管理、代码生成、文件管理、邮件、短信、支付(支付宝/微信)等
- **接口文档**:Knife4j 自动生成,支持在线调试
## 项目目录
```
src/main/java/com/ichangzuo/
iczApplication.java # 启动类
common/ # 公共模块:注解、常量、枚举、异常、工具类
config/ # 自动装配配置(CORS、Redis、Security、Swagger 等)
core/ # 核心功能:切面(日志、防重提交)、过滤器、Security
module/ # 业务模块
auth/ # 认证模块
codegen/ # 代码生成器
file/ # 文件模块
mail/ # 邮件模块
sms/ # 短信模块
websocket/ # WebSocket 模块
system/ # 系统管理模块:用户、角色、菜单、部门、字典等
controller/
converter/ # MapStruct 转换器
event/ # 事件处理
handler/
listener/
model/ # bo, dto, entity, form, query, vo
mapper/
service/
```
## 技术选型
| 技术 | 版本 | 说明 |
|------|------|------|
| JDK | 17 | |
| Spring Boot | 3.3.2 | 开发框架 |
| Spring Security | 6 | 安全认证 |
| MyBatis-Plus | 3.5.8 | ORM 框架 |
| Knife4j | 4.5.0 | 接口文档 |
| MapStruct | 1.5.5.Final | 对象映射 |
| Druid | 1.2.23 | 数据库连接池 |
| XXL-JOB | 2.4.2 | 定时任务 |
| Redisson | 3.30.0 | 分布式锁 |
## 环境依赖
- **MySQL**:数据库名 `music`,开发配置在 `application-dev.yml`
- **Redis**:端口 16378
- **运行时 Profile**:
- `dev`(默认):本地开发
- `prod`:生产环境
## 快速开始
### 1. 配置数据库
修改 `src/main/resources/application-dev.yml` 中的 MySQL、Redis 连接配置。
### 2. 启动项目
```bash
# 开发模式启动(使用 dev profile)
mvn spring-boot:run -P dev
```
### 3. 验证启动
访问接口文档地址 [http://localhost:8989/doc.html](http://localhost:8989/doc.html) 验证项目启动是否成功。
## 常用命令
```bash
# 开发模式启动
mvn spring-boot:run -P dev
# 打包(默认 dev)
mvn clean package
# 生产环境打包
mvn clean package -P prod
# 运行测试
mvn test
# 跳过测试打包
mvn clean package -DskipTests
```
## RAG 智能问答集成
系统集成了 Python FastAPI RAG 服务(端口 8990),提供 AI 自动问答能力。
### 架构
```
用户提问 (C++ 桌面端)
↓
Spring Boot (8989) — QuestionPollingService 定时轮询 state=0 的新问题
↓
RagClientService → FastAPI (8990/py/rag/*)
↓
LangGraph Pipeline: 嵌入 → 相似度匹配 → 知识库检索 → LLM 生成 → 置信度评分
↓
结果写回 MySQL mc_help_qa 表
```
### 核心组件
| 组件 | 路径 | 职责 |
|------|------|------|
| `RagClientService` | `service/RagClientService.java` | HTTP 客户端,调用 FastAPI `/py/rag/*` 端点(含 FAQ 上传/删除) |
| `QuestionPollingService` | `service/impl/QuestionPollingService.java` | 定时轮询(默认 10s),处理新问题,路由置信度 |
| `RagController` | `controller/RagController.java` | 管理端 API:知识库统计、文件上传/删除(按 category 路由 FAQ/手册) |
| `HelperController` | `controller/HelperController.java` | 问答管理:列表、编辑、入库/移出向量库 |
| `RagConfig` | `config/RagConfig.java` | HTTP 连接池配置(最大 20 连接) |
| `RagProperties` | `config/property/RagProperties.java` | 配置属性:`rag.baseUrl`、超时、批量大小 |
### RAG 端点(Spring Boot → FastAPI)
| Spring Boot 端点 | 转发到 FastAPI | 说明 |
|---|---|---|
| `POST /api/v1/rag/query` | `POST /py/rag/query` | RAG 智能问答 |
| `GET /api/v1/rag/kb/stats` | `GET /py/rag/stats` | 知识库统计 |
| `GET /api/v1/rag/kb/files` | MySQL 直读 | 文件列表(Java 侧管理,含 `fileCategory`) |
| `POST /api/v1/rag/kb/upload` | `POST /py/rag/addKF` 或 `/py/rag/addQAF` | 上传文档,按 `category` 路由:0=手册→addKF,1=FAQ→addQAF |
| `DELETE /api/v1/rag/kb/files/{fileId}` | `DELETE /py/rag/delKF/{fileId}` 或 `/py/rag/delQAF/{fileId}` | 删除文档,按 `fileCategory` 路由 |
### 问答管理端点
| 端点 | 说明 |
|---|---|
| `GET /api/v1/helper/listQnA/{id}` | 按类型获取问答列表 |
| `GET /api/v1/helper/pageQnA` | 分页查询问答 |
| `PUT /api/v1/helper/updateQnA` | 新增/编辑问答 |
| `PUT /api/v1/helper/setQnAByIds/{ids}` | 批量设置状态 |
| `POST /api/v1/helper/updateVectors` | 批量入库/移出向量库 |
### 问题处理流程
1. C++ 桌面端提交问题 → MySQL `mc_help_qa` (state=0)
2. `QuestionPollingService` 每 10s 轮询 → 批量 claim (state=0→1)
3. 重复问题拦截:与历史记录(state=3/4/5)比对,重复 → state=5 跳过
4. 逐条调用 `RagClientService.query()` → FastAPI RAG 流水线
5. 命中问题库(helpId 非空)→ 复制匹配状态与答案(匹配 state=4/6 时从 MySQL 取答案)
6. 未命中:置信度 ≥ 0.9 → 自动通过 (state=4);< 0.9 → 待人工编辑 (state=2)
7. 人工审核 → 编辑/批准/不处理/无效/发布
### 问题状态机
| state | 含义 | 说明 |
|-------|------|------|
| 0 | 未查阅 | C++ 桌面端提交的初始状态 |
| 1 | 已领取 | 轮询服务原子性 claim,处理中 |
| 2 | 待人工编辑 | RAG 置信度 < 0.9,转人工 |
| 3 | 不处理 | 粗口/违规,人工标记 |
| 4 | 已回答 | RAG 置信度 ≥ 0.9 自动通过,或 QV 匹配复制 |
| 5 | 无效 | 重复问题,自动跳过 |
| 6 | 公开 | 已发布,QV 匹配时从 MySQL 取答案 |
### 配置
```yaml
rag:
base-url: http://localhost:8990/py # FastAPI 地址
connect-timeout: 5000 # 连接超时 (ms)
read-timeout: 120000 # 读取超时 (ms)
batch-size: 20 # 批量操作大小
enabled: true # 是否启用 RAG
polling:
fixed-delay: 10000 # 轮询间隔 (ms)
batch-size: 2 # 每轮最多处理条数
```
## 接口文档
- `knife4j` 接口文档:[http://localhost:8989/doc.html](http://localhost:8989/doc.html)
- `swagger` 接口文档:[http://localhost:8989/swagger-ui/index.html](http://localhost:8989/swagger-ui/index.html)