package org.jeecg.modules.workFlow.flowplus.workflow.controller;

import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.workFlow.flowplus.workflow.controller.req.*;
import org.jeecg.modules.workFlow.flowplus.workflow.service.WorkspaceService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 流程工作台相关接口
 *

 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/workflow/workspace")
public class WorkspaceController {

    private final WorkspaceService workspaceService;

    /**
     * 启动流程
     */
    @PostMapping("/process/start")
    public Result<?> startProcess(@RequestBody StartProcessReq startProcessReq) {
        try {
            workspaceService.startProcess(startProcessReq);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询流程信息
     */
    @GetMapping("/process/detail")
    public Result<?> processDetail(@RequestParam String templateId) {
        try{
            return Result.ok(workspaceService.processDetail(templateId));
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 流程过程预览
     */
    @PostMapping("/process/preview")
    public Result<?> processPreview(@RequestBody StartProcessReq params) {
        try{
            return Result.ok(workspaceService.processPreview(params));
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 待审批任务
     */
    @PostMapping("/process/todoTask")
    public Result<?> getTodoTask(@RequestBody ProcessListReq queryParams) {
        try{
            return Result.ok(workspaceService.getTodoTask(queryParams));
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 我发起的流程
     */
    @PostMapping("/process/applyList")
    public Result<?> getApplyList(@RequestBody ProcessListReq queryParams) {
        try{
            return Result.ok(workspaceService.getApplyList(queryParams));
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 抄送我的
     */
    @PostMapping("/process/ccList")
    public Result<?> getCcList(@RequestBody ProcessListReq queryParams) {
        try{
            return Result.ok(workspaceService.getCcList(queryParams));
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 已审批列表
     */
    @PostMapping("/process/doneList")
    public Result<?> getDoneList(@RequestBody ProcessListReq queryParams) {
        try{
           return Result.ok(workspaceService.getDoneList(queryParams));
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询实例表单数据
     */
    @GetMapping("/process/instanceForm")
    public Result<?> getInstanceForm(@RequestParam String instanceId) {
        try{
            return Result.ok(workspaceService.getInstanceForm(instanceId));
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询实例动作记录
     */
    @GetMapping("/process/instanceAction")
    public Result<?> getInstanceAction(@RequestParam String instanceId) {
        try{
            return Result.ok(workspaceService.getInstanceAction(instanceId));
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询当前节点信息
     */
    @GetMapping("/process/getNodeInfo")
    public Result<?> getNodeInfo(@RequestParam String processId, @RequestParam String nodeId) {
        try{
            return Result.ok(workspaceService.getNodeInfo(processId, nodeId));
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 同意
     */
    @PostMapping("/agree")
    public Result<?> agree(@RequestBody ProcessAgreeReq processAgreeReq) {
        try{
            workspaceService.agree(processAgreeReq);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 拒绝
     */
    @PostMapping("/refuse")
    public Result<?> refuse(@RequestBody ProcessRefuseReq processRefuseReq) {
        try{
            workspaceService.refuse(processRefuseReq);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 撤销流程
     */
    @PostMapping("/revoke")
    public Result<?> revoke(@RequestBody ProcessRevokeReq processRevokeReq) {
        try{
            workspaceService.revoke(processRevokeReq);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 评论
     */
    @PostMapping("/comment")
    public Result<?> comment(@RequestBody ProcessCommentReq params) {
        try{
            workspaceService.comment(params);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     *  再次提交被驳回到发起人的流程
     * @param req 请求参数
     * @return  Result
     */
    @PostMapping("/process/resubmit")
    public Result<?> resubmit(@RequestBody ResubmitProcessReq req) {
        try{
            workspaceService.resubmit(req);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }
}
