// 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 (
	regionSlurmFieldNames          = builder.RawFieldNames(&RegionSlurm{})
	regionSlurmRows                = strings.Join(regionSlurmFieldNames, ",")
	regionSlurmRowsExpectAutoSet   = strings.Join(stringx.Remove(regionSlurmFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
	regionSlurmRowsWithPlaceHolder = strings.Join(stringx.Remove(regionSlurmFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
)

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

	defaultRegionSlurmModel struct {
		conn  sqlx.SqlConn
		table string
	}

	RegionSlurm struct {
		Id                  int64  `db:"id"`
		RegionName          string `db:"RegionName"`          // 域名
		SoftStack           string `db:"SoftStack"`           // 软件栈
		SlurmNum            int64  `db:"SlurmNum"`            // 超算域适配slurm数量
		AdaptorInterfaceSum int64  `db:"AdaptorInterfaceSum"` // 适配接口数量
	}
)

func newRegionSlurmModel(conn sqlx.SqlConn) *defaultRegionSlurmModel {
	return &defaultRegionSlurmModel{
		conn:  conn,
		table: "`region_slurm`",
	}
}

func (m *defaultRegionSlurmModel) 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 *defaultRegionSlurmModel) FindOne(ctx context.Context, id int64) (*RegionSlurm, error) {
	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", regionSlurmRows, m.table)
	var resp RegionSlurm
	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 *defaultRegionSlurmModel) Insert(ctx context.Context, data *RegionSlurm) (sql.Result, error) {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?)", m.table, regionSlurmRowsExpectAutoSet)
	ret, err := m.conn.ExecCtx(ctx, query, data.RegionName, data.SoftStack, data.SlurmNum, data.AdaptorInterfaceSum)
	return ret, err
}

func (m *defaultRegionSlurmModel) Update(ctx context.Context, data *RegionSlurm) error {
	query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, regionSlurmRowsWithPlaceHolder)
	_, err := m.conn.ExecCtx(ctx, query, data.RegionName, data.SoftStack, data.SlurmNum, data.AdaptorInterfaceSum, data.Id)
	return err
}

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