This repository has been archived by the owner on Mar 16, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 206
/
cronjob.go
133 lines (110 loc) · 3.84 KB
/
cronjob.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* Created by GoLand.
* User: zghua
* Email: xzghua@gmail.com
* Date: 2019-06-25
* Time: 21:10
*/
package repository
import (
"github.com/jinzhu/gorm"
"github.com/kplcloud/kplcloud/src/repository/types"
)
type CronjobRepository interface {
Create(*types.Cronjob) (*types.Cronjob, error)
Find(cId int64) (*types.Cronjob, error)
Update(cj *types.Cronjob, id int64) error
Delete(id int64) error
GetCronjobByNs(ns string) ([]*types.Cronjob, error)
GetCronjobByNameLikeAndNs(nameLike string, ns string) ([]*types.Cronjob, error)
GetCronJobByNameAndNs(name string, ns string) (*types.Cronjob, bool)
CronJobCountWithGroup(name string, ns string, group int64) (int64, error)
CronJobPaginateWithGroup(name string, ns string, group int64, offset int, limit int) ([]*types.Cronjob, error)
}
type cronjob struct {
db *gorm.DB
}
func NewCronjobRepository(db *gorm.DB) CronjobRepository {
return &cronjob{db: db}
}
func (c *cronjob) Find(cId int64) (*types.Cronjob, error) {
var cron types.Cronjob
err := c.db.First(&cron, cId).Error
return &cron, err
}
func (c *cronjob) GetCronjobByNs(ns string) (res []*types.Cronjob, err error) {
err = c.db.Where("namespace = ?", ns).Find(&res).Error
return
}
func (c *cronjob) GetCronjobByNameLikeAndNs(nameLike string, ns string) (res []*types.Cronjob, err error) {
err = c.db.Where("namespace = ?", ns).
Where("name like ?", "%"+nameLike+"%").
Find(&res).Error
return
}
func (c *cronjob) GetCronJobByNameAndNs(name string, ns string) (*types.Cronjob, bool) {
var cronjob types.Cronjob
isExists := c.db.Where("name = ?", name).Where("namespace = ?", ns).First(&cronjob).RecordNotFound()
return &cronjob, isExists
}
func (c *cronjob) Create(cj *types.Cronjob) (*types.Cronjob, error) {
err := c.db.Create(&cj).Error
return cj, err
}
func (c *cronjob) Update(cj *types.Cronjob, id int64) error {
return c.db.Where("id = ?", id).Save(&cj).Error
}
func (c *cronjob) Delete(id int64) (err error) {
return c.db.Delete(&types.Cronjob{ID: id}).Error
}
func (c *cronjob) CronJobCountWithGroup(name string, ns string, group int64) (cnt int64, err error) {
var cjs []*types.Cronjob
var cj types.Cronjob
query := c.db
if group > 0 {
var gc types.GroupsCronjobs
var g types.Groups
query = query.Joins("inner join "+gc.TableName()+" T1 ON T1.`cronjobs_id` = "+cj.TableName()+".`id` "+
" inner join "+g.TableName()+" T2 on T2.`id` = T1.`groups_id` ").Where("T2.id = ?", group)
}
if name != "" {
query = query.Where(cj.TableName()+".name = ?", name)
}
if ns != "" {
query = query.Where(cj.TableName()+".namespace = ?", ns)
}
err = query.Find(&cjs).Count(&cnt).Error
return cnt, err
}
func (c *cronjob) CronJobPaginateWithGroup(name string, ns string, group int64, offset int, limit int) (cronjobs []*types.Cronjob, err error) {
var cjs []*types.Cronjob
var cj types.Cronjob
query := c.db
//SELECT T0.`id`, T0.`name`, T0.`namespace`, T0.`schedule`, T0.`git_type`, T0.`git_path`, T0.`image`, T0.`suspend`, T0.`active`, T0.`last_schedule`, T0.`conf_map_name`, T0.`args`, T0.`log_path`, T0.`add_type`, T0.`created_at`, T0.`updated_at`, T0.`member_id`
// FROM `cronjobs` T0
// INNER JOIN `groups_cronjobss` T1
// ON T1.`cronjobs_id` = T0.`id`
// INNER JOIN `groups` T2
// ON
// T2.`id` = T1.`groups_id`
// WHERE
// T0.`namespace` = ?
// AND
// T2.`id` = ?
// ORDER BY T0.`id` DESC
// LIMIT 10
if group > 0 {
var gc types.GroupsCronjobs
var g types.Groups
query = query.Joins("inner join "+gc.TableName()+" T1 ON T1.`cronjobs_id` = "+cj.TableName()+".`id` "+
" inner join "+g.TableName()+" T2 on T2.`id` = T1.`groups_id` ").Where("T2.id = ?", group)
}
if name != "" {
query = query.Where(cj.TableName()+".name = ?", name)
}
if ns != "" {
query = query.Where(cj.TableName()+".namespace = ?", ns)
}
err = query.Offset(offset).Limit(limit).Order(cj.TableName() + ".id").Find(&cjs).Error
return cjs, err
}