// 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/sqlc"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"github.com/zeromicro/go-zero/core/stringx"
)

var (
	computeClusterFieldNames          = builder.RawFieldNames(&ComputeCluster{})
	computeClusterRows                = strings.Join(computeClusterFieldNames, ",")
	computeClusterRowsExpectAutoSet   = strings.Join(stringx.Remove(computeClusterFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	computeClusterRowsWithPlaceHolder = strings.Join(stringx.Remove(computeClusterFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
)

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

	defaultComputeClusterModel struct {
		conn  sqlx.SqlConn
		table string
	}

	ComputeCluster struct {
		Id             int64           `db:"id"`               // 集群id
		Name           sql.NullString  `db:"name"`             // 集群名
		Type           sql.NullString  `db:"type"`             // 集群类型
		CenterId       sql.NullInt64   `db:"center_id"`        // 数据中心id
		CenterName     sql.NullString  `db:"center_name"`      // 数据中心名称
		JcceDomainId   sql.NullInt64   `db:"jcce_domain_id"`   // JCCE侧域ID
		JcceDomainName sql.NullString  `db:"jcce_domain_name"` // JCCE侧域名
		Longitude      sql.NullFloat64 `db:"longitude"`        // 经度
		Latitude       sql.NullFloat64 `db:"latitude"`         // 纬度
		Description    sql.NullString  `db:"description"`      // 描述
	}
)

func newComputeClusterModel(conn sqlx.SqlConn) *defaultComputeClusterModel {
	return &defaultComputeClusterModel{
		conn:  conn,
		table: "`compute_cluster`",
	}
}

func (m *defaultComputeClusterModel) Delete(ctx context.Context, id int64) error {
	query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
	_, err := m.conn.ExecCtx(ctx, query, id)
	return err
}

func (m *defaultComputeClusterModel) FindOne(ctx context.Context, id int64) (*ComputeCluster, error) {
	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", computeClusterRows, m.table)
	var resp ComputeCluster
	err := m.conn.QueryRowCtx(ctx, &resp, query, id)
	switch err {
	case nil:
		return &resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

func (m *defaultComputeClusterModel) Insert(ctx context.Context, data *ComputeCluster) (sql.Result, error) {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, computeClusterRowsExpectAutoSet)
	ret, err := m.conn.ExecCtx(ctx, query, data.Name, data.Type, data.CenterId, data.CenterName, data.JcceDomainId, data.JcceDomainName, data.Longitude, data.Latitude, data.Description)
	return ret, err
}

func (m *defaultComputeClusterModel) Update(ctx context.Context, data *ComputeCluster) error {
	query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, computeClusterRowsWithPlaceHolder)
	_, err := m.conn.ExecCtx(ctx, query, data.Name, data.Type, data.CenterId, data.CenterName, data.JcceDomainId, data.JcceDomainName, data.Longitude, data.Latitude, data.Description, data.Id)
	return err
}

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