package cn.ey.biz.module.system.controller.bpm.instance;

import cn.ey.biz.common.pojo.R;
import cn.ey.biz.module.system.controller.bpm.instance.vo.*;
import cn.ey.biz.module.system.service.bpm.BpmProcessInstanceService;
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.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import static cn.ey.biz.common.pojo.R.success;
import static cn.ey.biz.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Api(tags = "流程管理 - 流程实例") // 流程实例，通过流程定义创建的一次“申请”
@RestController
@RequestMapping("/system/bpm/process-instance")
@Validated
public class BpmProcessInstanceController {

    @Resource
    private BpmProcessInstanceService processInstanceService;

    @PostMapping("/create")
    @ApiOperation("新建流程实例")
    @PreAuthorize("@ss.hasPermission('bpm:process-instance:create')")
    public R<String> createProcessInstance(@Valid @RequestBody BpmProcessInstanceCreateReqVO createReqVO) {
        return success(processInstanceService.createProcessInstance(createReqVO));
    }

    @DeleteMapping("/cancel")
    @ApiOperation(value = "取消流程实例", notes = "撤回发起的流程")
    @PreAuthorize("@ss.hasPermission('bpm:process-instance:cancel')")
    public R<Boolean> cancelProcessInstance(@Valid @RequestBody BpmProcessInstanceCancelReqVO cancelReqVO) {
        processInstanceService.cancelProcessInstance(cancelReqVO);
        return success(true);
    }

    @GetMapping("/workflow-request")
    @ApiOperation("获取流程信息")
    @PreAuthorize("@ss.hasPermission('bpm:process-instance:query')")
    public R<BpmWorkflowRespVO> getWorkflowRequest(@Valid BpmWorkflowReqVO reqVO) {
        BpmWorkflowRespVO response=processInstanceService.getWorkflowRequest(reqVO);
        return success(response);
    }

    @GetMapping("/workflow-request-logs")
    @ApiOperation("获取指定流程的签字意见")
    @PreAuthorize("@ss.hasPermission('bpm:process-instance:log:query')")
    public  R<List<BpmWorkflowRequestLogsRespVO>> getWorkflowRequestLogs(@Valid BpmWorkflowLogsReqVO reqVO) {
        List<BpmWorkflowRequestLogsRespVO> response=processInstanceService.getWorkflowRequestLogs(getLoginUserId(),reqVO);
        return  success(response);
    }

    @GetMapping("/request-flow-chart")
    @ApiOperation("获取流程图链接")
    @PreAuthorize("@ss.hasPermission('bpm:process-instance:chart:query')")
    public  R<BpmFlowChartRespVO> getRequestFlowChart(@Valid BpmFlowChartReqVO reqVO) {
        BpmFlowChartRespVO response=processInstanceService.getRequestFlowChart(reqVO);
        return success(response);
    }

    @GetMapping("/get")
    @ApiOperation(value = "获得指定流程实例", notes = "在【流程详细】界面中，进行调用")
    @PreAuthorize("@ss.hasPermission('bpm:process-instance:query')")
    public R<BpmProcessInstanceRespVO> getProcessInstance(@Valid BpmProcessInstanceReqVO reqVO) {
        return success(processInstanceService.getProcessInstanceVO(reqVO));
    }

    @PostMapping("/do-force-over")
    @ApiOperation(value = "强制归档")
    @PreAuthorize("@ss.hasPermission('bpm:process-instance:cancel')")
    public R<Boolean> doForceOver(@Valid @RequestBody BpmDoForceOverReqVO reqVO) {
        processInstanceService.doForceOver(reqVO);
        return success(true);
    }

    @PostMapping("/forward-request")
    @ApiOperation(value = "转发、意见征询、转办")
    @PreAuthorize("@ss.hasPermission('bpm:process-instance:cancel')")
    public R<Boolean> forwardRequest(@Valid @RequestBody List<BpmForwardRequestReqVO> reqVO) {
        processInstanceService.forwardRequest(reqVO);
        return success(true);
    }

    @GetMapping("/get-simple-mode-data")
    @ApiOperation("获取流程图数据")
    @PreAuthorize("@ss.hasPermission('bpm:process-instance:chart:query')")
    public  R<BpmSimpleModeDataRespVo> getSimpleModeData(@Valid BpmSimpleModeDataReqVO reqVO) {
        BpmSimpleModeDataRespVo response=processInstanceService.getSimpleModeData(reqVO);
        return success(response);
    }

}
