批量空投
This commit is contained in:
162
internal/model/nh_airdrop_log_model.go
Executable file
162
internal/model/nh_airdrop_log_model.go
Executable file
@@ -0,0 +1,162 @@
|
||||
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))
|
||||
}
|
||||
98
internal/model/nh_airdrop_log_model_gen.go
Executable file
98
internal/model/nh_airdrop_log_model_gen.go
Executable file
@@ -0,0 +1,98 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// versions:
|
||||
// goctl version: 1.7.6
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
var (
|
||||
nhAirdropLogFieldNames = builder.RawFieldNames(&NhAirdropLog{})
|
||||
nhAirdropLogRows = strings.Join(nhAirdropLogFieldNames, ",")
|
||||
nhAirdropLogRowsExpectAutoSet = strings.Join(stringx.Remove(nhAirdropLogFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||||
nhAirdropLogRowsWithPlaceHolder = strings.Join(stringx.Remove(nhAirdropLogFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||||
)
|
||||
|
||||
type (
|
||||
nhAirdropLogModel interface {
|
||||
Insert(ctx context.Context, data *NhAirdropLog) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id uint) (*NhAirdropLog, error)
|
||||
Update(ctx context.Context, data *NhAirdropLog) error
|
||||
Delete(ctx context.Context, id uint) error
|
||||
}
|
||||
|
||||
defaultNhAirdropLogModel struct {
|
||||
conn sqlx.SqlConn
|
||||
table string
|
||||
}
|
||||
|
||||
NhAirdropLog struct {
|
||||
Id uint `db:"id"`
|
||||
Address string `db:"address"` // 用户钱包地址
|
||||
Amount decimal.Decimal `db:"amount"` // 空投数量
|
||||
Status string `db:"status"` // 状态
|
||||
TxHash string `db:"tx_hash"` // 交易hash
|
||||
SubmitAt sql.NullTime `db:"submit_at"` // 提交时间
|
||||
BatchDate sql.NullTime `db:"batch_date"` // 批次-日期
|
||||
BatchNum uint `db:"batch_num"` // 批次-数量
|
||||
AdminId uint `db:"admin_id"` // 管理员ID
|
||||
IsDel int8 `db:"is_del"` // 是否已删除:0否,1是
|
||||
CreatedAt time.Time `db:"created_at"` // 创建时间
|
||||
UpdatedAt time.Time `db:"updated_at"` // 修改时间
|
||||
}
|
||||
)
|
||||
|
||||
func newNhAirdropLogModel(conn sqlx.SqlConn) *defaultNhAirdropLogModel {
|
||||
return &defaultNhAirdropLogModel{
|
||||
conn: conn,
|
||||
table: "`nh_airdrop_log`",
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultNhAirdropLogModel) Delete(ctx context.Context, id uint) error {
|
||||
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
|
||||
_, err := m.conn.ExecCtx(ctx, query, id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultNhAirdropLogModel) FindOne(ctx context.Context, id uint) (*NhAirdropLog, error) {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", nhAirdropLogRows, m.table)
|
||||
var resp NhAirdropLog
|
||||
err := m.conn.QueryRowCtx(ctx, &resp, query, id)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlx.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultNhAirdropLogModel) Insert(ctx context.Context, data *NhAirdropLog) (sql.Result, error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, nhAirdropLogRowsExpectAutoSet)
|
||||
ret, err := m.conn.ExecCtx(ctx, query, data.Address, data.Amount, data.Status, data.TxHash, data.SubmitAt, data.BatchDate, data.BatchNum, data.AdminId, data.IsDel)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (m *defaultNhAirdropLogModel) Update(ctx context.Context, data *NhAirdropLog) error {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, nhAirdropLogRowsWithPlaceHolder)
|
||||
_, err := m.conn.ExecCtx(ctx, query, data.Address, data.Amount, data.Status, data.TxHash, data.SubmitAt, data.BatchDate, data.BatchNum, data.AdminId, data.IsDel, data.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultNhAirdropLogModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
Reference in New Issue
Block a user