package cn.fateverse.workflow.controller;

import cn.hutool.core.util.StrUtil;
import cn.fateverse.common.core.result.Result;
import cn.fateverse.common.core.result.page.TableDataInfo;
import cn.fateverse.common.core.utils.ObjectUtils;
import cn.fateverse.workflow.entity.bpmn.CommentInfo;
import cn.fateverse.workflow.entity.dto.TaskDto;
import cn.fateverse.workflow.entity.vo.task.TaskResultVo;
import cn.fateverse.workflow.entity.vo.task.TaskVo;
import cn.fateverse.workflow.service.ProcessTaskService;
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/task")
public class ProcessTaskController {

    private final ProcessTaskService processTaskService;

    public ProcessTaskController(ProcessTaskService processTaskService) {
        this.processTaskService = processTaskService;
    }

    @ApiOperation("工作流流程查询")
    @GetMapping
    @PreAuthorize("@ss.hasPermission('workflow:task:list')")
    public Result<TableDataInfo<TaskVo>> list() {
        TableDataInfo<TaskVo> dataInfo = processTaskService.searchTaskList();
        return Result.ok(dataInfo);
    }

    @ApiOperation("工作流流程查询")
    @GetMapping("/{taskId}")
    @PreAuthorize("@ss.hasPermission('workflow:task:info')")
    public Result<TaskResultVo> info(@PathVariable String taskId) {
        TaskResultVo resultVo = processTaskService.searchTaskByTaskId(taskId);
        return Result.ok(resultVo);
    }

    @ApiOperation("完成任务")
    @PutMapping("/complete")
    @PreAuthorize("@ss.hasPermission('workflow:task:complete')")
    public Result<Void> complete(@RequestBody @Validated TaskDto taskDto) {
        processTaskService.completeTask(taskDto);
        return Result.ok("任务完成!");
    }

    @ApiOperation("拒绝任务")
    @PutMapping("/refuse")
    @PreAuthorize("@ss.hasPermission('workflow:task:delegate')")
    public Result<Void> refuse(@RequestBody @Validated TaskDto taskDto) {
        processTaskService.refuseTask(taskDto);
        return Result.ok("操作完成!");
    }

    @ApiOperation("委派")
    @PutMapping("/delegate")
    @PreAuthorize("@ss.hasPermission('workflow:task:delegate')")
    public Result<Void> delegate(@RequestBody @Validated TaskDto taskDto) {
        if (ObjectUtils.isEmpty(taskDto.getDelegateUserInfo())){
            return Result.error("委托人信息不能为空!");
        }
        processTaskService.delegateTas(taskDto);
        return Result.ok("委派成功!");
    }

    @ApiOperation("委派人完成")
    @PutMapping("/resolve/complete")
    @PreAuthorize("@ss.hasPermission('workflow:task:resolve')")
    public Result<Void> resolveComplete(@RequestBody @Validated TaskDto taskDto) {
        processTaskService.resolveCompleteTask(taskDto);
        return Result.ok("任务完成!");
    }

    @ApiOperation("撤销")
    @PutMapping("/revoke")
    @PreAuthorize("@ss.hasPermission('workflow:task:revoke')")
    public Result<Void> revoke(@RequestBody @Validated TaskDto taskDto) {
        processTaskService.revokeTask(taskDto);
        return Result.ok("撤销成功!");
    }

    @ApiOperation("移交转办")
    @PutMapping("/assignee")
    @PreAuthorize("@ss.hasPermission('workflow:task:assignee')")
    public Result<Void> assignee(@RequestBody @Validated TaskDto taskDto) {
        if (ObjectUtils.isEmpty(taskDto.getTransferUserInfo())){
            return Result.error("移交转办人信息不能为空!");
        }
        processTaskService.assigneeTask(taskDto);
        return Result.ok("移交转办成功!");
    }

    @ApiOperation("退回")
    @PutMapping("/rollback")
    @PreAuthorize("@ss.hasPermission('workflow:task:rollback')")
    public Result<Void> rollback(@RequestBody @Validated TaskDto taskDto) {
        if (StrUtil.isEmpty(taskDto.getRollBackId())){
            return Result.error("退回节点不能为空!");
        }
        processTaskService.rollbackTask(taskDto);
        return Result.ok("退回成功!");
    }

    @ApiOperation("加签")
    @PutMapping("/add/multi")
    @PreAuthorize("@ss.hasPermission('workflow:task:addmulti')")
    public Result<Void> addMulti(@RequestBody @Validated TaskDto taskDto) {
        if (ObjectUtils.isEmpty(taskDto.getMultiAddUserInfo())){
            return Result.error("加签人信息不能为空!");
        }
        processTaskService.addMultiTask(taskDto);
        return Result.ok("加签成功!");
    }

    @ApiOperation("查看签上的人")
    @PutMapping("/list/multi")
    @PreAuthorize("@ss.hasPermission('workflow:task:listmulti')")
    public Result<Void> listMulti(@RequestBody String taskId) {
        if (ObjectUtils.isEmpty(taskId)){
            return Result.error("任务节点不能为空!");
        }
        processTaskService.searchMultiUsersInfo(taskId);
        return Result.ok("");
    }


    @ApiOperation("减签")
    @DeleteMapping("/multi/{taskId}")
    @PreAuthorize("@ss.hasPermission('workflow:task:delmulti')")
    public Result<Void> delMulti(@PathVariable String taskId) {
        if (ObjectUtils.isEmpty(taskId)){
            return Result.error("任务节点不能为空!");
        }
        processTaskService.deleteMultiTask(taskId);
        return Result.ok("减签成功!");
    }

    @ApiOperation("添加评论")
    @PostMapping("/comment")
    @PreAuthorize("@ss.hasPermission('workflow:task:comment')")
    public Result<Void> comment(@RequestBody @Validated CommentInfo commentInfo) {
        processTaskService.addCommentTask(commentInfo);
        return Result.ok("提交成功!");
    }

}
