54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package dao
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type User struct {
|
|
BaseModel
|
|
Username string `gorm:"column:username" json:"username"`
|
|
Phone string `gorm:"column:phone" json:"phone"`
|
|
Password string `gorm:"column:password" json:"password"`
|
|
WechatOpenID string `gorm:"column:wechat_open_id" json:"wechat_open_id"`
|
|
WechatUnionID string `gorm:"column:wechat_union_id" json:"wechat_union_id"`
|
|
}
|
|
|
|
func (u *User) TableName() string {
|
|
return "users"
|
|
}
|
|
|
|
type UserDao struct {
|
|
}
|
|
|
|
func NewUserDao() *UserDao {
|
|
return &UserDao{}
|
|
}
|
|
|
|
func (dao *UserDao) Create(tx *gorm.DB, user *User) error {
|
|
return tx.Create(user).Error
|
|
}
|
|
|
|
func (dao *UserDao) GetByPhone(tx *gorm.DB, phone string) (*User, error) {
|
|
var user User
|
|
if err := tx.Where("phone = ?", phone).First(&user).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (dao *UserDao) GetByID(tx *gorm.DB, id int64) (*User, error) {
|
|
var user User
|
|
if err := tx.Where("id = ?", id).First(&user).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|