package com.crm.rely.backend.aspect;

import org.springframework.beans.BeansException;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class EntityScanPathResolver implements ApplicationContextAware {

    private String[] entityScanPaths;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Class<?> mainApplicationClass = Arrays.stream(applicationContext.getBeanDefinitionNames())
                .map(applicationContext::getType)
                .filter(type -> type != null && type.isAnnotationPresent(SpringBootApplication.class))
                .findFirst()
                .orElseThrow(() -> new IllegalStateException("Cannot find SpringBootApplication class"));

        EntityScan annotation = AnnotationUtils.findAnnotation(mainApplicationClass, EntityScan.class);
        if (annotation != null) {
            entityScanPaths = annotation.value();
        }
    }

    // 创建一个方法返回EntityScan的路径
    public String[] getEntityScanPaths() {
        return entityScanPaths;
    }
}