package cn.com.qmth.examcloud.core.questions.config;

import com.mongodb.DBRef;
import org.bson.Document;
import org.springframework.data.mongodb.core.convert.DbRefProxyHandler;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DbRefResolverCallback;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.lang.Nullable;

import java.util.ArrayList;
import java.util.List;

public enum MyDbRefResolver implements DbRefResolver {

    INSTANCE;

    @Override
    @Nullable
    public Object resolveDbRef(MongoPersistentProperty property, @Nullable DBRef dbref,
                               DbRefResolverCallback callback, DbRefProxyHandler proxyHandler) {
        return callback.resolve(property);
    }

    @Override
    @Nullable
    public Document fetch(DBRef dbRef) {
        Document document = new Document();
        document.put("_id", dbRef.getId());
        return document;
    }

    @Override
    public List<Document> bulkFetch(List<DBRef> dbRefs) {
        List<Document> list = new ArrayList<>();
        for (DBRef dbRef : dbRefs) {
            Document document = new Document();
            document.put("_id", dbRef.getId());
            list.add(document);
        }
        return list;
    }

}
