/*

 Copyright (c) [2023] [pcm]
 [pcm-coordinator] is licensed under Mulan PSL v2.
 You can use this software according to the terms and conditions of the Mulan PSL v2.
 You may obtain a copy of Mulan PSL v2 at:
          http://license.coscl.org.cn/MulanPSL2
 THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 See the Mulan PSL v2 for more details.

*/

package models

import (
	"context"
	"database/sql"
	"github.com/Masterminds/squirrel"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlc"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

type (
	centerOverviewModel interface {
		FindAll(ctx context.Context) (*[]CenterOverview, error)
	}

	defaultCenterOverviewModel struct {
		sqlc.CachedConn
		table string
	}

	CenterOverview struct {
		Id               sql.NullInt64   `json:"id"`
		CenterSource     sql.NullString  `json:"centerSource"`
		SourceId         sql.NullString  `json:"sourceId"`
		Name             sql.NullString  `json:"name"`
		Description      sql.NullString  `json:"description"`
		Type             sql.NullString  `json:"type"`
		Area             sql.NullString  `json:"area"`
		City             sql.NullString  `json:"city"`
		Longitude        sql.NullFloat64 `json:"longitude"`
		Latitude         sql.NullFloat64 `json:"latitude"`
		Status           sql.NullString  `json:"status"`
		UserNum          sql.NullInt64   `json:"userNum"`
		DeletedFlag      sql.NullInt64   `json:"deletedFlag"`
		CloudClusterNum  sql.NullInt64   `json:"cloudClusterNum"`
		CloudNodeNum     sql.NullInt64   `json:"cloudNodeNum"`
		CloudCpuNum      sql.NullInt64   `json:"cloudCpuNum"`
		CloudGpuNum      sql.NullInt64   `json:"cloudGpuNum"`
		CloudMngFlops    sql.NullInt64   `json:"cloudMngFlops"`
		CloudUmngFlops   sql.NullInt64   `json:"cloudUmngFlops"`
		CloudMngStorage  sql.NullInt64   `json:"CloudMngStorage"`
		CloudUmngStorage sql.NullInt64   `json:"CloudUmngStorage"`
		AiClusterNum     sql.NullInt64   `json:"aiClusterNum"`
		AiNodeNum        sql.NullInt64   `json:"aiNodeNum"`
		AiCpuNum         sql.NullInt64   `json:"aiCpuNum"`
		AiGpuNum         sql.NullInt64   `json:"aiGpuNum"`
		AiMngFlops       sql.NullInt64   `json:"aiMngFlops"`
		AiUmngFlops      sql.NullInt64   `json:"aiUmngFlops"`
		AiMngStorage     sql.NullInt64   `json:"aiMngStorage"`
		AiUmngStorage    sql.NullInt64   `json:"aiUmngStorage"`
		HpcClusterNum    sql.NullInt64   `json:"hpcClusterNum"`
		HpcNodeNum       sql.NullInt64   `json:"hpcNodeNum"`
		HpcCpuNum        sql.NullInt64   `json:"hpcCpuNum"`
		HpcGpuNum        sql.NullInt64   `json:"hpcGpuNum"`
		HpcMngFlops      sql.NullInt64   `json:"hpcMngFlops"`
		HpcUmngFlops     sql.NullInt64   `json:"hpcUmngFlops"`
		HpcMngStorage    sql.NullInt64   `json:"hpcMngStorage"`
		HpcUmngStorage   sql.NullInt64   `json:"hpcUmngStorage"`
		Edwc             sql.NullBool    `json:"edwc"`
		Ydyl             sql.NullBool    `json:"ydyl"`
		HubCode          sql.NullInt64   `json:"hubCode"`
	}
)

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

func (m *defaultCenterOverviewModel) FindAll(ctx context.Context) (*[]CenterOverview, error) {
	var centers []CenterOverview

	centerSelect := squirrel.Select(`cc.*`, `ac.cluster_num`, `ac.node_num`, `ac.cpu_num`, `ac.gpu_num`, `ac.managed_flops`, `ac.unmanaged_flops`, `ac.managed_storage`, `ac.unmanaged_storage`, `hc.cluster_num`, `c.node_num`, `hc.cpu_num`, `hc.gpu_num`, `hc.managed_flops`, `hc.unmanaged_flops`, `hc.managed_storage`, `hc.unmanaged_storage`, `c.cluster_num`, `c.node_num`, `c.cpu_num`, `c.gpu_num`, `c.managed_flops`, `c.unmanaged_flops`, `c.managed_storage`, `c.unmanaged_storage`, `ct.edwc`, `ct.ydyl`).
		From("compute_center cc").
		LeftJoin(`ai_center ac on cc.id = ac.id`).
		LeftJoin(`hpc_center hc on cc.id = hc.id`).
		LeftJoin(`cloud_center c on cc.id = c.id`).
		LeftJoin(`center_tag ct on cc.id = ct.id`).
		Where(`cc.deleted_flag = 0`)
	query, values, _ := centerSelect.ToSql()

	err := m.QueryRowsNoCacheCtx(ctx, &centers, query, values...)
	if err != nil {
		return nil, err
	}

	return &centers, nil

}
