package net.northking.cctp.dw.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileUtils {

    public static final String basePath = "/home/cctp/cctp-dw";

    public static final String execJobRatio = "/execJobRatioLogText.txt";

    public static final String execJobOverall = "/execJobOverallLogText.txt";

    public static final String execJobChart = "/execJobChartLogText.txt";

    public static final String execJobTrendChart = "/execJobTrendChartLogText.txt";

    public static final String testPointComplete = "/testPointCompleteLogText.txt";

    public static final String testPointStatistics = "/testPointStatisticsLogText.txt";

    public static final String prepareChartProgressData= "/prepareChartProgressDataLogText.txt";

    // 是否开启日志打印
    public static final Boolean openOrCloseLog = false;
    private final static Logger logger = LoggerFactory.getLogger(FileUtils.class);


    public static boolean createTxtFile(String filePath) {
        boolean flag = false;
        if (openOrCloseLog) {
            try {
                File newFile = new File(filePath);
                if (!newFile.exists()) {
                    if (newFile.createNewFile()) {
                        flag = true;
                    }
                }
            } catch (Exception e) {
                logger.error("文件创建失败",e);
            }
        }
        return flag;
    }

    public static boolean writeTxtFileAppend(String filePath, String content) {
        boolean flag = false;
        if (openOrCloseLog) {
            FileWriter fw = null;
            try {
                fw= new FileWriter(filePath, true);
                fw.write(content);
                fw.flush();
                fw.close();
                flag=true;
            } catch (Exception e) {
                logger.error("日志写入失败",e);
            }finally {
                if(fw!=null){
                    try {
                        fw.close();
                    } catch (IOException e) {
                        logger.error("文件流关闭失败",e);
                    }
                }
            }
        }
        return flag;

    }
}
