diff --git a/README.md b/README.md index 2146600..f0fb24c 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,9 @@ ```shell goctl api go -api .\game_open_api\app_user.api -dir .\game_open_api\ --style=go_zero - goctl rpc protoc .\dw_server\dw_server.proto --go-grpc_out .\dw_server\ --go_out .\dw_server\ --zrpc_out .\dw_server\ -c -style go_zero +goctl model mysql datasource -c -dir .\game_open_api\model\ --url 'root:youtu!0113@tcp(127.0.0.1:3306)/ecpm' -t game_score -style go_zero + docker exec -i mysql mysqldump -uroot -p'youtu!0113' ecpm > ecpm_backup.sql ``` \ No newline at end of file diff --git a/game_open_api/common.api b/game_open_api/common.api index 600f59a..d5787bf 100644 --- a/game_open_api/common.api +++ b/game_open_api/common.api @@ -9,4 +9,9 @@ type Auth { type Base { Code int `json:"code"` Message string `json:"message,omitempty"` +} + +type PageBase { + Page int `json:"page" form:"page,default=1"` + PageSize int `json:"page_size" form:"page_size,default=20"` } \ No newline at end of file diff --git a/game_open_api/game.api b/game_open_api/game.api index 285b515..c2295c0 100644 --- a/game_open_api/game.api +++ b/game_open_api/game.api @@ -4,6 +4,7 @@ import "common.api" type SetUserGameScoreRequest { Score uint64 `json:"score"` + Type uint64 `json:"type"` } type RankingData { @@ -20,6 +21,11 @@ type RankingResponse { RankingData []RankingData `json:"data"` } +type RankingListRequest { + Type uint64 `json:"type" form:"type"` + Page PageBase `json:"page" form:"page"` +} + @server( group: game @@ -31,7 +37,7 @@ type RankingResponse { service game_open_api-api { @handler rankingList - get /ranking/list returns ([]RankingData) + get /ranking/list (RankingListRequest) returns ([]RankingData) @handler rankingSetScore post /ranking/set_score (SetUserGameScoreRequest) returns (Base) diff --git a/game_open_api/internal/handler/game/ranking_list_handler.go b/game_open_api/internal/handler/game/ranking_list_handler.go deleted file mode 100644 index 44c486f..0000000 --- a/game_open_api/internal/handler/game/ranking_list_handler.go +++ /dev/null @@ -1,21 +0,0 @@ -package game - -import ( - "net/http" - - "gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/logic/game" - "gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/svc" - "github.com/zeromicro/go-zero/rest/httpx" -) - -func RankingListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - l := game.NewRankingListLogic(r.Context(), svcCtx) - resp, err := l.RankingList() - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } - } -} diff --git a/game_open_api/internal/logic/game/ranking_list_logic.go b/game_open_api/internal/logic/game/ranking_list_logic.go index f2b627d..ecc8c9b 100644 --- a/game_open_api/internal/logic/game/ranking_list_logic.go +++ b/game_open_api/internal/logic/game/ranking_list_logic.go @@ -22,14 +22,14 @@ func NewRankingListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ranki } } -func (l *RankingListLogic) RankingList() (resp *types.RankingResponse, err error) { +func (l *RankingListLogic) RankingList(req *types.RankingListRequest) (resp *types.RankingResponse, err error) { resp = new(types.RankingResponse) at, err := svc.GetCtxToken(l.ctx) if err != nil { return nil, err } - resp.RankingData, err = l.svcCtx.GameScore.GetRankList(l.ctx, at.AppId, at.UserId) + resp.RankingData, err = l.svcCtx.GameScore.GetRankList(l.ctx, at.AppId, req.Type, req.Page) if err != nil { return nil, err } @@ -45,7 +45,7 @@ func (l *RankingListLogic) RankingList() (resp *types.RankingResponse, err error } if !flag { - userRank, err := l.svcCtx.GameScore.GetUserRank(l.ctx, at.AppId, at.UserId) + userRank, err := l.svcCtx.GameScore.GetUserRank(l.ctx, at.AppId, at.UserId, req.Type) if err != nil { return nil, err } diff --git a/game_open_api/internal/logic/game/ranking_set_score_logic.go b/game_open_api/internal/logic/game/ranking_set_score_logic.go index 2401864..85d781d 100644 --- a/game_open_api/internal/logic/game/ranking_set_score_logic.go +++ b/game_open_api/internal/logic/game/ranking_set_score_logic.go @@ -33,7 +33,7 @@ func (l *RankingSetScoreLogic) RankingSetScore(req *types.SetUserGameScoreReques return } - oldScore, err := l.svcCtx.GameScore.FindUserScore(l.ctx, at.AppId, at.UserId) + oldScore, err := l.svcCtx.GameScore.FindUserScore(l.ctx, at.AppId, at.UserId, req.Type) if err != nil && !errors.Is(err, sqlc.ErrNotFound) { return } @@ -43,9 +43,10 @@ func (l *RankingSetScoreLogic) RankingSetScore(req *types.SetUserGameScoreReques } if oldScore != nil { - err = l.svcCtx.GameScore.UpdateScore(l.ctx, at.AppId, at.UserId, req.Score) + oldScore.Score = req.Score + err = l.svcCtx.GameScore.UpdateScore(l.ctx, oldScore) } else { - _, err = l.svcCtx.GameScore.Insert(l.ctx, &model.GameScore{ + err = l.svcCtx.GameScore.CreateScore(l.ctx, &model.GameScore{ AppUserId: at.UserId, AppAccount: at.AppId, Score: req.Score, diff --git a/game_open_api/internal/types/types.go b/game_open_api/internal/types/types.go index 2753018..2a58d9b 100644 --- a/game_open_api/internal/types/types.go +++ b/game_open_api/internal/types/types.go @@ -19,6 +19,11 @@ type DouyinCode2TokenRequest struct { AnonymousCode string `form:"anonymousCode,optional"` } +type PageBase struct { + Page int `json:"page" form:"page,default=1"` + PageSize int `json:"page_size" form:"page_size,default=20"` +} + type RankingData struct { Nickname string `json:"nickname" db:"nickname"` // 昵称 Avatar string `json:"avatar" db:"avatar"` // 头像 @@ -28,6 +33,11 @@ type RankingData struct { Self bool `json:"self" db:"-"` // 是否是自己 } +type RankingListRequest struct { + Type uint64 `json:"type" form:"type"` + Page PageBase `json:"page" form:"page"` +} + type RankingResponse struct { Base RankingData []RankingData `json:"data"` @@ -40,6 +50,7 @@ type SetAppUserRequest struct { type SetUserGameScoreRequest struct { Score uint64 `json:"score"` + Type uint64 `json:"type"` } type UserId struct { diff --git a/game_open_api/model/appaccountmodel.go b/game_open_api/model/app_account_model.go similarity index 100% rename from game_open_api/model/appaccountmodel.go rename to game_open_api/model/app_account_model.go diff --git a/game_open_api/model/appaccountmodel_gen.go b/game_open_api/model/app_account_model_gen.go similarity index 100% rename from game_open_api/model/appaccountmodel_gen.go rename to game_open_api/model/app_account_model_gen.go diff --git a/game_open_api/model/appusermodel.go b/game_open_api/model/app_user_model.go similarity index 100% rename from game_open_api/model/appusermodel.go rename to game_open_api/model/app_user_model.go diff --git a/game_open_api/model/appusermodel_gen.go b/game_open_api/model/app_user_model_gen.go similarity index 100% rename from game_open_api/model/appusermodel_gen.go rename to game_open_api/model/app_user_model_gen.go diff --git a/game_open_api/model/douyinecpmconfigmodel.go b/game_open_api/model/douyin_ecpm_config_model.go similarity index 100% rename from game_open_api/model/douyinecpmconfigmodel.go rename to game_open_api/model/douyin_ecpm_config_model.go diff --git a/game_open_api/model/douyinecpmconfigmodel_gen.go b/game_open_api/model/douyin_ecpm_config_model_gen.go similarity index 100% rename from game_open_api/model/douyinecpmconfigmodel_gen.go rename to game_open_api/model/douyin_ecpm_config_model_gen.go diff --git a/game_open_api/model/game_score_model.go b/game_open_api/model/game_score_model.go new file mode 100644 index 0000000..27e7eb6 --- /dev/null +++ b/game_open_api/model/game_score_model.go @@ -0,0 +1,181 @@ +package model + +import ( + "context" + "database/sql" + "errors" + "fmt" + "gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/types" + "github.com/zeromicro/go-zero/core/stores/cache" + "github.com/zeromicro/go-zero/core/stores/sqlc" + "github.com/zeromicro/go-zero/core/stores/sqlx" +) + +var _ GameScoreModel = (*customGameScoreModel)(nil) + +const ( + cacheEcpmGameScorePrefix = "cache:ecpm:gameScore:" +) + +type ( + // GameScoreModel is an interface to be customized, add more methods here, + // and implement the added methods in customGameScoreModel. + GameScoreModel interface { + gameScoreModel + + GetRankList(ctx context.Context, appId uint64, t uint64, page types.PageBase) ([]types.RankingData, error) + GetUserRank(ctx context.Context, appId uint64, userId uint64, t uint64) (types.RankingData, error) + FindUserScore(ctx context.Context, appId, userId, t uint64) (*GameScore, error) + + UpdateScore(ctx context.Context, data *GameScore) error + CreateScore(ctx context.Context, data *GameScore) error + } + + customGameScoreModel struct { + *defaultGameScoreModel + } +) + +// NewGameScoreModel returns a model for the database table. +func NewGameScoreModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) GameScoreModel { + return &customGameScoreModel{ + defaultGameScoreModel: newGameScoreModel(conn, c, opts...), + } +} + +func (m *defaultGameScoreModel) pageCacheKey(page types.PageBase) string { + return fmt.Sprintf(":page:%d:pageSize:%d", page.Page, page.PageSize) +} + +// rankListCacheKey 排行榜缓存key +func (m *defaultGameScoreModel) rankListCacheKey(appId uint64, t uint64) string { + return fmt.Sprintf("%s:rankList:appId:%d:t:%d", cacheEcpmGameScorePrefix, appId, t) +} + +// userScoreCacheKey 用户游戏分数缓存key +func (m *defaultGameScoreModel) userScoreCacheKey(userId, appId, t uint64) string { + return fmt.Sprintf("%suserId:%d:appId:%v:t:%d", cacheEcpmGameScorePrefix, userId, appId, t) +} + +// userRankCacheKey 用户游戏排名缓存key +func (m *defaultGameScoreModel) userRankCacheKey(userId, appId, t uint64) string { + return fmt.Sprintf("%srank:userId:%d:appId:%v:t:%d", cacheEcpmGameScorePrefix, userId, appId, t) +} + +// GetRankList 获取排行榜列表 +func (m *defaultGameScoreModel) GetRankList(ctx context.Context, appId uint64, t uint64, page types.PageBase) (resp []types.RankingData, err error) { + limit, offset := page.PageSize, (page.Page-1)*page.PageSize + cacheKey := m.rankListCacheKey(appId, t) + m.pageCacheKey(page) + err = m.QueryRowCtx(ctx, &resp, cacheKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { + query := ` +SELECT + game_score.score, + app_user.nickname, + app_user.avatar, + game_score.app_user_id, + rank() over (ORDER BY game_score.score DESC) t_rank +FROM + game_score + JOIN app_user ON app_user.id = game_score.app_user_id +WHERE + game_score.app_account = ? + AND game_score.t = ? + LIMIT ? + OFFSET ?` + return conn.QueryRowsPartialCtx(ctx, v, query, appId, t, limit, offset) + }) + switch { + case err == nil: + return resp, nil + case errors.Is(err, sqlc.ErrNotFound): + return nil, ErrNotFound + default: + return nil, err + } +} + +// GetUserRank 获取用户排名 +func (m *defaultGameScoreModel) GetUserRank(ctx context.Context, appId uint64, userId uint64, t uint64) (resp types.RankingData, err error) { + var res []types.RankingData + err = m.QueryRowCtx(ctx, &res, m.userRankCacheKey(userId, appId, t), func(ctx context.Context, conn sqlx.SqlConn, v any) error { + query := ` +SELECT + app_user.nickname, + app_user.avatar, + gs.score, + gs.app_user_id, + gs.t_rank +FROM + ( + SELECT + game_score.score, + game_score.app_user_id, + game_score.app_account, + rank() over (ORDER BY game_score.score DESC) t_rank + FROM + game_score + WHERE + game_score.t = ?) AS gs +LEFT JOIN app_user ON app_user.id = gs.app_user_id + AND gs.app_account = ? +WHERE + gs.app_user_id = ? + LIMIT 1` + return conn.QueryRowsPartialCtx(ctx, v, query, appId, t, userId) + }) + switch { + case err == nil: + if len(res) == 1 { + resp = res[0] + } + return resp, nil + case errors.Is(err, sqlc.ErrNotFound): + return resp, ErrNotFound + default: + return resp, err + } +} + +// FindUserScore 查询用户游戏分数 +func (m *defaultGameScoreModel) FindUserScore(ctx context.Context, appId, userId, t uint64) (*GameScore, error) { + var ( + resp GameScore + err error + ) + err = m.QueryRowCtx(ctx, &resp, m.userScoreCacheKey(userId, appId, t), func(ctx context.Context, conn sqlx.SqlConn, v any) error { + query := fmt.Sprintf("select %s from %s where app_account = ? and `app_user_id` = ? and t = ? limit 1", gameScoreRows, m.table) + return conn.QueryRowCtx(ctx, v, query, userId, appId) + }) + switch err { + case nil: + return &resp, nil + case sqlc.ErrNotFound: + return nil, ErrNotFound + default: + return nil, err + } +} + +// UpdateScore 更新游戏分数记录 +func (m *defaultGameScoreModel) UpdateScore(ctx context.Context, data *GameScore) error { + cacheKeys := []string{ + m.rankListCacheKey(data.AppAccount, data.T), // 删除排行榜缓存 + m.userRankCacheKey(data.AppUserId, data.AppAccount, data.T), // 删除用户排行缓存 + m.userScoreCacheKey(data.AppUserId, data.AppAccount, data.T), // 删除用户分数缓存 + } + + _, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { + query := fmt.Sprintf("update %s set `score` = ? where `id` = ?", m.table) + return conn.ExecCtx(ctx, query, data.Score, data.Id) + }, cacheKeys...) + return err +} + +// CreateScore 创建游戏分数记录 +func (m *defaultGameScoreModel) CreateScore(ctx context.Context, data *GameScore) error { + _, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { + query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?)", m.table, gameScoreRowsExpectAutoSet) + return conn.ExecCtx(ctx, query, data.AppAccount, data.AppUserId, data.Score, data.T) + }, m.rankListCacheKey(data.AppAccount, data.T)) + return err +} diff --git a/game_open_api/model/game_score_model_gen.go b/game_open_api/model/game_score_model_gen.go new file mode 100644 index 0000000..498e6dc --- /dev/null +++ b/game_open_api/model/game_score_model_gen.go @@ -0,0 +1,113 @@ +// Code generated by goctl. DO NOT EDIT. +// versions: +// goctl version: 1.7.5 + +package model + +import ( + "context" + "database/sql" + "fmt" + "strings" + + "github.com/zeromicro/go-zero/core/stores/builder" + "github.com/zeromicro/go-zero/core/stores/cache" + "github.com/zeromicro/go-zero/core/stores/sqlc" + "github.com/zeromicro/go-zero/core/stores/sqlx" + "github.com/zeromicro/go-zero/core/stringx" +) + +var ( + gameScoreFieldNames = builder.RawFieldNames(&GameScore{}) + gameScoreRows = strings.Join(gameScoreFieldNames, ",") + gameScoreRowsExpectAutoSet = strings.Join(stringx.Remove(gameScoreFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",") + gameScoreRowsWithPlaceHolder = strings.Join(stringx.Remove(gameScoreFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?" + + cacheEcpmGameScoreIdPrefix = "cache:ecpm:gameScore:id:" +) + +type ( + gameScoreModel interface { + Insert(ctx context.Context, data *GameScore) (sql.Result, error) + FindOne(ctx context.Context, id uint64) (*GameScore, error) + Update(ctx context.Context, data *GameScore) error + Delete(ctx context.Context, id uint64) error + } + + defaultGameScoreModel struct { + sqlc.CachedConn + table string + } + + GameScore struct { + Id uint64 `db:"id"` + AppAccount uint64 `db:"app_account"` // 小游戏id + AppUserId uint64 `db:"app_user_id"` // 用户id + Score uint64 `db:"score"` // 得分 + T uint64 `db:"t"` // 得分类型(区分相同小游戏中的不同模式得分) + } +) + +func newGameScoreModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultGameScoreModel { + return &defaultGameScoreModel{ + CachedConn: sqlc.NewConn(conn, c, opts...), + table: "`game_score`", + } +} + +func (m *defaultGameScoreModel) Delete(ctx context.Context, id uint64) error { + ecpmGameScoreIdKey := fmt.Sprintf("%s%v", cacheEcpmGameScoreIdPrefix, id) + _, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { + query := fmt.Sprintf("delete from %s where `id` = ?", m.table) + return conn.ExecCtx(ctx, query, id) + }, ecpmGameScoreIdKey) + return err +} + +func (m *defaultGameScoreModel) FindOne(ctx context.Context, id uint64) (*GameScore, error) { + ecpmGameScoreIdKey := fmt.Sprintf("%s%v", cacheEcpmGameScoreIdPrefix, id) + var resp GameScore + err := m.QueryRowCtx(ctx, &resp, ecpmGameScoreIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { + query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", gameScoreRows, m.table) + return conn.QueryRowCtx(ctx, v, query, id) + }) + switch err { + case nil: + return &resp, nil + case sqlc.ErrNotFound: + return nil, ErrNotFound + default: + return nil, err + } +} + +func (m *defaultGameScoreModel) Insert(ctx context.Context, data *GameScore) (sql.Result, error) { + ecpmGameScoreIdKey := fmt.Sprintf("%s%v", cacheEcpmGameScoreIdPrefix, data.Id) + ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { + query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?)", m.table, gameScoreRowsExpectAutoSet) + return conn.ExecCtx(ctx, query, data.AppAccount, data.AppUserId, data.Score, data.T) + }, ecpmGameScoreIdKey) + return ret, err +} + +func (m *defaultGameScoreModel) Update(ctx context.Context, data *GameScore) error { + ecpmGameScoreIdKey := fmt.Sprintf("%s%v", cacheEcpmGameScoreIdPrefix, data.Id) + _, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { + query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, gameScoreRowsWithPlaceHolder) + return conn.ExecCtx(ctx, query, data.AppAccount, data.AppUserId, data.Score, data.T, data.Id) + }, ecpmGameScoreIdKey) + return err +} + +func (m *defaultGameScoreModel) formatPrimary(primary any) string { + return fmt.Sprintf("%s%v", cacheEcpmGameScoreIdPrefix, primary) +} + +func (m *defaultGameScoreModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error { + query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", gameScoreRows, m.table) + return conn.QueryRowCtx(ctx, v, query, primary) +} + +func (m *defaultGameScoreModel) tableName() string { + return m.table +} diff --git a/game_open_api/model/gamescoremodel.go b/game_open_api/model/gamescoremodel.go deleted file mode 100644 index 3476eef..0000000 --- a/game_open_api/model/gamescoremodel.go +++ /dev/null @@ -1,127 +0,0 @@ -package model - -import ( - "context" - "database/sql" - "errors" - "fmt" - "gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/types" - "github.com/zeromicro/go-zero/core/stores/cache" - "github.com/zeromicro/go-zero/core/stores/sqlc" - "github.com/zeromicro/go-zero/core/stores/sqlx" -) - -var _ GameScoreModel = (*customGameScoreModel)(nil) - -const ( - cacheEcpmRankListPrefix = "cache:ecpm:game:rankList:appId:" - cacheEcpmUserRankPrefix = "cache:ecpm:game:userRank:userId:" - cacheEcpmGameScoreAppUserIdAppAccountPrefix = "cache:ecpm:gameScore:" -) - -type ( - // GameScoreModel is an interface to be customized, add more methods here, - // and implement the added methods in customGameScoreModel. - GameScoreModel interface { - gameScoreModel - GetRankList(ctx context.Context, appId uint64, userId uint64) ([]types.RankingData, error) - GetUserRank(ctx context.Context, appId uint64, userId uint64) (types.RankingData, error) - FindUserScore(ctx context.Context, appId uint64, userId uint64) (*GameScore, error) - UpdateScore(ctx context.Context, appId uint64, userId uint64, score uint64) error - } - - customGameScoreModel struct { - *defaultGameScoreModel - } -) - -// NewGameScoreModel returns a model for the database table. -func NewGameScoreModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) GameScoreModel { - return &customGameScoreModel{ - defaultGameScoreModel: newGameScoreModel(conn, c, opts...), - } -} - -func (m *defaultGameScoreModel) GetRankList(ctx context.Context, appId uint64, userId uint64) (resp []types.RankingData, err error) { - ecpmGameScoreAppUserIdKey := fmt.Sprintf("%s%v", cacheEcpmRankListPrefix, appId) - err = m.QueryRowCtx(ctx, &resp, ecpmGameScoreAppUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { - query := "SELECT game_score.score,app_user.nickname,app_user.avatar,game_score.app_user_id,rank() over(ORDER BY game_score.score desc) t_rank FROM `game_score` JOIN app_user ON app_user.id=game_score.app_user_id WHERE game_score.app_account=? ORDER BY game_score.score DESC LIMIT 20" - return conn.QueryRowsPartialCtx(ctx, v, query, appId) - }) - switch { - case err == nil: - return resp, nil - case errors.Is(err, sqlc.ErrNotFound): - return nil, ErrNotFound - default: - return nil, err - } -} - -func (m *defaultGameScoreModel) GetUserRank(ctx context.Context, appId uint64, userId uint64) (resp types.RankingData, err error) { - ecpmGameScoreAppUserIdKey := fmt.Sprintf("%s%v", cacheEcpmUserRankPrefix, userId) - var res []types.RankingData - err = m.QueryRowCtx(ctx, &res, ecpmGameScoreAppUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { - query := `SELECT - app_user.nickname, - app_user.avatar, - gs.score, - gs.app_user_id, - gs.t_rank -FROM - ( - SELECT - game_score.score, - game_score.app_user_id, - game_score.app_account, - rank() over (ORDER BY game_score.score DESC) t_rank - FROM - game_score) AS gs - LEFT JOIN app_user ON app_user.id = gs.app_user_id - AND gs.app_account = ? -WHERE - gs.app_user_id = ? - LIMIT 1 ` - return conn.QueryRowsPartialCtx(ctx, v, query, appId, userId) - }) - switch { - case err == nil: - if len(res) == 1 { - resp = res[0] - } - return resp, nil - case errors.Is(err, sqlc.ErrNotFound): - return resp, ErrNotFound - default: - return resp, err - } -} - -func (m *defaultGameScoreModel) FindUserScore(ctx context.Context, appId uint64, userId uint64) (*GameScore, error) { - ecpmGameScoreAppUserIdKey := fmt.Sprintf("%sappId:%d:userId:%d", cacheEcpmGameScoreAppUserIdAppAccountPrefix, appId, userId) - var ( - resp GameScore - err error - ) - err = m.QueryRowCtx(ctx, &resp, ecpmGameScoreAppUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { - query := fmt.Sprintf("select %s from %s where `app_user_id` = ? and app_account = ? limit 1", gameScoreRows, m.table) - return conn.QueryRowCtx(ctx, v, query, userId, appId) - }) - switch err { - case nil: - return &resp, nil - case sqlc.ErrNotFound: - return nil, ErrNotFound - default: - return nil, err - } -} - -func (m *defaultGameScoreModel) UpdateScore(ctx context.Context, appId uint64, userId uint64, score uint64) error { - ecpmGameScoreAppUserIdAppAccountKey := fmt.Sprintf("%sappId:%d:userId:%d", cacheEcpmGameScoreAppUserIdAppAccountPrefix, appId, userId) - _, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { - query := fmt.Sprintf("update %s set `score` = ? where `app_account` = ? and `app_user_id` = ?", m.table) - return conn.ExecCtx(ctx, query, score, appId, userId) - }, ecpmGameScoreAppUserIdAppAccountKey) - return err -} diff --git a/game_open_api/model/gamescoremodel_gen.go b/game_open_api/model/gamescoremodel_gen.go deleted file mode 100644 index 5daa11d..0000000 --- a/game_open_api/model/gamescoremodel_gen.go +++ /dev/null @@ -1,145 +0,0 @@ -// Code generated by goctl. DO NOT EDIT. -// versions: -// goctl version: 1.7.5 - -package model - -import ( - "context" - "database/sql" - "fmt" - "strings" - - "github.com/zeromicro/go-zero/core/stores/builder" - "github.com/zeromicro/go-zero/core/stores/cache" - "github.com/zeromicro/go-zero/core/stores/sqlc" - "github.com/zeromicro/go-zero/core/stores/sqlx" - "github.com/zeromicro/go-zero/core/stringx" -) - -var ( - gameScoreFieldNames = builder.RawFieldNames(&GameScore{}) - gameScoreRows = strings.Join(gameScoreFieldNames, ",") - gameScoreRowsExpectAutoSet = strings.Join(stringx.Remove(gameScoreFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",") - gameScoreRowsWithPlaceHolder = strings.Join(stringx.Remove(gameScoreFieldNames, "`app_user_id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?" - - cacheEcpmGameScoreAppUserIdPrefix = "cache:ecpm:gameScore:appUserId:" -) - -type ( - gameScoreModel interface { - Insert(ctx context.Context, data *GameScore) (sql.Result, error) - FindOne(ctx context.Context, appUserId uint64) (*GameScore, error) - FindOneByAppUserIdAppAccount(ctx context.Context, appUserId uint64, appAccount uint64) (*GameScore, error) - Update(ctx context.Context, data *GameScore) error - Delete(ctx context.Context, appUserId uint64) error - } - - defaultGameScoreModel struct { - sqlc.CachedConn - table string - } - - GameScore struct { - AppUserId uint64 `db:"app_user_id"` - AppAccount uint64 `db:"app_account"` - Score uint64 `db:"score"` - } -) - -func newGameScoreModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultGameScoreModel { - return &defaultGameScoreModel{ - CachedConn: sqlc.NewConn(conn, c, opts...), - table: "`game_score`", - } -} - -func (m *defaultGameScoreModel) Delete(ctx context.Context, appUserId uint64) error { - data, err := m.FindOne(ctx, appUserId) - if err != nil { - return err - } - - ecpmGameScoreAppUserIdAppAccountKey := fmt.Sprintf("%s%v:%v", cacheEcpmGameScoreAppUserIdAppAccountPrefix, data.AppUserId, data.AppAccount) - ecpmGameScoreAppUserIdKey := fmt.Sprintf("%s%v", cacheEcpmGameScoreAppUserIdPrefix, appUserId) - _, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { - query := fmt.Sprintf("delete from %s where `app_user_id` = ?", m.table) - return conn.ExecCtx(ctx, query, appUserId) - }, ecpmGameScoreAppUserIdAppAccountKey, ecpmGameScoreAppUserIdKey) - return err -} - -func (m *defaultGameScoreModel) FindOne(ctx context.Context, appUserId uint64) (*GameScore, error) { - ecpmGameScoreAppUserIdKey := fmt.Sprintf("%s%v", cacheEcpmGameScoreAppUserIdPrefix, appUserId) - var resp GameScore - err := m.QueryRowCtx(ctx, &resp, ecpmGameScoreAppUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { - query := fmt.Sprintf("select %s from %s where `app_user_id` = ? limit 1", gameScoreRows, m.table) - return conn.QueryRowCtx(ctx, v, query, appUserId) - }) - switch err { - case nil: - return &resp, nil - case sqlc.ErrNotFound: - return nil, ErrNotFound - default: - return nil, err - } -} - -func (m *defaultGameScoreModel) FindOneByAppUserIdAppAccount(ctx context.Context, appUserId uint64, appAccount uint64) (*GameScore, error) { - ecpmGameScoreAppUserIdAppAccountKey := fmt.Sprintf("%s%v:%v", cacheEcpmGameScoreAppUserIdAppAccountPrefix, appUserId, appAccount) - var resp GameScore - err := m.QueryRowIndexCtx(ctx, &resp, ecpmGameScoreAppUserIdAppAccountKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) { - query := fmt.Sprintf("select %s from %s where `app_user_id` = ? and `app_account` = ? limit 1", gameScoreRows, m.table) - if err := conn.QueryRowCtx(ctx, &resp, query, appUserId, appAccount); err != nil { - return nil, err - } - return resp.AppUserId, nil - }, m.queryPrimary) - switch err { - case nil: - return &resp, nil - case sqlc.ErrNotFound: - return nil, ErrNotFound - default: - return nil, err - } -} - -func (m *defaultGameScoreModel) Insert(ctx context.Context, data *GameScore) (sql.Result, error) { - ecpmGameScoreAppUserIdAppAccountKey := fmt.Sprintf("%s%v:%v", cacheEcpmGameScoreAppUserIdAppAccountPrefix, data.AppUserId, data.AppAccount) - ecpmGameScoreAppUserIdKey := fmt.Sprintf("%s%v", cacheEcpmGameScoreAppUserIdPrefix, data.AppUserId) - ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { - query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?)", m.table, gameScoreRowsExpectAutoSet) - return conn.ExecCtx(ctx, query, data.AppUserId, data.AppAccount, data.Score) - }, ecpmGameScoreAppUserIdAppAccountKey, ecpmGameScoreAppUserIdKey) - return ret, err -} - -func (m *defaultGameScoreModel) Update(ctx context.Context, newData *GameScore) error { - data, err := m.FindOne(ctx, newData.AppUserId) - if err != nil { - return err - } - - ecpmGameScoreAppUserIdAppAccountKey := fmt.Sprintf("%s%v:%v", cacheEcpmGameScoreAppUserIdAppAccountPrefix, data.AppUserId, data.AppAccount) - ecpmGameScoreAppUserIdKey := fmt.Sprintf("%s%v", cacheEcpmGameScoreAppUserIdPrefix, data.AppUserId) - _, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { - query := fmt.Sprintf("update %s set %s where `app_user_id` = ?", m.table, gameScoreRowsWithPlaceHolder) - return conn.ExecCtx(ctx, query, newData.AppAccount, newData.Score, newData.AppUserId) - }, ecpmGameScoreAppUserIdAppAccountKey, ecpmGameScoreAppUserIdKey) - return err -} - -func (m *defaultGameScoreModel) formatPrimary(primary any) string { - return fmt.Sprintf("%s%v", cacheEcpmGameScoreAppUserIdPrefix, primary) -} - -func (m *defaultGameScoreModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error { - query := fmt.Sprintf("select %s from %s where `app_user_id` = ? limit 1", gameScoreRows, m.table) - return conn.QueryRowCtx(ctx, v, query, primary) -} - -func (m *defaultGameScoreModel) tableName() string { - return m.table -}