// 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 (
	storelinkCenterFieldNames          = builder.RawFieldNames(&StorelinkCenter{})
	storelinkCenterRows                = strings.Join(storelinkCenterFieldNames, ",")
	storelinkCenterRowsExpectAutoSet   = strings.Join(stringx.Remove(storelinkCenterFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	storelinkCenterRowsWithPlaceHolder = strings.Join(stringx.Remove(storelinkCenterFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"

	cachePcmStorelinkCenterIdPrefix = "cache:pcm:storelinkCenter:id:"
)

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

	defaultStorelinkCenterModel struct {
		sqlc.CachedConn
		table string
	}

	StorelinkCenter struct {
		Id          int64         `db:"id"`           // id
		Name        string        `db:"name"`         // 名称
		Address     string        `db:"address"`      // 集群p端地址
		RpcAddress  string        `db:"rpc_address"`  // rpc服务链接地址
		Type        string        `db:"type"`         // 类型:1-章鱼；2-modelarts;3-曙光AI
		DeletedFlag int64         `db:"deleted_flag"` // 是否删除
		CreatedBy   sql.NullInt64 `db:"created_by"`   // 创建人
		CreatedTime time.Time     `db:"created_time"` // 创建时间
		UpdatedBy   sql.NullInt64 `db:"updated_by"`   // 更新人
		UpdatedTime time.Time     `db:"updated_time"` // 更新时间
	}
)

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

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

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

func (m *defaultStorelinkCenterModel) FindOne(ctx context.Context, id int64) (*StorelinkCenter, error) {
	pcmStorelinkCenterIdKey := fmt.Sprintf("%s%v", cachePcmStorelinkCenterIdPrefix, id)
	var resp StorelinkCenter
	err := m.QueryRowCtx(ctx, &resp, pcmStorelinkCenterIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
		query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", storelinkCenterRows, 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 *defaultStorelinkCenterModel) Insert(ctx context.Context, data *StorelinkCenter) (sql.Result, error) {
	pcmStorelinkCenterIdKey := fmt.Sprintf("%s%v", cachePcmStorelinkCenterIdPrefix, 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, storelinkCenterRowsExpectAutoSet)
		return conn.ExecCtx(ctx, query, data.Id, data.Name, data.Address, data.RpcAddress, data.Type, data.DeletedFlag, data.CreatedBy, data.CreatedTime, data.UpdatedBy, data.UpdatedTime)
	}, pcmStorelinkCenterIdKey)
	return ret, err
}

func (m *defaultStorelinkCenterModel) Update(ctx context.Context, data *StorelinkCenter) error {
	pcmStorelinkCenterIdKey := fmt.Sprintf("%s%v", cachePcmStorelinkCenterIdPrefix, 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, storelinkCenterRowsWithPlaceHolder)
		return conn.ExecCtx(ctx, query, data.Name, data.Address, data.RpcAddress, data.Type, data.DeletedFlag, data.CreatedBy, data.CreatedTime, data.UpdatedBy, data.UpdatedTime, data.Id)
	}, pcmStorelinkCenterIdKey)
	return err
}

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

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

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