tlogarchiverecordmodel.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package model
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "github.com/zeromicro/go-zero/core/stores/sqlx"
  8. )
  9. // TLogArchiveRecord is one archive run: the dumped rows are stored as a SQL
  10. // file in MinIO (ArchiveFileKey) and then removed from t_system_log.
  11. type TLogArchiveRecord struct {
  12. Id int64 `db:"id"` // 主键
  13. ArchiveName string `db:"archive_name"` // 归档任务名
  14. RecordCount int64 `db:"record_count"` // 归档记录数
  15. ArchiveFileKey string `db:"archive_file_key"` // 归档SQL文件FileKey
  16. ArchiveMode int64 `db:"archive_mode"` // 归档方式:1-手动,2-自动
  17. BatchSize int64 `db:"batch_size"` // 本次归档的阈值N
  18. ArchivedBy string `db:"archived_by"` // 归档人账号
  19. ArchivedAt time.Time `db:"archived_at"` // 归档时间
  20. }
  21. // LogArchiveFilter mirrors LogArchiveListReq; empty fields = no filtering.
  22. type LogArchiveFilter struct {
  23. ArchiveName string
  24. ArchivedBy string
  25. ArchivedStartTime string
  26. ArchivedEndTime string
  27. }
  28. type TLogArchiveRecordModel interface {
  29. // FindPage lists archive runs with optional filters, newest first.
  30. FindPage(ctx context.Context, page, pageSize int64, f LogArchiveFilter) ([]TLogArchiveRecord, int64, error)
  31. // FindByIds returns the archive runs with any of the given ids.
  32. FindByIds(ctx context.Context, ids []int64) ([]TLogArchiveRecord, error)
  33. // CountAll returns the total number of archive runs.
  34. CountAll(ctx context.Context) (int64, error)
  35. }
  36. type customTLogArchiveRecordModel struct {
  37. conn sqlx.SqlConn
  38. table string
  39. }
  40. var _ TLogArchiveRecordModel = (*customTLogArchiveRecordModel)(nil)
  41. // NewTLogArchiveRecordModel returns the model for t_log_archive_record.
  42. func NewTLogArchiveRecordModel(conn sqlx.SqlConn) TLogArchiveRecordModel {
  43. return &customTLogArchiveRecordModel{
  44. conn: conn,
  45. table: "`t_log_archive_record`",
  46. }
  47. }
  48. func (m *customTLogArchiveRecordModel) FindPage(ctx context.Context, page, pageSize int64, f LogArchiveFilter) ([]TLogArchiveRecord, int64, error) {
  49. var conds []string
  50. var args []any
  51. if f.ArchiveName != "" {
  52. conds = append(conds, "`archive_name` like ? escape '\\\\'")
  53. args = append(args, "%"+escapeLike(f.ArchiveName)+"%")
  54. }
  55. if f.ArchivedBy != "" {
  56. conds = append(conds, "`archived_by` like ? escape '\\\\'")
  57. args = append(args, "%"+escapeLike(f.ArchivedBy)+"%")
  58. }
  59. if f.ArchivedStartTime != "" {
  60. conds = append(conds, "`archived_at` >= ?")
  61. args = append(args, f.ArchivedStartTime)
  62. }
  63. if f.ArchivedEndTime != "" {
  64. conds = append(conds, "`archived_at` <= ?")
  65. args = append(args, f.ArchivedEndTime)
  66. }
  67. where := ""
  68. if len(conds) > 0 {
  69. where = " where " + strings.Join(conds, " and ")
  70. }
  71. var total int64
  72. if err := m.conn.QueryRowCtx(ctx, &total, fmt.Sprintf("select count(*) from %s%s", m.table, where), args...); err != nil {
  73. return nil, 0, err
  74. }
  75. if total == 0 {
  76. return nil, 0, nil
  77. }
  78. offset := (page - 1) * pageSize
  79. args = append(args, pageSize, offset)
  80. query := fmt.Sprintf(
  81. "select `id`, `archive_name`, `record_count`, `archive_file_key`, `archive_mode`, `batch_size`, `archived_by`, `archived_at` from %s%s order by `archived_at` desc limit ? offset ?",
  82. m.table, where)
  83. var resp []TLogArchiveRecord
  84. if err := m.conn.QueryRowsCtx(ctx, &resp, query, args...); err != nil {
  85. return nil, 0, err
  86. }
  87. return resp, total, nil
  88. }
  89. func (m *customTLogArchiveRecordModel) FindByIds(ctx context.Context, ids []int64) ([]TLogArchiveRecord, error) {
  90. placeholders := strings.TrimSuffix(strings.Repeat("?,", len(ids)), ",")
  91. args := make([]any, len(ids))
  92. for i, v := range ids {
  93. args[i] = v
  94. }
  95. var resp []TLogArchiveRecord
  96. query := fmt.Sprintf(
  97. "select `id`, `archive_name`, `record_count`, `archive_file_key`, `archive_mode`, `batch_size`, `archived_by`, `archived_at` from %s where `id` in (%s)",
  98. m.table, placeholders)
  99. if err := m.conn.QueryRowsCtx(ctx, &resp, query, args...); err != nil {
  100. return nil, err
  101. }
  102. return resp, nil
  103. }
  104. func (m *customTLogArchiveRecordModel) CountAll(ctx context.Context) (int64, error) {
  105. var total int64
  106. if err := m.conn.QueryRowCtx(ctx, &total, fmt.Sprintf("select count(*) from %s", m.table)); err != nil {
  107. return 0, err
  108. }
  109. return total, nil
  110. }