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

import cn.axzo.workflow.common.model.request.es.TaskSearchReqDTO;
import cn.axzo.workflow.es.mapper.EsProcessTaskMapper;
import cn.axzo.workflow.es.model.ProcessInstanceDocument;
import cn.axzo.workflow.es.model.ProcessTaskDocument;
import cn.axzo.workflow.es.service.EsProcessTaskService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.easyes.annotation.IndexName;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.dromara.easyes.core.toolkit.FieldUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.List;

import static cn.axzo.workflow.common.constant.BpmnConstants.ES_FIXED_ROUTING;

/**
 * 操作流程任务的 ES Service 实现
 *
 * @author wangli
 * @since 2024-09-27 11:09
 */
@Service
@AllArgsConstructor
@Slf4j
public class EsProcessTaskServiceImpl implements EsProcessTaskService {

    private final EsProcessTaskMapper esProcessTaskMapper;

    /**
     * 删除流程实例索引
     *
     * @return
     */
    @Override
    public Boolean deleteIndex() {
        IndexName annotation = AnnotationUtils.findAnnotation(ProcessTaskDocument.class, IndexName.class);
        return esProcessTaskMapper.deleteIndex(annotation.value());
    }

    /**
     * 新增流程实例文档文档
     *
     * @param processTaskDocument
     * @return 成功的条数
     */
    public Integer insert(String routing, String parentId, ProcessTaskDocument processTaskDocument) {
        return esProcessTaskMapper.insert(routing, parentId, processTaskDocument);
    }

    @Override
    public Integer insertBatch(String routing, String parentId, Collection<ProcessTaskDocument> processTaskDocuments) {
        return esProcessTaskMapper.insertBatch(routing, parentId, processTaskDocuments);
    }

    @Override
    public List<ProcessTaskDocument> search(TaskSearchReqDTO dto) {
        LambdaEsQueryWrapper<ProcessTaskDocument> wrapper = new LambdaEsQueryWrapper<ProcessTaskDocument>()
                .eq(StringUtils.hasText(dto.getAssigneeOuId()), ProcessTaskDocument::getAssigneeOuId, dto.getAssigneeOuId())
                .eq(StringUtils.hasText(dto.getAssigneeTenantId()), ProcessTaskDocument::getAssigneeTenantId, dto.getAssigneeTenantId())
                .eq(StringUtils.hasText(dto.getAssigneePersonId()), ProcessTaskDocument::getAssigneePersonId, dto.getAssigneePersonId())
                .matchPhrase(StringUtils.hasText(dto.getAssigneeName()), ProcessTaskDocument::getAssigneeName, dto.getAssigneeName())
                .hasParent(StringUtils.hasText(dto.getProcessInstanceId()), "process_instance_document",
                        w-> w.eq(FieldUtils.val(ProcessInstanceDocument::getInstanceId), dto.getProcessInstanceId()))
//                .parentId(StringUtils.hasText(dto.getProcessInstanceId()), dto.getProcessInstanceId(), "process_instance_document")
                .routing(ES_FIXED_ROUTING)
        ;
        return esProcessTaskMapper.selectList(wrapper);
    }

    @Override
    public Integer delete(String routing, String processInstanceId) {
        LambdaEsQueryWrapper<ProcessTaskDocument> wrapper = new LambdaEsQueryWrapper<ProcessTaskDocument>()
                .eq(FieldUtils.val(ProcessTaskDocument::getProcessInstanceId), processInstanceId)
                .routing(ES_FIXED_ROUTING);
        return esProcessTaskMapper.delete(wrapper);
    }
}
