fix: Service 层移除直接持有的 *gorm.DB,引入 Repository Transaction 方法

- ReactionStore/FollowStore/EnergyStore/FavoriteStore 接口新增 Transaction 方法
- EnergyStore.Transaction 支持跨仓库事务(EnergyStore + FundStore)
- ReactionRepo/FollowRepo/FavoriteRepo/EnergyRepo 实现 Transaction 方法
- ReactionRepo 新增 GetPostLikesCount 方法,消除 Service 直查 DB
- ReactionService/FollowService/FavoriteService/EnergyService 移除 db 字段
- EnergyRepo 通过 SetFundStore 注入 FundStore 用于跨仓库事务
- 更新 deps_extra.go 构造函数调用
This commit is contained in:
2026-06-02 16:46:44 +08:00
parent 1cb7a832f1
commit 3df7b2e672
14 changed files with 94 additions and 92 deletions

View File

@ -1,9 +1,5 @@
package service
import (
"gorm.io/gorm"
)
// expAdder EnergyService 对经验增加的最小依赖ISP
type expAdder interface {
AddExp(userID uint, delta int) (newExp int, levelUp bool, err error)
@ -11,16 +7,15 @@ type expAdder interface {
// EnergyService 域能业务逻辑
type EnergyService struct {
db *gorm.DB
repo EnergyStore
fundRepo FundStore
fundRepo FundStore // 只读查询用GetFundBalance/GetFundLogs
expSvc expAdder
notifSvc energyNotifier
}
// NewEnergyService 构造函数
func NewEnergyService(db *gorm.DB, repo EnergyStore, expSvc expAdder, notifSvc energyNotifier) *EnergyService {
return &EnergyService{db: db, repo: repo, expSvc: expSvc, notifSvc: notifSvc}
func NewEnergyService(repo EnergyStore, expSvc expAdder, notifSvc energyNotifier) *EnergyService {
return &EnergyService{repo: repo, expSvc: expSvc, notifSvc: notifSvc}
}
// SetExpAdder 链式注入经验服务(用于赋能获得经验)
@ -28,7 +23,7 @@ func (s *EnergyService) SetExpAdder(svc expAdder) {
s.expSvc = svc
}
// SetFundRepo 链式注入公户仓储
// SetFundRepo 链式注入公户仓储(只读查询)
func (s *EnergyService) SetFundRepo(repo FundStore) {
s.fundRepo = repo
}