// 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 (
	tBaseResourceSpecFieldNames          = builder.RawFieldNames(&TBaseResourceSpec{})
	tBaseResourceSpecRows                = strings.Join(tBaseResourceSpecFieldNames, ",")
	tBaseResourceSpecRowsExpectAutoSet   = strings.Join(stringx.Remove(tBaseResourceSpecFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	tBaseResourceSpecRowsWithPlaceHolder = strings.Join(stringx.Remove(tBaseResourceSpecFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
)

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

	defaultTBaseResourceSpecModel struct {
		conn  sqlx.SqlConn
		table string
	}

	TBaseResourceSpec struct {
		Id             int64          `db:"id" json:"id"`                           // 主键id
		ResourceSpecId int64          `db:"resource_spec_id" json:"resourceSpecId"` // 关联资源规格ID
		Type           string         `db:"type" json:"type"`                       // 类型名称
		Name           string         `db:"name" json:"name"`                       // 名称（如显存类型）
		TotalValue     float64        `db:"total_value" json:"totalValue"`          // 总量值
		TotalUnit      string         `db:"total_unit" json:"totalUnit"`            // 总量值单位（GB/core等）
		AvailableValue float64        `db:"available_value" json:"availableValue"`  // 可用值
		AvailableUnit  string         `db:"available_unit" json:"availableUnit"`    // 可用值单位（GB/core等）
		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:"-"`                    // 删除时间
	}
)

func newTBaseResourceSpecModel(conn sqlx.SqlConn) *defaultTBaseResourceSpecModel {
	return &defaultTBaseResourceSpecModel{
		conn:  conn,
		table: "`t_base_resource_spec`",
	}
}

func (m *defaultTBaseResourceSpecModel) 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 *defaultTBaseResourceSpecModel) FindOne(ctx context.Context, id int64) (*TBaseResourceSpec, error) {
	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", tBaseResourceSpecRows, m.table)
	var resp TBaseResourceSpec
	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 *defaultTBaseResourceSpecModel) Insert(ctx context.Context, data *TBaseResourceSpec) (sql.Result, error) {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, tBaseResourceSpecRowsExpectAutoSet)
	ret, err := m.conn.ExecCtx(ctx, query, data.ResourceSpecId, data.Type, data.Name, data.TotalValue, data.TotalUnit, data.AvailableValue, data.AvailableUnit, data.UserId, data.DeletedAt)
	return ret, err
}

func (m *defaultTBaseResourceSpecModel) Update(ctx context.Context, data *TBaseResourceSpec) error {
	query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, tBaseResourceSpecRowsWithPlaceHolder)
	_, err := m.conn.ExecCtx(ctx, query, data.ResourceSpecId, data.Type, data.Name, data.TotalValue, data.TotalUnit, data.AvailableValue, data.AvailableUnit, data.UserId, data.DeletedAt, data.Id)
	return err
}

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