mylomen-server/infrastructure/repository/user_base_repository.go

258 lines
7.2 KiB
Go
Raw Normal View History

2024-04-29 19:35:18 +08:00
package repository
import (
"context"
"gorm.io/gorm"
"gorm.io/gorm/clause"
2024-09-29 21:12:57 +08:00
"mylomen_server/common/dto"
2024-04-29 19:35:18 +08:00
"time"
)
2024-09-29 20:14:50 +08:00
// UserDO 游戏帐号表
type UserDO struct {
2024-09-29 21:12:57 +08:00
Id uint64 `gorm:"column:id;type:bigint(20) unsigned;primary_key;AUTO_INCREMENT" json:"id"`
Sn string `gorm:"column:sn;type:varchar(64);comment:sn;NOT NULL" json:"sn"`
Account *string `gorm:"column:account;type:varchar(64);comment:账号;NOT NULL" json:"account"`
Pwd *string `gorm:"column:pwd;type:varchar(128);comment:密码;NOT NULL" json:"pwd"`
NickName *string `gorm:"column:nickname;type:varchar(64);nickname:昵称" json:"nickname"`
Avatar *string `gorm:"column:avatar;type:varchar(128);comment:头像" json:"avatar"`
Gender uint `gorm:"column:gender;type:smallint(4) unsigned;default:0;comment:性别。0:未知 ;1:男;2:女; 3:其他" json:"gender"`
Phone *string `gorm:"column:phone;type:varchar(16);comment:手机号码" json:"phone"`
2024-09-30 15:41:00 +08:00
WxId *string `gorm:"column:wx_id;type:varchar(16);comment:微信unionId" json:"wx_id"`
QqId *string `gorm:"column:qq_id;type:varchar(16);comment:qqId" json:"qq_id"`
2024-09-29 21:12:57 +08:00
Email *string `gorm:"column:email;type:varchar(64);comment:email" json:"email"`
GoogleId *string `gorm:"column:google_id;type:varchar(32);comment:GoogleId" json:"google_id"`
FacebookId *string `gorm:"column:facebook_id;type:varchar(32);comment:FacebookId" json:"facebook_id"`
2024-09-29 20:14:50 +08:00
VipType uint `gorm:"column:vip_type;type:smallint(4) unsigned;default:0;comment:会员类型0:非会员1:普通会员2:永久会员;NOT NULL" json:"vip_type"`
VipExpired *uint64 `gorm:"column:vip_expired;type:smallint(4) unsigned;default:0;comment:会员过期时间;NOT NULL" json:"vip_expired"`
2024-10-02 00:43:45 +08:00
RegisterSource *string `gorm:"column:register_source;type:varchar(16);comment:注册来源" json:"register_source"`
LatestIp *string `gorm:"column:latest_ip;type:varchar(32);comment:最近登录的ip" json:"latest_ip"`
LatestLoginTime uint64 `gorm:"column:latest_login_time;type:varchar(32);comment:最近登录的时间戳" json:"latest_login_time"`
2024-09-29 20:14:50 +08:00
2024-10-02 00:43:45 +08:00
Status uint `gorm:"column:status;type:smallint(4) unsigned;default:0;comment:状态。0:正常1:冻结;NOT NULL" json:"status"`
2024-09-30 15:41:00 +08:00
Deleted bool `gorm:"column:deleted;type:smallint(4) unsigned;default:0;comment:逻辑删除。 0:未删除1:已删除;NOT NULL" json:"deleted"`
2024-10-02 00:43:45 +08:00
Remark *string `gorm:"column:remark;type:varchar(64);comment:备注" json:"remark"`
2024-09-29 20:14:50 +08:00
CreateTime time.Time `gorm:"column:create_time;type:timestamp;default:CURRENT_TIMESTAMP;comment:创建时间;NOT NULL" json:"create_time"`
UpdateTime time.Time `gorm:"column:update_time;type:timestamp;default:CURRENT_TIMESTAMP;comment:更新时间;NOT NULL" json:"update_time"`
2024-04-29 19:35:18 +08:00
}
2024-09-29 20:14:50 +08:00
func (m *UserDO) TableName() string {
2024-09-30 15:41:00 +08:00
return "user_base"
2024-04-29 19:35:18 +08:00
}
2024-09-30 22:49:21 +08:00
type userRepositoryImpl struct {
2024-04-29 19:35:18 +08:00
}
2024-09-30 22:49:21 +08:00
func (rp *userRepositoryImpl) FindByReq(ctx context.Context, req dto.LoginReq) *UserDO {
2024-09-29 21:12:57 +08:00
if req.Account != nil {
return rp.FindByAccount(ctx, *req.Account)
}
if req.Email != nil {
return rp.FindByEmail(ctx, *req.Email)
}
if req.Phone != nil {
return rp.FindByPhone(ctx, *req.Phone)
}
if req.WxId != nil {
return rp.FindByWxId(ctx, *req.WxId)
}
if req.QqId != nil {
return rp.FindByQqId(ctx, *req.QqId)
}
if req.GoogleId != nil {
return rp.FindByGoogleId(ctx, *req.GoogleId)
}
if req.FacebookId != nil {
return rp.FindByFacebookId(ctx, *req.FacebookId)
}
return nil
}
2024-09-30 22:49:21 +08:00
func (rp *userRepositoryImpl) FindById(ctx context.Context, id int64) *UserDO {
2024-09-29 20:14:50 +08:00
//验证参数
if id <= 0 {
return nil
}
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
var model UserDO
sqlErr := db.Model(&model).Where("id=?", id).First(&model).Error
if sqlErr != nil {
return nil
}
return &model
}
2024-09-30 22:49:21 +08:00
func (rp *userRepositoryImpl) FindBySn(ctx context.Context, sn string) *UserDO {
//验证参数
if sn == "" || len(sn) > 16 {
return nil
}
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
var model UserDO
sqlErr := db.Model(&model).Where("sn=?", sn).First(&model).Error
if sqlErr != nil {
return nil
}
return &model
}
func (rp *userRepositoryImpl) FindByAccount(ctx context.Context, account string) *UserDO {
2024-05-15 17:29:08 +08:00
//验证参数
2024-09-29 21:12:57 +08:00
if account == "" || len(account) > 32 {
2024-09-29 20:14:50 +08:00
return nil
}
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
var model UserDO
sqlErr := db.Model(&model).Where("account=?", account).First(&model).Error
if sqlErr != nil {
return nil
}
return &model
}
2024-05-15 17:29:08 +08:00
2024-09-30 22:49:21 +08:00
func (rp *userRepositoryImpl) FindByEmail(ctx context.Context, email string) *UserDO {
2024-09-29 20:14:50 +08:00
//验证参数
2024-09-29 21:12:57 +08:00
if email == "" || len(email) > 64 {
2024-09-29 20:14:50 +08:00
return nil
}
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
var model UserDO
sqlErr := db.Model(&model).Where("email=?", email).First(&model).Error
if sqlErr != nil {
return nil
2024-05-15 17:29:08 +08:00
}
2024-09-29 20:14:50 +08:00
return &model
}
2024-05-15 17:29:08 +08:00
2024-09-30 22:49:21 +08:00
func (rp *userRepositoryImpl) FindByPhone(ctx context.Context, phone string) *UserDO {
2024-09-29 20:14:50 +08:00
//验证参数
2024-09-29 21:12:57 +08:00
if phone == "" || len(phone) > 16 {
2024-09-29 20:14:50 +08:00
return nil
}
2024-04-29 19:35:18 +08:00
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
2024-09-29 20:14:50 +08:00
var model UserDO
sqlErr := db.Model(&model).Where("phone=?", phone).First(&model).Error
if sqlErr != nil {
return nil
}
return &model
}
2024-04-29 19:35:18 +08:00
2024-09-30 22:49:21 +08:00
func (rp *userRepositoryImpl) FindByWxId(ctx context.Context, wxId string) *UserDO {
2024-09-29 20:14:50 +08:00
//验证参数
2024-09-29 21:12:57 +08:00
if wxId == "" || len(wxId) > 16 {
2024-09-29 20:14:50 +08:00
return nil
}
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
var model UserDO
2024-09-30 15:41:00 +08:00
sqlErr := db.Model(&model).Where("wx_id=?", wxId).First(&model).Error
2024-09-29 20:14:50 +08:00
if sqlErr != nil {
return nil
}
return &model
}
2024-04-29 19:35:18 +08:00
2024-09-30 22:49:21 +08:00
func (rp *userRepositoryImpl) FindByQqId(ctx context.Context, qqId string) *UserDO {
2024-09-29 20:14:50 +08:00
//验证参数
2024-09-29 21:12:57 +08:00
if qqId == "" || len(qqId) > 32 {
2024-09-29 20:14:50 +08:00
return nil
}
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
var model UserDO
2024-09-30 15:41:00 +08:00
sqlErr := db.Model(&model).Where("qq_id=?", qqId).First(&model).Error
2024-04-29 19:35:18 +08:00
if sqlErr != nil {
return nil
}
2024-09-29 20:14:50 +08:00
return &model
}
2024-04-29 19:35:18 +08:00
2024-09-30 22:49:21 +08:00
func (rp *userRepositoryImpl) FindByGoogleId(ctx context.Context, googleId string) *UserDO {
2024-09-29 20:14:50 +08:00
//验证参数
2024-09-29 21:12:57 +08:00
if googleId == "" || len(googleId) > 32 {
2024-09-29 20:14:50 +08:00
return nil
}
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
var model UserDO
2024-09-30 15:41:00 +08:00
sqlErr := db.Model(&model).Where("google_id=?", googleId).First(&model).Error
2024-09-29 20:14:50 +08:00
if sqlErr != nil {
return nil
}
return &model
}
2024-09-30 22:49:21 +08:00
func (rp *userRepositoryImpl) FindByFacebookId(ctx context.Context, facebookId string) *UserDO {
2024-09-29 20:14:50 +08:00
//验证参数
2024-09-29 21:12:57 +08:00
if facebookId == "" || len(facebookId) > 32 {
2024-09-29 20:14:50 +08:00
return nil
}
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
var model UserDO
2024-09-30 15:41:00 +08:00
sqlErr := db.Model(&model).Where("facebook_id=?", facebookId).First(&model).Error
2024-09-29 20:14:50 +08:00
if sqlErr != nil {
return nil
}
2024-04-29 19:35:18 +08:00
return &model
}
// Create 创建用户
2024-09-30 22:49:21 +08:00
func (rp *userRepositoryImpl) Create(ctx context.Context, data *UserDO) error {
2024-04-29 19:35:18 +08:00
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
2024-09-30 15:41:00 +08:00
data.Deleted = false
2024-04-29 19:35:18 +08:00
return db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "email"}},
DoUpdates: clause.AssignmentColumns([]string{"deleted"}),
}).Create(data).Error
}
// UpdateById 根据ID更新用户
2024-09-30 22:49:21 +08:00
func (rp *userRepositoryImpl) UpdateById(ctx context.Context, data *UserDO) error {
2024-04-29 19:35:18 +08:00
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
sqlErr := db.Model(data).Where("id=?", data.Id).Updates(data).Error
if sqlErr != nil {
return sqlErr
}
return nil
}