// 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 (
	domainResourceFieldNames          = builder.RawFieldNames(&DomainResource{})
	domainResourceRows                = strings.Join(domainResourceFieldNames, ",")
	domainResourceRowsExpectAutoSet   = strings.Join(stringx.Remove(domainResourceFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	domainResourceRowsWithPlaceHolder = strings.Join(stringx.Remove(domainResourceFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"

	cachePcmDomainResourceIdPrefix = "cache:pcm:domainResource:id:"
)

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

	defaultDomainResourceModel struct {
		sqlc.CachedConn
		table string
	}

	DomainResource struct {
		Id           int64     `db:"id"`            // id
		DomainId     string    `db:"domain_id"`     // 资源域id
		DomainName   string    `db:"domain_name"`   // 资源域名称
		JobCount     int64     `db:"job_count"`     // 资源域任务数量
		DomainSource int64     `db:"domain_source"` // 资源域数据来源：0-nudt，1-鹏城
		Stack        string    `db:"stack"`         // 技术栈
		ResourceType string    `db:"resource_type"` // 资源类型
		Cpu          string    `db:"cpu"`           // cpu
		Memory       string    `db:"memory"`        // 内存
		Disk         string    `db:"disk"`          // 存储
		NodeCount    string    `db:"node_count"`    // 节点数量
		CreateTime   time.Time `db:"create_time"`   // 数据创建时间
		UpdateTime   time.Time `db:"update_time"`   // 数据更新时间
		DeleteFlag   int64     `db:"delete_flag"`   // 是否删除 0:未删除，1:已经删除
		Description  string    `db:"description"`   //集群描述
		ClusterName  string    `db:"cluster_name"`  //集群名称
	}
)

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

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

func (m *defaultDomainResourceModel) FindOne(ctx context.Context, id int64) (*DomainResource, error) {
	pcmDomainResourceIdKey := fmt.Sprintf("%s%v", cachePcmDomainResourceIdPrefix, id)
	var resp DomainResource
	err := m.QueryRowCtx(ctx, &resp, pcmDomainResourceIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
		query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", domainResourceRows, 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 *defaultDomainResourceModel) Insert(ctx context.Context, data *DomainResource) (sql.Result, error) {
	pcmDomainResourceIdKey := fmt.Sprintf("%s%v", cachePcmDomainResourceIdPrefix, 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, domainResourceRowsExpectAutoSet)
		return conn.ExecCtx(ctx, query, data.DomainId, data.DomainName, data.JobCount, data.DomainSource, data.Stack, data.ResourceType, data.Cpu, data.Memory, data.Disk, data.DeleteFlag)
	}, pcmDomainResourceIdKey)
	return ret, err
}

func (m *defaultDomainResourceModel) Update(ctx context.Context, data *DomainResource) error {
	pcmDomainResourceIdKey := fmt.Sprintf("%s%v", cachePcmDomainResourceIdPrefix, 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, domainResourceRowsWithPlaceHolder)
		return conn.ExecCtx(ctx, query, data.DomainId, data.DomainName, data.JobCount, data.DomainSource, data.Stack, data.ResourceType, data.Cpu, data.Memory, data.Disk, data.DeleteFlag, data.Id)
	}, pcmDomainResourceIdKey)
	return err
}

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

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

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