DeleteLogsByTimeRange 根据时间范围删除日志
(ctx context.Context, startTimestamp int64, endTimestamp int64, limit int)
| 434 | |
| 435 | // DeleteLogsByTimeRange 根据时间范围删除日志 |
| 436 | func DeleteLogsByTimeRange(ctx context.Context, startTimestamp int64, endTimestamp int64, limit int) (int64, error) { |
| 437 | var total int64 = 0 |
| 438 | var result *gorm.DB |
| 439 | |
| 440 | for { |
| 441 | if nil != ctx.Err() { |
| 442 | return total, ctx.Err() |
| 443 | } |
| 444 | |
| 445 | // 构建查询条件 |
| 446 | query := LOG_DB |
| 447 | if startTimestamp > 0 { |
| 448 | query = query.Where("created_at >= ?", startTimestamp) |
| 449 | } |
| 450 | if endTimestamp > 0 { |
| 451 | query = query.Where("created_at <= ?", endTimestamp) |
| 452 | } |
| 453 | |
| 454 | // 执行删除操作 |
| 455 | result = query.Limit(limit).Delete(&Log{}) |
| 456 | if nil != result.Error { |
| 457 | return total, result.Error |
| 458 | } |
| 459 | |
| 460 | total += result.RowsAffected |
| 461 | |
| 462 | if result.RowsAffected < int64(limit) { |
| 463 | break |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | return total, nil |
| 468 | } |
no test coverage detected