// Code generated by goctl. DO NOT EDIT.

package models

import (
	"context"
	"database/sql"
	"fmt"
	"strings"

	"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 (
	computeCenterFieldNames          = builder.RawFieldNames(&ComputeCenter{})
	computeCenterRows                = strings.Join(computeCenterFieldNames, ",")
	computeCenterRowsExpectAutoSet   = strings.Join(stringx.Remove(computeCenterFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	computeCenterRowsWithPlaceHolder = strings.Join(stringx.Remove(computeCenterFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"

	cachePcmComputeCenterIdPrefix = "cache:pcm:computeCenter:id:"
)

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

	defaultComputeCenterModel struct {
		sqlc.CachedConn
		table string
	}

	ComputeCenter struct {
		Id           int64           `db:"id"`            // 平台唯一id
		CenterSource sql.NullString  `db:"center_source"` // 中心来源(鹏城,nudt,公有云等)
		SourceId     sql.NullString  `db:"source_id"`     // 数据来源原id
		Name         sql.NullString  `db:"name"`          // 中心名称
		Description  sql.NullString  `db:"description"`   // 详细描述
		Type         sql.NullString  `db:"type"`          // 中心类型(云算、智算 or 超算)
		Area         sql.NullString  `db:"area"`          // 资源区域
		City         sql.NullString  `db:"city"`          // 所在城市
		Longitude    sql.NullFloat64 `db:"longitude"`     // 经度
		Latitude     sql.NullFloat64 `db:"latitude"`      // 纬度
		Status       sql.NullString  `db:"status"`        // 接入状态
		UserNum      sql.NullInt64   `db:"user_num"`      // 用户数量
		DeletedFlag  sql.NullInt64   `db:"deleted_flag"`  // 是否删除（0-否，1-是）
	}
)

func newComputeCenterModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultComputeCenterModel {
	return &defaultComputeCenterModel{
		CachedConn: sqlc.NewConn(conn, c),
		table:      "`compute_center`",
	}
}

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

func (m *defaultComputeCenterModel) FindOne(ctx context.Context, id int64) (*ComputeCenter, error) {
	pcmComputeCenterIdKey := fmt.Sprintf("%s%v", cachePcmComputeCenterIdPrefix, id)
	var resp ComputeCenter
	err := m.QueryRowCtx(ctx, &resp, pcmComputeCenterIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
		query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", computeCenterRows, 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 *defaultComputeCenterModel) Insert(ctx context.Context, data *ComputeCenter) (sql.Result, error) {
	pcmComputeCenterIdKey := fmt.Sprintf("%s%v", cachePcmComputeCenterIdPrefix, 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, computeCenterRowsExpectAutoSet)
		return conn.ExecCtx(ctx, query, data.Id, data.CenterSource, data.SourceId, data.Name, data.Description, data.Type, data.Area, data.City, data.Longitude, data.Latitude, data.Status, data.UserNum, data.DeletedFlag)
	}, pcmComputeCenterIdKey)
	return ret, err
}

func (m *defaultComputeCenterModel) Update(ctx context.Context, data *ComputeCenter) error {
	pcmComputeCenterIdKey := fmt.Sprintf("%s%v", cachePcmComputeCenterIdPrefix, 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, computeCenterRowsWithPlaceHolder)
		return conn.ExecCtx(ctx, query, data.CenterSource, data.SourceId, data.Name, data.Description, data.Type, data.Area, data.City, data.Longitude, data.Latitude, data.Status, data.UserNum, data.DeletedFlag, data.Id)
	}, pcmComputeCenterIdKey)
	return err
}

func (m *defaultComputeCenterModel) formatPrimary(primary interface{}) string {
	return fmt.Sprintf("%s%v", cachePcmComputeCenterIdPrefix, primary)
}

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

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