package model import ( "context" "errors" "fmt" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/stores/sqlx" "time" ) var _ NhAirdropLogModel = (*customNhAirdropLogModel)(nil) const ( AirdropStatusNewImport StatusType = "新导入" AirdropStatusSent StatusType = "已发送交易" AirdropStatusSuccess StatusType = "交易成功" AirdropStatusFailed StatusType = "交易失败" AirdropStatusReSent StatusType = "已重新发送交易" AirdropStatusIllegal StatusType = "非法交易" ) type ( // NhAirdropLogModel is an interface to be customized, add more methods here, // and implement the added methods in customNhAirdropLogModel. NhAirdropLogModel interface { nhAirdropLogModel withSession(session sqlx.Session) NhAirdropLogModel FindAirdropNewImportList(ctx context.Context, count int) ([]*NhAirdropLog, error) FindAirdropFailedList(ctx context.Context, count int) ([]*NhAirdropLog, error) FindRequireCheckList(ctx context.Context, count int) ([]*NhAirdropLog, error) SetSent(ctx context.Context, id uint, txHash string, isRetry bool) bool SetIllegal(ctx context.Context, id uint) bool SetSuccess(ctx context.Context, id uint) bool SetFailed(ctx context.Context, id uint, txHash string) bool SetTxHash(ctx context.Context, id uint, txHash string) } customNhAirdropLogModel struct { *defaultNhAirdropLogModel } StatusType string ) // NewNhAirdropLogModel returns a model for the database table. func NewNhAirdropLogModel(conn sqlx.SqlConn) NhAirdropLogModel { return &customNhAirdropLogModel{ defaultNhAirdropLogModel: newNhAirdropLogModel(conn), } } func (m *customNhAirdropLogModel) SetTxHash(ctx context.Context, id uint, txHash string) { update := fmt.Sprintf("update %s set `tx_hash` = ? where `id` = ?", m.table) _, err := m.conn.ExecCtx(ctx, update, txHash, id) if err != nil { logx.Errorw("set txHash error", logx.Field("err", err), logx.Field("id", id)) } } func (m *customNhAirdropLogModel) SetFailed(ctx context.Context, id uint, txHash string) bool { update := fmt.Sprintf("update %s set `status` = '%s', `tx_hash` = ? where `id` = ? and (`status` = '%s' or `status` = '%s')", m.table, AirdropStatusFailed, AirdropStatusSent, AirdropStatusReSent) result, err := m.conn.ExecCtx(ctx, update, txHash, id) if err != nil { logx.Errorw("set failed error", logx.Field("err", err), logx.Field("id", id)) return false } rows, err := result.RowsAffected() if err != nil { logx.Errorw("set failed RowsAffected error", logx.Field("err", err), logx.Field("id", id)) return false } return rows > 0 } func (m *customNhAirdropLogModel) SetSuccess(ctx context.Context, id uint) bool { update := fmt.Sprintf("update %s set `status` = '%s' where `id` = ? and (`status` = '%s' or `status` = '%s')", m.table, AirdropStatusSuccess, AirdropStatusSent, AirdropStatusReSent) result, err := m.conn.ExecCtx(ctx, update, id) if err != nil { logx.Errorw("set success error", logx.Field("err", err), logx.Field("id", id)) return false } rows, err := result.RowsAffected() if err != nil { logx.Errorw("set success RowsAffected error", logx.Field("err", err), logx.Field("id", id)) return false } return rows > 0 } func (t StatusType) String() string { return string(t) } func (m *customNhAirdropLogModel) SetIllegal(ctx context.Context, id uint) bool { update := fmt.Sprintf("update %s set `status` = '%s' where `id` = ? and (`status` = '%s' or `status` = '%s')", m.table, AirdropStatusIllegal, AirdropStatusSent, AirdropStatusReSent) result, err := m.conn.ExecCtx(ctx, update, id) if err != nil { logx.Errorw("set illegal error", logx.Field("err", err), logx.Field("id", id)) return false } rows, err := result.RowsAffected() if err != nil { logx.Errorw("set illegal RowsAffected error", logx.Field("err", err), logx.Field("id", id)) return false } return rows > 0 } func (m *customNhAirdropLogModel) SetSent(ctx context.Context, id uint, txHash string, isRetry bool) bool { var update string if isRetry { update = fmt.Sprintf("update %s set `status` = '%s', `tx_hash` = ?, `submit_at` = ? where `id` = ? and `status` = '%s'", m.table, AirdropStatusReSent, AirdropStatusFailed) } else { update = fmt.Sprintf("update %s set `status` = '%s', `tx_hash` = ?, `submit_at` = ? where `id` = ? and `status` = '%s'", m.table, AirdropStatusSent, AirdropStatusNewImport) } result, err := m.conn.ExecCtx(ctx, update, txHash, time.Now(), id) if err != nil { logx.Errorw("set sent error", logx.Field("err", err), logx.Field("id", id)) return false } rows, err := result.RowsAffected() if err != nil { logx.Errorw("set sent RowsAffected error", logx.Field("err", err), logx.Field("id", id)) return false } return rows > 0 } func (m *customNhAirdropLogModel) FindAirdropNewImportList(ctx context.Context, count int) ([]*NhAirdropLog, error) { query := fmt.Sprintf("select %s from %s where `status` = '%s' and `is_del` = 0 limit ?", nhAirdropLogRows, m.table, AirdropStatusNewImport) var tasks []*NhAirdropLog err := m.conn.QueryRowsCtx(ctx, &tasks, query, count) if err != nil && !errors.Is(err, sqlx.ErrNotFound) { return nil, err } return tasks, nil } func (m *customNhAirdropLogModel) FindAirdropFailedList(ctx context.Context, count int) ([]*NhAirdropLog, error) { query := fmt.Sprintf("select %s from %s where `status` = '%s' and `is_del` = 0 limit ?", nhAirdropLogRows, m.table, AirdropStatusFailed) var tasks []*NhAirdropLog err := m.conn.QueryRowsCtx(ctx, &tasks, query, count) if err != nil && !errors.Is(err, sqlx.ErrNotFound) { return nil, err } return tasks, nil } func (m *customNhAirdropLogModel) FindRequireCheckList(ctx context.Context, count int) ([]*NhAirdropLog, error) { query := fmt.Sprintf("select %s from %s where (`status` = '%s' or `status` = '%s') and `is_del` = 0 limit ?", nhAirdropLogRows, m.table, AirdropStatusSent, AirdropStatusReSent) var tasks []*NhAirdropLog err := m.conn.QueryRowsCtx(ctx, &tasks, query, count) if err != nil && !errors.Is(err, sqlx.ErrNotFound) { return nil, err } return tasks, nil } func (m *customNhAirdropLogModel) withSession(session sqlx.Session) NhAirdropLogModel { return NewNhAirdropLogModel(sqlx.NewSqlConnFromSession(session)) }