package cn.axzo.workflow.core.service.impl;

import cn.axzo.workflow.common.model.request.bpmn.task.ExtHiTaskSearchDTO;
import cn.axzo.workflow.core.repository.entity.ExtAxHiTaskInst;
import cn.axzo.workflow.core.repository.entity.ExtAxProcessLog;
import cn.axzo.workflow.core.service.BpmnProcessTaskForEsService;
import cn.axzo.workflow.core.service.ExtAxHiTaskInstService;
import cn.axzo.workflow.core.service.ExtAxProcessLogService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.HistoryService;
import org.flowable.engine.TaskService;
import org.flowable.engine.task.Attachment;
import org.flowable.engine.task.Comment;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 专用与对接 ES 的流程任务相关操作
 *
 * @author wangli
 * @since 2024-09-29 10:55
 */
@Service
@Slf4j
public class BpmnProcessTaskForEsServiceImpl implements BpmnProcessTaskForEsService {
    @Resource
    @Lazy
    private HistoryService historyService;
    @Resource
    @Lazy
    private TaskService taskService;
    @Resource
    private ExtAxHiTaskInstService extAxHiTaskInstService;
    @Resource
    private ExtAxProcessLogService extAxProcessLogService;

    @Override
    public List<HistoricTaskInstance> queryHistoricProcessTaskByProcessInstanceId(String processInstanceId) {
//        List<BpmnHistoricTaskInstanceVO> historicTaskListByProcessInstanceId = bpmnProcessTaskService.getHistoricTaskListByProcessInstanceId(processInstanceId, null);
        if (!StringUtils.hasText(processInstanceId)) {
            return Collections.emptyList();
        }
        return historyService.createHistoricTaskInstanceQuery()
                .processInstanceId(processInstanceId)
                .list();
    }

    public List<ExtAxHiTaskInst> queryExtAxHiTaskInstByProcessInstanceId(String processInstanceId) {
        if (!StringUtils.hasText(processInstanceId)) {
            return Collections.emptyList();
        }
        ExtHiTaskSearchDTO searchDTO = new ExtHiTaskSearchDTO();
        searchDTO.setProcessInstanceId(processInstanceId);
        return extAxHiTaskInstService.queryList(searchDTO);
    }

    @Override
    public List<Comment> queryCommentByProcessInstanceId(String processInstanceId) {
        if (!StringUtils.hasText(processInstanceId)) {
            return Collections.emptyList();
        }
        return taskService.getProcessInstanceComments(processInstanceId);
    }

    @Override
    public List<Attachment> queryAttachmentByProcessInstanceId(String processInstanceId) {
        if (!StringUtils.hasText(processInstanceId)) {
            return Collections.emptyList();
        }
        return taskService.getProcessInstanceAttachments(processInstanceId);
    }

    @Override
    public List<ExtAxProcessLog> queryProcessLogByProcessInstanceId(String processInstanceId) {
        if(!StringUtils.hasText(processInstanceId)){
            return Collections.emptyList();
        }
        ExtAxProcessLog queryLog = new ExtAxProcessLog();
        queryLog.setProcessInstanceId(processInstanceId);
        return extAxProcessLogService.genericQuery(queryLog);
    }
}
