enterprise/common/dao/staff_user.go

118 lines
2.6 KiB
Go

package dao
import (
"enterprise/common/model"
"fmt"
"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(page, size int, corpId int64, status int, username, realname, phone, idno string) ([]*model.StaffUser, int64, error) {
var u []*model.StaffUser
tx := GetDB().Table(d.TableName())
tx.Where("corp_id = ?", corpId)
if status != 0 {
tx = tx.Where("status = ?", status)
}
if username != "" {
tx.Where("username = ?", username)
}
if realname != "" {
tx.Where("realname LIKE ?", fmt.Sprintf("%%%s%%", realname))
}
if phone != "" {
tx.Where("phone LIKE ?", fmt.Sprintf("%%%s%%", phone))
}
if idno != "" {
tx.Where("idno LIKE ?", fmt.Sprintf("%%%s%%", idno))
}
var count int64
tx.Count(&count)
tx.Offset((page - 1) * size).Limit(size)
res := tx.Find(&u)
if res.Error == gorm.ErrRecordNotFound {
return nil, 0, nil
}
if res.Error != nil {
return nil, 0, res.Error
}
return u, count, nil
}