package cn.quant.baa.pay.config;

import cn.quant.baa.pay.jpa.entity.DictionaryEntity;
import cn.quant.baa.pay.jpa.repository.DictionaryRepository;
import cn.quant.baa.pay.model.DictionaryItem;
import cn.quant.spring.data.jpa.PersistentEntityInvoker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.repository.support.Repositories;

import java.util.List;

/**
 * Created by Administrator on 2021/9/18 0018.
 */
@Configuration
public class ServerConfiguration {

    @Autowired
    private DictionaryRepository dictionaryRepository;

    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    public DictionaryViewer dictionaryViewer() {
        DictionaryViewer dictionary = new DictionaryViewer();
        List<DictionaryEntity> entities = dictionaryRepository.findAll();
        for (DictionaryEntity entity : entities) {
            DictionaryItem item = new DictionaryItem();
            item.setName(entity.getName());
            item.setCode(entity.getCode());
            item.setType(entity.getType());
            item.setImageUrl(entity.getImageUrl());
            dictionary.add(item);
        }
        return dictionary;
    }

    @Bean
    public PersistentEntityInvoker persistentEntityInvoker() {
        Repositories repositories = new Repositories(applicationContext);
        PersistentEntityInvoker invoker = new PersistentEntityInvoker(repositories);
        return invoker;
    }
}

