游戏分数表添加t(类型)字段
This commit is contained in:
parent
f703b14b5e
commit
f6536f6ad7
@ -3,8 +3,9 @@
|
|||||||
```shell
|
```shell
|
||||||
goctl api go -api .\game_open_api\app_user.api -dir .\game_open_api\ --style=go_zero
|
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 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
|
docker exec -i mysql mysqldump -uroot -p'youtu!0113' ecpm > ecpm_backup.sql
|
||||||
```
|
```
|
@ -9,4 +9,9 @@ type Auth {
|
|||||||
type Base {
|
type Base {
|
||||||
Code int `json:"code"`
|
Code int `json:"code"`
|
||||||
Message string `json:"message,omitempty"`
|
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"`
|
||||||
}
|
}
|
@ -4,6 +4,7 @@ import "common.api"
|
|||||||
|
|
||||||
type SetUserGameScoreRequest {
|
type SetUserGameScoreRequest {
|
||||||
Score uint64 `json:"score"`
|
Score uint64 `json:"score"`
|
||||||
|
Type uint64 `json:"type"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type RankingData {
|
type RankingData {
|
||||||
@ -20,6 +21,11 @@ type RankingResponse {
|
|||||||
RankingData []RankingData `json:"data"`
|
RankingData []RankingData `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type RankingListRequest {
|
||||||
|
Type uint64 `json:"type" form:"type"`
|
||||||
|
Page PageBase `json:"page" form:"page"`
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@server(
|
@server(
|
||||||
group: game
|
group: game
|
||||||
@ -31,7 +37,7 @@ type RankingResponse {
|
|||||||
|
|
||||||
service game_open_api-api {
|
service game_open_api-api {
|
||||||
@handler rankingList
|
@handler rankingList
|
||||||
get /ranking/list returns ([]RankingData)
|
get /ranking/list (RankingListRequest) returns ([]RankingData)
|
||||||
|
|
||||||
@handler rankingSetScore
|
@handler rankingSetScore
|
||||||
post /ranking/set_score (SetUserGameScoreRequest) returns (Base)
|
post /ranking/set_score (SetUserGameScoreRequest) returns (Base)
|
||||||
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -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)
|
resp = new(types.RankingResponse)
|
||||||
at, err := svc.GetCtxToken(l.ctx)
|
at, err := svc.GetCtxToken(l.ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -45,7 +45,7 @@ func (l *RankingListLogic) RankingList() (resp *types.RankingResponse, err error
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !flag {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ func (l *RankingSetScoreLogic) RankingSetScore(req *types.SetUserGameScoreReques
|
|||||||
return
|
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) {
|
if err != nil && !errors.Is(err, sqlc.ErrNotFound) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -43,9 +43,10 @@ func (l *RankingSetScoreLogic) RankingSetScore(req *types.SetUserGameScoreReques
|
|||||||
}
|
}
|
||||||
|
|
||||||
if oldScore != nil {
|
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 {
|
} else {
|
||||||
_, err = l.svcCtx.GameScore.Insert(l.ctx, &model.GameScore{
|
err = l.svcCtx.GameScore.CreateScore(l.ctx, &model.GameScore{
|
||||||
AppUserId: at.UserId,
|
AppUserId: at.UserId,
|
||||||
AppAccount: at.AppId,
|
AppAccount: at.AppId,
|
||||||
Score: req.Score,
|
Score: req.Score,
|
||||||
|
@ -19,6 +19,11 @@ type DouyinCode2TokenRequest struct {
|
|||||||
AnonymousCode string `form:"anonymousCode,optional"`
|
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 {
|
type RankingData struct {
|
||||||
Nickname string `json:"nickname" db:"nickname"` // 昵称
|
Nickname string `json:"nickname" db:"nickname"` // 昵称
|
||||||
Avatar string `json:"avatar" db:"avatar"` // 头像
|
Avatar string `json:"avatar" db:"avatar"` // 头像
|
||||||
@ -28,6 +33,11 @@ type RankingData struct {
|
|||||||
Self bool `json:"self" db:"-"` // 是否是自己
|
Self bool `json:"self" db:"-"` // 是否是自己
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type RankingListRequest struct {
|
||||||
|
Type uint64 `json:"type" form:"type"`
|
||||||
|
Page PageBase `json:"page" form:"page"`
|
||||||
|
}
|
||||||
|
|
||||||
type RankingResponse struct {
|
type RankingResponse struct {
|
||||||
Base
|
Base
|
||||||
RankingData []RankingData `json:"data"`
|
RankingData []RankingData `json:"data"`
|
||||||
@ -40,6 +50,7 @@ type SetAppUserRequest struct {
|
|||||||
|
|
||||||
type SetUserGameScoreRequest struct {
|
type SetUserGameScoreRequest struct {
|
||||||
Score uint64 `json:"score"`
|
Score uint64 `json:"score"`
|
||||||
|
Type uint64 `json:"type"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserId struct {
|
type UserId struct {
|
||||||
|
181
game_open_api/model/game_score_model.go
Normal file
181
game_open_api/model/game_score_model.go
Normal file
@ -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
|
||||||
|
}
|
113
game_open_api/model/game_score_model_gen.go
Normal file
113
game_open_api/model/game_score_model_gen.go
Normal file
@ -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
|
||||||
|
}
|
@ -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
|
|
||||||
}
|
|
@ -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
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user