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

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * @author reghao
 * @date 2022-03-28 11:54:49
 */
@Slf4j
@Component
public class SpringLifecycle implements ApplicationRunner, DisposableBean {
    private final AppProperties appProperties;

    public SpringLifecycle(AppProperties appProperties) {
        this.appProperties = appProperties;
    }

    @Override
    public void run(ApplicationArguments args) {
        String nativeLuceneDir = String.format("%s/%s", appProperties.getBaseDir(), appProperties.getNativeLuceneDirname());
        createDir(nativeLuceneDir);
        String hibernateLuceneDir = String.format("%s/%s", appProperties.getBaseDir(), appProperties.getHibernateLuceneDirname());
        createDir(hibernateLuceneDir);
        log.info("SearchService 启动...");
    }

    private void createDir(String dirPath) {
        File dir = new File(dirPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
    }

    @Override
    public void destroy() {
        log.info("SearchService 停止...");
    }
}
