package cn.reghao.tnb.search.app.lucene;

import cn.reghao.tnb.search.app.model.po.VideoText;
import cn.reghao.tnb.search.app.model.po.Wenshu;
import cn.reghao.tnb.search.app.model.po.WenshuLucene;
import org.apache.lucene.document.*;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;

/**
 * @author reghao
 * @date 2025-04-02 11:12:48
 */
@Service
public class LuceneDocument {
    public Object getObject(Class<?> clazz, Document document) throws Exception {
        Field[] fields = clazz.getDeclaredFields();
        Object object = clazz.getDeclaredConstructor().newInstance();
        for (int i = 0; i < fields.length; i++) {
            String name = fields[i].getName();
            String value = document.get(name);
            if (value == null) {
                continue;
            }

            Class<?> clazzType = fields[i].getType();
            if (clazzType.equals(String.class)) {
                fields[i].setAccessible(true);
                fields[i].set(object, value);
            } else {
                Object result = null;
                if (clazzType.equals(Long.class)) {
                    result = Long.parseLong(value);
                } else if (clazzType.equals(Double.class)) {
                    result = Double.parseDouble(value);
                } else if (clazzType.equals(Integer.class)) {
                    result = Integer.parseInt(value);
                } else if (clazzType.equals(Boolean.class)) {
                    result = Boolean.parseBoolean(value);
                }

                fields[i].setAccessible(true);
                fields[i].set(object, result);
            }
        }

        return object;
    }

    public Document getDocumentByVideoText(VideoText videoText) {
        Document doc = new Document();
        doc.add(new StringField("id", videoText.getId(), org.apache.lucene.document.Field.Store.YES));
        doc.add(new TextField("title", videoText.getTitle(), org.apache.lucene.document.Field.Store.YES));
        doc.add(new TextField("description", videoText.getDescription(), org.apache.lucene.document.Field.Store.YES));
        // 对 int 型字段索引(只索引不存储)
        doc.add(new IntPoint("scope", videoText.getScope()));
        doc.add(new StoredField("scope", videoText.getScope()));
        // 对 long 型字段索引(只索引不存储)
        doc.add(new LongPoint("publishTime", videoText.getPublishTime()));
        // 使用 StoredField 存储 long 型字段, StoredField 只会存储, 不会分词, 也不会索引
        doc.add(new StoredField("publishTime", videoText.getPublishTime()));
        return doc;
    }

    public Document getDocumentByWenshuLucene(WenshuLucene wenshuLucene) {
        Document doc = new Document();
        /*doc.add(new NumericDocValuesField("id", wenshuLucene.getId()));
        doc.add(new StoredField("id", wenshuLucene.getId()));*/
        doc.add(new StringField("id", wenshuLucene.getId(), org.apache.lucene.document.Field.Store.YES));
        doc.add(new TextField("caseName", wenshuLucene.getCaseName(), org.apache.lucene.document.Field.Store.YES));
        doc.add(new TextField("fullText", wenshuLucene.getFullText(), org.apache.lucene.document.Field.Store.YES));
        return doc;
    }

    public Document getDocumentByWenshu(Wenshu wenshu) {
        Document doc = new Document();
        doc.add(new NumericDocValuesField("id", Long.parseLong(wenshu.getId())));
        doc.add(new StoredField("id", wenshu.getId()));
        doc.add(new StringField("caseId", wenshu.getCaseId(), org.apache.lucene.document.Field.Store.YES));
        doc.add(new TextField("caseName", wenshu.getCaseName(), org.apache.lucene.document.Field.Store.YES));
        doc.add(new StringField("court", wenshu.getCourt(), org.apache.lucene.document.Field.Store.YES));
        doc.add(new StringField("region", wenshu.getRegion(), org.apache.lucene.document.Field.Store.YES));
        doc.add(new StringField("caseType", wenshu.getCaseType(), org.apache.lucene.document.Field.Store.YES));
        //doc.add(new StringField("caseTypeId", wenshu.getCaseTypeId(), org.apache.lucene.document.Field.Store.YES));
        doc.add(new StringField("procedure", wenshu.getProcedure(), org.apache.lucene.document.Field.Store.YES));
        doc.add(new StringField("judgmentDate", wenshu.getJudgmentDate(), org.apache.lucene.document.Field.Store.YES));
        doc.add(new StringField("publicDate", wenshu.getPublicDate(), org.apache.lucene.document.Field.Store.YES));
        doc.add(new StringField("parties", wenshu.getParties(), org.apache.lucene.document.Field.Store.YES));
        doc.add(new StringField("cause", wenshu.getCause(), org.apache.lucene.document.Field.Store.YES));
        doc.add(new StringField("legalBasis", wenshu.getLegalBasis(), org.apache.lucene.document.Field.Store.YES));
        doc.add(new TextField("fullText", wenshu.getFullText(), org.apache.lucene.document.Field.Store.YES));
        return doc;
    }
}
