package com.base.common.lucene.init;


import com.alibaba.fastjson.JSON;

import com.base.orbit.lucene.constants.BasicIndexFieldEnum;
import com.base.orbit.lucene.constants.IndexEnum;
import com.base.orbit.lucene.constants.LuceneConfig;
import com.base.orbit.lucene.dto.FieldTemplateDTO;
import com.base.orbit.lucene.dto.IndexTemplateDTO;
import com.base.orbit.lucene.handler.IndexTemplate;
import com.base.orbit.lucene.service.IndexPlusService;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.document.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author nuoka
 * @version 1.0.0
 * <li>IntelliJ IDEA</li>
 * <li></li>
 * @date 2021/10/4 11:27
 **/

@Component
@Slf4j
public class LuceneInitialization implements ApplicationRunner {

    @Resource
    private IndexPlusService indexPlusService;

    @Autowired
    List<IndexTemplate> indexTemplateList;




    @Override
    public void run(ApplicationArguments args) throws Exception {
        this.init();
    }




    private void init() throws IOException {
        if (CollectionUtils.isEmpty(indexTemplateList)){
            log.error("没有现实lucene索引库");
            return;
        }
        // 获取所有父类的子类
        for (IndexTemplate indexTemplate: indexTemplateList) {
            String template = indexTemplate.getTemplate();
            IndexTemplateDTO indexTemplateDTO = JSON.parseObject(template, IndexTemplateDTO.class);
            // 校验系统占用标识
            this.existSystemField(indexTemplateDTO);
            List<FieldTemplateDTO> fieldTemplateDTOList = indexTemplateDTO.getFieldDTOList();
            if (CollectionUtils.isEmpty(fieldTemplateDTOList)) {
                return;
            }
            List<Document> list = this.indexPlusService.getIndexInfo(indexTemplateDTO.getIndexName());
            if (list.size() + fieldTemplateDTOList.size() > LuceneConfig.FIELD_MAX_COUNT) {
                return;
            }
            Map<String, Document> docMap = new HashMap();
            list.forEach(doc -> docMap.put(doc.get(BasicIndexFieldEnum.FIELD_NAME.getValue()), doc));
            // 不存在则初始化基础索引
            List<FieldTemplateDTO> initFieldList = fieldTemplateDTOList.stream()
                .filter(t -> !docMap.containsKey(t.getFieldName())).collect(
                    Collectors.toList());
            indexTemplateDTO.setFieldDTOList(initFieldList);
            this.indexPlusService.initBasicIndex(indexTemplateDTO);
        }
    }




    private void existSystemField(IndexTemplateDTO indexTemplateDTO) {
        Boolean flag = IndexEnum.BASIC_INDEX.getName().equals(indexTemplateDTO.getIndexName());
        if(Boolean.TRUE.equals(flag)) {
            return;
        }
        List<FieldTemplateDTO> fieldTemplateDTOList = indexTemplateDTO.getFieldDTOList();
        flag = fieldTemplateDTOList.stream().anyMatch(t->IndexEnum.TIMESTAMP.getName().equals(t.getFieldName()));
        if(Boolean.TRUE.equals(flag)) {
            return;
        }
    }
}
