@@ -21,6 +21,7 @@ import (
21
21
"context"
22
22
"errors"
23
23
"fmt"
24
+ "strings"
24
25
25
26
"github.com/openimsdk/openim-sdk-core/v3/pkg/constant"
26
27
"github.com/openimsdk/openim-sdk-core/v3/pkg/db/model_struct"
@@ -221,88 +222,124 @@ func (d *DataBase) DeleteConversationMsgsBySeqs(ctx context.Context, conversatio
221
222
return errs .WrapMsg (d .conn .WithContext (ctx ).Table (utils .GetTableName (conversationID )).Where ("seq IN ?" , seqs ).Delete (model_struct.LocalChatLog {}).Error , "DeleteConversationMsgs failed" )
222
223
}
223
224
224
- func (d * DataBase ) SearchMessageByContentType (ctx context.Context , contentType []int , conversationID string , startTime , endTime int64 , offset , count int ) (result []* model_struct.LocalChatLog , err error ) {
225
+ func (d * DataBase ) SearchMessageByContentType (ctx context.Context , contentType []int , senderUserIDList [] string , conversationID string , startTime , endTime int64 , offset , count int ) (result []* model_struct.LocalChatLog , err error ) {
225
226
d .mRWMutex .RLock ()
226
227
defer d .mRWMutex .RUnlock ()
227
- condition := fmt .Sprintf ("send_time between %d and %d AND status <=%d And content_type IN ?" , startTime , endTime , constant .MsgStatusSendFailed )
228
- err = errs .WrapMsg (d .conn .WithContext (ctx ).Table (utils .GetTableName (conversationID )).Where (condition , contentType ).Order ("send_time DESC" ).Offset (offset ).Limit (count ).Find (& result ).Error , "SearchMessage failed" )
228
+
229
+ var condition strings.Builder
230
+ var args []interface {}
231
+
232
+ condition .WriteString ("send_time between ? AND ? AND status <= ? AND content_type IN (?) " )
233
+ args = append (args , startTime , endTime , constant .MsgStatusSendFailed , contentType )
234
+
235
+ if len (senderUserIDList ) != 0 {
236
+ condition .WriteString (" And send_id IN (?)" )
237
+ args = append (args , senderUserIDList )
238
+ }
239
+
240
+ err = errs .WrapMsg (d .conn .WithContext (ctx ).Table (utils .GetTableName (conversationID )).Where (condition .String (), args ... ).Order ("send_time DESC" ).Offset (offset ).Limit (count ).Find (& result ).Error , "SearchMessage failed" )
229
241
return result , err
230
242
}
231
- func (d * DataBase ) SearchMessageByKeyword (ctx context.Context , contentType []int , keywordList []string , keywordListMatchType int , conversationID string , startTime , endTime int64 , offset , count int ) (result []* model_struct.LocalChatLog , err error ) {
243
+
244
+ func (d * DataBase ) SearchMessageByKeyword (ctx context.Context , contentType []int , senderUserIDList []string , keywordList []string , keywordListMatchType int , conversationID string , startTime , endTime int64 , offset , count int ) (result []* model_struct.LocalChatLog , err error ) {
232
245
d .mRWMutex .RLock ()
233
246
defer d .mRWMutex .RUnlock ()
234
- var condition string
235
- var subCondition string
247
+
248
+ var condition strings.Builder
249
+ var subCondition strings.Builder
250
+ var args []interface {}
251
+
252
+ condition .WriteString (" send_time between ? AND ? AND status <= ? AND content_type IN (?)" )
253
+ args = append (args , startTime , endTime , constant .MsgStatusSendFailed , contentType )
254
+
255
+ // Construct a sub-condition for SQL query based on keyword list and match type
236
256
if keywordListMatchType == constant .KeywordMatchOr {
237
- for i := 0 ; i < len (keywordList ); i ++ {
238
- if i == 0 {
239
- subCondition += "And ("
257
+ // Use OR logic if keywordListMatchType is KeywordMatchOr
258
+ subCondition .WriteString (" AND (" )
259
+ for i , keyword := range keywordList {
260
+ if i > 0 {
261
+ subCondition .WriteString (" OR " )
240
262
}
241
- if i + 1 >= len (keywordList ) {
242
- subCondition += "content like " + "'%" + keywordList [i ] + "%') "
243
- } else {
244
- subCondition += "content like " + "'%" + keywordList [i ] + "%' " + "or "
245
263
246
- }
264
+ subCondition .WriteString ("content LIKE ?" )
265
+ args = append (args , "%" + keyword + "%" )
247
266
}
267
+ subCondition .WriteString (") " )
248
268
} else {
249
- for i := 0 ; i < len (keywordList ); i ++ {
250
- if i == 0 {
251
- subCondition += "And ("
252
- }
253
- if i + 1 >= len (keywordList ) {
254
- subCondition += "content like " + "'%" + keywordList [i ] + "%') "
255
- } else {
256
- subCondition += "content like " + "'%" + keywordList [i ] + "%' " + "and "
269
+ // Use AND logic for other keywordListMatchType
270
+ subCondition .WriteString (" AND (" )
271
+ for i , keyword := range keywordList {
272
+ if i > 0 {
273
+ subCondition .WriteString (" AND " )
257
274
}
275
+
276
+ subCondition .WriteString ("content LIKE ?" )
277
+ args = append (args , "%" + keyword + "%" )
258
278
}
279
+ subCondition .WriteString (") " )
259
280
}
260
- condition = fmt .Sprintf (" send_time between %d and %d AND status <=%d And content_type IN ? " , startTime , endTime , constant .MsgStatusSendFailed )
261
- condition += subCondition
262
- err = errs .WrapMsg (d .conn .WithContext (ctx ).Table (utils .GetTableName (conversationID )).Where (condition , contentType ).Order ("send_time DESC" ).Offset (offset ).Limit (count ).Find (& result ).Error , "SearchMessage failed" )
281
+
282
+ condition .WriteString (subCondition .String ())
283
+
284
+ if senderUserIDList != nil {
285
+ condition .WriteString (" And send_id IN (?)" )
286
+ args = append (args , senderUserIDList )
287
+ }
288
+
289
+ err = errs .WrapMsg (d .conn .WithContext (ctx ).Table (utils .GetTableName (conversationID )).Where (condition .String (), args ... ).Order ("send_time DESC" ).Offset (offset ).Limit (count ).Find (& result ).Error , "SearchMessage failed" )
290
+
263
291
return result , err
264
292
}
265
293
266
294
// SearchMessageByContentTypeAndKeyword searches for messages in the database that match specified content types and keywords within a given time range.
267
- func (d * DataBase ) SearchMessageByContentTypeAndKeyword (ctx context.Context , contentType []int , conversationID string , keywordList []string , keywordListMatchType int , startTime , endTime int64 ) (result []* model_struct.LocalChatLog , err error ) {
295
+ func (d * DataBase ) SearchMessageByContentTypeAndKeyword (ctx context.Context , contentType []int , conversationID string , senderUserIDList [] string , keywordList []string , keywordListMatchType int , startTime , endTime int64 ) (result []* model_struct.LocalChatLog , err error ) {
268
296
d .mRWMutex .RLock ()
269
297
defer d .mRWMutex .RUnlock ()
270
- var condition string
271
- var subCondition string
298
+
299
+ var condition strings.Builder
300
+ var subCondition strings.Builder
301
+ var args []interface {}
302
+
303
+ // Construct the main SQL condition string
304
+ condition .WriteString (" send_time between ? AND ? AND status <= ? AND content_type IN (?)" )
305
+ args = append (args , startTime , endTime , constant .MsgStatusSendFailed , contentType )
272
306
273
307
// Construct a sub-condition for SQL query based on keyword list and match type
274
308
if keywordListMatchType == constant .KeywordMatchOr {
275
309
// Use OR logic if keywordListMatchType is KeywordMatchOr
276
- for i := 0 ; i < len (keywordList ); i ++ {
277
- if i == 0 {
278
- subCondition += "And ("
279
- }
280
- if i + 1 >= len (keywordList ) {
281
- subCondition += "content like " + "'%" + keywordList [i ] + "%') "
282
- } else {
283
- subCondition += "content like " + "'%" + keywordList [i ] + "%' " + "or "
310
+ subCondition .WriteString (" AND (" )
311
+ for i , keyword := range keywordList {
312
+ if i > 0 {
313
+ subCondition .WriteString (" OR " )
284
314
}
315
+
316
+ subCondition .WriteString ("content LIKE ?" )
317
+ args = append (args , "%" + keyword + "%" )
285
318
}
319
+ subCondition .WriteString (") " )
286
320
} else {
287
321
// Use AND logic for other keywordListMatchType
288
- for i := 0 ; i < len (keywordList ); i ++ {
289
- if i == 0 {
290
- subCondition += "And ("
291
- }
292
- if i + 1 >= len (keywordList ) {
293
- subCondition += "content like " + "'%" + keywordList [i ] + "%') "
294
- } else {
295
- subCondition += "content like " + "'%" + keywordList [i ] + "%' " + "and "
322
+ subCondition .WriteString (" AND (" )
323
+ for i , keyword := range keywordList {
324
+ if i > 0 {
325
+ subCondition .WriteString (" AND " )
296
326
}
327
+
328
+ subCondition .WriteString ("content LIKE ?" )
329
+ args = append (args , "%" + keyword + "%" )
297
330
}
331
+ subCondition .WriteString (") " )
298
332
}
299
333
300
- // Construct the main SQL condition string
301
- condition = fmt .Sprintf ("send_time between %d and %d AND status <=%d And content_type IN ? " , startTime , endTime , constant .MsgStatusSendFailed )
302
- condition += subCondition
334
+ condition .WriteString (subCondition .String ())
335
+
336
+ if senderUserIDList != nil {
337
+ condition .WriteString (" And send_id IN (?)" )
338
+ args = append (args , senderUserIDList )
339
+ }
303
340
304
341
// Execute the query using the constructed condition and handle errors
305
- err = errs .WrapMsg (d .conn .WithContext (ctx ).Table (utils .GetTableName (conversationID )).Where (condition , contentType ).Order ("send_time DESC" ).Find (& result ).Error , "SearchMessage failed" )
342
+ err = errs .WrapMsg (d .conn .WithContext (ctx ).Table (utils .GetTableName (conversationID )).Where (condition . String (), args ... ).Order ("send_time DESC" ).Find (& result ).Error , "SearchMessage failed" )
306
343
307
344
return result , err
308
345
}
0 commit comments