fix: 修复域能管理操作无响应及公户账务错误

- 修复前端 energy.js api.post/get 回调调用为 Promise 链式调用,解决操作无响应
- 修复 AdminAdjust system_operation 模式错误扣减公户余额,现不碰公户
- 修复 AdminAdjust admin_transfer 模式公户按用户数量扣款(amount * len(userIDs))
- 修复 AdminAdjust 余额检查移入事务内并加行锁(SELECT FOR UPDATE),防 TOCTOU 竞态
- FundStore 接口及 FundRepo 新增 LockAndGetBalance 方法
This commit is contained in:
2026-06-01 15:26:25 +08:00
parent 26e557de8a
commit 4815060839
4 changed files with 39 additions and 30 deletions

View File

@ -5,6 +5,7 @@ import (
"metazone.cc/metalab/internal/service"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
// FundRepo 公户数据访问
@ -32,6 +33,16 @@ func (r *FundRepo) GetBalance() (int, error) {
return fund.Balance, nil
}
// LockAndGetBalance 加行锁查询公户余额(事务内使用,防止 TOCTOU 竞态)
func (r *FundRepo) LockAndGetBalance() (int, error) {
var fund model.CommunityFund
err := r.db.Clauses(clause.Locking{Strength: "UPDATE"}).First(&fund, 1).Error
if err != nil {
return 0, err
}
return fund.Balance, nil
}
// IncrBalance 原子增减公户余额
func (r *FundRepo) IncrBalance(amount int) error {
return r.db.Model(&model.CommunityFund{}).