// 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 (
	cloudCenterFieldNames          = builder.RawFieldNames(&CloudCenter{})
	cloudCenterRows                = strings.Join(cloudCenterFieldNames, ",")
	cloudCenterRowsExpectAutoSet   = strings.Join(stringx.Remove(cloudCenterFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	cloudCenterRowsWithPlaceHolder = strings.Join(stringx.Remove(cloudCenterFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"

	cachePcmCloudCenterIdPrefix = "cache:pcm:cloudCenter:id:"
)

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

	defaultCloudCenterModel struct {
		sqlc.CachedConn
		table string
	}

	CloudCenter 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 newCloudCenterModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultCloudCenterModel {
	return &defaultCloudCenterModel{
		CachedConn: sqlc.NewConn(conn, c),
		table:      "`cloud_center`",
	}
}

func (m *defaultCloudCenterModel) Delete(ctx context.Context, id int64) error {
	pcmCloudCenterIdKey := fmt.Sprintf("%s%v", cachePcmCloudCenterIdPrefix, 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)
	}, pcmCloudCenterIdKey)
	return err
}

func (m *defaultCloudCenterModel) FindOne(ctx context.Context, id int64) (*CloudCenter, error) {
	pcmCloudCenterIdKey := fmt.Sprintf("%s%v", cachePcmCloudCenterIdPrefix, id)
	var resp CloudCenter
	err := m.QueryRowCtx(ctx, &resp, pcmCloudCenterIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
		query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", cloudCenterRows, 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 *defaultCloudCenterModel) Insert(ctx context.Context, data *CloudCenter) (sql.Result, error) {
	pcmCloudCenterIdKey := fmt.Sprintf("%s%v", cachePcmCloudCenterIdPrefix, 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, cloudCenterRowsExpectAutoSet)
		return conn.ExecCtx(ctx, query, data.Id, data.ClusterNum, data.NodeNum, data.CpuNum, data.GpuNum, data.ManagedFlops, data.UnmanagedFlops, data.ManagedStorage, data.UnmanagedStorage)
	}, pcmCloudCenterIdKey)
	return ret, err
}

func (m *defaultCloudCenterModel) Update(ctx context.Context, data *CloudCenter) error {
	pcmCloudCenterIdKey := fmt.Sprintf("%s%v", cachePcmCloudCenterIdPrefix, 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, cloudCenterRowsWithPlaceHolder)
		return conn.ExecCtx(ctx, query, data.ClusterNum, data.NodeNum, data.CpuNum, data.GpuNum, data.ManagedFlops, data.UnmanagedFlops, data.ManagedStorage, data.UnmanagedStorage, data.Id)
	}, pcmCloudCenterIdKey)
	return err
}

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

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

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