// 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/sqlc"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"github.com/zeromicro/go-zero/core/stringx"
)

var (
	aiDeployInstanceTaskFieldNames          = builder.RawFieldNames(&AiDeployInstanceTask{})
	aiDeployInstanceTaskRows                = strings.Join(aiDeployInstanceTaskFieldNames, ",")
	aiDeployInstanceTaskRowsExpectAutoSet   = strings.Join(stringx.Remove(aiDeployInstanceTaskFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	aiDeployInstanceTaskRowsWithPlaceHolder = strings.Join(stringx.Remove(aiDeployInstanceTaskFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
)

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

	defaultAiDeployInstanceTaskModel struct {
		conn  sqlx.SqlConn
		table string
	}

	AiDeployInstanceTask struct {
		Id         int64  `db:"id"`
		Name       string `db:"name"`
		ModelName  string `db:"model_name"`
		ModelType  string `db:"model_type"`
		Desc       string `db:"desc"`
		CreateTime string `db:"create_time"`
		UpdateTime string `db:"update_time"`
	}
)

func newAiDeployInstanceTaskModel(conn sqlx.SqlConn) *defaultAiDeployInstanceTaskModel {
	return &defaultAiDeployInstanceTaskModel{
		conn:  conn,
		table: "`ai_deploy_instance_task`",
	}
}

func (m *defaultAiDeployInstanceTaskModel) 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 *defaultAiDeployInstanceTaskModel) FindOne(ctx context.Context, id int64) (*AiDeployInstanceTask, error) {
	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", aiDeployInstanceTaskRows, m.table)
	var resp AiDeployInstanceTask
	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 *defaultAiDeployInstanceTaskModel) Insert(ctx context.Context, data *AiDeployInstanceTask) (sql.Result, error) {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?)", m.table, aiDeployInstanceTaskRowsExpectAutoSet)
	ret, err := m.conn.ExecCtx(ctx, query, data.Name, data.ModelName, data.ModelType, data.Desc)
	return ret, err
}

func (m *defaultAiDeployInstanceTaskModel) Update(ctx context.Context, data *AiDeployInstanceTask) error {
	query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, aiDeployInstanceTaskRowsWithPlaceHolder)
	_, err := m.conn.ExecCtx(ctx, query, data.Name, data.ModelName, data.ModelType, data.Desc, data.Id)
	return err
}

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