package com.codvision.workflow.controller;

import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.commoncore.exception.BusinessException;
import com.codvision.workflow.entity.dto.WorkFlowDynamicAllocationDTO;
import com.codvision.workflow.entity.dto.WorkFlowTaskHandleDTO;
import com.codvision.workflow.service.WorkFlowTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author hxl
 */
@Slf4j
@Api(tags = "工作流流程任务")
@RestController
@RequiredArgsConstructor
@RequestMapping("/flowTask")
public class WorkFlowTaskController {

    private final WorkFlowTaskService service;

    @ApiOperation("审批任务")
    @PostMapping("/complete")
    public ResponseEntity<String> complete(@Validated @RequestBody WorkFlowTaskHandleDTO taskHandleDTO) {
        service.complete(taskHandleDTO);
        return new ResponseEntity<String>().ok();
    }

    @ApiOperation("拒绝任务")
    @PostMapping("/reject")
    public ResponseEntity<String> reject(@Validated @RequestBody WorkFlowTaskHandleDTO taskHandleDTO) {
        service.reject(taskHandleDTO);
        return new ResponseEntity<String>().ok();
    }

    @ApiOperation("任务转办")
    @PostMapping("/transferTask")
    public ResponseEntity<String> transferTask(@Validated @RequestBody WorkFlowTaskHandleDTO taskHandleDTO) {
        service.transferTask(taskHandleDTO);
        return new ResponseEntity<String>().ok();
    }

    @ApiOperation("任务委派")
    @PostMapping("/delegateTask")
    public ResponseEntity<String> delegateTask(@Validated @RequestBody WorkFlowTaskHandleDTO taskHandleDTO) {
        service.delegateTask(taskHandleDTO);
        return new ResponseEntity<String>().ok();
    }

    @ApiOperation("撤回重办")
    @PostMapping("/revokeRedo")
    public ResponseEntity<String> revokeRedo(@Validated @RequestBody WorkFlowTaskHandleDTO taskHandleDTO) {
        service.revokeRedo(taskHandleDTO);
        return new ResponseEntity<String>().ok();
    }

    @ApiOperation("退回")
    @PostMapping("/taskReturn")
    public ResponseEntity<String> taskReturn(@Validated @RequestBody WorkFlowTaskHandleDTO taskHandleDTO) {
        service.taskReturn(taskHandleDTO);
        return new ResponseEntity<String>().ok();
    }

    @ApiOperation("任务催办")
    @PostMapping("/urgingTask")
    public ResponseEntity<String> urgingTask(@RequestParam(value = "type") Integer type,
                                             @RequestParam(value = "content") String content,
                                             @RequestParam(value = "instanceId") String instanceId) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        if (type == null || StringUtils.isBlank(instanceId) || StringUtils.isBlank(content)) {
            throw new BusinessException(30002);
        }

        service.urgingTask(instanceId, type, content);
        return responseEntity.ok();
    }

    @ApiOperation("撤销任务")
    @PostMapping("/cancel")
    public ResponseEntity<String> cancel(@RequestParam(value = "instanceId") String instanceId) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        if (StringUtils.isBlank(instanceId)) {
            throw new BusinessException(30002);
        }

        service.cancel(instanceId);
        return responseEntity.ok();
    }

    @ApiOperation("认领/签收任务")
    @PostMapping("/claim")
    public ResponseEntity<String> claim(@RequestParam(value = "taskId") String taskId) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        if (StringUtils.isBlank(taskId)) {
            throw new BusinessException(30002);
        }

        service.claim(taskId);
        return responseEntity.ok();
    }

    @ApiOperation("取消认领/签收任务")
    @PostMapping("/unClaim")
    public ResponseEntity<String> unClaim(@RequestParam(value = "taskId") String taskId) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        if (StringUtils.isBlank(taskId)) {
            throw new BusinessException(30002);
        }

        service.unClaim(taskId);
        return responseEntity.ok();
    }

    @ApiOperation("分配下一节点处理人或者处理组织（需结合任务监听器一起使用）")
    @PostMapping("/dynamicAllocation")
    public void dynamicAllocation(@Validated @RequestBody WorkFlowDynamicAllocationDTO workFlowDynamicAllocationDTO) {
        service.dynamicAllocation(workFlowDynamicAllocationDTO);
    }
}
