107 lines
3.7 KiB
Go
107 lines
3.7 KiB
Go
package repo
|
|
|
|
import (
|
|
userModel "busniess-user-center/internal/models/user"
|
|
"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
|
|
SetUserStatus(ctx context.Context, id uint, status userModel.UserStatus) error
|
|
Search(ctx context.Context, query *userModel.Query) ([]User, error)
|
|
ResetPwd(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
|
|
}
|
|
|
|
func (u *userRepoS) SetUserStatus(ctx context.Context, id uint, status userModel.UserStatus) error {
|
|
err := u.db.Model(&User{}).Where("id = ?", id).Update("status", status).Error
|
|
return err
|
|
}
|
|
|
|
func (u *userRepoS) Search(ctx context.Context, query *userModel.Query) ([]User, error) {
|
|
users := make([]User, 0)
|
|
|
|
keyword := fmt.Sprintf("%%%s%%", query.Keyword)
|
|
db := u.db.Model(&User{})
|
|
if query.Keyword != "" {
|
|
db = db.Where("account like ? or name like ?", keyword, keyword)
|
|
}
|
|
|
|
err := db.Order(query.Sort).Limit(query.PageSize).Offset(query.Page * query.PageSize).Find(&users).Error
|
|
return users, err
|
|
}
|
|
|
|
func (u *userRepoS) ResetPwd(ctx context.Context, user User) error {
|
|
err := u.db.Model(&User{}).Where("id = ?", user.ID).Updates(map[string]any{"pwd": user.Pwd, "salt": user.Salt}).Error
|
|
return err
|
|
}
|