// 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 (
	scNodePhyInfoFieldNames          = builder.RawFieldNames(&ScNodePhyInfo{})
	scNodePhyInfoRows                = strings.Join(scNodePhyInfoFieldNames, ",")
	scNodePhyInfoRowsExpectAutoSet   = strings.Join(stringx.Remove(scNodePhyInfoFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	scNodePhyInfoRowsWithPlaceHolder = strings.Join(stringx.Remove(scNodePhyInfoFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"

	cachePcmScNodePhyInfoIdPrefix = "cache:pcm:scNodePhyInfo:id:"
)

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

	defaultScNodePhyInfoModel struct {
		sqlc.CachedConn
		table string
	}

	ScNodePhyInfo struct {
		Id            int64         `db:"id"`             // id
		NodeName      string        `db:"node_name"`      // 节点名称
		OsName        string        `db:"os_name"`        // 系统名称
		OsVersion     int64         `db:"os_version"`     // 系统版本
		ArchType      string        `db:"arch_type"`      // 架构类型
		ArchName      string        `db:"arch_name"`      // 架构名称
		ArchFreq      string        `db:"arch_freq"`      // 架构频率
		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 newScNodePhyInfoModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultScNodePhyInfoModel {
	return &defaultScNodePhyInfoModel{
		CachedConn: sqlc.NewConn(conn, c, opts...),
		table:      "`sc_node_phy_info`",
	}
}

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

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

func (m *defaultScNodePhyInfoModel) FindOne(ctx context.Context, id int64) (*ScNodePhyInfo, error) {
	pcmScNodePhyInfoIdKey := fmt.Sprintf("%s%v", cachePcmScNodePhyInfoIdPrefix, id)
	var resp ScNodePhyInfo
	err := m.QueryRowCtx(ctx, &resp, pcmScNodePhyInfoIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
		query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", scNodePhyInfoRows, 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 *defaultScNodePhyInfoModel) Insert(ctx context.Context, data *ScNodePhyInfo) (sql.Result, error) {
	pcmScNodePhyInfoIdKey := fmt.Sprintf("%s%v", cachePcmScNodePhyInfoIdPrefix, 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, scNodePhyInfoRowsExpectAutoSet)
		return conn.ExecCtx(ctx, query, data.Id, data.NodeName, data.OsName, data.OsVersion, data.ArchType, data.ArchName, data.ArchFreq, data.ParticipantId, data.DeletedFlag, data.CreatedBy, data.CreatedTime, data.UpdatedBy, data.UpdatedTime)
	}, pcmScNodePhyInfoIdKey)
	return ret, err
}

func (m *defaultScNodePhyInfoModel) Update(ctx context.Context, data *ScNodePhyInfo) error {
	pcmScNodePhyInfoIdKey := fmt.Sprintf("%s%v", cachePcmScNodePhyInfoIdPrefix, 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, scNodePhyInfoRowsWithPlaceHolder)
		return conn.ExecCtx(ctx, query, data.NodeName, data.OsName, data.OsVersion, data.ArchType, data.ArchName, data.ArchFreq, data.ParticipantId, data.DeletedFlag, data.CreatedBy, data.CreatedTime, data.UpdatedBy, data.UpdatedTime, data.Id)
	}, pcmScNodePhyInfoIdKey)
	return err
}

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

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

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