mylomen-server/infrastructure/repository/g_account_repository.go
2024-04-29 19:35:18 +08:00

80 lines
2.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package repository
import (
"context"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"time"
)
// AccountDO 游戏帐号表
type AccountDO struct {
Id uint64 `gorm:"column:id;type:bigint(20) unsigned;primary_key;AUTO_INCREMENT" json:"id"`
Email string `gorm:"column:email;type:varchar(64);comment:帐号;NOT NULL" json:"email"`
Pwd string `gorm:"column:pwd;type:varchar(128);comment:密码;NOT NULL" json:"pwd"`
Name string `gorm:"column:name;type:varchar(64);comment:用户名" json:"name"`
Avatar string `gorm:"column:avatar;type:varchar(128);comment:头像" json:"avatar"`
Gender uint `gorm:"column:gender;type:tinyint(4) unsigned;comment:性别。0:未知 ;1:男;2:女; 3:其他" json:"gender"`
Phone string `gorm:"column:phone;type:varchar(32);comment:手机号码" json:"phone"`
Status uint `gorm:"column:status;type:tinyint(4) unsigned;default:1;comment:是否有效。0:否1:是;NOT NULL" json:"status"`
RegisterSource string `gorm:"column:register_source;type:varchar(16);comment:注册来源" json:"register_source"`
LastIp string `gorm:"column:last_ip;type:varchar(32);comment:最近登录的ip" json:"last_ip"`
Deleted uint `gorm:"column:deleted;type:tinyint(4) unsigned;default:0;comment:逻辑删除。 0:未删除1:已删除;NOT NULL" json:"deleted"`
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"`
}
func (m *AccountDO) TableName() string {
return "g_account"
}
type gUserRepositoryImpl struct {
}
// FindByAccount 根据 account 和 pwd 查询用户
func (gUserRepositoryImpl) FindByAccount(ctx context.Context, account string) *AccountDO {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
var model AccountDO
sqlErr := db.Model(&model).Where("email=?", account).First(&model).Error
if sqlErr != nil {
return nil
}
return &model
}
// Create 创建用户
func (gUserRepositoryImpl) Create(ctx context.Context, data *AccountDO) error {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
data.Deleted = 0
return db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "email"}},
DoUpdates: clause.AssignmentColumns([]string{"deleted"}),
}).Create(data).Error
}
// UpdateById 根据ID更新用户
func (gUserRepositoryImpl) UpdateById(ctx context.Context, data *AccountDO) error {
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
}