package adapters

import (
	"context"

	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"

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

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

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

func (l *GetClusterSumLogic) GetClusterSum(req *types.ClusterSumReq) (resp *types.ClusterSumReqResp, err error) {
	// todo: add your logic here and delete this line
	resp = &types.ClusterSumReqResp{}

	var AdapterSum int //
	var podSum int     //
	var vmSum int      //
	var TaskSum int    //
	//
	sqlStr := "SELECT COUNT(*) FROM `t_adapter` t where t.type ='0' and deleted_at is null"
	tx := l.svcCtx.DbEngin.Raw(sqlStr).Scan(&AdapterSum)
	if tx.Error != nil {
		logx.Error(err)
		return nil, tx.Error
	}
	//vm
	sqlStrVm := "SELECT COUNT(*) FROM `t_adapter` t  left join  t_cluster  tc on t.id=tc.adapter_id where t.type = '0' and t.resource_type='02' and tc.deleted_at is null"
	txClusterVm := l.svcCtx.DbEngin.Raw(sqlStrVm).Scan(&vmSum)
	if txClusterVm.Error != nil {
		logx.Error(err)
		return nil, txClusterVm.Error
	}
	//pod
	sqlStrPod := "SELECT COUNT(*) FROM `t_adapter` t  left join  t_cluster  tc on t.id=tc.adapter_id where t.type = '0' and t.resource_type='02' and tc.deleted_at is null"
	txClusterPod := l.svcCtx.DbEngin.Raw(sqlStrPod).Scan(&podSum)
	if txClusterPod.Error != nil {
		logx.Error(err)
		return nil, txClusterPod.Error
	}
	//
	sqlStrTask := "SELECT COUNT(*) FROM `task` where adapter_type_dict = '0'"
	txTask := l.svcCtx.DbEngin.Raw(sqlStrTask).Scan(&TaskSum)
	if txTask.Error != nil {
		logx.Error(err)
		return nil, txTask.Error
	}
	resp.TaskSum = TaskSum
	resp.VmSum = vmSum
	resp.PodSum = podSum
	resp.AdapterSum = AdapterSum
	return resp, nil
}
