// 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 (
	aiCenterFieldNames          = builder.RawFieldNames(&AiCenter{})
	aiCenterRows                = strings.Join(aiCenterFieldNames, ",")
	aiCenterRowsExpectAutoSet   = strings.Join(stringx.Remove(aiCenterFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	aiCenterRowsWithPlaceHolder = strings.Join(stringx.Remove(aiCenterFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"

	cachePcmAiCenterIdPrefix = "cache:pcm:aiCenter:id:"
)

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

	defaultAiCenterModel struct {
		sqlc.CachedConn
		table string
	}

	AiCenter 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 newAiCenterModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultAiCenterModel {
	return &defaultAiCenterModel{
		CachedConn: sqlc.NewConn(conn, c),
		table:      "`ai_center`",
	}
}

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

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

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

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

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

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