@@ -35,10 +35,11 @@ func Init(db *gorm.DB) {
35
35
}
36
36
37
37
type Page [T any ] struct {
38
- Current int
39
- Size int
40
- Total int64
41
- Records []* T
38
+ Current int
39
+ Size int
40
+ Total int64
41
+ Records []* T
42
+ RecordsMap []T
42
43
}
43
44
44
45
type Dao [T any ] struct {}
@@ -100,15 +101,22 @@ func DeleteByIds[T any](ids any, opts ...OptionFunc) *gorm.DB {
100
101
101
102
// Delete 根据条件删除记录
102
103
func Delete [T any ](q * QueryCond [T ], opts ... OptionFunc ) * gorm.DB {
103
- db := getDb (opts ... )
104
104
var entity T
105
- resultDb := db .Where (q .queryBuilder .String (), q .queryArgs ... ).Delete (& entity )
105
+ resultDb := buildCondition [T ](q , opts ... )
106
+ resultDb .Delete (& entity )
106
107
return resultDb
107
108
}
108
109
109
- // UpdateById 根据 ID 更新
110
+ // UpdateById 根据 ID 更新,默认零值不更新
110
111
func UpdateById [T any ](entity * T , opts ... OptionFunc ) * gorm.DB {
111
112
db := getDb (opts ... )
113
+ resultDb := db .Model (entity ).Updates (entity )
114
+ return resultDb
115
+ }
116
+
117
+ // UpdateZeroById 根据 ID 零值更新
118
+ func UpdateZeroById [T any ](entity * T , opts ... OptionFunc ) * gorm.DB {
119
+ db := getDb (opts ... )
112
120
113
121
// 如果用户没有设置选择更新的字段,默认更新所有的字段,包括零值更新
114
122
updateAllIfNeed (entity , opts , db )
@@ -131,8 +139,8 @@ func updateAllIfNeed(entity any, opts []OptionFunc, db *gorm.DB) {
131
139
132
140
// Update 根据 Map 更新
133
141
func Update [T any ](q * QueryCond [T ], opts ... OptionFunc ) * gorm.DB {
134
- db := getDb ( opts ... )
135
- resultDb := db . Model ( new ( T )). Where ( q . queryBuilder . String (), q . queryArgs ... ) .Updates (& q .updateMap )
142
+ resultDb := buildCondition [ T ]( q , opts ... )
143
+ resultDb .Updates (& q .updateMap )
136
144
return resultDb
137
145
}
138
146
@@ -196,9 +204,9 @@ func SelectCount[T any](q *QueryCond[T], opts ...OptionFunc) (int64, *gorm.DB) {
196
204
}
197
205
198
206
// Exists 根据条件判断记录是否存在
199
- func Exists [T any ](q * QueryCond [T ], opts ... OptionFunc ) (bool , error ) {
200
- _ , dbRes := SelectOne [T ](q , opts ... )
201
- return dbRes . RowsAffected > 0 , dbRes . Error
207
+ func Exists [T any ](q * QueryCond [T ], opts ... OptionFunc ) (bool , * gorm. DB ) {
208
+ count , resultDb := SelectCount [T ](q , opts ... )
209
+ return count > 0 , resultDb
202
210
}
203
211
204
212
// SelectPageGeneric 根据传入的泛型封装分页记录
@@ -215,10 +223,16 @@ func SelectPageGeneric[T any, R any](page *Page[R], q *QueryCond[T], opts ...Opt
215
223
page .Total = total
216
224
}
217
225
resultDb := buildCondition (q , opts ... )
218
- var results []R
219
- resultDb .Scopes (paginate (page )).Scan (& results )
220
- for _ , m := range results {
221
- page .Records = append (page .Records , & m )
226
+ var r R
227
+ switch any (r ).(type ) {
228
+ case map [string ]any :
229
+ var results []R
230
+ resultDb .Scopes (paginate (page )).Scan (& results )
231
+ page .RecordsMap = results
232
+ default :
233
+ var results []* R
234
+ resultDb .Scopes (paginate (page )).Scan (& results )
235
+ page .Records = results
222
236
}
223
237
return page , resultDb
224
238
}
0 commit comments