enterprise/common/dao/staff_user.go

103 lines
2.1 KiB
Go
Raw Normal View History

2025-03-04 23:14:09 +08:00
package dao
import (
"enterprise/common/model"
"gorm.io/gorm"
"time"
)
type StaffUserDao struct {
}
func NewStaffUserDao() *StaffUserDao {
return &StaffUserDao{}
}
func (d *StaffUserDao) TableName() string {
return "staff_user"
}
func (d *StaffUserDao) Create(o *model.StaffUser) (int64, error) {
o.CreateTime = time.Now().Unix()
res := GetDB().Table(d.TableName()).Create(o)
return o.Id, res.Error
}
func (d *StaffUserDao) Update(o *model.StaffUser) error {
o.UpdateTime = time.Now().Unix()
tx := GetDB().Table(d.TableName())
res := tx.Save(o)
return res.Error
}
func (d *StaffUserDao) Delete(id int64) error {
res := GetDB().Table(d.TableName()).Delete(&model.StaffUser{}, id)
return res.Error
}
func (d *StaffUserDao) Get(id int64) (*model.StaffUser, error) {
var u model.StaffUser
tx := GetDB().Table(d.TableName())
tx = tx.Where("id = ?", id)
res := tx.First(&u)
if res.Error == gorm.ErrRecordNotFound {
return nil, nil
}
if res.Error != nil {
return nil, res.Error
}
return &u, nil
}
func (d *StaffUserDao) GetByUsername(corpId int64, username string) (*model.StaffUser, error) {
var u model.StaffUser
tx := GetDB().Table(d.TableName())
tx.Where("corp_id = ?", corpId)
tx = tx.Where("username = ?", username)
res := tx.First(&u)
if res.Error == gorm.ErrRecordNotFound {
return nil, nil
}
if res.Error != nil {
return nil, res.Error
}
return &u, nil
}
func (d *StaffUserDao) GetByPhone(corpId int64, phone string) (*model.StaffUser, error) {
var u model.StaffUser
tx := GetDB().Table(d.TableName())
tx.Where("corp_id = ?", corpId)
tx = tx.Where("phone = ?", phone)
res := tx.First(&u)
if res.Error == gorm.ErrRecordNotFound {
return nil, nil
}
if res.Error != nil {
return nil, res.Error
}
return &u, nil
}
func (d *StaffUserDao) Query(corpId int64, status int) ([]*model.StaffUser, error) {
var u []*model.StaffUser
tx := GetDB().Table(d.TableName())
tx.Where("corp_id = ?", corpId)
if status != 0 {
tx = tx.Where("status = ?", status)
}
res := tx.Find(&u)
if res.Error == gorm.ErrRecordNotFound {
return nil, nil
}
if res.Error != nil {
return nil, res.Error
}
return u, nil
}