// 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/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlc"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"github.com/zeromicro/go-zero/core/stringx"
)

var (
	hpcCenterFieldNames          = builder.RawFieldNames(&HpcCenter{})
	hpcCenterRows                = strings.Join(hpcCenterFieldNames, ",")
	hpcCenterRowsExpectAutoSet   = strings.Join(stringx.Remove(hpcCenterFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	hpcCenterRowsWithPlaceHolder = strings.Join(stringx.Remove(hpcCenterFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"

	cachePcmHpcCenterIdPrefix = "cache:pcm:hpcCenter:id:"
)

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

	defaultHpcCenterModel struct {
		sqlc.CachedConn
		table string
	}

	HpcCenter struct {
		Id               int64          `db:"id"`                // 平台唯一id
		ClusterNum       sql.NullInt64  `db:"cluster_num"`       // 集群数量
		NodeNum          sql.NullInt64  `db:"node_num"`          // 节点数量
		CpuNum           sql.NullInt64  `db:"cpu_num"`           // CPU核数
		GpuNum           sql.NullInt64  `db:"gpu_num"`           // GPU卡数
		ManagedFlops     sql.NullString `db:"managed_flops"`     // 已接入算力
		UnmanagedFlops   sql.NullString `db:"unmanaged_flops"`   // 未接入算力
		ManagedStorage   sql.NullString `db:"managed_storage"`   // 已接入存储
		UnmanagedStorage sql.NullString `db:"unmanaged_storage"` // 未接入存储
	}
)

func newHpcCenterModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultHpcCenterModel {
	return &defaultHpcCenterModel{
		CachedConn: sqlc.NewConn(conn, c),
		table:      "`hpc_center`",
	}
}

func (m *defaultHpcCenterModel) Delete(ctx context.Context, id int64) error {
	pcmHpcCenterIdKey := fmt.Sprintf("%s%v", cachePcmHpcCenterIdPrefix, id)
	_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
		query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
		return conn.ExecCtx(ctx, query, id)
	}, pcmHpcCenterIdKey)
	return err
}

func (m *defaultHpcCenterModel) FindOne(ctx context.Context, id int64) (*HpcCenter, error) {
	pcmHpcCenterIdKey := fmt.Sprintf("%s%v", cachePcmHpcCenterIdPrefix, id)
	var resp HpcCenter
	err := m.QueryRowCtx(ctx, &resp, pcmHpcCenterIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
		query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", hpcCenterRows, m.table)
		return conn.QueryRowCtx(ctx, v, query, id)
	})
	switch err {
	case nil:
		return &resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

func (m *defaultHpcCenterModel) Insert(ctx context.Context, data *HpcCenter) (sql.Result, error) {
	pcmHpcCenterIdKey := fmt.Sprintf("%s%v", cachePcmHpcCenterIdPrefix, data.Id)
	ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
		query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, hpcCenterRowsExpectAutoSet)
		return conn.ExecCtx(ctx, query, data.Id, data.ClusterNum, data.NodeNum, data.CpuNum, data.GpuNum, data.ManagedFlops, data.UnmanagedFlops, data.ManagedStorage, data.UnmanagedStorage)
	}, pcmHpcCenterIdKey)
	return ret, err
}

func (m *defaultHpcCenterModel) Update(ctx context.Context, data *HpcCenter) error {
	pcmHpcCenterIdKey := fmt.Sprintf("%s%v", cachePcmHpcCenterIdPrefix, data.Id)
	_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
		query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, hpcCenterRowsWithPlaceHolder)
		return conn.ExecCtx(ctx, query, data.ClusterNum, data.NodeNum, data.CpuNum, data.GpuNum, data.ManagedFlops, data.UnmanagedFlops, data.ManagedStorage, data.UnmanagedStorage, data.Id)
	}, pcmHpcCenterIdKey)
	return err
}

func (m *defaultHpcCenterModel) formatPrimary(primary interface{}) string {
	return fmt.Sprintf("%s%v", cachePcmHpcCenterIdPrefix, primary)
}

func (m *defaultHpcCenterModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary interface{}) error {
	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", hpcCenterRows, m.table)
	return conn.QueryRowCtx(ctx, v, query, primary)
}

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