// Code generated by goctl. DO NOT EDIT.

package models

import (
	"context"
	"database/sql"
	"fmt"
	"strings"
	"time"

	"github.com/zeromicro/go-zero/core/stores/builder"
	"github.com/zeromicro/go-zero/core/stores/sqlc"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"github.com/zeromicro/go-zero/core/stringx"
)

var (
	tClusterTaskQueueFieldNames          = builder.RawFieldNames(&TClusterTaskQueue{})
	tClusterTaskQueueRows                = strings.Join(tClusterTaskQueueFieldNames, ",")
	tClusterTaskQueueRowsExpectAutoSet   = strings.Join(stringx.Remove(tClusterTaskQueueFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	tClusterTaskQueueRowsWithPlaceHolder = strings.Join(stringx.Remove(tClusterTaskQueueFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
)

type (
	tClusterTaskQueueModel interface {
		Insert(ctx context.Context, data *TClusterTaskQueue) (sql.Result, error)
		FindOne(ctx context.Context, id int64) (*TClusterTaskQueue, error)
		Update(ctx context.Context, data *TClusterTaskQueue) error
		Delete(ctx context.Context, id int64) error
	}

	defaultTClusterTaskQueueModel struct {
		conn  sqlx.SqlConn
		table string
	}

	TClusterTaskQueue struct {
		Id        int64     `db:"id"`         // id
		AdapterId int64     `db:"adapter_id"` // 适配器id
		ClusterId int64     `db:"cluster_id"` // 集群id
		QueueNum  int64     `db:"queue_num"`  // 任务排队数量
		Date      time.Time `db:"date"`
	}
)

func newTClusterTaskQueueModel(conn sqlx.SqlConn) *defaultTClusterTaskQueueModel {
	return &defaultTClusterTaskQueueModel{
		conn:  conn,
		table: "`t_cluster_task_queue`",
	}
}

func (m *defaultTClusterTaskQueueModel) withSession(session sqlx.Session) *defaultTClusterTaskQueueModel {
	return &defaultTClusterTaskQueueModel{
		conn:  sqlx.NewSqlConnFromSession(session),
		table: "`t_cluster_task_queue`",
	}
}

func (m *defaultTClusterTaskQueueModel) Delete(ctx context.Context, id int64) error {
	query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
	_, err := m.conn.ExecCtx(ctx, query, id)
	return err
}

func (m *defaultTClusterTaskQueueModel) FindOne(ctx context.Context, id int64) (*TClusterTaskQueue, error) {
	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", tClusterTaskQueueRows, m.table)
	var resp TClusterTaskQueue
	err := m.conn.QueryRowCtx(ctx, &resp, query, id)
	switch err {
	case nil:
		return &resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

func (m *defaultTClusterTaskQueueModel) Insert(ctx context.Context, data *TClusterTaskQueue) (sql.Result, error) {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?)", m.table, tClusterTaskQueueRowsExpectAutoSet)
	ret, err := m.conn.ExecCtx(ctx, query, data.AdapterId, data.ClusterId, data.QueueNum, data.Date)
	return ret, err
}

func (m *defaultTClusterTaskQueueModel) Update(ctx context.Context, data *TClusterTaskQueue) error {
	query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, tClusterTaskQueueRowsWithPlaceHolder)
	_, err := m.conn.ExecCtx(ctx, query, data.AdapterId, data.ClusterId, data.QueueNum, data.Date, data.Id)
	return err
}

func (m *defaultTClusterTaskQueueModel) tableName() string {
	return m.table
}
