// Code generated by goctl. DO NOT EDIT.
// versions:
//  goctl version: 1.8.1

package models

import (
	"context"
	"database/sql"
	"fmt"
	"gorm.io/gorm"
	"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 (
	hpcAppTemplateFieldNames          = builder.RawFieldNames(&HpcAppTemplate{})
	hpcAppTemplateRows                = strings.Join(hpcAppTemplateFieldNames, ",")
	hpcAppTemplateRowsExpectAutoSet   = strings.Join(stringx.Remove(hpcAppTemplateFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	hpcAppTemplateRowsWithPlaceHolder = strings.Join(stringx.Remove(hpcAppTemplateFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
)

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

	defaultHpcAppTemplateModel struct {
		conn  sqlx.SqlConn
		table string
	}

	HpcAppTemplate struct {
		Id          int64          `db:"id"`
		Name        string         `db:"name"`                          // 模板名称
		App         string         `db:"app"`                           // 应用名称
		AppType     string         `db:"app_type"`                      // 应用类型
		ClusterId   int64          `db:"cluster_id"`                    // 集群id
		Content     string         `db:"content"`                       // 模板内容
		Description string         `db:"description"`                   // 模板描述
		Status      int64          `db:"status"`                        // 状态0-禁用, 1-启用
		CreateTime  time.Time      `db:"create_time" json:"createTime"` // 创建时间
		UpdateTime  time.Time      `db:"update_time" json:"updateTime"` // 更新时间
		DeletedAt   gorm.DeletedAt `db:"deleted_at" json:"-"`           // 删除时间
	}
)

func newHpcAppTemplateModel(conn sqlx.SqlConn) *defaultHpcAppTemplateModel {
	return &defaultHpcAppTemplateModel{
		conn:  conn,
		table: "`hpc_app_template`",
	}
}

func (m *defaultHpcAppTemplateModel) 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 *defaultHpcAppTemplateModel) FindOne(ctx context.Context, id int64) (*HpcAppTemplate, error) {
	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", hpcAppTemplateRows, m.table)
	var resp HpcAppTemplate
	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 *defaultHpcAppTemplateModel) Insert(ctx context.Context, data *HpcAppTemplate) (sql.Result, error) {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?)", m.table, hpcAppTemplateRowsExpectAutoSet)
	ret, err := m.conn.ExecCtx(ctx, query, data.Name, data.App, data.AppType, data.ClusterId, data.Content, data.Description, data.Status, data.DeletedAt)
	return ret, err
}

func (m *defaultHpcAppTemplateModel) Update(ctx context.Context, data *HpcAppTemplate) error {
	query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, hpcAppTemplateRowsWithPlaceHolder)
	_, err := m.conn.ExecCtx(ctx, query, data.Name, data.App, data.AppType, data.ClusterId, data.Content, data.Description, data.Status, data.DeletedAt, data.Id)
	return err
}

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