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.pojo.vo.flowable.FlowTaskVo;
import com.njcn.process.service.flowable.IFlowInstanceService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>工作流流程实例管理<p>
 *
 * @author Tony
 * @date 2021-04-03
 */
@Slf4j
@Api(tags = "工作流流程实例管理")
@RestController
@RequestMapping("/flowable/instance")
public class FlowInstanceController extends BaseController {

    @Autowired
    private IFlowInstanceService flowInstanceService;

    @ApiOperation(value = "根据流程定义id启动流程实例")
    @PostMapping("/startBy/{procDefId}")
    public HttpResult startById(@ApiParam(value = "流程定义id") @PathVariable(value = "procDefId") String procDefId,
                                @ApiParam(value = "变量集合,json对象") @RequestBody Map<String, Object> variables) {
        String methodDescribe = getMethodDescribe("startById");
        String result = flowInstanceService.startProcessInstanceById(procDefId, variables);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);


    }


    @ApiOperation(value = "激活或挂起流程实例")
    @PostMapping(value = "/updateState")
    public HttpResult updateState(@ApiParam(value = "1:激活,2:挂起", required = true) @RequestParam Integer state,
                                  @ApiParam(value = "流程实例ID", required = true) @RequestParam String instanceId) {
        String methodDescribe = getMethodDescribe("updateState");

        flowInstanceService.updateState(state,instanceId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);

    }

    @ApiOperation("结束流程实例")
    @PostMapping(value = "/stopProcessInstance")
    public HttpResult stopProcessInstance(@RequestBody FlowTaskVo flowTaskVo) {

        String methodDescribe = getMethodDescribe("stopProcessInstance");

        flowInstanceService.stopProcessInstance(flowTaskVo);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);

    }

    @ApiOperation(value = "删除流程实例")
    @GetMapping(value = "/delete")
    public HttpResult delete(@ApiParam(value = "流程实例ID", required = true) @RequestParam String instanceIds) {
        String methodDescribe = getMethodDescribe("delete");
        flowInstanceService.delete(instanceIds,"测试删除");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);

    }
}
