// Code generated by goctl. DO NOT EDIT.

package models

import (
	"context"
	"database/sql"
	"fmt"
	"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"
	"strings"
)

var (
	scTenantInfoFieldNames          = builder.RawFieldNames(&ScTenantInfo{})
	scTenantInfoRows                = strings.Join(scTenantInfoFieldNames, ",")
	scTenantInfoRowsExpectAutoSet   = strings.Join(stringx.Remove(scTenantInfoFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	scTenantInfoRowsWithPlaceHolder = strings.Join(stringx.Remove(scTenantInfoFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"

	cachePcmScTenantInfoIdPrefix = "cache:pcm:scTenantInfo:id:"
)

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

	defaultScTenantInfoModel struct {
		sqlc.CachedConn
		table string
	}

	ScTenantInfo struct {
		Id          int64  `db:"id"`           // id
		TenantName  string `db:"tenant_name"`  // 租户名称
		TenantDesc  string `db:"tenant_desc"`  // 描述信息
		Clusters    string `db:"clusters"`     // 集群名称，用","分割
		Type        int64  `db:"type"`         // 租户所属(0数算，1超算，2智算）
		DeletedFlag int64  `db:"deleted_flag"` // 是否删除
	}
)

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

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

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

func (m *defaultScTenantInfoModel) FindOne(ctx context.Context, id int64) (*ScTenantInfo, error) {
	pcmScTenantInfoIdKey := fmt.Sprintf("%s%v", cachePcmScTenantInfoIdPrefix, id)
	var resp ScTenantInfo
	err := m.QueryRowCtx(ctx, &resp, pcmScTenantInfoIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
		query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", scTenantInfoRows, 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 *defaultScTenantInfoModel) formatPrimary(primary any) string {
	return fmt.Sprintf("%s%v", cachePcmScTenantInfoIdPrefix, primary)
}

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

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