enterprise/common/dao/staff_config.go

86 lines
1.7 KiB
Go
Raw Normal View History

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