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.

133 lines
3.7 KiB
Go

package integral_manager
import (
"context"
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
"live-service/app/user_center/model"
"live-service/app/user_center/rpc/internal/common/reward_pool"
"live-service/app/user_center/rpc/pb"
"live-service/common/nerr"
)
type (
Manager struct {
cfg Config // 配置
userIntegralModel model.UserIntegralModel
rewardPoolManager *reward_pool.PoolManager
logx.Logger
}
Config struct {
GiftRatio float64
}
ChangeIntegralReq struct {
BattleId int64 // 战局ID
UserId int64 // 用户ID
Change int64 // 变化量(负数为减扣)
Type pb.IntegralType // 积分类型
}
ChangeIntegralResp struct {
BattleId int64 // 战局ID
UserId int64 // 用户ID
Change int64 // 变化量(负数为减扣)
Integral int64 // 剩余积分
}
)
func NewIntegralManager(userIntegralModel model.UserIntegralModel, rewardPoolManager *reward_pool.PoolManager) *Manager {
return &Manager{
userIntegralModel: userIntegralModel,
rewardPoolManager: rewardPoolManager,
Logger: logx.WithContext(context.Background()),
}
}
func (m *Manager) GetUserIntegral(ctx context.Context, userId int64) (int64, error) {
resp, err := m.userIntegralModel.FindOne(ctx, nil, userId)
if err != nil {
return 0, err
}
return resp.Integral, err
}
func (m *Manager) TransferIntegral(ctx context.Context, req *pb.TransferUserIntegralReq) *pb.TransferUserIntegralResp {
resp := &pb.TransferUserIntegralResp{
UserId: req.UserId,
TargetUserId: req.TargetUserId,
}
if err := m.userIntegralModel.TransactCtx(ctx, nil, func(tx *gorm.DB) error {
if req.UserId == 0 {
return nerr.NewError(nerr.RequestParamError, "源用户ID为空")
}
if req.TargetUserId == 0 {
return nerr.NewError(nerr.RequestParamError, "目标用户ID为空")
}
integral, err := m.userIntegralModel.ChangeIntegral(ctx, tx, req.UserId, -req.Transfer)
if err != nil {
return err
}
targetIntegral, err := m.userIntegralModel.ChangeIntegral(ctx, tx, req.TargetUserId, req.Transfer)
if err != nil {
return err
}
resp.Code = int32(nerr.OK)
resp.Msg = "转移成功"
resp.UserIntegral = integral
resp.TargetUserIntegral = targetIntegral
return nil
}); err != nil {
if e, ok := err.(*nerr.Error); ok {
resp.Code = int32(e.Code)
resp.Msg = e.Msg
} else {
resp.Code = int32(nerr.ServerCommonError)
resp.Msg = "转移失败,未知原因"
}
}
return resp
}
func (m *Manager) ChangeIntegral(ctx context.Context, req *ChangeIntegralReq, rewardPool bool) (*ChangeIntegralResp, error) {
integral, err := m.userIntegralModel.ChangeIntegral(ctx, nil, req.UserId, req.Change)
if err != nil {
return nil, err
}
if req.BattleId != 0 && rewardPool {
m.rewardPoolManager.Pool(req.BattleId).Add(req.UserId, req.Change, req.Type)
}
return &ChangeIntegralResp{
UserId: req.UserId,
Change: req.Change,
Integral: integral,
}, nil
}
func (m *Manager) ChangeIntegralBatch(ctx context.Context, req []ChangeIntegralReq, rewardPool bool) (map[int64]ChangeIntegralResp, error) {
resp := make(map[int64]ChangeIntegralResp)
if err := m.userIntegralModel.TransactCtx(ctx, nil, func(tx *gorm.DB) error {
for _, item := range req {
integral, err := m.userIntegralModel.ChangeIntegral(ctx, tx, item.UserId, item.Change)
if err != nil {
return err
}
resp[item.UserId] = ChangeIntegralResp{
BattleId: item.BattleId,
UserId: item.UserId,
Change: item.Change,
Integral: integral,
}
}
return nil
}); err != nil {
return resp, err
}
// 奖池
for _, item := range req {
if item.BattleId != 0 && rewardPool {
m.rewardPoolManager.Pool(item.BattleId).Add(item.UserId, item.Change, item.Type)
}
}
return resp, nil
}