// Code generated by goctl. DO NOT EDIT.

package models

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

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

var (
	tAdapterInfoFieldNames          = builder.RawFieldNames(&TAdapterInfo{})
	tAdapterInfoRows                = strings.Join(tAdapterInfoFieldNames, ",")
	tAdapterInfoRowsExpectAutoSet   = strings.Join(stringx.Remove(tAdapterInfoFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	tAdapterInfoRowsWithPlaceHolder = strings.Join(stringx.Remove(tAdapterInfoFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
)

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

	defaultTAdapterInfoModel struct {
		conn  sqlx.SqlConn
		table string
	}

	TAdapterInfo struct {
		Id        int64  `db:"id"`         // 主键
		AdapterId int64  `db:"adapter_id"` // 适配器id
		InfoName  string `db:"info_name"`  // 对象类型名称
	}
)

func newTAdapterInfoModel(conn sqlx.SqlConn) *defaultTAdapterInfoModel {
	return &defaultTAdapterInfoModel{
		conn:  conn,
		table: "`t_adapter_info`",
	}
}

func (m *defaultTAdapterInfoModel) 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 *defaultTAdapterInfoModel) FindOne(ctx context.Context, id int64) (*TAdapterInfo, error) {
	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", tAdapterInfoRows, m.table)
	var resp TAdapterInfo
	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 *defaultTAdapterInfoModel) Insert(ctx context.Context, data *TAdapterInfo) (sql.Result, error) {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?)", m.table, tAdapterInfoRowsExpectAutoSet)
	ret, err := m.conn.ExecCtx(ctx, query, data.Id, data.AdapterId, data.InfoName)
	return ret, err
}

func (m *defaultTAdapterInfoModel) Update(ctx context.Context, data *TAdapterInfo) error {
	query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, tAdapterInfoRowsWithPlaceHolder)
	_, err := m.conn.ExecCtx(ctx, query, data.AdapterId, data.InfoName, data.Id)
	return err
}

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