package hpc

import (
	"github.com/zeromicro/go-zero/rest/httpx"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/logic/hpc"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
	"net/http"
	"strconv"
)

func CommitHpcTaskHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.CommitHpcTaskReq
		if err := httpx.Parse(r, &req); err != nil {
			result.ParamErrorResult(r, w, err)
			return
		}
		// 获取ip信息
		ip := utils.GetClientIP(r)
		req.Parameters["UserIp"] = ip
		// 获取token信息
		token := r.Header.Get("Authorization")
		req.Parameters["Token"] = token
		// 获取用户信息
		jccUserInfo, err := utils.ParseTokenWithoutVerify(token)
		if err != nil {
			result.ParamErrorResult(r, w, err)
			return
		}
		req.Parameters["UserId"] = strconv.FormatInt(jccUserInfo.Id, 10)

		l := hpc.NewCommitHpcTaskLogic(r.Context(), svcCtx)
		resp, err := l.CommitHpcTask(&req)
		result.HttpResult(r, w, resp, err)
	}
}
