fusenapi/model/gmodel/ldap_apis_logic.go
2023-11-17 14:53:28 +08:00

73 lines
1.8 KiB
Go

package gmodel
import (
"context"
"time"
"gorm.io/gorm"
)
// TODO: 使用model的属性做你想做的
type FindPageReq struct {
Fields string //筛选的字段
Sort string //排序
Page int //当前页
Limit int //每页数量
}
// FindAll 全部查询
func (s *LdapApisModel) FindAll(ctx context.Context, gorm *gorm.DB) (resp []LdapApis, err error) {
var db = gorm
if gorm == nil {
db = s.db.WithContext(ctx).Model(&LdapApis{})
} else {
db = db.WithContext(ctx).Model(&LdapApis{})
}
err = db.Find(&resp).Error
return resp, err
}
// FindPage 分页查询
func (s *LdapApisModel) FindPage(ctx context.Context, req FindPageReq) (resp []LdapApis, total int64, err error) {
db := s.db.WithContext(ctx).Model(&LdapApis{})
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 *LdapApisModel) FindOneById(ctx context.Context, id int64) (resp LdapApis, err error) {
db := s.db.WithContext(ctx).Model(&LdapApis{}).Where("id = ?", id)
err = db.Take(&resp).Error
return resp, err
}
// InsertOne 单个插入
func (s *LdapApisModel) InsertOne(ctx context.Context, insertData LdapApis) error {
db := s.db.WithContext(ctx).Model(&LdapApis{})
var nowTime = time.Now().UTC()
insertData.Ctime = &nowTime
insertData.Utime = &nowTime
result := db.Create(&insertData)
return result.Error
}
// UpdateOne 单个更新
func (s *LdapApisModel) UpdateOne(ctx context.Context, model LdapApis, updateData map[string]interface{}) error {
db := s.db.WithContext(ctx).Model(&model)
updateData["utime"] = time.Now().UTC()
result := db.Updates(updateData)
return result.Error
}