You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
digital-rmb-backend/.workbuddy/memory/MEMORY.md

104 lines
8.6 KiB
Markdown

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# digital-rmb-backend 项目长期约定
## 技术栈
Spring Boot 2.7.18 + Java 8;MyBatis-Plus 3.5.17;Spring Security + JWT;
统一响应 `ApiResponse<T>`(code/message/data/traceId/timestamp);异常用
`BusinessException(ErrorCode, message)`;当前用户用 `CurrentUserService.getCurrentUser()`
(schoolId 来自 sys_user),institutionidentity 模块另有 `AuthContextHolder`。
## 建库脚本(高频踩坑)
- 新增表必须**同时**改 `src/main/resources/schema.sql` 和 `src/test/resources/schema.sql`。
测试 classpath 优先加载 test 那份,只改 main 会导致 H2 报 `Table ... not found`。
- 存量库升级脚本放 `src/main/resources/db/manual/`。
- `application-dev.yml` 是 `spring.sql.init.mode: never`,schema.sql 不会自动执行到运行库。
- `stu_module_score_details` 唯一约束 `uk_user_moudule_serial (user_id, moudule, serial_number)`,
同模块多条数据 serial_number 必须递增。
## 构建与测试
- **不要用 `mvn clean`**:本机因文件占用删除 `target/classes/com/yau/digitalrmb/progress/infrastructure` 失败。
改用 PowerShell `Remove-Item -Recurse -Force target\classes` 与 `target\test-classes`。
- `target` 增量编译有历史残留,会出现主编译"成功"但缺包;先删 classes/test-classes 再编译。
- 沙箱会拦截 WMIC.exe,mvn 结束后 shell 退出码非 0 —— **以日志里的 BUILD SUCCESS 为准**。
- 跑单测:`mvn -o test -Dtest=XXXTest -DfailIfNoSpecifiedTests=false`。
## 六实验模块编号
1 中国人民银行生成数字货币 / 2 中国人民银行发行数字货币 / 3 开通数字货币钱包 /
4 用户兑换数字货币 / 5 用户支付数字货币 / 6 数字人民币创新应用场景仿真。
注意:成绩表 `stu_module_score_details.moudule` 里模块二写作"发行数字货币"、
模块四写作"数字货币兑换",与设置页标准名不同 —— 映射见
`com.yau.digitalrmb.training.score.application.TrainingModules`。
## 成绩与权重
- 学生端汇总:`GET /api/v1/training-scores`(参数 moudule 可空)。
**模块权重与扣分来自 `training_module_weight`**:查不到本校配置时由 `TrainingWeightService.moduleWeight`
落一条默认记录(含默认扣分 0.50)后返回,因此学生查成绩也会写库。返回体新增 `deductScore`
(数值,来自权重表),`scoringCriteria` 文本也由该值拼出。
- 教师端权重:`/api/v1/training-weights` 下 GET/POST `/school`、GET/POST `/modules`。
- 默认值集中在 `TrainingModules`:模块权重 0.30/0.25/0.10/0.10/0.10/0.15,
总成绩 0.80/0.20,扣分 0.50(**截图未能读取,待用户核对**)。
- 表结构(用户明确要求,勿改回纵向):
- `training_school_score_weight`:一校一行,只有 training_weight / report_weight,**无索引**。
- `training_module_weight`:一校一行,6 个模块权重**横向**存
`module1_weight`…`module6_weight`,`deduct_score` 也在这张表;不要 moudule / moudule_code 列,不要唯一索引。
- 接口语义(用户明确要求):**查询接口首次查不到数据时直接落库一条默认记录**并返回 id;
**修改接口按 id 更新**(请求体必传 id),不是按 schoolId upsert。
`id` 不存在 → 404;记录不属于当前学校 → 403;权重和不为 1 → 400。
- 返回 `{id, deductScore, modules:[...], configured}`;`configured=false` 表示这条默认记录是本次查询刚生成的。
- **性能红线(防止回归)**:教师端总成绩/详情/导出**严禁逐学生调用 `overall`/`summary`**(N+1:page=10 触发 200+ 次查询)。
已改为按分页学生整体批量——模块最高分/进度各一条 GROUP BY、报告一条 IN、权重整请求只查一次。
批量口径:`TeacherModuleScoreMapper.selectMaxScoresByUserIds`(最高分含已删除轮次)/
`selectLatestScheduleByUserIds`(进度取最新有效记录);学生端自身 `summary` 因含操作次数/明细/最值,仍按单用户调用。
- MySQL 8 新建表 CREATE TABLE 必须带 `DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci`(对齐 sys_user 老表);漏 COLLATE 会按服务器默认建成 0900_ai_ci,与老表 join 时报 Illegal mix of collations。修复样例见 db/manual/experiment_report_upgrade.sql 末尾的 ALTER。
## 协作与排查约定(踩坑总结)
- 用户说"写到文档/接口文档"时,需求是**改 md 文档**,不要顺手写实现代码;先确认"只要文档还是也要代码"。
反之"帮我把 X 接口实现/改一下"才是写代码。
- 测试失败先别怀疑编译残留:先看目标 controller 的 handler 是否还在(方法是否被注释掉),
再查历史基准日志(项目根 ts.log / teacher-detail-test.log / test_r.log 记录了各测试类的通过情况),
最后才考虑删 target\classes、target\test-classes 重编译。
- 学生端成绩详情 /details 与教师端 teacher/details 的综合分数必须同口径:
Σ(模块最高分 × 模块权重),缺失模块计 0,保留 2 位 HALF_UP;最高分含已删除轮次。
## 单点登录(SSO)· 智云是中枢,digital-rmb 与 cross_training_platform 是平级子系统
- **拓扑**:智云(ZhiYun)是 SSO 中枢并签发 token;`digital-rmb` 与 `cross_training_platform`
都是它的平级子系统,互相独立、互不跳转。前端点的"智云单点登录"跳转直接带智云 token 到
`digital-rmb` 的 `/api/user/login`,**不经过 cross_training_platform**,后者不是"主系统"。
- 登录入口 `UserController` 的 `POST /api/user/login`(`@ConditionalOnProperty platform-integration.enabled=true`)。
参数 `TOKEN` 走 `tokenLogin` → `PlatformTokenVerifier.verify`;账号密码走 `LocalAccountAuthenticationService`。
- **秘钥已和智云对齐(写死常量)**:`PlatformTokenVerifier` 内常量 `ZHIYUN_SECRET = "zy_kzy_mnjy_fp76ckwuczzmb67w0a8x0"`
(HS256),即 cross_training_platform 的 `TokenProvider.SECRET_ZHIYUN`,与智云签发密钥一致。
2026-10-09 起已**移除 `platform-integration.token.link-secret-key` 配置项**(dev/test/prod/test-classpath 四处
yml 的 `token:` 块、`PlatformIntegrationProperties.Token` 类全部删掉),verifier 直接写死该常量,
不再读配置/env,避免 test profile 误用 `local-token-sso-test-secret-key-123456` 导致 401。
- 解析逻辑照抄 `getJWTUserByZhiYun`:HS256 验签 → 提取 userId/username/name/password(默认123qwe)/
roleid(默认4)/schoolId(默认0)/school→schoolName/class→className/studentNo→studentId/
college→collegeName/major→majorName;解析失败直接抛 `平台登录凭据无效`(无 subsystem 兜底)。
- 学生端登录 `UserController.tokenLogin` → `PlatformTokenVerifier.verify` → `LocalSsoAccountService.synchronize`
(查/建 UserEntity,差异仅数据表)→ `JwtTokenService.issueFor`,与 cross_training_platform 的
`UserController.login` 分支2 同构。
- **平台用户信息不再用快照表**(2026-10-10 起):`platform_user_snapshot` 表已删除,学校/学院/专业/班级
统一存在 `sys_user` 主表(`college_id/college_name/major_id/major_name` 等列)。
登录 `LocalSsoAccountService.synchronize` 直接写 `sys_user`;`UserController.success` 与
`CurrentUserService.getCurrentUser` 从 `sys_user` 读学院/专业。存量库升级脚本 `db/manual/sys_user_profile_upgrade.sql`。
- **两系统差异(待用户拍板)**:cross_training_platform 验完智云 token 后还远程调
`TzApi.foreignExchangeTradingLogin` 回智云再核用户;digital-rmb 的 `PlatformTokenVerifier` **只验签名+过期、不远程核身**。
- 排查 SSO 秘钥问题时,直接看上述三处 yml + `PlatformTokenVerifier`(验签 HS256)+ `cross_training_platform/.../TokenProvider.SECRET_ZHIYUN`,
别绕去 `PlatformSsoController`(那是另一条 platformintegration 链路)。
# 模块六 · 对公钱包ID格式(2026-10-31 起)
- 对公钱包ID = `CORP_` + 步骤一 SM3 摘要值(64 位 hex),统一由
`com.yau.digitalrmb.corporatewallet.domain.CorporateWalletId.normalize()` 生成/归一化;
临柜 `CorporateWalletService.outputWallet` 与远程 `RemoteWalletService.outputWallet` 两个输出接口都走它。
- 历史数据可能只有纯 hex:读取一律 `normalize`,比对一律 `CorporateWalletId.matches()`(忽略前缀+大小写),
不要再用裸 `equalsIgnoreCase` 比钱包ID。
- 捐赠 `POST /api/v1/donation/digest`:付款钱包 = 本企业对公钱包(临柜优先,回退远程),精确比对;
收款钱包**只判 `CORP_` 前缀且不等于付款钱包**,不查库、不校验受赠机构。