package apps

import (
	"context"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"

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

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

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

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

func (l *PauseAppByAppNameLogic) PauseAppByAppName(req *types.DeleteAppReq) (resp *types.AppResp, err error) {
	resp = &types.AppResp{}
	var task = &Task{}
	//查询应用的yamlString
	l.svcCtx.DbEngin.Raw(`select * from task where ns_id= ? and name= ? AND deleted_at IS NULL`, req.NsID, req.Name).Scan(&task)
	if task.Id == 0 {
		resp.Code = 500
		resp.Msg = "App not fount"
		return resp, err
	}
	// 将子任务状态修改为待暂停
	l.svcCtx.DbEngin.Model(&models.Cloud{}).Where("task_id", task.Id).Update("status", constants.WaitPause)
	return
}
