You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
134 lines
3.2 KiB
134 lines
3.2 KiB
1 year ago
|
package model
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"gorm.io/gorm"
|
||
|
)
|
||
|
|
||
|
type (
|
||
|
accountModel interface {
|
||
|
Insert(ctx context.Context, data *Account) error
|
||
|
Update(ctx context.Context, id int64, updates map[string]any) error
|
||
|
Delete(ctx context.Context, id int64) error
|
||
|
FindOne(ctx context.Context, id int64) (*Account, error)
|
||
|
FindUserId(ctx context.Context, userId int32, userType int32) (*Account, error)
|
||
|
}
|
||
|
Account struct {
|
||
|
updates map[string]any
|
||
|
Ac string `gorm:"column:ac"`
|
||
|
Id int64 `gorm:"column:id"`
|
||
|
UserId int32 `gorm:"column:user_id"`
|
||
|
UserType int32 `gorm:"column:user_type"` //1 user 2 employer
|
||
|
Name string `gorm:"column:name"`
|
||
|
Email string `gorm:"column:email"`
|
||
|
Password string `gorm:"column:password"`
|
||
|
Phone string `gorm:"column:phone"` //+80-18010489927
|
||
|
WechatUniqueId string `gorm:"column:wechat_unique_id"`
|
||
|
}
|
||
|
defaultAccountModel struct {
|
||
|
db *gorm.DB
|
||
|
}
|
||
|
)
|
||
|
|
||
|
func (s *Account) TableName() string {
|
||
|
return "account"
|
||
|
}
|
||
|
|
||
|
func NewAccount() *Account {
|
||
|
return &Account{
|
||
|
updates: make(map[string]any),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (s *Account) SetAc(ac string) *Account {
|
||
|
s.Ac = ac
|
||
|
s.set("ac", ac)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *Account) SetId(id int64) *Account {
|
||
|
s.Id = id
|
||
|
s.set("id", id)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *Account) SetUserId(userId int32) *Account {
|
||
|
s.UserId = userId
|
||
|
s.set("user_id", userId)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *Account) SetUserType(userType int32) *Account {
|
||
|
s.UserType = userType
|
||
|
s.set("user_type", userType)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *Account) SetName(name string) *Account {
|
||
|
s.Name = name
|
||
|
s.set("name", name)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *Account) SetEmail(email string) *Account {
|
||
|
s.Email = email
|
||
|
s.set("email", email)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *Account) SetPassword(password string) *Account {
|
||
|
s.Password = password
|
||
|
s.set("password", password)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *Account) SetPhone(phone string) *Account {
|
||
|
s.Phone = phone
|
||
|
s.set("phone", phone)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *Account) SetWechatUniqueId(wechatUniqueId string) *Account {
|
||
|
s.WechatUniqueId = wechatUniqueId
|
||
|
s.set("wechat_unique_id", wechatUniqueId)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *Account) set(key string, val any) {
|
||
|
s.updates[key] = val
|
||
|
}
|
||
|
|
||
|
func (s *Account) UpdateColumn() map[string]any {
|
||
|
return s.updates
|
||
|
}
|
||
|
|
||
|
func newAccountDAO(db *gorm.DB) *defaultAccountModel {
|
||
|
return &defaultAccountModel{
|
||
|
db: db,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (s *defaultAccountModel) Insert(ctx context.Context, data *Account) error {
|
||
|
return s.db.Create(data).Error
|
||
|
}
|
||
|
|
||
|
func (s *defaultAccountModel) Update(ctx context.Context, id int64, updates map[string]any) error {
|
||
|
return s.db.Model(Account{}).Where("id=?", id).Updates(updates).Error
|
||
|
}
|
||
|
|
||
|
func (s *defaultAccountModel) Delete(ctx context.Context, id int64) error {
|
||
|
return s.db.Where("id=?", id).Delete(&Account{}).Error
|
||
|
}
|
||
|
|
||
|
func (s *defaultAccountModel) FindOne(ctx context.Context, id int64) (*Account, error) {
|
||
|
row := Account{}
|
||
|
err := s.db.Where("id=?", id).Find(&row).Error
|
||
|
return findResultWithError(&row, err)
|
||
|
}
|
||
|
|
||
|
func (s *defaultAccountModel) FindUserId(ctx context.Context, userId int32, userType int32) (*Account, error) {
|
||
|
row := Account{}
|
||
|
err := s.db.Where("user_id=? and user_type=?", userId, userType).Find(&row).Error
|
||
|
return findResultWithError(&row, err)
|
||
|
}
|