// 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 (
	taskAiAsynchronousFieldNames          = builder.RawFieldNames(&TaskAiAsynchronous{})
	taskAiAsynchronousRows                = strings.Join(taskAiAsynchronousFieldNames, ",")
	taskAiAsynchronousRowsExpectAutoSet   = strings.Join(stringx.Remove(taskAiAsynchronousFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	taskAiAsynchronousRowsWithPlaceHolder = strings.Join(stringx.Remove(taskAiAsynchronousFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
)

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

	defaultTaskAiAsynchronousModel struct {
		conn  sqlx.SqlConn
		table string
	}

	TaskAiAsynchronous struct {
		Id          int64  `db:"id"`           // 训练作业资源规格id
		TaskId      int64  `db:"task_id"`      // 任务id
		AdapterId   int64  `db:"adapter_id"`   // 执行任务的适配器id
		AdapterName string `db:"adapter_name"` // 适配器名称
		ClusterId   int64  `db:"cluster_id"`   // 集群id
		ClusterName string `db:"cluster_name"` // 集群名称
		Name        string `db:"name"`         // 任务名
		Replica     int64  `db:"replica"`      // 执行数
		JobId       string `db:"job_id"`       // 集群返回任务id
		StartTime   string `db:"start_time"`   // 开始时间
		RunningTime string `db:"running_time"` // 运行时间
		Result      string `db:"result"`       // 运行结果
		DeletedAt   string `db:"deleted_at"`   // 删除时间
		ImageId     string `db:"image_id"`     // 镜像id
		Command     string `db:"command"`      // 命令行
		FlavorId    string `db:"flavor_id"`    // 训练作业资源规格id
		Status      string `db:"status"`       // 任务状态
	}
)

func newTaskAiAsynchronousModel(conn sqlx.SqlConn) *defaultTaskAiAsynchronousModel {
	return &defaultTaskAiAsynchronousModel{
		conn:  conn,
		table: "`task_ai_asynchronous`",
	}
}

func (m *defaultTaskAiAsynchronousModel) 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 *defaultTaskAiAsynchronousModel) FindOne(ctx context.Context, id int64) (*TaskAiAsynchronous, error) {
	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", taskAiAsynchronousRows, m.table)
	var resp TaskAiAsynchronous
	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 *defaultTaskAiAsynchronousModel) Insert(ctx context.Context, data *TaskAiAsynchronous) (sql.Result, error) {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, taskAiAsynchronousRowsExpectAutoSet)
	ret, err := m.conn.ExecCtx(ctx, query, data.TaskId, data.AdapterId, data.AdapterName, data.ClusterId, data.ClusterName, data.Name, data.Replica, data.JobId, data.StartTime, data.RunningTime, data.Result, data.DeletedAt, data.ImageId, data.Command, data.FlavorId, data.Status)
	return ret, err
}

func (m *defaultTaskAiAsynchronousModel) Update(ctx context.Context, data *TaskAiAsynchronous) error {
	query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, taskAiAsynchronousRowsWithPlaceHolder)
	_, err := m.conn.ExecCtx(ctx, query, data.TaskId, data.AdapterId, data.AdapterName, data.ClusterId, data.ClusterName, data.Name, data.Replica, data.JobId, data.StartTime, data.RunningTime, data.Result, data.DeletedAt, data.ImageId, data.Command, data.FlavorId, data.Status, data.Id)
	return err
}

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