2025-01-20 01:55:44 +08:00
package model
import (
2025-01-20 14:37:14 +08:00
"context"
2025-01-20 17:36:03 +08:00
"database/sql"
2025-01-20 14:37:14 +08:00
"errors"
"fmt"
2025-01-21 10:38:27 +08:00
"gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/types"
2025-01-20 01:55:44 +08:00
"github.com/zeromicro/go-zero/core/stores/cache"
2025-01-20 14:37:14 +08:00
"github.com/zeromicro/go-zero/core/stores/sqlc"
2025-01-20 01:55:44 +08:00
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var _ GameScoreModel = ( * customGameScoreModel ) ( nil )
2025-01-20 14:37:14 +08:00
const (
2025-01-20 17:36:03 +08:00
cacheEcpmRankListPrefix = "cache:ecpm:game:rankList:appId:"
cacheEcpmUserRankPrefix = "cache:ecpm:game:userRank:userId:"
cacheEcpmGameScoreAppUserIdAppAccountPrefix = "cache:ecpm:gameScore:"
2025-01-20 14:37:14 +08:00
)
2025-01-20 01:55:44 +08:00
type (
// GameScoreModel is an interface to be customized, add more methods here,
// and implement the added methods in customGameScoreModel.
GameScoreModel interface {
gameScoreModel
2025-01-20 14:37:14 +08:00
GetRankList ( ctx context . Context , appId uint64 , userId uint64 ) ( [ ] types . RankingData , error )
2025-01-21 10:02:04 +08:00
GetUserRank ( ctx context . Context , appId uint64 , userId uint64 ) ( types . RankingData , error )
2025-01-20 17:36:03 +08:00
FindUserScore ( ctx context . Context , appId uint64 , userId uint64 ) ( * GameScore , error )
UpdateScore ( ctx context . Context , appId uint64 , userId uint64 , score uint64 ) error
2025-01-20 01:55:44 +08:00
}
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 ... ) ,
}
}
2025-01-20 14:37:14 +08:00
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
}
}
2025-01-21 10:02:04 +08:00
func ( m * defaultGameScoreModel ) GetUserRank ( ctx context . Context , appId uint64 , userId uint64 ) ( resp types . RankingData , err error ) {
2025-01-20 14:37:14 +08:00
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 {
2025-01-21 10:02:04 +08:00
resp = res [ 0 ]
2025-01-20 14:37:14 +08:00
}
return resp , nil
case errors . Is ( err , sqlc . ErrNotFound ) :
2025-01-21 10:02:04 +08:00
return resp , ErrNotFound
2025-01-20 14:37:14 +08:00
default :
2025-01-21 10:02:04 +08:00
return resp , err
2025-01-20 14:37:14 +08:00
}
}
2025-01-20 17:36:03 +08:00
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
}