package com.activiti6.controller.activiti;


import com.activiti6.domain.*;

import com.activiti6.service.activiti.ProcessInstanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/processInstance")
public class ProcessInstanceController extends BaseController {
    @Autowired
    private ProcessInstanceService processInstanceService;

    @GetMapping("list")
    public TableDataInfo list() {
        TableDataInfo list = processInstanceService.list();
        return list;
    }

    @GetMapping("listTask")
    public AjaxResult listTask(String processInstanceId) {
        List<TreeVo> list = processInstanceService.listTask(processInstanceId);
        return AjaxResult.success(list);
    }

    @GetMapping("listUser")
    public AjaxResult listUser() {
        List<TreeVo> list = processInstanceService.listUser();
        return AjaxResult.success(list);
    }

    /**
     * 提交流程
     */
    @PostMapping(value = "/commit")
    public R<String> commit(@RequestBody ProcessInstanceCommitFrom processInstanceCommitFrom) {
        return R.ok(processInstanceService.commit(processInstanceCommitFrom));
    }

    /**
     * 重新提交流程
     */
    @PostMapping(value = "/recommit")
    public R<String> recommit(@RequestBody ProcessInstanceCommitFrom processInstanceCommitFrom) {
        return R.ok(processInstanceService.recommit(processInstanceCommitFrom));
    }

    /**
     * 提交流程
     */
    @PostMapping(value = "/task/check")
    public R<Map<String, String>> check(@RequestBody ProcessInstanceCheckFrom processInstanceCommitFrom) {
        return R.ok(processInstanceService.check(processInstanceCommitFrom));
    }


    /**
     * 挂起流程实例
     *
     * @param processInstanceId
     * @return
     */
    @PostMapping("suspend")
    public AjaxResult suspendProcess(String processInstanceId, Integer status) {
        processInstanceService.suspendProcess(processInstanceId, status);
        return AjaxResult.success();
    }

    /**
     * 转办流程
     *
     * @param taskId
     * @param userId
     * @return
     */
    @PostMapping("transToOffice")
    public AjaxResult transToOffice(String taskId, String userId) {
        processInstanceService.transToOffice(taskId, userId);
        return AjaxResult.success();
    }

    /**
     * 委托
     *
     * @param taskId
     * @param userId
     * @return
     */
    @PostMapping("entrust/{taskId}/{userId}")
    public AjaxResult entrust(@PathVariable("taskId") String taskId, @PathVariable("userId") String userId) {
        processInstanceService.entrust(taskId, userId);
        return AjaxResult.success();
    }


    /**
     * 删除运行中流程实例
     *
     * @param processInstanceId
     * @return
     */
    @DeleteMapping("{processInstanceId}")
    public AjaxResult deleteProcess(@PathVariable("processInstanceId") String processInstanceId) {
        processInstanceService.deleteProcess(processInstanceId);
        return AjaxResult.success();
    }

    /**
     * 删除流程实例
     *
     * @param list
     * @return
     */
    @DeleteMapping()
    public AjaxResult deleteProcess(@RequestBody List<String> list) {
        processInstanceService.deleteProcess(list);
        return AjaxResult.success();
    }
}
