package com.yh.saas.plugin.yiliangyiyun.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.winsea.svc.base.base.entity.CommonStaff;
import com.winsea.svc.base.security.util.AuthSecurityUtils;
import com.winsea.svc.base.workflow.entity.Workflow;
import com.yh.saas.common.support.util.StringUtils;
import com.yh.saas.plugin.yiliangyiyun.service.INewWorkflowService;
import com.yh.saas.toolkit.workflow.command.WorkflowCommand;
import com.yh.saas.toolkit.workflow.service.IWorkflowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

/**
 * 工作流API
 *
 * @author gdc
 */
@RequestMapping("/newWorkflow")
@RestController
public class NewWorkflowController {

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private INewWorkflowService workflowService;

    /**
     * 创建工作流
     *
     * @param workflow 工作流对象
     * @return 工作流ID
     */
    @PostMapping("/api/create")
    public String create(@RequestBody Workflow workflow) {
        if (StringUtils.isEmpty(workflow.getCompId())) {
            workflow.setCompId(AuthSecurityUtils.getCurrentUserInfo().getCompId());
        }
        return workflowService.create(workflow);
    }

    /**
     * 修改工作流信息
     *
     * @param workflow 工作流对象
     * @return 工作流ID
     */
    @PostMapping("/api/changeInfo")
    public String changeInfo(@RequestBody Workflow workflow) {
        if (StringUtils.isEmpty(workflow.getCompId())) {
            workflow.setCompId(AuthSecurityUtils.getCurrentUserInfo().getCompId());
        }
        return workflowService.changeInfo(workflow);
    }

    /**
     * 开始工作流
     *
     * @param command 开始命令
     */
    @PostMapping("/api/startInstance")
    public void startInstance(@RequestBody WorkflowCommand.StartInstance command) {
        workflowService.startInstance(command.getWorkflowId(), command.getBusinessKey());
    }

    /**
     * 激活工作流
     *
     * @param command 激活命令
     */
    @PostMapping("/api/activateInstance")
    public void activateInstance(@RequestBody WorkflowCommand.ActivateInstance command) {
        workflowService.activateInstance(command.getWorkflowId(), command.getBusinessKey());
    }

    /**
     * 挂起工作流
     *
     * @param command 挂起命令
     */
    @PostMapping("/api/suspendInstance")
    public void suspendInstance(@RequestBody WorkflowCommand.SuspendInstance command) {
        workflowService.suspendInstance(command.getWorkflowId(), command.getBusinessKey());
    }

    /**
     * 签收
     *
     * @param command 签收命令
     */
    @PostMapping("/api/claim")
    public void claim(@RequestBody WorkflowCommand.ClaimTask command) {
        if (StringUtils.isEmpty(command.getUserId())) {
            workflowService.claim(command.getTaskId());
        } else {
            workflowService.claim(command.getTaskId(), command.getUserId());
        }
    }

    /**
     * 处理任务
     *
     * @param command 处理任务命令
     */
    @PostMapping("/api/handle")
    public void handle(@RequestBody WorkflowCommand.HandleTask command) {
        workflowService.handle(command.getTaskId(), command.getApproved(), command.getAuditMind(),
                command.getNeedReapply());
    }


    /**
     * 获取流程图
     *
     * @param workflowId 工作流ID
     * @throws IOException
     */
    @GetMapping("/query/processDiagram")
    public void getInstancePicture(@RequestParam String workflowId) throws IOException {
        InputStream processDiagram = workflowService.getProcessDiagram(workflowId);
        if (null != processDiagram) {
            response.setContentType("application/force-download");
            response.setHeader("Content-Disposition", "attachment;fileName=" + workflowId + ".png");

            StreamUtils.copy(processDiagram, response.getOutputStream());
            response.getOutputStream().close();
        }
    }

    /**
     * 获取工作流定义列表
     *
     * @param businessCode 业务编码
     * @param tmpCompId    公司ID
     * @return 工作流定义列表
     */
    @GetMapping("/query/workflowDefinitions")
    public List<Workflow> getWorkflowDefinitions(@RequestParam String businessCode,
                                                 @RequestParam(name = "compId", required = false) String tmpCompId) {
        String compId = tmpCompId;
        if (StringUtils.isEmpty(tmpCompId)) {
            compId = AuthSecurityUtils.getCurrentUserInfo().getCompId();
        }
        return workflowService.getWorkflowDefinitions(businessCode, compId);
    }

    /**
     * 通过业务编码查询工作流列表
     *
     * @param businessCode 业务编号
     * @return 工作流列表
     */
    @GetMapping("/query/workflowsByBusinessCode")
    public List<Workflow> findWorkflowsByBusinessCode(@RequestParam String businessCode) {
        return workflowService.findWorkflowsByBusinessCode(businessCode);
    }

    /**
     * 获取工作流详情
     *
     * @param workflowId 工作流ID
     * @return 工作流详情
     */
    @GetMapping("/query/workflowInfo")
    public Workflow getWorkflowInfo(@RequestParam String workflowId) {
        return workflowService.getWorkflowDetail(workflowId);
    }

    /**
     * 获取任务列表
     *
     * @param workflowIds 工作流ID列表
     * @param currentPage 页码
     * @param pageSize    分页大小
     * @return 任务列表
     */
    @GetMapping("/query/tasks")
    public Page<Object> getTaskList(@RequestParam String workflowIds, @RequestParam Integer currentPage,
                                    @RequestParam Integer pageSize) {
        return workflowService.getTaskList(Arrays.asList(workflowIds.split(",")), currentPage, pageSize);
    }

    /**
     * 获取活动的任务
     *
     * @param workflowIds 工作流ID列表
     * @param businessKey 业务编码
     * @return 活动的任务
     */
    @GetMapping("/query/activeTask")
    public JSONObject getActiveTask(@RequestParam String workflowIds, @RequestParam String businessKey) {
        return workflowService.getActiveTask(Arrays.asList(workflowIds.split(",")), businessKey);
    }

    /**
     * 获取活动的任务
     *
     * @param workflowIds 工作流ID列表
     * @param businessKey 业务编码
     * @return 活动的任务
     */
    @GetMapping("/query/preActiveTask")
    public JSONObject getActiveTask(@RequestParam String staffId,@RequestParam String workflowIds, @RequestParam String businessKey) {
        return workflowService.getActiveTask(staffId,Arrays.asList(workflowIds.split(",")), businessKey);
    }


    /**
     * 获取流程当前任务候选人
     *
     * @param workflowId  工作流ID
     * @param businessKey 业务编码
     * @return 任务候选人
     */
    @GetMapping("/query/taskCandidates")
    public List<CommonStaff> getTaskCandidates(@RequestParam String workflowId, @RequestParam String businessKey) {
        return workflowService.getTaskCandidates(workflowId, businessKey);
    }
    /**
     * 获取流程当前任务候选人
     *
     * @param workflowId  工作流ID
     * @param businessKey 业务编码
     * @return 任务候选人
     */
    @GetMapping("/getTaskWebsocket")
    public void getTaskWebsocket(@RequestParam String workflowId, @RequestParam String businessKey,@RequestParam String type) {
         workflowService.getTaskWebsocket(workflowId, businessKey,type);
    }


    /**
     * 查询 - 当前审核流业务实例处于第几步骤
     *
     * @param workflowId  审核流id
     * @param businessKey 主键业务
     * @return 第几步
     */
    @GetMapping("/query/findWorkflowStepNumber")
    public Integer findWorkflowStepNumber(@RequestParam String workflowId, @RequestParam String businessKey) {
        return workflowService.findWorkflowStepNumber(workflowId, businessKey);
    }

    /**
     * 判断当前任务是否处于最后一级节点
     * <p>
     * `     * @param taskId 任务id
     *
     * @return 是||否
     */
    @GetMapping("/query/judgeBusinessLastStep")
    public boolean judgeBusinessLastStep(@RequestParam String taskId) {
        return workflowService.judgeBusinessLastStep(taskId);
    }

    /**
     * 删除  - 删除最新审核流(改为旧审核流)
     *
     * @param workflow 审核流配置对象
     */
    @PostMapping("/api/removeLatestWorkflow")
    public void removeLatestWorkflow(@RequestBody Workflow workflow) {
        workflowService.removeLatestWorkflow(workflow.getId());
    }
}
