// 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 (
	vmFieldNames          = builder.RawFieldNames(&Vm{})
	vmRows                = strings.Join(vmFieldNames, ",")
	vmRowsExpectAutoSet   = strings.Join(stringx.Remove(vmFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	vmRowsWithPlaceHolder = strings.Join(stringx.Remove(vmFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
)

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

	defaultVmModel struct {
		conn  sqlx.SqlConn
		table string
	}

	Vm struct {
		Id            int64          `db:"id"`             // id
		TaskId        int64          `db:"task_id"`        // 任务id
		ParticipantId int64          `db:"participant_id"` // p端id
		ApiVersion    sql.NullString `db:"api_version"`    // api版本
		Name          sql.NullString `db:"name"`           // 名字
		Namespace     sql.NullString `db:"namespace"`      // 命名空间
		Kind          sql.NullString `db:"kind"`           // 种类
		CreatedBy     sql.NullInt64  `db:"created_by"`     // 创建人
		CreatedTime   sql.NullTime   `db:"created_time"`   // 创建时间
		UpdateBy      sql.NullInt64  `db:"update_by"`      // 修改人
		UpdateTime    sql.NullTime   `db:"update_time"`    // 修改时间
		Status        string         `db:"status"`         // 状态
	}
)

func newVmModel(conn sqlx.SqlConn) *defaultVmModel {
	return &defaultVmModel{
		conn:  conn,
		table: "`vm`",
	}
}

func (m *defaultVmModel) withSession(session sqlx.Session) *defaultVmModel {
	return &defaultVmModel{
		conn:  sqlx.NewSqlConnFromSession(session),
		table: "`vm`",
	}
}

func (m *defaultVmModel) 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 *defaultVmModel) FindOne(ctx context.Context, id int64) (*Vm, error) {
	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", vmRows, m.table)
	var resp Vm
	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 *defaultVmModel) Insert(ctx context.Context, data *Vm) (sql.Result, error) {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, vmRowsExpectAutoSet)
	ret, err := m.conn.ExecCtx(ctx, query, data.TaskId, data.ParticipantId, data.ApiVersion, data.Name, data.Namespace, data.Kind, data.CreatedBy, data.CreatedTime, data.UpdateBy, data.Status)
	return ret, err
}

func (m *defaultVmModel) Update(ctx context.Context, data *Vm) error {
	query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, vmRowsWithPlaceHolder)
	_, err := m.conn.ExecCtx(ctx, query, data.TaskId, data.ParticipantId, data.ApiVersion, data.Name, data.Namespace, data.Kind, data.CreatedBy, data.CreatedTime, data.UpdateBy, data.Status, data.Id)
	return err
}

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