package cn.fateverse.workflow.controller;

import cn.fateverse.common.core.result.Result;
import cn.fateverse.common.core.result.page.TableDataInfo;
import cn.fateverse.workflow.entity.vo.ProcessInstanceDetailVo;
import cn.fateverse.workflow.entity.dto.ProcessInstanceDto;
import cn.fateverse.workflow.entity.vo.ProcessInstanceVo;
import cn.fateverse.workflow.service.ProcessInstanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 工作流流程实例
 *
 * @author Clay
 * @date 2022/12/24
 */
@Api(value = "工作流流程实例" , tags = "工作流流程实例")
@RestController
@RequestMapping("/process/instance")
public class ProcessInstanceController {

    private final ProcessInstanceService processInstanceService;

    public ProcessInstanceController(ProcessInstanceService processInstanceService) {
        this.processInstanceService = processInstanceService;
    }


    @ApiOperation("工作流流程查询")
    @GetMapping
    @PreAuthorize("@ss.hasPermission('workflow:instance:list')")
    public Result<TableDataInfo<ProcessInstanceVo>> list(String state) {
        TableDataInfo<ProcessInstanceVo> dataInfo = processInstanceService.searchList(state);
        return Result.ok(dataInfo);
    }


    @ApiOperation("工作流流程查询")
    @GetMapping("/self")
    public Result<TableDataInfo<ProcessInstanceVo>> listSelf(String state, String departmentName) {
        TableDataInfo<ProcessInstanceVo> dataInfo = processInstanceService.searchSelfList(state, departmentName);
        return Result.ok(dataInfo);
    }

    @ApiOperation("工作流流程查询")
    @GetMapping("/about")
    public Result<TableDataInfo<ProcessInstanceVo>> aboutMeList(String state, String deploymentName) {
        TableDataInfo<ProcessInstanceVo> dataInfo = processInstanceService.searchAboutMeList(state, deploymentName);
        return Result.ok(dataInfo);
    }

    @ApiOperation("工作流流程查询")
    @GetMapping("/info/{processInstanceId}")
    public Result<ProcessInstanceDetailVo> selfInfo(@PathVariable String processInstanceId) {
        ProcessInstanceDetailVo detailVo = processInstanceService.searchSelfInfo(processInstanceId);
        return Result.ok(detailVo);
    }


    @ApiOperation("以现有流程数据为基础,重新启动流程(获取数据信息)")
    @GetMapping("/re/info/{instanceId}")
    public Result<ProcessInstanceDetailVo> reInfo(@PathVariable String instanceId) {
        ProcessInstanceDetailVo detailVo = processInstanceService.searchReInfo(instanceId);
        return Result.ok(detailVo);
    }


    @ApiOperation("开始工作流实例")
    @PostMapping("/start")
    public Result<Void> start(@RequestBody @Validated ProcessInstanceDto processInstanceDto) {
        processInstanceService.start(processInstanceDto);
        return Result.ok("流程启动成功");
    }


    @ApiOperation("重新工作流实例")
    @PostMapping("/restart")
    public Result<Void> restart(@RequestBody @Validated ProcessInstanceDto processInstanceDto) {
        processInstanceService.restart(processInstanceDto);
        return Result.ok("流程启动成功");
    }


}
