package com.chenyang.nse.bussiness.tools.sql;

import com.chenyang.nse.bussiness.config.PropertiesLoaderUtils;
import com.chenyang.nse.bussiness.tools.command.CommandTool;
import java.io.File;
import java.util.Properties;

public class SqlFileTool {
   public static void exportSql(String tablename) {
      new Properties();

      try {
         Properties props = PropertiesLoaderUtils.loadAllProperties("config.properties");
         String exportPath = props.getProperty("ResetResult") + File.separator + tablename + ".sql";
         String user = props.getProperty("jdbc.user");
         String password = props.getProperty("jdbc.password");
         String[] strl = props.getProperty("jdbc.url").split("\\:");
         String host = strl[2].replace("//", "");
         String port = strl[3].substring(0, 4);
         String database = strl[3].substring(5, strl[3].indexOf("?"));
         CommandTool.command1("chmod -R 777 /data/infa_file/resetresult.sh");
         Runtime runtime = Runtime.getRuntime();
         runtime.exec(new String[]{"sh", "/data/infa_file/resetresult.sh", user, password, host, port, database, tablename, exportPath});
      } catch (Exception e) {
         e.printStackTrace();
      }

   }

   public static void importSql(String tablename) {
      new Properties();

      try {
         Properties props = PropertiesLoaderUtils.loadAllProperties("config.properties");
         String exportPath = props.getProperty("ResetResult") + File.separator + tablename + ".sql";
         String user = props.getProperty("jdbc.user");
         String password = props.getProperty("jdbc.password");
         String[] strl = props.getProperty("jdbc.url").split("\\:");
         String host = strl[2].replace("//", "");
         String port = strl[3].substring(0, 4);
         String database = strl[3].substring(5, strl[3].indexOf("?"));
         CommandTool.command1("chmod -R 777 /data/infa_file/resetresultread.sh");
         Runtime runtime = Runtime.getRuntime();
         Process process = runtime.exec(new String[]{"sh", "/data/infa_file/resetresultread.sh", user, password, host, port, database, exportPath});
         process.waitFor();
         System.out.println("导入成功》》》》》》》》》》》》》》》");
      } catch (Exception e) {
         e.printStackTrace();
      }

   }
}
