package cn.axzo.workflow.server.controller.web.bpmn;

import cn.axzo.workflow.client.feign.bpmn.ProcessJobApi;
import cn.axzo.workflow.core.common.exception.WorkflowEngineException;
import cn.axzo.workflow.core.service.BpmnProcessJobService;
import cn.axzo.workflow.server.common.annotation.ErrorReporter;
import cn.azxo.framework.common.model.CommonResponse;
import com.alibaba.nacos.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import static cn.axzo.workflow.core.common.code.AsyncJobRespCode.RESUME_JOB_REQUEST_PARAM_ERROR;
import static cn.azxo.framework.common.model.CommonResponse.success;

/**
 * 流程job控制器
 */
@Slf4j
@RequestMapping({"/web/v1/api/process/job", "/api/process/job"})
@RestController
@ErrorReporter
@Validated
public class BpmnProcessJobController implements ProcessJobApi {

    @Resource
    private BpmnProcessJobService bpmnProcessJobService;

    @Override
    @GetMapping("/dead-letter/resume")
    public CommonResponse<Void> executeDeadLetterJobAction(@RequestParam String jobId, @RequestParam String procInstId) {
        if (StringUtils.isBlank(jobId) && StringUtils.isBlank(procInstId)) {
            throw new WorkflowEngineException(RESUME_JOB_REQUEST_PARAM_ERROR);
        }
        if (StringUtils.isNotBlank(jobId)) {
            bpmnProcessJobService.executeDeadLetterJobActionByJobId(jobId);
        } else {
            bpmnProcessJobService.executeDeadLetterJobActionByProcInstId(procInstId);
        }
        return success();
    }
}
