package cn.edu.whu.boot;

import cn.edu.whu.boot.common.domain.CommonConstant;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableAsync;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
@SpringBootApplication
@MapperScan("cn.edu.whu.boot.*.mapper")
@EnableAsync
@EnableCaching
public class BootApplication extends SpringBootServletInitializer implements CommandLineRunner{

	/**
	 * 必要的文件夹对应的路径
	 */
	@Value("${MediaPath}")
	private String mediaPath;

	@Value("${HistoryMediaPath}")
	private String historyMediaPath;
	@Value("${XmlFilePath}")
	private String xmlFilePath;
	@Value("${tpk.root}")
	private String tpkRoot;
	@Value("${tpkx.root}")
	private String tpkxRoot;
	@Value("${ApkFilePath}")
	private String apkFilePath;
	@Value("${photozippath}")
	private String photozippath;


	public static void main(String[] args) {
		SpringApplication.run(BootApplication.class, args);
	}

	/**
	 * 打成war包需要
	 */
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return super.configure(builder);
	}


	/**
	 * 创建必要的文件夹
	 */
	@Override
	public void run(String... args) throws Exception{


		List<String> paths = new ArrayList<>();
		paths.add(mediaPath);
		paths.add(historyMediaPath);
		paths.add(xmlFilePath);
		paths.add(tpkRoot);
		paths.add(tpkxRoot);
		paths.add(CommonConstant.TEMP_FILE_PATHNAME);
		paths.add(apkFilePath);
		paths.add(photozippath);
		paths.forEach(path -> {
			File file = new File(path);
			if(!file.exists()){
				file.mkdirs();
			}
		});
	}
}
