66 lines
1.7 KiB
Go
66 lines
1.7 KiB
Go
package gmodel
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// TODO: 使用model的属性做你想做的
|
|
|
|
// FindAll 全部查询
|
|
func (s *LdapGroupModel) FindAll(ctx context.Context, gorm *gorm.DB) (resp []LdapGroup, err error) {
|
|
var db = gorm
|
|
if gorm == nil {
|
|
db = s.db.WithContext(ctx).Model(&LdapGroup{})
|
|
} else {
|
|
db = db.WithContext(ctx).Model(&LdapGroup{})
|
|
}
|
|
err = db.Find(&resp).Error
|
|
return resp, err
|
|
}
|
|
|
|
// FindPage 分页查询
|
|
func (s *LdapGroupModel) FindPage(ctx context.Context, req FindPageReq) (resp []LdapGroup, total int64, err error) {
|
|
db := s.db.WithContext(ctx).Model(&LdapGroup{})
|
|
if req.Fields != "" {
|
|
db = db.Select(req.Fields)
|
|
}
|
|
if req.Sort != "" {
|
|
db = db.Order(req.Sort)
|
|
}
|
|
//查询数量
|
|
if err = db.Limit(1).Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
offset := (req.Page - 1) * req.Limit
|
|
err = db.Offset(offset).Limit(req.Limit).Find(&resp).Error
|
|
return resp, total, err
|
|
}
|
|
|
|
func (s *LdapGroupModel) FindOneById(ctx context.Context, id int64) (resp LdapGroup, err error) {
|
|
db := s.db.WithContext(ctx).Model(&LdapGroup{}).Where("id = ?", id)
|
|
|
|
err = db.Take(&resp).Error
|
|
return resp, err
|
|
}
|
|
|
|
// InsertOne 单个插入
|
|
func (s *LdapGroupModel) InsertOne(ctx context.Context, insertData LdapGroup) error {
|
|
db := s.db.WithContext(ctx).Model(&LdapGroup{})
|
|
var nowTime = time.Now().UTC()
|
|
insertData.Ctime = &nowTime
|
|
insertData.Utime = &nowTime
|
|
result := db.Create(&insertData)
|
|
return result.Error
|
|
}
|
|
|
|
// UpdateOne 单个更新
|
|
func (s *LdapGroupModel) UpdateOne(ctx context.Context, model LdapGroup, updateData map[string]interface{}) error {
|
|
db := s.db.WithContext(ctx).Model(&model)
|
|
updateData["utime"] = time.Now().UTC()
|
|
result := db.Updates(updateData)
|
|
return result.Error
|
|
}
|