package cn.estsh.i3plus.pojo.base.jpa.factory;

import cn.estsh.i3plus.pojo.base.jpa.daoimpl.BaseMongoRepositoryImpl;
import cn.estsh.i3plus.pojo.base.codemaker.SnowflakeIdMaker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.mongodb.repository.support.MongoRepositoryFactory;
import org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;

import java.io.Serializable;

/**
 * @Description : 自定义mongodb持久化工厂类
 * @Reference :
 * @Author : alwaysfrin
 * @CreateDate : 2018-09-13 14:55
 * @Modify:
 **/
public class BaseMongoRepositoryFactoryBean<R extends MongoRepository<T, I>, T, I extends Serializable> extends MongoRepositoryFactoryBean<R, T, I> {
    public static final Logger LOGGER = LoggerFactory.getLogger(BaseMongoRepositoryFactoryBean.class);

    @Autowired
    public SnowflakeIdMaker snowflakeIdMaker;

    public BaseMongoRepositoryFactoryBean(Class<? extends R> repositoryInterface) {
        super(repositoryInterface);
    }

    @Override
    protected RepositoryFactorySupport getFactoryInstance(MongoOperations operations) {
        //LOGGER.info("【初始mongo持久仓】");
        //生成持久mongo仓库实例
        return new BaseMongoRepositoryFactory(operations,snowflakeIdMaker);
    }

    //创建一个内部类，该类不用在外部访问
    private static class BaseMongoRepositoryFactory<T, I extends Serializable> extends MongoRepositoryFactory {
        private final MongoOperations mongoOperations;
        //private final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
        private final SnowflakeIdMaker snowflakeIdMaker;

        public BaseMongoRepositoryFactory(MongoOperations mongoOperations,SnowflakeIdMaker snowflakeIdMaker) {
            super(mongoOperations);
            this.mongoOperations = mongoOperations;
            this.snowflakeIdMaker = snowflakeIdMaker;
            //this.mappingContext = mongoOperations.getConverter().getMappingContext();
        }

        //设置具体的实现类是BaseRepositoryImpl
        @Override
        protected Object getTargetRepository(RepositoryInformation information) {
            MongoEntityInformation<?, Serializable> entityInformation = getEntityInformation(information.getDomainType());
            return new BaseMongoRepositoryImpl(entityInformation, mongoOperations,snowflakeIdMaker);
        }

        //设置具体的实现类的class
        @Override
        protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
            return BaseMongoRepositoryImpl.class;
        }
    }
}
