Some checks failed
Build docker and publish / linux (1.24.5) (push) Failing after 25s
feat(admin): 新增工会管理功能 feat(activity): 添加活动管理相关服务 feat(user): 实现用户道具卡和积分管理 feat(guild): 新增工会成员管理功能 fix: 修复数据库连接配置 fix: 修正jwtoken导入路径 fix: 解决端口冲突问题 style: 统一代码格式和注释风格 style: 更新项目常量命名 docs: 添加项目框架和开发规范文档 docs: 更新接口文档注释 chore: 移除无用代码和文件 chore: 更新Makefile和配置文件 chore: 清理日志文件 test: 添加道具卡测试脚本
58 lines
2.0 KiB
Go
58 lines
2.0 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
"time"
|
|
|
|
"bindbox-game/internal/repository/mysql/dao"
|
|
"bindbox-game/internal/repository/mysql/model"
|
|
)
|
|
|
|
func (s *service) AddPoints(ctx context.Context, userID int64, points int64, kind string, remark string, validStart *time.Time, validEnd *time.Time) error {
|
|
if points == 0 {
|
|
return nil
|
|
}
|
|
return s.writeDB.Transaction(func(tx *dao.Query) error {
|
|
var existing *model.UserPoints
|
|
if kind != "" {
|
|
existing, _ = tx.UserPoints.WithContext(ctx).Where(tx.UserPoints.UserID.Eq(userID)).Where(tx.UserPoints.Kind.Eq(kind)).First()
|
|
} else {
|
|
existing, _ = tx.UserPoints.WithContext(ctx).Where(tx.UserPoints.UserID.Eq(userID)).First()
|
|
}
|
|
if existing == nil {
|
|
item := &model.UserPoints{UserID: userID, Kind: kind, Points: points}
|
|
if validStart != nil {
|
|
item.ValidStart = *validStart
|
|
}
|
|
if validEnd != nil {
|
|
item.ValidEnd = *validEnd
|
|
}
|
|
do := tx.UserPoints.WithContext(ctx)
|
|
if validEnd == nil || validEnd.IsZero() {
|
|
do = do.Omit(tx.UserPoints.ValidEnd)
|
|
}
|
|
if err := do.Create(item); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
set := map[string]any{"points": existing.Points + points}
|
|
if validStart != nil {
|
|
set["valid_start"] = *validStart
|
|
}
|
|
if validEnd != nil {
|
|
set["valid_end"] = *validEnd
|
|
}
|
|
if _, err := tx.UserPoints.WithContext(ctx).Where(tx.UserPoints.ID.Eq(existing.ID)).Updates(set); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
ledger := &model.UserPointsLedger{UserID: userID, Action: "manual_add", Points: points, RefTable: "user_points", RefID: strconv.FormatInt(userID, 10), Remark: remark}
|
|
if err := tx.UserPointsLedger.WithContext(ctx).Create(ledger); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|