//package lucene;
//
//import com.dayou.BaseApplication;
//import com.dayou.annotation.LuceneResource;
//import com.dayou.annotation.LuceneSearchable;
//import com.dayou.entity.Major;
//import com.dayou.entity.Personal;
//import com.dayou.enums.MainBusinessEnum;
//import com.dayou.service.ILuceneSearchService;
//import com.dayou.service.IMajorService;
//import com.dayou.service.IPersonalService;
//import com.dayou.utils.ReflectUtils;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.lucene.analysis.Analyzer;
//import org.apache.lucene.document.Document;
//import org.apache.lucene.document.Field;
//import org.apache.lucene.document.StringField;
//import org.apache.lucene.document.TextField;
//import org.apache.lucene.index.IndexWriter;
//import org.apache.lucene.index.IndexWriterConfig;
//import org.apache.lucene.store.Directory;
//import org.apache.lucene.store.FSDirectory;
//import org.junit.Test;
//import org.junit.runner.RunWith;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
//import org.springframework.core.io.support.ResourcePatternResolver;
//import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
//import org.springframework.core.type.classreading.MetadataReader;
//import org.springframework.core.type.classreading.MetadataReaderFactory;
//import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//import org.springframework.util.ClassUtils;
//import org.wltea.analyzer.lucene.IKAnalyzer;
//import org.springframework.core.io.Resource;
//
//import java.io.IOException;
//import java.nio.file.FileSystems;
//import java.util.*;
//
///**
// * 类说明：
// *
// * @author: wucl
// * @since: 2024/4/25
// * created with IntelliJ IDEA.
// */
//@Slf4j
//@SpringBootTest(classes = BaseApplication.class)
//@RunWith(value = SpringJUnit4ClassRunner.class)
//public class LuceneTest {
//
//    @Autowired
//    private IPersonalService personalService;
//
//    @Autowired
//    private IMajorService majorService;
//
//    @Autowired
//    private ILuceneSearchService luceneSearchService;
//
//
//
//    @Test
//    public void createIndex() throws NoSuchFieldException {
//        List<Personal> list = personalService.list();
//        List<Major> majors = majorService.list();
//        Collection<Document> docs = new ArrayList<>();
//        java.lang.reflect.Field[] personalFields = Personal.class.getDeclaredFields();
//        java.lang.reflect.Field[] majorFields = Major.class.getDeclaredFields();
//        for (Personal personal : list) {
//            Object id = ReflectUtils.getFieldValue(personal, "id");
//            for (java.lang.reflect.Field field : personalFields) {
//                if (field.getAnnotation(LuceneSearchable.class) != null) {
//                    Document document = new Document();
//                    Object fieldValue = ReflectUtils.getFieldValue(personal, field.getName());
//                    if (fieldValue != null) {
//                        document.add(new StringField("id", String.valueOf(id), Field.Store.YES));
//                        document.add(new TextField("result", String.valueOf(fieldValue), Field.Store.YES));
//                        document.add(new TextField("businessEnum", MainBusinessEnum.PERSONAL_BUSINESS.getMsg(), Field.Store.YES));
//                        document.add(new TextField("menuName", "正在进行", Field.Store.YES));
//                        document.add(new TextField("url", "/personal/pending/list", Field.Store.YES));
//                        docs.add(document);
//                    }
//
//                }
//            }
//        }
//
//        for (Major major : majors) {
//            Object id = ReflectUtils.getFieldValue(major, "id");
//            for (java.lang.reflect.Field field : majorFields) {
//                if (field.getAnnotation(LuceneSearchable.class) != null) {
//                    Document document = new Document();
//                    Object fieldValue = ReflectUtils.getFieldValue(major, field.getName());
//                    if (fieldValue != null) {
//                        document.add(new StringField("id", String.valueOf(id), Field.Store.YES));
//                        document.add(new TextField("result", String.valueOf(fieldValue), Field.Store.YES));
//                        document.add(new TextField("businessEnum", MainBusinessEnum.MAJOR_BUSINESS.getMsg(), Field.Store.YES));
//                        document.add(new TextField("menuName", "正在进行", Field.Store.YES));
//                        document.add(new TextField("url", "/major/list", Field.Store.YES));
//                        docs.add(document);
//                    }
//
//                }
//            }
//
//
//        }
//        Analyzer analyzer = new IKAnalyzer();
//        IndexWriterConfig config = new IndexWriterConfig(analyzer);
//        config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
//
//
//        try (Directory directory = FSDirectory.open(FileSystems.getDefault().getPath("G:\\luceneIndex"));
//             IndexWriter indexWriter = new IndexWriter(directory, config)) {
//            indexWriter.addDocuments(docs);
//            indexWriter.commit();
//        } catch (Exception e) {
//            log.error("create index error");
//        }
//    }
//
//    private final String BASE_PACKAGE = "com.dayou.entity";
//    private final String RESOURCE_PATTERN = "/**/*.class";
//
//    @Test
//    public void scaningEntity() {
//
//        Map<String, Class> handlerMap = new HashMap<String, Class>();
//
//        //spring工具类，可以获取指定路径下的全部类
//        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
//        try {
//            String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
//                    ClassUtils.convertClassNameToResourcePath(BASE_PACKAGE) + RESOURCE_PATTERN;
//            Resource[] resources = resourcePatternResolver.getResources(pattern);
//            //MetadataReader 的工厂类
//            MetadataReaderFactory readerfactory = new CachingMetadataReaderFactory(resourcePatternResolver);
//            for (Resource resource : resources) {
//                //用于读取类信息
//                MetadataReader reader = readerfactory.getMetadataReader(resource);
//                //扫描到的class
//                String classname = reader.getClassMetadata().getClassName();
//                Class<?> clazz = Class.forName(classname);
//                //判断是否有指定主解
//                LuceneResource anno = clazz.getAnnotation(LuceneResource.class);
//                if (anno != null) {
//                    //将注解中的类型值作为key，对应的类作为 value
//                    handlerMap.put(classname, clazz);
//                }
//            }
//        } catch (IOException | ClassNotFoundException e) {
//        }
//    }
//
//    @Test
//    public void testQuery(){
//        luceneSearchService.initializeIndex();
//    }
//}
