// 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 (
	scParticipantPhyInfoFieldNames          = builder.RawFieldNames(&ScParticipantPhyInfo{})
	scParticipantPhyInfoRows                = strings.Join(scParticipantPhyInfoFieldNames, ",")
	scParticipantPhyInfoRowsExpectAutoSet   = strings.Join(stringx.Remove(scParticipantPhyInfoFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	scParticipantPhyInfoRowsWithPlaceHolder = strings.Join(stringx.Remove(scParticipantPhyInfoFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"

	cachePcmScParticipantPhyInfoIdPrefix = "cache:pcm:scParticipantPhyInfo:id:"
)

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

	defaultScParticipantPhyInfoModel struct {
		sqlc.CachedConn
		table string
	}

	ScParticipantPhyInfo struct {
		Id                int64     `db:"id"`                  // id
		Name              string    `db:"name"`                // 名称
		Address           string    `db:"address"`             // 集群p端地址
		Token             string    `db:"token"`               // 数算集群token
		MetricsUrl        string    `db:"metrics_url"`         // 监控url
		RpcAddress        string    `db:"rpc_address"`         // rpc服务链接地址
		NetworkType       string    `db:"network_type"`        // 集群网络类型
		NetworkBandwidth  string    `db:"network_bandwidth"`   // 集群网络带宽
		StorageType       string    `db:"storage_type"`        // 集群存储类型
		StorageSpace      string    `db:"storage_space"`       // 集群存储空间
		StorageAvailSpace string    `db:"storage_avail_space"` // 集群存储可用空间
		StorageBandwidth  string    `db:"storage_bandwidth"`   // 集群存储带宽
		TenantId          int64     `db:"tenant_id"`           // 租户信息id
		Type              string    `db:"type"`                // 类型:0-数算集群；1-智算集群；2-超算集群
		DeletedFlag       int64     `db:"deleted_flag"`        // 是否删除
		CreatedBy         int64     `db:"created_by"`          // 创建人
		CreatedTime       time.Time `db:"created_time"`        // 创建时间
		UpdatedBy         int64     `db:"updated_by"`          // 更新人
		UpdatedTime       time.Time `db:"updated_time"`        // 更新时间
	}
)

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

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

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

func (m *defaultScParticipantPhyInfoModel) FindOne(ctx context.Context, id int64) (*ScParticipantPhyInfo, error) {
	pcmScParticipantPhyInfoIdKey := fmt.Sprintf("%s%v", cachePcmScParticipantPhyInfoIdPrefix, id)
	var resp ScParticipantPhyInfo
	err := m.QueryRowCtx(ctx, &resp, pcmScParticipantPhyInfoIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
		query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", scParticipantPhyInfoRows, 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 *defaultScParticipantPhyInfoModel) Insert(ctx context.Context, data *ScParticipantPhyInfo) (sql.Result, error) {
	pcmScParticipantPhyInfoIdKey := fmt.Sprintf("%s%v", cachePcmScParticipantPhyInfoIdPrefix, 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, scParticipantPhyInfoRowsExpectAutoSet)
		return conn.ExecCtx(ctx, query, data.Id, data.Name, data.Address, data.Token, data.MetricsUrl, data.RpcAddress, data.NetworkType, data.NetworkBandwidth, data.StorageType, data.StorageSpace, data.StorageAvailSpace, data.StorageBandwidth, data.TenantId, data.Type, data.DeletedFlag, data.CreatedBy, data.CreatedTime, data.UpdatedBy, data.UpdatedTime)
	}, pcmScParticipantPhyInfoIdKey)
	return ret, err
}

func (m *defaultScParticipantPhyInfoModel) Update(ctx context.Context, data *ScParticipantPhyInfo) error {
	pcmScParticipantPhyInfoIdKey := fmt.Sprintf("%s%v", cachePcmScParticipantPhyInfoIdPrefix, 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, scParticipantPhyInfoRowsWithPlaceHolder)
		return conn.ExecCtx(ctx, query, data.Name, data.Address, data.Token, data.MetricsUrl, data.RpcAddress, data.NetworkType, data.NetworkBandwidth, data.StorageType, data.StorageSpace, data.StorageAvailSpace, data.StorageBandwidth, data.TenantId, data.Type, data.DeletedFlag, data.CreatedBy, data.CreatedTime, data.UpdatedBy, data.UpdatedTime, data.Id)
	}, pcmScParticipantPhyInfoIdKey)
	return err
}

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

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

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