// 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 (
	taskAiSubFieldNames          = builder.RawFieldNames(&TaskAiSub{})
	taskAiSubRows                = strings.Join(taskAiSubFieldNames, ",")
	taskAiSubRowsExpectAutoSet   = strings.Join(stringx.Remove(taskAiSubFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	taskAiSubRowsWithPlaceHolder = strings.Join(stringx.Remove(taskAiSubFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
)

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

	defaultTaskAiSubModel struct {
		conn  sqlx.SqlConn
		table string
	}

	TaskAiSub struct {
		Id          int64  `db:"id"`           // id
		TaskId      int64  `db:"task_id"`      // 主任务id
		TaskName    string `db:"task_name"`    // 主任务名称
		TaskAiId    int64  `db:"task_ai_id"`   // 子任务id
		TaskAiName  string `db:"task_ai_name"` // 子任务名称
		ImageName   string `db:"image_name"`   // 图片名称
		Result      string `db:"result"`       // 识别结果
		Card        string `db:"card"`         // 加速卡
		ClusterId   int64  `db:"cluster_id"`   // 集群id
		ClusterName string `db:"cluster_name"` // 集群名称
	}
)

func newTaskAiSubModel(conn sqlx.SqlConn) *defaultTaskAiSubModel {
	return &defaultTaskAiSubModel{
		conn:  conn,
		table: "`task_ai_sub`",
	}
}

func (m *defaultTaskAiSubModel) 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 *defaultTaskAiSubModel) FindOne(ctx context.Context, id int64) (*TaskAiSub, error) {
	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", taskAiSubRows, m.table)
	var resp TaskAiSub
	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 *defaultTaskAiSubModel) Insert(ctx context.Context, data *TaskAiSub) (sql.Result, error) {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?)", m.table, taskAiSubRowsExpectAutoSet)
	ret, err := m.conn.ExecCtx(ctx, query, data.ImageName, data.Result, data.Card, data.ClusterId, data.ClusterName)
	return ret, err
}

func (m *defaultTaskAiSubModel) Update(ctx context.Context, data *TaskAiSub) error {
	query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, taskAiSubRowsWithPlaceHolder)
	_, err := m.conn.ExecCtx(ctx, query, data.ImageName, data.Result, data.Card, data.ClusterId, data.ClusterName, data.Id)
	return err
}

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