You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
3.4 KiB
Go

/**
* @Author: jager
* @Email: lhj168os@gmail.com
* @File: user
* @Date: 2021/12/31 2:52 下午
* @package: user
* @Version: v1.0.0
*
* @Description:
*
*/
package user
import (
"fmt"
"github.com/jageros/hawox/encrypt"
"github.com/jageros/hawox/errcode"
"github.com/jageros/hawox/redis"
"github.com/tal-tech/go-zero/core/stores/cache"
"github.com/tal-tech/go-zero/core/stores/sqlx"
"idata/internal/service/user/dao"
"idata/internal/types"
"sync"
)
var (
userModel dao.UsersModel
userPool sync.Pool
)
func init() {
userPool = sync.Pool{
New: func() interface{} {
return user{}
},
}
}
func InitDao(conn sqlx.SqlConn, c cache.CacheConf) {
userModel = dao.NewUsersModel(conn, c)
}
type user struct {
data *dao.Users
}
func newUser(uid string) (*user, error) {
uData, err := userModel.FindOneByUid(uid)
if err == nil {
return nil, errcode.New(5000, "uid exist")
}
if err != dao.ErrNotFound {
return nil, err
}
uData = &dao.Users{
Uid: uid,
Phone: uid,
Openid: uid,
Secret: uid,
Name: fmt.Sprintf("U_%s", uid),
}
usr := userPool.Get().(user)
usr.data = uData
return &usr, nil
}
func getRUser(uid string) (user, error) {
usr := userPool.Get().(user)
uData, err := userModel.FindOneByUid(uid)
usr.data = uData
return usr, err
}
func getRWUser(uid string) (*user, error) {
uData, err := userModel.FindOneByUid(uid)
if err != nil {
return nil, err
}
usr := userPool.Get().(user)
usr.data = uData
return &usr, nil
}
func OperateSync(uid string, f func(types.RWUser)) error {
var err error
err2 := redis.LockExec(uid, func(key string) {
var u *user
u, err = getRWUser(uid)
if err == nil {
f(u)
err = userModel.Update(*u.data)
}
userPool.Put(*u)
})
if err2 != nil {
return err2
}
return err
}
func Operate(uid string, f func(u types.RUser)) error {
u, err := getRUser(uid)
if err == nil {
f(u)
}
userPool.Put(u)
return err
}
func RegistryUser(uid string, f func(u types.RWUser)) error {
var err error
err2 := redis.LockExec(uid, func(key string) {
var u *user
u, err = newUser(uid)
if err == nil {
f(u)
_, err = userModel.Insert(*u.data)
}
userPool.Put(*u)
})
if err2 != nil {
return err2
}
return err
}
// ====== Types.RUser 实现 ======
func (u user) Uid() string {
return u.data.Uid
}
func (u user) Openid() string {
if u.data.Openid == u.data.Uid {
return ""
}
return u.data.Openid
}
func (u user) Phone() string {
if u.data.Phone == u.data.Uid {
return ""
}
return u.data.Phone
}
func (u user) Secret() string {
return u.data.Secret
}
func (u user) Name() string {
return u.data.Name
}
func (u user) Gender() int64 {
return u.data.Gender
}
func (u user) Score() int64 {
return u.data.Score
}
func (u user) Avatar() string {
return u.data.Avatar
}
// ========== end ==========
// ====== Types.RWUser 实现 ======
func (u *user) SetOpenid(openid string) {
u.data.Openid = openid
}
func (u *user) SetPhone(phone string) {
u.data.Phone = phone
}
func (u *user) SetSecret(secret string) {
u.data.Secret = secret
}
func (u *user) SetName(name string) {
u.data.Name = name
}
func (u *user) SetAvatar(avatar string) {
u.data.Avatar = avatar
}
func (u *user) SetGender(gender int64) {
u.data.Gender = gender
}
func (u *user) SetScore(score int64) {
u.data.Score = score
}
func (u *user) SetPassword(pwd string) {
u.data.Password = encrypt.Md5(pwd)
}
// ========== end ==========