package model import ( "context" "fmt" "github.com/go-redis/redis/v8" "samba/stub" "samba/util/rdbkey" "strconv" ) type UserMstatOp struct { rdb *redis.Client } func NewUserMstatOp() *UserMstatOp { return &UserMstatOp{rdb: rdbBaseInfo} } // GetUserGameCount 获取用户某个子游戏次数 func (op *UserMstatOp) GetUserGameCount(uid int64, name string) (int64, error) { hKeys := make([]string, 0) ids := stub.FindPlayTypeCnfByGame(name) if len(ids) <= 0 { return 0, fmt.Errorf("game %s not found", name) } for _, id := range ids { hKeys = append(hKeys, fmt.Sprintf("gwtimes_%d", id)) hKeys = append(hKeys, fmt.Sprintf("gltimes_%d", id)) } res, err := op.rdb.HMGet(context.TODO(), rdbkey.UserMStatKey(uid), hKeys...).Result() if err != nil { return 0, err } var sum int64 for _, re := range res { val, ok := re.(string) if !ok { continue } valI64, err := strconv.ParseInt(val, 10, 64) if err != nil { return 0, err } sum += valI64 } return sum, nil }