enterprise/common/dao/staff_info.go

85 lines
1.7 KiB
Go
Raw Normal View History

2023-08-31 22:20:40 +08:00
package dao
import (
"enterprise/common/model"
"gorm.io/gorm"
"time"
)
type StaffInfoDao struct {
}
func NewStaffInfoDao() *StaffInfoDao {
return &StaffInfoDao{}
}
func (d *StaffInfoDao) TableName() string {
return "staff_info"
}
func (d *StaffInfoDao) Create(o *model.StaffInfo) (int64, error) {
o.CreateTime = time.Now().Unix()
res := GetDB().Table(d.TableName()).Create(o)
return o.Id, res.Error
}
func (d *StaffInfoDao) Update(o *model.StaffInfo) error {
o.UpdateTime = time.Now().Unix()
tx := GetDB().Table(d.TableName())
res := tx.Save(o)
return res.Error
}
func (d *StaffInfoDao) Delete(id int64) error {
res := GetDB().Table(d.TableName()).Delete(&model.StaffInfo{}, id)
return res.Error
}
func (d *StaffInfoDao) Get(id int64) (*model.StaffInfo, error) {
var u model.StaffInfo
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 *StaffInfoDao) GetByUsername(username string) (*model.StaffInfo, error) {
var u model.StaffInfo
tx := GetDB().Table(d.TableName())
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 *StaffInfoDao) Query(status int) ([]*model.StaffInfo, error) {
var u []*model.StaffInfo
tx := GetDB().Table(d.TableName())
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
}