// 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 (
	taskVmFieldNames          = builder.RawFieldNames(&TaskVm{})
	taskVmRows                = strings.Join(taskVmFieldNames, ",")
	taskVmRowsExpectAutoSet   = strings.Join(stringx.Remove(taskVmFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	taskVmRowsWithPlaceHolder = strings.Join(stringx.Remove(taskVmFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
)

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

	defaultTaskVmModel struct {
		conn  sqlx.SqlConn
		table string
	}

	TaskVm struct {
		Id          int64  `db:"id"`           // id
		TaskId      int64  `db:"task_id"`      // 任务id
		Name        string `db:"name"`         // 任务名称
		AdapterId   int64  `db:"adapter_id"`   // 执行任务的适配器id
		AdapterName string `db:"adapter_name"` // 适配器名称
		ClusterId   int64  `db:"cluster_id"`   // 执行任务的集群id
		ClusterName string `db:"cluster_name"` // 集群名称
		FlavorRef   string `db:"flavor_ref"`   // 规格索引
		ImageRef    string `db:"image_ref"`    // 镜像索引
		Status      string `db:"status"`       // 状态
		Platform    string `db:"platform"`     // 平台
		MinCount    int64  `db:"min_count"`    // 数量
		Uuid        string `db:"uuid"`         // 网络id
		StartTime   string `db:"start_time"`   // 开始时间
		RunningTime string `db:"running_time"` // 运行时间
		Result      string `db:"result"`       // 运行结果
		Remark      string `db:"remark"`       // 备注
		DeletedAt   string `db:"deleted_at"`   // 删除时间
		VmName      string `db:"vm_name"`      // 虚拟机名称
		Replicas    int64  `db:"replicas"`     // 副本数
		ServerId    string `db:"server_id"`    // 虚拟机id
	}
)

func newTaskVmModel(conn sqlx.SqlConn) *defaultTaskVmModel {
	return &defaultTaskVmModel{
		conn:  conn,
		table: "`task_vm`",
	}
}

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

func (m *defaultTaskVmModel) 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 *defaultTaskVmModel) FindOne(ctx context.Context, id int64) (*TaskVm, error) {
	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", taskVmRows, m.table)
	var resp TaskVm
	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 *defaultTaskVmModel) Insert(ctx context.Context, data *TaskVm) (sql.Result, error) {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, taskVmRowsExpectAutoSet)
	ret, err := m.conn.ExecCtx(ctx, query, data.TaskId, data.Name, data.AdapterId, data.AdapterName, data.ClusterId, data.ClusterName, data.FlavorRef, data.ImageRef, data.Status, data.Platform, data.MinCount, data.Uuid, data.StartTime, data.RunningTime, data.Result, data.Remark, data.DeletedAt, data.VmName, data.Replicas, data.ServerId)
	return ret, err
}

func (m *defaultTaskVmModel) Update(ctx context.Context, data *TaskVm) error {
	query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, taskVmRowsWithPlaceHolder)
	_, err := m.conn.ExecCtx(ctx, query, data.TaskId, data.Name, data.AdapterId, data.AdapterName, data.ClusterId, data.ClusterName, data.FlavorRef, data.ImageRef, data.Status, data.Platform, data.MinCount, data.Uuid, data.StartTime, data.RunningTime, data.Result, data.Remark, data.DeletedAt, data.VmName, data.Replicas, data.ServerId, data.Id)
	return err
}

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