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

import cn.axzo.workflow.common.model.response.bpmn.process.ProcessNodeDetailVO;
import cn.axzo.workflow.core.service.BpmnProcessInstanceService;
import cn.axzo.workflow.core.service.support.FlowNodeForecastService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.common.engine.impl.util.IoUtil;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
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 java.io.InputStream;
import java.util.List;

/**
 * TODO
 *
 * @author wangli
 * @since 2023/10/10 13:59
 */
@Slf4j
@RequestMapping("/web/v1/api")
@RestController
@Validated
public class TestController {

    @Autowired
    private FlowNodeForecastService forecastService;
    @Autowired
    private BpmnProcessInstanceService instanceService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private RepositoryService repositoryService;

    @GetMapping("/test")
    public void test(@RequestParam String processInstanceId) {
        List<FlowElement> flowElements = forecastService.performProcessForecasting(processInstanceId, null);
        System.out.println("flowElements = " + flowElements);
    }

    @GetMapping("/test2")
    public void test2(@RequestParam String processInstanceId) {
        List<ProcessNodeDetailVO> detailVOS = instanceService.getProcessInstanceNodeForecast(processInstanceId, "296");
        System.out.println("detailVOS = " + detailVOS);
    }

    /**
     * 操作 act_ge_bytearray
     *
     * @param processInstanceId
     * @return
     */
    @GetMapping("/test3")
    public String test3(@RequestParam String processInstanceId) {
        Deployment deploy =
                repositoryService.createDeployment().addBytes("自定义资源", processInstanceId.getBytes()).deploy();
        InputStream is = repositoryService.getResourceAsStream(deploy.getId(), "自定义资源");
        byte[] bytes = IoUtil.readInputStream(is, "自定义资源");
        return new String(bytes);
    }

}
