// 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 (
	tResourceSpecFieldNames          = builder.RawFieldNames(&TResourceSpec{})
	tResourceSpecRows                = strings.Join(tResourceSpecFieldNames, ",")
	tResourceSpecRowsExpectAutoSet   = strings.Join(stringx.Remove(tResourceSpecFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	tResourceSpecRowsWithPlaceHolder = strings.Join(stringx.Remove(tResourceSpecFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
)

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

	defaultTResourceSpecModel struct {
		conn  sqlx.SqlConn
		table string
	}

	TResourceSpec struct {
		Id             int64          `db:"id" json:"id"`                          // 主键id
		SourceKey      string         `db:"source_key" json:"sourceKey"`           // 数据源标识（type-name）
		Type           string         `db:"type" json:"type"`                      // 类型名称
		Name           string         `db:"name" json:"name"`                      // 规格名称
		TotalCount     int64          `db:"total_count" json:"totalCount"`         // 资源总量
		AvailableCount int64          `db:"available_count" json:"availableCount"` // 可用数量
		ChangeType     int64          `db:"change_type" json:"changeType"`         // 变更类型（0: 正常，1：变更，2：删除）
		Status         int64          `db:"status" json:"status"`                  // 状态（0：未上架，1：已上架）
		Region         string         `db:"region" json:"region"`                  // 所属区域（可扩展多区域）
		ClusterId      int64          `db:"cluster_id" json:"clusterId,string"`    // 集群ID
		CostPerUnit    float64        `db:"cost_per_unit" json:"costPerUnit"`      // 单位时间积分消耗
		CostType       string         `db:"cost_type" json:"costType"`             // 计费类型（hourly, daily, monthly,perUse）
		Tag            string         `db:"tag" json:"tag"`                        // 标签（Train: 训练，Inference：推理）
		UserId         int64          `db:"user_id" json:"userId"`                 // 用户ID
		CreateTime     time.Time      `db:"create_time" json:"createTime"`         // 创建时间
		UpdateTime     time.Time      `db:"update_time" json:"updateTime"`         // 更新时间
		DeletedAt      gorm.DeletedAt `db:"deleted_at" json:"-"`                   // 删除时间

		BaseResourceSpecs []TBaseResourceSpec `gorm:"foreignKey:ResourceSpecId" json:"baseResourceSpecs,omitempty"`
	}
)

func newTResourceSpecModel(conn sqlx.SqlConn) *defaultTResourceSpecModel {
	return &defaultTResourceSpecModel{
		conn:  conn,
		table: "`t_resource_spec`",
	}
}

func (m *defaultTResourceSpecModel) 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 *defaultTResourceSpecModel) FindOne(ctx context.Context, id int64) (*TResourceSpec, error) {
	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", tResourceSpecRows, m.table)
	var resp TResourceSpec
	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 *defaultTResourceSpecModel) Insert(ctx context.Context, data *TResourceSpec) (sql.Result, error) {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, tResourceSpecRowsExpectAutoSet)
	ret, err := m.conn.ExecCtx(ctx, query, data.Type, data.Name, data.TotalCount, data.AvailableCount, data.ChangeType, data.Status, data.Region, data.ClusterId, data.UserId, data.DeletedAt)
	return ret, err
}

func (m *defaultTResourceSpecModel) Update(ctx context.Context, data *TResourceSpec) error {
	query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, tResourceSpecRowsWithPlaceHolder)
	_, err := m.conn.ExecCtx(ctx, query, data.Type, data.Name, data.TotalCount, data.AvailableCount, data.ChangeType, data.Status, data.Region, data.ClusterId, data.UserId, data.DeletedAt, data.Id)
	return err
}

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