package com.qqchen.deploy.backend.workflow.service;

import com.qqchen.deploy.backend.framework.service.IBaseService;
import com.qqchen.deploy.backend.workflow.dto.WorkflowHistoricalInstancesDTO;
import com.qqchen.deploy.backend.workflow.dto.WorkflowInstanceDTO;
import com.qqchen.deploy.backend.workflow.dto.WorkflowInstanceStartRequest;
import com.qqchen.deploy.backend.workflow.dto.WorkflowTemplateWithInstancesDTO;
import com.qqchen.deploy.backend.workflow.dto.query.WorkflowInstanceQuery;
import com.qqchen.deploy.backend.workflow.entity.WorkflowInstance;
import com.qqchen.deploy.backend.workflow.enums.WorkflowInstanceStatusEnums;
import com.qqchen.deploy.backend.workflow.dto.query.WorkflowDefinitionQuery;
import com.qqchen.deploy.backend.workflow.dto.query.WorkflowHistoricalInstancesQuery;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.data.domain.Page;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

public interface IWorkflowInstanceService extends IBaseService<WorkflowInstance, WorkflowInstanceDTO, WorkflowInstanceQuery, Long> {
    
    /**
     * 创建工作流实例并关联Flowable实例
     *
     * @param processInstance Flowable流程实例
     * @return 工作流实例
     */
    WorkflowInstanceDTO createWorkflowInstance(Long workflowDefinitionId, String businessKey, ProcessInstance processInstance);
    
    /**
     * 更新工作流实例状态
     *
     * @param processInstanceId Flowable流程实例ID
     * @param status 新状态
     * @return 更新后的工作流实例
     */
    WorkflowInstance updateInstanceStatus(String processInstanceId, WorkflowInstanceStatusEnums status, LocalDateTime endTime);
    
    /**
     * 获取工作流实例详情
     *
     * @param processInstanceId Flowable流程实例ID
     * @return 工作流实例详情DTO
     */
    WorkflowInstanceDTO getInstanceDetails(String processInstanceId);
    
    /**
     * 根据业务标识查询工作流实例
     *
     * @param businessKey 业务标识
     * @return 工作流实例列表
     */
    List<WorkflowInstanceDTO> findByBusinessKey(String businessKey);
    
    /**
     * 完成工作流实例
     *
     * @param processInstanceId Flowable流程实例ID
     * @param variables 完成时的变量
     */
    void completeInstance(String processInstanceId, Map<String, Object> variables);

    WorkflowInstanceDTO startWorkflow(WorkflowInstanceStartRequest request);

    Page<WorkflowTemplateWithInstancesDTO> findTemplatesWithRecentInstances(WorkflowDefinitionQuery query);

    Page<WorkflowHistoricalInstancesDTO> historicalInstances(WorkflowHistoricalInstancesQuery query);
}
