// 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 (
	scParticipantAvailInfoFieldNames          = builder.RawFieldNames(&ScParticipantAvailInfo{})
	scParticipantAvailInfoRows                = strings.Join(scParticipantAvailInfoFieldNames, ",")
	scParticipantAvailInfoRowsExpectAutoSet   = strings.Join(stringx.Remove(scParticipantAvailInfoFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	scParticipantAvailInfoRowsWithPlaceHolder = strings.Join(stringx.Remove(scParticipantAvailInfoFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"

	cachePcmScParticipantAvailInfoIdPrefix = "cache:pcm:scParticipantAvailInfo:id:"
)

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

	defaultScParticipantAvailInfoModel struct {
		sqlc.CachedConn
		table string
	}

	ScParticipantAvailInfo struct {
		Id                int64         `db:"id"`                  // id
		AvailStorageSpace int64         `db:"avail_storage_space"` // 集群存储可用空间
		UserNum           int64         `db:"user_num"`            // 用户数量
		PendingJobNum     int64         `db:"pending_job_num"`     // 待处理作业数量
		RunningJobNum     int64         `db:"running_job_num"`     // 运行作业数量
		ParticipantId     int64         `db:"participant_id"`      // 集群静态信息id
		DeletedFlag       int64         `db:"deleted_flag"`        // 是否删除
		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 newScParticipantAvailInfoModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultScParticipantAvailInfoModel {
	return &defaultScParticipantAvailInfoModel{
		CachedConn: sqlc.NewConn(conn, c, opts...),
		table:      "`sc_participant_avail_info`",
	}
}

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

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

func (m *defaultScParticipantAvailInfoModel) FindOne(ctx context.Context, id int64) (*ScParticipantAvailInfo, error) {
	pcmScParticipantAvailInfoIdKey := fmt.Sprintf("%s%v", cachePcmScParticipantAvailInfoIdPrefix, id)
	var resp ScParticipantAvailInfo
	err := m.QueryRowCtx(ctx, &resp, pcmScParticipantAvailInfoIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
		query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", scParticipantAvailInfoRows, 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 *defaultScParticipantAvailInfoModel) Insert(ctx context.Context, data *ScParticipantAvailInfo) (sql.Result, error) {
	pcmScParticipantAvailInfoIdKey := fmt.Sprintf("%s%v", cachePcmScParticipantAvailInfoIdPrefix, 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, scParticipantAvailInfoRowsExpectAutoSet)
		return conn.ExecCtx(ctx, query, data.Id, data.AvailStorageSpace, data.UserNum, data.PendingJobNum, data.RunningJobNum, data.ParticipantId, data.DeletedFlag, data.CreatedBy, data.CreatedTime, data.UpdatedBy, data.UpdatedTime)
	}, pcmScParticipantAvailInfoIdKey)
	return ret, err
}

func (m *defaultScParticipantAvailInfoModel) Update(ctx context.Context, data *ScParticipantAvailInfo) error {
	pcmScParticipantAvailInfoIdKey := fmt.Sprintf("%s%v", cachePcmScParticipantAvailInfoIdPrefix, 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, scParticipantAvailInfoRowsWithPlaceHolder)
		return conn.ExecCtx(ctx, query, data.AvailStorageSpace, data.UserNum, data.PendingJobNum, data.RunningJobNum, data.ParticipantId, data.DeletedFlag, data.CreatedBy, data.CreatedTime, data.UpdatedBy, data.UpdatedTime, data.Id)
	}, pcmScParticipantAvailInfoIdKey)
	return err
}

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

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

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