-
Notifications
You must be signed in to change notification settings - Fork 84
/
operation.go
70 lines (57 loc) · 2.1 KB
/
operation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package operation
import (
"context"
"gorm.io/gorm"
"github.com/noovertime7/kubemanage/dao/model"
"github.com/noovertime7/kubemanage/dto"
)
type Operation interface {
Find(ctx context.Context, in *model.SysOperationRecord) (*model.SysOperationRecord, error)
PageList(ctx context.Context, params *dto.OperationListInput) ([]*model.SysOperationRecord, int64, error)
Save(ctx context.Context, in *model.SysOperationRecord) error
Delete(ctx context.Context, in *model.SysOperationRecord) error
DeleteList(ctx context.Context, in []int) error
}
type operation struct {
db *gorm.DB
}
func (o *operation) Find(ctx context.Context, in *model.SysOperationRecord) (*model.SysOperationRecord, error) {
out := &model.SysOperationRecord{}
return out, o.db.WithContext(ctx).Where(in).Find(&out).Error
}
func (o *operation) Save(ctx context.Context, in *model.SysOperationRecord) error {
return o.db.WithContext(ctx).Create(in).Error
}
func (o *operation) Delete(ctx context.Context, in *model.SysOperationRecord) error {
return o.db.WithContext(ctx).Delete(in).Error
}
func (o *operation) DeleteList(ctx context.Context, in []int) error {
return o.db.WithContext(ctx).Delete(&[]model.SysOperationRecord{}, "id in (?)", in).Error
}
func NewOperation(db *gorm.DB) *operation {
return &operation{db: db}
}
func (o *operation) PageList(ctx context.Context, params *dto.OperationListInput) ([]*model.SysOperationRecord, int64, error) {
var total int64 = 0
limit := params.PageSize
offset := params.PageSize * (params.Page - 1)
query := o.db.WithContext(ctx)
var list []*model.SysOperationRecord
// 如果有条件搜索 下方会自动创建搜索语句
if params.Method != "" {
query = query.Where("method = ?", params.Method)
}
if params.Path != "" {
query = query.Where("path = ?", params.Path)
}
if params.Status != 0 {
query = query.Where("status = ?", params.Status)
}
if err := query.Find(&list).Count(&total).Error; err != nil {
return nil, 0, err
}
if err := query.Order("id desc").Limit(limit).Offset(offset).Preload("User").Find(&list).Error; err != nil {
return nil, 0, err
}
return list, total, nil
}