feat: 实现社区激励金(公户)系统,经济闭环
- 新增 CommunityFund / FundLog 模型(公户余额单行表 + 6种流水类型) - 新增 FundRepo 仓储(GetBalance / IncrBalance / CreateLog / ListLogs / WithTx) - 改造 EnergyService 五个方法:Energize 税收入公户、DeductOnRename/DeductOnDeletePost 入公户、RefundOnRenameReject 从公户支出、AdminAdjust 拆分为 admin_transfer/system_operation - 新增 ErrInsufficientFund 错误哨兵 - 管理后台新增公户余额展示 + 公户流水页面 + 域能调整资金来源选择 - 新增 energy.css / energy.js 前端交互 - 侧边栏新增公户流水入口
This commit is contained in:
@ -19,6 +19,7 @@ type expAdder interface {
|
||||
type EnergyService struct {
|
||||
db *gorm.DB
|
||||
repo EnergyStore
|
||||
fundRepo FundStore
|
||||
expSvc expAdder
|
||||
notifSvc energyNotifier
|
||||
}
|
||||
@ -28,6 +29,16 @@ func NewEnergyService(db *gorm.DB, repo EnergyStore, expSvc expAdder, notifSvc e
|
||||
return &EnergyService{db: db, repo: repo, expSvc: expSvc, notifSvc: notifSvc}
|
||||
}
|
||||
|
||||
// SetExpAdder 链式注入经验服务(用于赋能获得经验)
|
||||
func (s *EnergyService) SetExpAdder(svc expAdder) {
|
||||
s.expSvc = svc
|
||||
}
|
||||
|
||||
// SetFundRepo 链式注入公户仓储
|
||||
func (s *EnergyService) SetFundRepo(repo FundStore) {
|
||||
s.fundRepo = repo
|
||||
}
|
||||
|
||||
// EnergyInfo 域能信息(含每日赋能经验状态)
|
||||
type EnergyInfo struct {
|
||||
Energy int // 域能余额(乘10存储)
|
||||
@ -36,11 +47,6 @@ type EnergyInfo struct {
|
||||
DailyExpCapReached bool // 是否已达上限
|
||||
}
|
||||
|
||||
// SetExpAdder 链式注入经验服务(用于赋能获得经验)
|
||||
func (s *EnergyService) SetExpAdder(svc expAdder) {
|
||||
s.expSvc = svc
|
||||
}
|
||||
|
||||
// 域能相关常量(乘10存储,DB值)
|
||||
const (
|
||||
EnergyCheckIn = 10 // +1.0 域能
|
||||
@ -69,6 +75,10 @@ func (s *EnergyService) Energize(energizerID uint, postID uint, amount int) (int
|
||||
|
||||
err := s.db.Transaction(func(tx *gorm.DB) error {
|
||||
txRepo := s.repo.WithTx(tx)
|
||||
var txFundRepo FundStore
|
||||
if s.fundRepo != nil {
|
||||
txFundRepo = s.fundRepo.WithTx(tx)
|
||||
}
|
||||
|
||||
// 1. 检查帖子存在,获取作者
|
||||
post, err := txRepo.FindPostByID(postID)
|
||||
@ -156,7 +166,28 @@ func (s *EnergyService) Energize(energizerID uint, postID uint, amount int) (int
|
||||
return fmt.Errorf("创建被赋能者流水失败: %w", err)
|
||||
}
|
||||
|
||||
// 10. 计算经验值(溢出截断)—— 在事务内读取以保证一致性
|
||||
// 10. 赋能税收入公户(差价 = amount - receivedAmount)
|
||||
if txFundRepo != nil {
|
||||
tax := amount - receivedAmount
|
||||
if tax > 0 {
|
||||
if err := txFundRepo.IncrBalance(tax); err != nil {
|
||||
return fmt.Errorf("公户入账失败: %w", err)
|
||||
}
|
||||
taxDisplay := float64(tax) / 10
|
||||
fundLog := &model.FundLog{
|
||||
Amount: tax,
|
||||
Type: model.FundTypeEnergizeTax,
|
||||
RelatedType: "post",
|
||||
RelatedID: postID,
|
||||
Description: fmt.Sprintf("赋能文章 #%d 税收 %.1f 域能,赋能者 uid=%d", postID, taxDisplay, energizerID),
|
||||
}
|
||||
if err := txFundRepo.CreateLog(fundLog); err != nil {
|
||||
return fmt.Errorf("创建公户流水失败: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 11. 计算经验值(溢出截断)—— 在事务内读取以保证一致性
|
||||
loc, _ := time.LoadLocation("Asia/Shanghai")
|
||||
now := time.Now().In(loc)
|
||||
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, loc)
|
||||
@ -188,9 +219,7 @@ func (s *EnergyService) Energize(energizerID uint, postID uint, amount int) (int
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// 11. 事务提交后,在事务外增加经验 + 更新每日汇总
|
||||
// 必须在事务外执行:LevelService.AddExp 也操作 users 表,
|
||||
// 若在事务内调用会造成连接池不同连接间行锁死锁。
|
||||
// 12. 事务提交后,在事务外增加经验 + 更新每日汇总
|
||||
if needExpUpdate {
|
||||
if s.expSvc != nil {
|
||||
if _, _, err := s.expSvc.AddExp(energizerID, expGained); err != nil {
|
||||
@ -226,46 +255,91 @@ func (s *EnergyService) CheckInEnergy(userID uint) error {
|
||||
}
|
||||
|
||||
// DeductOnRename 改名扣域能(首次改名免费,不调用此方法)
|
||||
// 事务包裹:扣用户域能 + 入公户 + 写流水
|
||||
func (s *EnergyService) DeductOnRename(userID uint) error {
|
||||
// 检查域能余额(非 owner 需 ≥ 0)
|
||||
user, err := s.repo.FindUserByID(userID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("查询用户失败: %w", err)
|
||||
}
|
||||
if user.Role != model.RoleOwner && user.Energy+EnergyRename < 0 {
|
||||
return common.ErrInsufficientEnergy
|
||||
}
|
||||
return s.db.Transaction(func(tx *gorm.DB) error {
|
||||
txRepo := s.repo.WithTx(tx)
|
||||
|
||||
if err := s.repo.AddEnergy(userID, EnergyRename); err != nil {
|
||||
return fmt.Errorf("扣减改名域能失败: %w", err)
|
||||
}
|
||||
// 检查域能余额(非 owner 需 ≥ 0)
|
||||
user, err := txRepo.FindUserByID(userID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("查询用户失败: %w", err)
|
||||
}
|
||||
if user.Role != model.RoleOwner && user.Energy+EnergyRename < 0 {
|
||||
return common.ErrInsufficientEnergy
|
||||
}
|
||||
|
||||
log := &model.EnergyLog{
|
||||
UserID: userID,
|
||||
Amount: EnergyRename,
|
||||
Type: model.EnergyTypeRename,
|
||||
Description: "改名消耗 6.0 域能",
|
||||
}
|
||||
return s.repo.CreateEnergyLog(log)
|
||||
if err := txRepo.AddEnergy(userID, EnergyRename); err != nil {
|
||||
return fmt.Errorf("扣减改名域能失败: %w", err)
|
||||
}
|
||||
|
||||
// 公户入账
|
||||
if s.fundRepo != nil {
|
||||
txFundRepo := s.fundRepo.WithTx(tx)
|
||||
if err := txFundRepo.IncrBalance(-EnergyRename); err != nil { // EnergyRename = -60,入公户 +60
|
||||
return fmt.Errorf("公户入账失败: %w", err)
|
||||
}
|
||||
fundLog := &model.FundLog{
|
||||
Amount: -EnergyRename,
|
||||
Type: model.FundTypeRenameDeduction,
|
||||
RelatedType: "user",
|
||||
RelatedID: userID,
|
||||
Description: fmt.Sprintf("用户 uid=%d 改名消耗 6.0 域能", userID),
|
||||
}
|
||||
if err := txFundRepo.CreateLog(fundLog); err != nil {
|
||||
return fmt.Errorf("创建公户流水失败: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
log := &model.EnergyLog{
|
||||
UserID: userID,
|
||||
Amount: EnergyRename,
|
||||
Type: model.EnergyTypeRename,
|
||||
Description: "改名消耗 6.0 域能",
|
||||
}
|
||||
return txRepo.CreateEnergyLog(log)
|
||||
})
|
||||
}
|
||||
|
||||
// RefundOnRenameReject 改名审核被拒,返还域能
|
||||
// RefundOnRenameReject 改名审核被拒,从公户返还域能
|
||||
func (s *EnergyService) RefundOnRenameReject(userID uint) error {
|
||||
if err := s.repo.AddEnergy(userID, EnergyRenameRefund); err != nil {
|
||||
return fmt.Errorf("返还改名域能失败: %w", err)
|
||||
}
|
||||
return s.db.Transaction(func(tx *gorm.DB) error {
|
||||
txRepo := s.repo.WithTx(tx)
|
||||
|
||||
log := &model.EnergyLog{
|
||||
UserID: userID,
|
||||
Amount: EnergyRenameRefund,
|
||||
Type: model.EnergyTypeRenameRefund,
|
||||
Description: "改名审核被拒,返还 6.0 域能",
|
||||
}
|
||||
return s.repo.CreateEnergyLog(log)
|
||||
// 从公户支出(允许透支)
|
||||
if s.fundRepo != nil {
|
||||
txFundRepo := s.fundRepo.WithTx(tx)
|
||||
if err := txFundRepo.IncrBalance(-EnergyRenameRefund); err != nil {
|
||||
return fmt.Errorf("公户出账失败: %w", err)
|
||||
}
|
||||
fundLog := &model.FundLog{
|
||||
Amount: -EnergyRenameRefund,
|
||||
Type: model.FundTypeRenameRefund,
|
||||
RelatedType: "user",
|
||||
RelatedID: userID,
|
||||
Description: fmt.Sprintf("用户 uid=%d 改名审核被拒,返还 6.0 域能", userID),
|
||||
}
|
||||
if err := txFundRepo.CreateLog(fundLog); err != nil {
|
||||
return fmt.Errorf("创建公户流水失败: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := txRepo.AddEnergy(userID, EnergyRenameRefund); err != nil {
|
||||
return fmt.Errorf("返还改名域能失败: %w", err)
|
||||
}
|
||||
|
||||
log := &model.EnergyLog{
|
||||
UserID: userID,
|
||||
Amount: EnergyRenameRefund,
|
||||
Type: model.EnergyTypeRenameRefund,
|
||||
Description: "改名审核被拒,返还 6.0 域能",
|
||||
}
|
||||
return txRepo.CreateEnergyLog(log)
|
||||
})
|
||||
}
|
||||
|
||||
// DeductOnDeletePost 删稿扣域能(扣文章作者的域能,无视负数)
|
||||
// 事务包裹:确保扣域能 + 写流水原子性
|
||||
// 事务包裹:扣用户域能 + 入公户 + 写流水
|
||||
func (s *EnergyService) DeductOnDeletePost(authorUserID uint, postID uint) error {
|
||||
return s.db.Transaction(func(tx *gorm.DB) error {
|
||||
txRepo := s.repo.WithTx(tx)
|
||||
@ -274,6 +348,24 @@ func (s *EnergyService) DeductOnDeletePost(authorUserID uint, postID uint) error
|
||||
return fmt.Errorf("扣减删稿域能失败: %w", err)
|
||||
}
|
||||
|
||||
// 公户入账
|
||||
if s.fundRepo != nil {
|
||||
txFundRepo := s.fundRepo.WithTx(tx)
|
||||
if err := txFundRepo.IncrBalance(-EnergyDeletePost); err != nil { // EnergyDeletePost = -20,入公户 +20
|
||||
return fmt.Errorf("公户入账失败: %w", err)
|
||||
}
|
||||
fundLog := &model.FundLog{
|
||||
Amount: -EnergyDeletePost,
|
||||
Type: model.FundTypeDeletePost,
|
||||
RelatedType: "post",
|
||||
RelatedID: postID,
|
||||
Description: fmt.Sprintf("用户 uid=%d 删除文章 #%d 消耗 2.0 域能", authorUserID, postID),
|
||||
}
|
||||
if err := txFundRepo.CreateLog(fundLog); err != nil {
|
||||
return fmt.Errorf("创建公户流水失败: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
log := &model.EnergyLog{
|
||||
UserID: authorUserID,
|
||||
Amount: EnergyDeletePost,
|
||||
@ -287,25 +379,87 @@ func (s *EnergyService) DeductOnDeletePost(authorUserID uint, postID uint) error
|
||||
}
|
||||
|
||||
// AdminAdjust 后台调整用户域能
|
||||
func (s *EnergyService) AdminAdjust(operatorUID uint, userIDs []uint, amount int, description string) error {
|
||||
for _, uid := range userIDs {
|
||||
if err := s.repo.AddEnergy(uid, amount); err != nil {
|
||||
return fmt.Errorf("调整用户 %d 域能失败: %w", uid, err)
|
||||
}
|
||||
// mode: "admin_transfer"(受公户余额约束)| "system_operation"(不受约束)
|
||||
func (s *EnergyService) AdminAdjust(operatorUID uint, userIDs []uint, amount int, description string, mode string) error {
|
||||
if mode == "" {
|
||||
mode = model.FundTypeAdminTransfer
|
||||
}
|
||||
|
||||
opUID := operatorUID
|
||||
log := &model.EnergyLog{
|
||||
UserID: uid,
|
||||
Amount: amount,
|
||||
Type: model.EnergyTypeAdminAdjust,
|
||||
Description: description,
|
||||
OperatorUID: &opUID,
|
||||
// admin_transfer 出账时检查公户余额
|
||||
if mode == model.FundTypeAdminTransfer && amount > 0 && s.fundRepo != nil {
|
||||
balance, err := s.fundRepo.GetBalance()
|
||||
if err != nil {
|
||||
return fmt.Errorf("查询公户余额失败: %w", err)
|
||||
}
|
||||
if err := s.repo.CreateEnergyLog(log); err != nil {
|
||||
return fmt.Errorf("创建调整流水失败: %w", err)
|
||||
if balance < amount {
|
||||
return common.ErrInsufficientFund
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
return s.db.Transaction(func(tx *gorm.DB) error {
|
||||
txRepo := s.repo.WithTx(tx)
|
||||
var txFundRepo FundStore
|
||||
if s.fundRepo != nil {
|
||||
txFundRepo = s.fundRepo.WithTx(tx)
|
||||
}
|
||||
|
||||
// 公户余额调整(给用户 energy=+amount → 公户 -amount,扣用户 energy=-amount → 公户 +amount)
|
||||
if txFundRepo != nil {
|
||||
if err := txFundRepo.IncrBalance(-amount); err != nil {
|
||||
return fmt.Errorf("公户余额调整失败: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, uid := range userIDs {
|
||||
if err := txRepo.AddEnergy(uid, amount); err != nil {
|
||||
return fmt.Errorf("调整用户 %d 域能失败: %w", uid, err)
|
||||
}
|
||||
|
||||
opUID := operatorUID
|
||||
energyLog := &model.EnergyLog{
|
||||
UserID: uid,
|
||||
Amount: amount,
|
||||
Type: model.EnergyTypeAdminAdjust,
|
||||
Description: description,
|
||||
OperatorUID: &opUID,
|
||||
}
|
||||
if err := txRepo.CreateEnergyLog(energyLog); err != nil {
|
||||
return fmt.Errorf("创建调整流水失败: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 公户流水
|
||||
if txFundRepo != nil {
|
||||
amountDisplay := float64(amount) / 10
|
||||
fundLog := &model.FundLog{
|
||||
Amount: -amount,
|
||||
Type: mode,
|
||||
OperatorUID: &operatorUID,
|
||||
Description: fmt.Sprintf("%s:调整用户域能 %.1f,说明:%s", getFundModeName(mode), amountDisplay, description),
|
||||
}
|
||||
if len(userIDs) > 0 {
|
||||
fundLog.RelatedType = "user"
|
||||
fundLog.RelatedID = userIDs[0]
|
||||
}
|
||||
if err := txFundRepo.CreateLog(fundLog); err != nil {
|
||||
return fmt.Errorf("创建公户流水失败: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// getFundModeName 返回操作模式中文名
|
||||
func getFundModeName(mode string) string {
|
||||
switch mode {
|
||||
case model.FundTypeAdminTransfer:
|
||||
return "管理转出"
|
||||
case model.FundTypeSystemOperation:
|
||||
return "系统操作"
|
||||
default:
|
||||
return "后台调整"
|
||||
}
|
||||
}
|
||||
|
||||
// GetEnergyInfo 获取用户域能余额和每日赋能经验状态
|
||||
@ -359,3 +513,28 @@ func (s *EnergyService) GetAdminEnergyLogs(energyType string, page, pageSize int
|
||||
p.DefaultPagination()
|
||||
return s.repo.ListAllEnergyLogs(energyType, p.Offset(), p.PageSize)
|
||||
}
|
||||
|
||||
// GetFundBalance 获取公户余额
|
||||
func (s *EnergyService) GetFundBalance() (int, error) {
|
||||
if s.fundRepo == nil {
|
||||
return 0, nil
|
||||
}
|
||||
return s.fundRepo.GetBalance()
|
||||
}
|
||||
|
||||
// GetFundLogs 分页查询公户流水
|
||||
func (s *EnergyService) GetFundLogs(logType string, page, pageSize int) ([]model.FundLog, int64, error) {
|
||||
p := common.Pagination{Page: page, PageSize: pageSize}
|
||||
p.DefaultPagination()
|
||||
if s.fundRepo == nil {
|
||||
return []model.FundLog{}, 0, nil
|
||||
}
|
||||
logs, total, err := s.fundRepo.ListLogs(logType, p.Offset(), p.PageSize)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if logs == nil {
|
||||
logs = []model.FundLog{}
|
||||
}
|
||||
return logs, total, nil
|
||||
}
|
||||
|
||||
@ -111,6 +111,15 @@ type EnergyStore interface {
|
||||
// energyStore 向后兼容别名
|
||||
type energyStore = EnergyStore
|
||||
|
||||
// FundStore 公户仓储接口(ISP)
|
||||
type FundStore interface {
|
||||
GetBalance() (int, error)
|
||||
IncrBalance(amount int) error
|
||||
CreateLog(log *model.FundLog) error
|
||||
ListLogs(logType string, offset, limit int) ([]model.FundLog, int64, error)
|
||||
WithTx(tx *gorm.DB) FundStore
|
||||
}
|
||||
|
||||
// energyNotifier EnergyService 所需的通知接口(ISP)
|
||||
type energyNotifier interface {
|
||||
Create(userID uint, notifyType, title, content string, relatedID, commentID *uint) error
|
||||
|
||||
Reference in New Issue
Block a user