// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1

package job

import (
	"context"

	jobusecase "haixun-backend/internal/model/job/domain/usecase"
	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"

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

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

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

func (l *FailWorkerJobLogic) FailWorkerJob(req *types.WorkerFailReq) (resp *types.JobData, err error) {
	run, err := l.svcCtx.Job.FailRun(l.ctx, jobusecase.FailRunRequest{
		JobID:    req.ID,
		WorkerID: req.WorkerID,
		Error:    req.Error,
		Phase:    req.Phase,
	})
	if err != nil {
		return nil, err
	}
	data := ToJobData(run)
	return &data, nil
}
