@@ -4,22 +4,24 @@ import (
4
4
"fmt"
5
5
6
6
"github.com/1Panel-dev/1Panel/core/constant"
7
+ "github.com/1Panel-dev/1Panel/core/global"
7
8
"gorm.io/gorm"
8
9
)
9
10
10
- type DBOption func (* gorm.DB ) * gorm.DB
11
-
12
11
type ICommonRepo interface {
13
- WithByID (id uint ) DBOption
14
- WithByIDs (ids []uint ) DBOption
15
- WithByName (name string ) DBOption
16
- WithLikeName (name string ) DBOption
17
- WithByType (ty string ) DBOption
18
- WithByKey (key string ) DBOption
19
- WithOrderBy (orderStr string ) DBOption
20
- WithByStatus (status string ) DBOption
12
+ WithByID (id uint ) global.DBOption
13
+ WithByGroupID (id uint ) global.DBOption
14
+
15
+ WithByName (name string ) global.DBOption
16
+ WithByType (ty string ) global.DBOption
17
+ WithByKey (key string ) global.DBOption
18
+ WithOrderBy (orderStr string ) global.DBOption
19
+ WithByStatus (status string ) global.DBOption
20
+ WithByGroupBelong (group string ) global.DBOption
21
21
22
- WithOrderRuleBy (orderBy , order string ) DBOption
22
+ WithByIDs (ids []uint ) global.DBOption
23
+
24
+ WithOrderRuleBy (orderBy , order string ) global.DBOption
23
25
}
24
26
25
27
type CommonRepo struct {}
@@ -28,57 +30,56 @@ func NewICommonRepo() ICommonRepo {
28
30
return & CommonRepo {}
29
31
}
30
32
31
- func (c * CommonRepo ) WithByID (id uint ) DBOption {
33
+ func (c * CommonRepo ) WithByID (id uint ) global. DBOption {
32
34
return func (g * gorm.DB ) * gorm.DB {
33
35
return g .Where ("id = ?" , id )
34
36
}
35
37
}
36
- func (c * CommonRepo ) WithByIDs ( ids [] uint ) DBOption {
38
+ func (c * CommonRepo ) WithByGroupID ( id uint ) global. DBOption {
37
39
return func (g * gorm.DB ) * gorm.DB {
38
- return g .Where ("id in (?) " , ids )
40
+ return g .Where ("group_id = ? " , id )
39
41
}
40
42
}
41
- func (c * CommonRepo ) WithByName (name string ) DBOption {
43
+
44
+ func (c * CommonRepo ) WithByIDs (ids []uint ) global.DBOption {
42
45
return func (g * gorm.DB ) * gorm.DB {
43
- if len (name ) == 0 {
44
- return g
45
- }
46
- return g .Where ("`name` = ?" , name )
46
+ return g .Where ("id in (?)" , ids )
47
47
}
48
48
}
49
- func (c * CommonRepo ) WithLikeName (name string ) DBOption {
49
+ func (c * CommonRepo ) WithByName (name string ) global. DBOption {
50
50
return func (g * gorm.DB ) * gorm.DB {
51
- if len (name ) == 0 {
52
- return g
53
- }
54
- return g .Where ("name like ? or command like ?" , "%" + name + "%" , "%" + name + "%" )
51
+ return g .Where ("`name` = ?" , name )
55
52
}
56
53
}
57
- func (c * CommonRepo ) WithByType (ty string ) DBOption {
54
+
55
+ func (c * CommonRepo ) WithByType (ty string ) global.DBOption {
58
56
return func (g * gorm.DB ) * gorm.DB {
59
- if len (ty ) == 0 {
60
- return g
61
- }
62
57
return g .Where ("`type` = ?" , ty )
63
58
}
64
59
}
65
- func (c * CommonRepo ) WithByKey (key string ) DBOption {
60
+ func (c * CommonRepo ) WithByKey (key string ) global. DBOption {
66
61
return func (g * gorm.DB ) * gorm.DB {
67
62
return g .Where ("key = ?" , key )
68
63
}
69
64
}
70
- func (c * CommonRepo ) WithByStatus (status string ) DBOption {
65
+ func (c * CommonRepo ) WithByStatus (status string ) global. DBOption {
71
66
return func (g * gorm.DB ) * gorm.DB {
72
67
return g .Where ("status = ?" , status )
73
68
}
74
69
}
75
- func (c * CommonRepo ) WithOrderBy (orderStr string ) DBOption {
70
+ func (c * CommonRepo ) WithByGroupBelong (group string ) global.DBOption {
71
+ return func (g * gorm.DB ) * gorm.DB {
72
+ return g .Where ("group_belong = ?" , group )
73
+ }
74
+ }
75
+
76
+ func (c * CommonRepo ) WithOrderBy (orderStr string ) global.DBOption {
76
77
return func (g * gorm.DB ) * gorm.DB {
77
78
return g .Order (orderStr )
78
79
}
79
80
}
80
81
81
- func (c * CommonRepo ) WithOrderRuleBy (orderBy , order string ) DBOption {
82
+ func (c * CommonRepo ) WithOrderRuleBy (orderBy , order string ) global. DBOption {
82
83
switch order {
83
84
case constant .OrderDesc :
84
85
order = "desc"
0 commit comments