// 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 (
	tClusterResourceFieldNames          = builder.RawFieldNames(&TClusterResource{})
	tClusterResourceRows                = strings.Join(tClusterResourceFieldNames, ",")
	tClusterResourceRowsExpectAutoSet   = strings.Join(stringx.Remove(tClusterResourceFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	tClusterResourceRowsWithPlaceHolder = strings.Join(stringx.Remove(tClusterResourceFieldNames, "`cluster_id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
)

type (
	tClusterResourceModel interface {
		Insert(ctx context.Context, data *TClusterResource) (sql.Result, error)
		FindOne(ctx context.Context, clusterId int64) (*TClusterResource, error)
		Update(ctx context.Context, data *TClusterResource) error
		Delete(ctx context.Context, clusterId int64) error
	}

	defaultTClusterResourceModel struct {
		conn  sqlx.SqlConn
		table string
	}

	TClusterResource struct {
		ClusterId     int64   `db:"cluster_id"`
		ClusterName   string  `db:"cluster_name"`
		ClusterType   int64   `db:"cluster_type"` // 类型0->容器，1->智算，2->超算，3-虚拟机
		CpuAvail      float64 `db:"cpu_avail"`
		CpuTotal      float64 `db:"cpu_total"`
		MemAvail      float64 `db:"mem_avail"`
		MemTotal      float64 `db:"mem_total"`
		DiskAvail     float64 `db:"disk_avail"`
		DiskTotal     float64 `db:"disk_total"`
		GpuAvail      float64 `db:"gpu_avail"`
		GpuTotal      float64 `db:"gpu_total"`
		CardTotal     int64   `db:"card_total"`      // 算力卡数量
		CardTopsTotal float64 `db:"card_tops_total"` // 算力总量tops
		AdapterId     int64   `db:"adapter_id"`
		CardHours     float64 `db:"card_hours"`
		Balance       float64 `db:"balance"`
		TaskCompleted int64   `db:"task_completed"`
	}
)

func newTClusterResourceModel(conn sqlx.SqlConn) *defaultTClusterResourceModel {
	return &defaultTClusterResourceModel{
		conn:  conn,
		table: "`t_cluster_resource`",
	}
}

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

func (m *defaultTClusterResourceModel) Delete(ctx context.Context, clusterId int64) error {
	query := fmt.Sprintf("delete from %s where `cluster_id` = ?", m.table)
	_, err := m.conn.ExecCtx(ctx, query, clusterId)
	return err
}

func (m *defaultTClusterResourceModel) FindOne(ctx context.Context, clusterId int64) (*TClusterResource, error) {
	query := fmt.Sprintf("select %s from %s where `cluster_id` = ? limit 1", tClusterResourceRows, m.table)
	var resp TClusterResource
	err := m.conn.QueryRowCtx(ctx, &resp, query, clusterId)
	switch err {
	case nil:
		return &resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

func (m *defaultTClusterResourceModel) Insert(ctx context.Context, data *TClusterResource) (sql.Result, error) {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, tClusterResourceRowsExpectAutoSet)
	ret, err := m.conn.ExecCtx(ctx, query, data.ClusterId, data.ClusterName, data.ClusterType, data.CpuAvail, data.CpuTotal, data.MemAvail, data.MemTotal, data.DiskAvail, data.DiskTotal, data.GpuAvail, data.GpuTotal, data.CardTotal, data.CardTopsTotal, data.AdapterId)
	return ret, err
}

func (m *defaultTClusterResourceModel) Update(ctx context.Context, data *TClusterResource) error {
	query := fmt.Sprintf("update %s set %s where `cluster_id` = ?", m.table, tClusterResourceRowsWithPlaceHolder)
	_, err := m.conn.ExecCtx(ctx, query, data.ClusterName, data.ClusterType, data.CpuAvail, data.CpuTotal, data.MemAvail, data.MemTotal, data.DiskAvail, data.DiskTotal, data.GpuAvail, data.GpuTotal, data.CardTotal, data.CardTopsTotal, data.AdapterId, data.ClusterId)
	return err
}

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