package model import ( "context" "fmt" "strings" "time" "github.com/zeromicro/go-zero/core/stores/sqlx" ) // TLogArchiveRecord is one archive run: the dumped rows are stored as a SQL // file in MinIO (ArchiveFileKey) and then removed from t_system_log. type TLogArchiveRecord struct { Id int64 `db:"id"` // 主键 ArchiveName string `db:"archive_name"` // 归档任务名 RecordCount int64 `db:"record_count"` // 归档记录数 ArchiveFileKey string `db:"archive_file_key"` // 归档SQL文件FileKey ArchiveMode int64 `db:"archive_mode"` // 归档方式:1-手动,2-自动 BatchSize int64 `db:"batch_size"` // 本次归档的阈值N ArchivedBy string `db:"archived_by"` // 归档人账号 ArchivedAt time.Time `db:"archived_at"` // 归档时间 } // LogArchiveFilter mirrors LogArchiveListReq; empty fields = no filtering. type LogArchiveFilter struct { ArchiveName string ArchivedBy string ArchivedStartTime string ArchivedEndTime string } type TLogArchiveRecordModel interface { // FindPage lists archive runs with optional filters, newest first. FindPage(ctx context.Context, page, pageSize int64, f LogArchiveFilter) ([]TLogArchiveRecord, int64, error) // FindByIds returns the archive runs with any of the given ids. FindByIds(ctx context.Context, ids []int64) ([]TLogArchiveRecord, error) // CountAll returns the total number of archive runs. CountAll(ctx context.Context) (int64, error) } type customTLogArchiveRecordModel struct { conn sqlx.SqlConn table string } var _ TLogArchiveRecordModel = (*customTLogArchiveRecordModel)(nil) // NewTLogArchiveRecordModel returns the model for t_log_archive_record. func NewTLogArchiveRecordModel(conn sqlx.SqlConn) TLogArchiveRecordModel { return &customTLogArchiveRecordModel{ conn: conn, table: "`t_log_archive_record`", } } func (m *customTLogArchiveRecordModel) FindPage(ctx context.Context, page, pageSize int64, f LogArchiveFilter) ([]TLogArchiveRecord, int64, error) { var conds []string var args []any if f.ArchiveName != "" { conds = append(conds, "`archive_name` like ? escape '\\\\'") args = append(args, "%"+escapeLike(f.ArchiveName)+"%") } if f.ArchivedBy != "" { conds = append(conds, "`archived_by` like ? escape '\\\\'") args = append(args, "%"+escapeLike(f.ArchivedBy)+"%") } if f.ArchivedStartTime != "" { conds = append(conds, "`archived_at` >= ?") args = append(args, f.ArchivedStartTime) } if f.ArchivedEndTime != "" { conds = append(conds, "`archived_at` <= ?") args = append(args, f.ArchivedEndTime) } where := "" if len(conds) > 0 { where = " where " + strings.Join(conds, " and ") } var total int64 if err := m.conn.QueryRowCtx(ctx, &total, fmt.Sprintf("select count(*) from %s%s", m.table, where), args...); err != nil { return nil, 0, err } if total == 0 { return nil, 0, nil } offset := (page - 1) * pageSize args = append(args, pageSize, offset) query := fmt.Sprintf( "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 ?", m.table, where) var resp []TLogArchiveRecord if err := m.conn.QueryRowsCtx(ctx, &resp, query, args...); err != nil { return nil, 0, err } return resp, total, nil } func (m *customTLogArchiveRecordModel) FindByIds(ctx context.Context, ids []int64) ([]TLogArchiveRecord, error) { placeholders := strings.TrimSuffix(strings.Repeat("?,", len(ids)), ",") args := make([]any, len(ids)) for i, v := range ids { args[i] = v } var resp []TLogArchiveRecord query := fmt.Sprintf( "select `id`, `archive_name`, `record_count`, `archive_file_key`, `archive_mode`, `batch_size`, `archived_by`, `archived_at` from %s where `id` in (%s)", m.table, placeholders) if err := m.conn.QueryRowsCtx(ctx, &resp, query, args...); err != nil { return nil, err } return resp, nil } func (m *customTLogArchiveRecordModel) CountAll(ctx context.Context) (int64, error) { var total int64 if err := m.conn.QueryRowCtx(ctx, &total, fmt.Sprintf("select count(*) from %s", m.table)); err != nil { return 0, err } return total, nil }