fusenapi/model/gmodel/ldap_apis_logic.go
2023-11-14 15:03:01 +08:00

59 lines
1.5 KiB
Go

package gmodel
import (
"context"
"time"
)
// TODO: 使用model的属性做你想做的
type FindPageReq struct {
Fields string //筛选的字段
Sort string //排序
Page int //当前页
Limit int //每页数量
}
// 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{})
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
}