package com.njcn.process.controller.flowable;

import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.process.service.flowable.IFlowDefinitionService;
import com.njcn.process.service.flowable.IFlowTaskService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.FlowableObjectNotFoundException;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

/**
 * pqs
 * 工作流
 * @author cdf
 * @date 2023/4/10
 */
@Slf4j
@Api(tags = "工作流")
@RestController
@RequestMapping("/flowable/definition")
@RequiredArgsConstructor
public class FlowDefinitionController extends BaseController {


    private final IFlowDefinitionService flowDefinitionService;

    private final RepositoryService repositoryService;

    private final RuntimeService runtimeService;

    private final TaskService taskService;

    private final HistoryService historyService;


    private final IFlowTaskService flowTaskService;



    @GetMapping("deployment")
    @ApiOperation(value = "工作流_部署流程")
    public void createDeployment() {
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("gaojing.bpmn20.xml")
                .name("技术监督告警流程").category("gaojing")
                .deploy();

        ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
        repositoryService.setProcessDefinitionCategory(definition.getId(), "gaojing");
        System.out.println(deployment.getId());
    }


    @ApiOperation(value = "工作流_发起流程")
    @PostMapping("/start")
    public HttpResult<String> start(@ApiParam(value = "流程定义id") @RequestParam(value = "procDefId") String procDefId,
                                    @ApiParam(value = "监督单id") @RequestParam(value = "thsIndex") String thsIndex,
                            @ApiParam(value = "变量集合,json对象") @RequestBody Map<String, Object> variables) {
        String methodDescribe = getMethodDescribe("start");
        String res = flowDefinitionService.startProcessInstanceById(procDefId,thsIndex, variables);
        if(Objects.nonNull(res)){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, res, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
    }


    @ApiOperation(value = "工作流_定义删除")
    @DeleteMapping(value = "delete/{deployIds}")
    public HttpResult<Object> delete(@PathVariable String[] deployIds) {
        String methodDescribe = getMethodDescribe("delete");
        for (String deployId : deployIds) {
            flowDefinitionService.delete(deployId);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }
}
