@@ -11,9 +11,15 @@ func Init(db *gorm.DB) {
11
11
gormDb = db
12
12
}
13
13
14
- type Page struct {
15
- Page int
16
- PageSize int
14
+ type Page [T any ] struct {
15
+ Current int
16
+ Size int
17
+ Total int64
18
+ Records []* T
19
+ }
20
+
21
+ func NewPage [T any ](current , size int ) * Page [T ] {
22
+ return & Page [T ]{Current : current , Size : size }
17
23
}
18
24
19
25
func Insert [T any ](entity * T ) * gorm.DB {
@@ -61,47 +67,81 @@ func Update[T any](q *Query[T]) *gorm.DB {
61
67
return resultDb
62
68
}
63
69
64
- func SelectById [T any ](id any ) (* gorm.DB , T ) {
65
- var entity T
66
- resultDb := gormDb .Limit (1 ).Find (& entity , id )
67
- return resultDb , entity
70
+ func SelectById [T any ](id any ) (* T , * gorm.DB ) {
71
+ var entity * T
72
+ resultDb := gormDb .Take (& entity , id )
73
+ if resultDb .RowsAffected == 0 {
74
+ return nil , resultDb
75
+ }
76
+ return entity , resultDb
68
77
}
69
78
70
- func SelectByIds [T any ](ids ... any ) (* gorm.DB , [] T ) {
71
- var results []T
79
+ func SelectByIds [T any ](ids any ) ([] * T , * gorm.DB ) {
80
+ var results []* T
72
81
resultDb := gormDb .Find (& results , ids )
73
- return resultDb , results
82
+ return results , resultDb
74
83
}
75
84
76
- func SelectOne [T any ](q * Query [T ]) (* gorm.DB , T ) {
77
- var entity T
78
- resultDb := gormDb .Select (q .SelectColumns ).Where (q .QueryBuilder .String (), q .QueryArgs ... ).Limit (1 ).Find (& entity )
79
- return resultDb , entity
85
+ func SelectOne [T any ](q * Query [T ]) (* T , * gorm.DB ) {
86
+ var entity * T
87
+ resultDb := buildCondition (q )
88
+ resultDb .Take (& entity )
89
+ if resultDb .RowsAffected == 0 {
90
+ return nil , resultDb
91
+ }
92
+ return entity , resultDb
80
93
}
81
94
82
- func SelectList [T any ](q * Query [T ]) (* gorm.DB , [] T ) {
95
+ func SelectList [T any ](q * Query [T ]) ([] * T , * gorm.DB ) {
83
96
resultDb := buildCondition (q )
84
- var results []T
97
+ var results []* T
85
98
resultDb .Find (& results )
86
- return resultDb , results
99
+ return results , resultDb
100
+ }
101
+
102
+ func SelectModelList [T any , R any ](q * Query [T ]) ([]* R , * gorm.DB ) {
103
+ resultDb := buildCondition (q )
104
+ var results []* R
105
+ resultDb .Scan (& results )
106
+ return results , resultDb
87
107
}
88
108
89
- func SelectPage [T any ](page Page , q * Query [T ]) (* gorm.DB , []T ) {
109
+ func SelectPage [T any ](page * Page [T ], q * Query [T ]) (* Page [T ], * gorm.DB ) {
110
+ total , countDb := SelectCount [T ](q )
111
+ if countDb .Error != nil {
112
+ return page , countDb
113
+ }
114
+ page .Total = total
90
115
resultDb := buildCondition (q )
91
- var results []T
116
+ var results []* T
92
117
resultDb .Scopes (paginate (page )).Find (& results )
93
- return resultDb , results
118
+ page .Records = results
119
+ return page , resultDb
120
+ }
121
+
122
+ func SelectModelPage [T any , R any ](page * Page [R ], q * Query [T ]) (* Page [R ], * gorm.DB ) {
123
+ total , countDb := SelectCount [T ](q )
124
+ if countDb .Error != nil {
125
+ return page , countDb
126
+ }
127
+ page .Total = total
128
+ resultDb := buildCondition (q )
129
+ var results []* R
130
+ resultDb .Scopes (paginate (page )).Scan (& results )
131
+ page .Records = results
132
+ return page , resultDb
94
133
}
95
134
96
- func SelectCount [T any ](q * Query [T ]) (* gorm.DB , int64 ) {
135
+ func SelectCount [T any ](q * Query [T ]) (int64 , * gorm.DB ) {
97
136
var count int64
98
- resultDb := gormDb .Model (new (T )).Where (q .QueryBuilder .String (), q .QueryArgs ... ).Count (& count )
99
- return resultDb , count
137
+ resultDb := buildCondition (q )
138
+ resultDb .Count (& count )
139
+ return count , resultDb
100
140
}
101
141
102
- func paginate (p Page ) func (db * gorm.DB ) * gorm.DB {
103
- page := p .Page
104
- pageSize := p .PageSize
142
+ func paginate [ T any ] (p * Page [ T ] ) func (db * gorm.DB ) * gorm.DB {
143
+ page := p .Current
144
+ pageSize := p .Size
105
145
return func (db * gorm.DB ) * gorm.DB {
106
146
if page <= 0 {
107
147
page = 1
0 commit comments