// Code generated by goctl. DO NOT EDIT.

package models

import (
	"context"
	"database/sql"
	"fmt"
	"strings"
	"time"

	"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 (
	scQueuePhyInfoFieldNames          = builder.RawFieldNames(&ScQueuePhyInfo{})
	scQueuePhyInfoRows                = strings.Join(scQueuePhyInfoFieldNames, ",")
	scQueuePhyInfoRowsExpectAutoSet   = strings.Join(stringx.Remove(scQueuePhyInfoFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	scQueuePhyInfoRowsWithPlaceHolder = strings.Join(stringx.Remove(scQueuePhyInfoFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"

	cachePcmScQueuePhyInfoIdPrefix = "cache:pcm:scQueuePhyInfo:id:"
)

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

	defaultScQueuePhyInfoModel struct {
		sqlc.CachedConn
		table string
	}

	ScQueuePhyInfo struct {
		Id             int64         `db:"id"`               // id
		AclHosts       string        `db:"aclHosts"`         // 可用节点，多个节点用逗号隔开
		QueueId        string        `db:"queue_id"`         // 队列名称
		Text           string        `db:"text"`             // 队列名称
		QueueName      string        `db:"queue_name"`       // 队列名称
		QueNodes       string        `db:"que_nodes"`        // 队列节点总数
		QueMinNodect   string        `db:"que_min_nodect"`   // 队列最小节点数
		QueMaxNgpus    string        `db:"que_max_ngpus"`    // 队列最大GPU卡数
		QueMaxPpn      string        `db:"que_max_ppn"`      // 使用该队列作业最大CPU核心数
		QueChargeRate  string        `db:"que_charge_rate"`  // 费率
		QueMaxNcpus    string        `db:"que_max_ncpus"`    // 用户最大可用核心数
		QueMaxNdcus    string        `db:"que_max_ndcus"`    // 队列总DCU卡数
		QueMinNcpus    string        `db:"que_min_ncpus"`    // 队列最小CPU核数
		QueFreeNodes   string        `db:"que_free_nodes"`   // 队列空闲节点数
		QueMaxNodect   string        `db:"que_max_nodect"`   // 队列作业最大节点数
		QueMaxGpuPN    string        `db:"que_max_gpu_PN"`   // 队列单作业最大GPU卡数
		QueMaxWalltime string        `db:"que_max_walltime"` // 队列最大运行时间
		QueMaxDcuPN    string        `db:"que_max_dcu_PN"`   // 队列单作业最大DCU卡数
		ParticipantId  int64         `db:"participant_id"`   // 集群动态信息id
		DeletedFlag    int64         `db:"deleted_flag"`     // 是否删除
		QueNcpus       string        `db:"que_ncpus"`
		QueFreeNcpus   string        `db:"que_free_ncpus"`
		CreatedBy      sql.NullInt64 `db:"created_by"`   // 创建人
		CreatedTime    time.Time     `db:"created_time"` // 创建时间
		UpdatedBy      sql.NullInt64 `db:"updated_by"`   // 更新人
		UpdatedTime    sql.NullTime  `db:"updated_time"` // 更新时间
	}
)

func newScQueuePhyInfoModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultScQueuePhyInfoModel {
	return &defaultScQueuePhyInfoModel{
		CachedConn: sqlc.NewConn(conn, c, opts...),
		table:      "`sc_queue_phy_info`",
	}
}

func (m *defaultScQueuePhyInfoModel) withSession(session sqlx.Session) *defaultScQueuePhyInfoModel {
	return &defaultScQueuePhyInfoModel{
		CachedConn: m.CachedConn.WithSession(session),
		table:      "`sc_queue_phy_info`",
	}
}

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

func (m *defaultScQueuePhyInfoModel) FindOne(ctx context.Context, id int64) (*ScQueuePhyInfo, error) {
	pcmScQueuePhyInfoIdKey := fmt.Sprintf("%s%v", cachePcmScQueuePhyInfoIdPrefix, id)
	var resp ScQueuePhyInfo
	err := m.QueryRowCtx(ctx, &resp, pcmScQueuePhyInfoIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
		query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", scQueuePhyInfoRows, 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 *defaultScQueuePhyInfoModel) Insert(ctx context.Context, data *ScQueuePhyInfo) (sql.Result, error) {
	pcmScQueuePhyInfoIdKey := fmt.Sprintf("%s%v", cachePcmScQueuePhyInfoIdPrefix, 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, scQueuePhyInfoRowsExpectAutoSet)
		return conn.ExecCtx(ctx, query, data.Id, data.AclHosts, data.QueueId, data.Text, data.QueueName, data.QueNodes, data.QueMinNodect, data.QueMaxNgpus, data.QueMaxPpn, data.QueChargeRate, data.QueMaxNcpus, data.QueMaxNdcus, data.QueMinNcpus, data.QueFreeNodes, data.QueMaxNodect, data.QueMaxGpuPN, data.QueMaxWalltime, data.QueMaxDcuPN, data.ParticipantId, data.DeletedFlag, data.CreatedBy, data.CreatedTime, data.UpdatedBy, data.UpdatedTime)
	}, pcmScQueuePhyInfoIdKey)
	return ret, err
}

func (m *defaultScQueuePhyInfoModel) Update(ctx context.Context, data *ScQueuePhyInfo) error {
	pcmScQueuePhyInfoIdKey := fmt.Sprintf("%s%v", cachePcmScQueuePhyInfoIdPrefix, 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, scQueuePhyInfoRowsWithPlaceHolder)
		return conn.ExecCtx(ctx, query, data.AclHosts, data.QueueId, data.Text, data.QueueName, data.QueNodes, data.QueMinNodect, data.QueMaxNgpus, data.QueMaxPpn, data.QueChargeRate, data.QueMaxNcpus, data.QueMaxNdcus, data.QueMinNcpus, data.QueFreeNodes, data.QueMaxNodect, data.QueMaxGpuPN, data.QueMaxWalltime, data.QueMaxDcuPN, data.ParticipantId, data.DeletedFlag, data.CreatedBy, data.CreatedTime, data.UpdatedBy, data.UpdatedTime, data.Id)
	}, pcmScQueuePhyInfoIdKey)
	return err
}

func (m *defaultScQueuePhyInfoModel) formatPrimary(primary any) string {
	return fmt.Sprintf("%s%v", cachePcmScQueuePhyInfoIdPrefix, primary)
}

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

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