/** 部署. */
  public Page findDeployments(String tenantId, Page page) {
    RepositoryService repositoryService = processEngine.getRepositoryService();
    long count = repositoryService.createDeploymentQuery().deploymentTenantId(tenantId).count();
    List<Deployment> deployments =
        repositoryService
            .createDeploymentQuery()
            .deploymentTenantId(tenantId)
            .listPage((int) page.getStart(), page.getPageSize());
    page.setResult(deployments);
    page.setTotalCount(count);

    return page;
  }
  /** 任务. */
  public Page findTasks(String tenantId, Page page) {
    TaskService taskService = processEngine.getTaskService();
    long count = taskService.createTaskQuery().taskTenantId(tenantId).count();
    List<Task> tasks =
        taskService
            .createTaskQuery()
            .taskTenantId(tenantId)
            .listPage((int) page.getStart(), page.getPageSize());
    page.setResult(tasks);
    page.setTotalCount(count);

    return page;
  }
  /** 作业. */
  public Page findJobs(String tenantId, Page page) {
    ManagementService managementService = processEngine.getManagementService();

    long count = managementService.createJobQuery().jobTenantId(tenantId).count();
    List<Job> jobs =
        managementService
            .createJobQuery()
            .jobTenantId(tenantId)
            .listPage((int) page.getStart(), page.getPageSize());
    page.setResult(jobs);
    page.setTotalCount(count);

    return page;
  }
  /** 历史任务. */
  public Page findHistoricTaskInstances(String tenantId, Page page) {
    HistoryService historyService = processEngine.getHistoryService();

    long count = historyService.createHistoricTaskInstanceQuery().taskTenantId(tenantId).count();
    List<HistoricTaskInstance> historicTaskInstances =
        historyService
            .createHistoricTaskInstanceQuery()
            .taskTenantId(tenantId)
            .listPage((int) page.getStart(), page.getPageSize());
    page.setResult(historicTaskInstances);
    page.setTotalCount(count);

    return page;
  }
  /** 流程实例. */
  public Page findProcessInstances(String tenantId, Page page) {
    RuntimeService runtimeService = processEngine.getRuntimeService();
    long count =
        runtimeService.createProcessInstanceQuery().processInstanceTenantId(tenantId).count();
    List<ProcessInstance> processInstances =
        runtimeService
            .createProcessInstanceQuery()
            .processInstanceTenantId(tenantId)
            .listPage((int) page.getStart(), page.getPageSize());
    page.setResult(processInstances);
    page.setTotalCount(count);

    return page;
  }
  /** 流程定义. */
  public Page findProcessDefinitions(String tenantId, Page page) {
    RepositoryService repositoryService = processEngine.getRepositoryService();
    long count =
        repositoryService
            .createProcessDefinitionQuery()
            .processDefinitionTenantId(tenantId)
            .count();
    List<ProcessDefinition> processDefinitions =
        repositoryService
            .createProcessDefinitionQuery()
            .processDefinitionTenantId(tenantId)
            .listPage((int) page.getStart(), page.getPageSize());
    page.setResult(processDefinitions);
    page.setTotalCount(count);

    return page;
  }
  /** 参与流程. */
  public Page findInvolvedProcessInstances(String userId, String tenantId, Page page) {
    HistoryService historyService = processEngine.getHistoryService();

    // TODO: finished(), unfinished()
    long count =
        historyService
            .createHistoricProcessInstanceQuery()
            .processInstanceTenantId(tenantId)
            .involvedUser(userId)
            .count();
    List<HistoricProcessInstance> historicProcessInstances =
        historyService
            .createHistoricProcessInstanceQuery()
            .processInstanceTenantId(tenantId)
            .involvedUser(userId)
            .listPage((int) page.getStart(), page.getPageSize());

    page.setResult(historicProcessInstances);
    page.setTotalCount(count);

    return page;
  }
  /** 未结流程. */
  public Page findRunningProcessInstances(String userId, String tenantId, Page page) {
    HistoryService historyService = processEngine.getHistoryService();

    // TODO: 改成通过runtime表搜索,提高效率
    long count =
        historyService
            .createHistoricProcessInstanceQuery()
            .processInstanceTenantId(tenantId)
            .startedBy(userId)
            .unfinished()
            .count();
    List<HistoricProcessInstance> historicProcessInstances =
        historyService
            .createHistoricProcessInstanceQuery()
            .processInstanceTenantId(tenantId)
            .startedBy(userId)
            .unfinished()
            .listPage((int) page.getStart(), page.getPageSize());

    page.setResult(historicProcessInstances);
    page.setTotalCount(count);

    return page;
  }