fusenapi/model/gmodel/ldap_apis_logic.go

73 lines
1.8 KiB
Go
Raw Normal View History

2023-11-13 09:59:46 +00:00
package gmodel
2023-11-14 07:03:01 +00:00
import (
"context"
"time"
2023-11-17 06:53:28 +00:00
"gorm.io/gorm"
2023-11-14 07:03:01 +00:00
)
// TODO: 使用model的属性做你想做的
type FindPageReq struct {
Fields string //筛选的字段
Sort string //排序
Page int //当前页
Limit int //每页数量
}
2023-11-17 06:53:28 +00:00
// 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
}
2023-11-14 07:03:01 +00:00
// 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) {
2023-11-17 03:38:05 +00:00
db := s.db.WithContext(ctx).Model(&LdapApis{}).Where("id = ?", id)
2023-11-14 07:03:01 +00:00
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
}