/*

 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 core

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
)

type ListCenterLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewListCenterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListCenterLogic {
	return &ListCenterLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ListCenterLogic) ListCenter() (*types.ListCenterResp, error) {
	var centers []types.Center
	var centersModel *[]models.CenterOverview

	//var centersModel []models.ComputeCenter
	var resp types.ListCenterResp

	l.svcCtx.DbEngin.Raw("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\nfrom compute_center cc\nleft join ai_center ac on cc.id = ac.id\nleft join hpc_center hc on cc.id = hc.id\nleft join cloud_center c on cc.id = c.id\nleft join center_tag ct on cc.id = ct.id").Scan(&centersModel)

	var centerModelV = *centersModel

	for _, overview := range centerModelV {
		var center types.Center
		center.Id = overview.Id.Int64
		center.CenterSource = overview.CenterSource.String
		center.SourceId = overview.SourceId.String
		center.Name = overview.Name.String
		center.Description = overview.Description.String
		center.Type = overview.Type.String
		center.Area = overview.Area.String
		center.City = overview.City.String
		center.Longitude = overview.Longitude.Float64
		center.Latitude = overview.Latitude.Float64
		center.Status = overview.Status.String
		center.UserNum = overview.UserNum.Int64
		center.CloudClusterNum = overview.CloudClusterNum.Int64
		center.AiClusterNum = overview.AiClusterNum.Int64
		center.HpcClusterNum = overview.HpcClusterNum.Int64
		center.CloudNodeNum = overview.CloudNodeNum.Int64
		center.AiNodeNum = overview.AiNodeNum.Int64
		center.HpcNodeNum = overview.HpcNodeNum.Int64
		center.CloudCpuNum = overview.CloudCpuNum.Int64
		center.AiCpuNum = overview.AiCpuNum.Int64
		center.HpcCpuNum = overview.HpcCpuNum.Int64
		center.CloudGpuNum = overview.CloudGpuNum.Int64
		center.AiGpuNum = overview.AiGpuNum.Int64
		center.HpcGpuNum = overview.HpcGpuNum.Int64
		center.CloudMngFlops = overview.CloudMngFlops.Int64
		center.AiMngFlops = overview.AiMngFlops.Int64
		center.HpcMngFlops = overview.HpcMngFlops.Int64
		center.CloudUmngFlops = overview.CloudUmngFlops.Int64
		center.AiUmngFlops = overview.AiUmngFlops.Int64
		center.HpcUmngFlops = overview.HpcUmngFlops.Int64
		center.CloudMngStorage = overview.CloudMngStorage.Int64
		center.AiMngStorage = overview.AiMngStorage.Int64
		center.HpcMngStorage = overview.HpcMngStorage.Int64
		center.CloudUmngStorage = overview.CloudUmngStorage.Int64
		center.AiUmngStorage = overview.AiUmngStorage.Int64
		center.HpcUmngStorage = overview.HpcUmngStorage.Int64
		center.Edwc = overview.Edwc.Bool
		center.Ydyl = overview.Ydyl.Bool
		center.HubCode = overview.HubCode.Int64
		center.DeletedFlag = overview.DeletedFlag.Int64
		centers = append(centers, center)
	}

	resp.Code = 200
	resp.Msg = "success"
	resp.Data.TotalCount = len(centers)
	resp.Data.Centers = centers
	return &resp, nil
}
