package cn.axzo.workflow.server.xxljob;

import cn.axzo.workflow.es.service.EsProcessInstanceService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.easyes.common.utils.RestHighLevelClientUtils;
import org.dromara.easyes.core.toolkit.IndexUtils;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import static org.dromara.easyes.annotation.rely.AnnotationConstants.DEFAULT_MAX_RESULT_WINDOW;
import static org.dromara.easyes.common.utils.RestHighLevelClientUtils.DEFAULT_DS;

/**
 * 生产环境下手动创建索引
 *
 * @author wangli
 * @since 2024-10-09 11:34
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class EsIndexOperationJobHandler {
    private final EsProcessInstanceService esProcessInstanceService;
    private final RestHighLevelClientUtils restHighLevelClientUtils;

    @XxlJob("esIndexOperation")
    public ReturnT<String> execute(String param) {
        if (!StringUtils.hasText(param)) {
            createIndex();
        } else if (StringUtils.hasText(param) && "delete".equalsIgnoreCase(param.trim())) {
            deleteIndex();
        } else if (StringUtils.hasText(param) && "update".equalsIgnoreCase(param.trim())) {
            updateIndex();
        } else if (StringUtils.hasText(param) && "reindex".equalsIgnoreCase(param.trim())) {
            reindex();
        }
        return ReturnT.SUCCESS;
    }

    private void reindex() {
        log.info("开始执行 reindex 操作...");
        XxlJobHelper.log("开始执行 reindex 操作...");
        //获取动态数据源 若未配置多数据源,则使用默认数据源
        RestHighLevelClient client = restHighLevelClientUtils.getClient(DEFAULT_DS);
        boolean reindex = IndexUtils.reindex(client, "workflow_engine_test_process_instance_document", "workflow_engine_dev_process_instance_document", DEFAULT_MAX_RESULT_WINDOW);
        log.info("reindex 操作完成. 响应结果:  {}", reindex);
        XxlJobHelper.log("reindex 操作完成. 响应结果:  {}", reindex);
    }

    /**
     * 更新索引
     */
    private void updateIndex() {
        log.info("开始更新父子文档索引...");
        XxlJobHelper.log("开始更新父子文档索引...");
        Boolean index = esProcessInstanceService.updateIndex();
        log.info("更新完成. 响应结果:  {}", index);
        XxlJobHelper.log("更新完成. 响应结果:  {}", index);
    }

    /**
     * 删除索引
     */
    private void deleteIndex() {
        log.info("开始删除父子文档索引...");
        XxlJobHelper.log("开始删除父子文档索引...");
        Boolean index = esProcessInstanceService.deleteIndex();
        log.info("删除完成. 响应结果:  {}", index);
        XxlJobHelper.log("删除完成. 响应结果:  {}", index);
    }

    /**
     * 创建索引
     */
    private void createIndex() {
        log.info("开始创建父子文档索引...");
        XxlJobHelper.log("开始创建父子文档索引...");
        Boolean index = esProcessInstanceService.createIndex();
        // 如果重复调用,避免报错,在创建的逻辑中,内置了判断是否存在指定索引,如果存在则不再创建,直接返回 false.
        log.info("创建完成. 响应结果:  {}", index);
        XxlJobHelper.log("创建完成. 响应结果:  {}", index);
    }
}
