package hpc

import (
	"context"
	"github.com/rs/zerolog/log"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

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

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

type ClusterInfo struct {
	Id       string `json:"id"`
	Name     string `json:"name"`
	Nickname string `json:"nickname"`
	Region   string `json:"region"`
}

func (l *GetHpcAppClusterLogic) GetHpcAppCluster(req *types.HpcAppClusterReq) (resp *types.ListResult, err error) {
	resp = &types.ListResult{}
	var clusterIds []string
	err = l.svcCtx.DbEngin.Table("hpc_app_template").Distinct("cluster_id").
		Where(" app = ? and status = 1 and deleted_at is null", req.App).
		Find(&clusterIds).Error
	if err != nil {
		log.Error().Msgf("获取HPC应用集群失败: %v", err)
		return nil, err
	}

	var listCluster []ClusterInfo
	err = l.svcCtx.DbEngin.Table("t_cluster").Where("id in (?)", clusterIds).Scan(&listCluster).Error
	if err != nil {
		log.Error().Msgf("获取集群信息失败: %v", err)
		return nil, err
	}
	resp.List = listCluster
	return
}
