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

var (
	taskAiFieldNames          = builder.RawFieldNames(&TaskAi{})
	taskAiRows                = strings.Join(taskAiFieldNames, ",")
	taskAiRowsExpectAutoSet   = strings.Join(stringx.Remove(taskAiFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	taskAiRowsWithPlaceHolder = strings.Join(stringx.Remove(taskAiFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
)

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

	defaultTaskAiModel struct {
		conn  sqlx.SqlConn
		table string
	}

	TaskAi 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
		Strategy    string     `db:"strategy"`     // 主任务使用策略
		Status      string     `db:"status"`       // 任务状态
		Msg         string     `db:"msg"`          // 集群返回任务信息
		CommitTime  time.Time  `db:"commit_time"`  // 提交时间
		StartTime   string     `db:"start_time"`   // 开始时间
		EndTime     string     `db:"end_time"`     // 结束时间
		TaskType    string     `db:"task_type"`
		DeletedAt   *time.Time `db:"deleted_at"`
		Card        string     `db:"card"`
		InferUrl    string     `db:"infer_url"`
		ModelName   string     `db:"model_name"`
		Output      string     `db:"output"`
	}
)

func newTaskAiModel(conn sqlx.SqlConn) *defaultTaskAiModel {
	return &defaultTaskAiModel{
		conn:  conn,
		table: "`task_ai`",
	}
}

func (m *defaultTaskAiModel) 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 *defaultTaskAiModel) FindOne(ctx context.Context, id int64) (*TaskAi, error) {
	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", taskAiRows, m.table)
	var resp TaskAi
	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 *defaultTaskAiModel) Insert(ctx context.Context, data *TaskAi) (sql.Result, error) {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, taskAiRowsExpectAutoSet)
	ret, err := m.conn.ExecCtx(ctx, query, data.TaskId, data.AdapterId, data.AdapterName, data.ClusterId, data.ClusterName, data.Name, data.Replica, data.JobId, data.Strategy, data.Status, data.Msg, data.CommitTime, data.StartTime, data.EndTime, data.TaskType, data.DeletedAt, data.Card, data.InferUrl, data.ModelName)
	return ret, err
}

func (m *defaultTaskAiModel) Update(ctx context.Context, data *TaskAi) error {
	query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, taskAiRowsWithPlaceHolder)
	_, err := m.conn.ExecCtx(ctx, query, data.TaskId, data.AdapterId, data.AdapterName, data.ClusterId, data.ClusterName, data.Name, data.Replica, data.JobId, data.Strategy, data.Status, data.Msg, data.CommitTime, data.StartTime, data.EndTime, data.TaskType, data.DeletedAt, data.Card, data.InferUrl, data.ModelName, data.Id)
	return err
}

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