80 lines
2.6 KiB
Go
80 lines
2.6 KiB
Go
package repo
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/samber/do"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func init() {
|
|
do.Provide[UserRepo](nil, NewUserRepoS)
|
|
}
|
|
|
|
// 用户表
|
|
type User struct {
|
|
ID uint `gorm:"column:id;primary_key;AUTO_INCREMENT"` // 用户id
|
|
Account string `gorm:"column:account;NOT NULL"` // 用户账号
|
|
Name string `gorm:"column:name"` // 姓名
|
|
Mobile string `gorm:"column:mobile"` // 手机
|
|
Email string `gorm:"column:email"` // 邮箱
|
|
Pwd string `gorm:"column:pwd"` // 密码
|
|
Salt string `gorm:"column:salt;NOT NULL"` // 盐
|
|
Sex int `gorm:"column:sex;default:0"` // 性别:0-女,1-男
|
|
CreatedBy string `gorm:"column:created_by"` // 创建人
|
|
CreatedOn time.Time `gorm:"column:created_on;default:CURRENT_TIMESTAMP;NOT NULL"` // 记录创建时间
|
|
ModifiedBy string `gorm:"column:modified_by"` // 修改人
|
|
ModifiedOn time.Time `gorm:"column:modified_on;default:CURRENT_TIMESTAMP"` // 记录修改时间
|
|
}
|
|
|
|
func (m *User) TableName() string {
|
|
return "user"
|
|
}
|
|
|
|
type UserRepo interface {
|
|
GetUserByAccount(ctx context.Context, account string) (user User, err error)
|
|
GetUserByMobile(ctx context.Context, mobile string) (user User, err error)
|
|
GetUserByEmail(ctx context.Context, mobile string) (user User, err error)
|
|
SaveUser(ctx context.Context, user User) error
|
|
CreateUser(ctx context.Context, user *User) error
|
|
}
|
|
|
|
type userRepoS struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewUserRepoS(i *do.Injector) (UserRepo, error) {
|
|
return &userRepoS{
|
|
db: do.MustInvoke[*gorm.DB](i),
|
|
}, nil
|
|
}
|
|
|
|
func (u *userRepoS) GetUserByAccount(ctx context.Context, account string) (user User, err error) {
|
|
err = u.db.Where("account = ?", account).Take(&user).Error
|
|
return
|
|
}
|
|
|
|
func (u *userRepoS) GetUserByMobile(ctx context.Context, mobile string) (user User, err error) {
|
|
err = u.db.Where("mobile = ?", mobile).Take(&user).Error
|
|
return
|
|
}
|
|
|
|
func (u *userRepoS) SaveUser(ctx context.Context, user User) error {
|
|
if user.ID <= 0 {
|
|
return fmt.Errorf("错误用户id:%d", user.ID)
|
|
}
|
|
|
|
return u.db.Save(user).Error
|
|
}
|
|
|
|
func (u *userRepoS) CreateUser(ctx context.Context, user *User) error {
|
|
return u.db.Create(user).Error
|
|
}
|
|
|
|
func (u *userRepoS) GetUserByEmail(ctx context.Context, email string) (user User, err error) {
|
|
err = u.db.Where("email = ?", email).Take(&user).Error
|
|
return
|
|
}
|