deason 4 лет назад
Родитель
Сommit
aed61d5f6b

+ 30 - 0
src/main/java/cn/com/qmth/dp/examcloud/oe/modules/export_exam_record_sql_file/ExportExamRecordSqlFile.java

@@ -1,11 +1,16 @@
 package cn.com.qmth.dp.examcloud.oe.modules.export_exam_record_sql_file;
 package cn.com.qmth.dp.examcloud.oe.modules.export_exam_record_sql_file;
 
 
+import cn.com.qmth.dp.examcloud.oe.util.FileUtil;
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Component;
 
 
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
 /**
 /**
  * 导出 - 考试明细SQL文件
  * 导出 - 考试明细SQL文件
  */
  */
@@ -18,7 +23,32 @@ public class ExportExamRecordSqlFile {
     private JdbcTemplate jdbcTemplate;
     private JdbcTemplate jdbcTemplate;
 
 
     public void start() {
     public void start() {
+        List<Long> examIds = new ArrayList<>();
+
+        // examIds.add(1L);
+
+        this.export(examIds);
+    }
+
+    private void export(List<Long> examIds) {
+        for (Long examId : examIds) {
+            List<Map<String, Object>> rows = new ArrayList<>();
+
+            // todo query
+
+            this.toSqlFile(examId, rows);
+        }
+    }
+
+    private void toSqlFile(Long examId, List<Map<String, Object>> rows) {
+        StringBuilder content = new StringBuilder();
+
+        for (Map<String, Object> row : rows) {
+            // todo content
+        }
 
 
+        final String filePath = String.format("/home/admin/project/oe/exam_record_%s.sql", examId);
+        FileUtil.writeFile(filePath, content.toString());
     }
     }
 
 
 }
 }

+ 76 - 59
src/main/java/cn/com/qmth/dp/examcloud/oe/util/FileUtil.java

@@ -7,34 +7,20 @@
 
 
 package cn.com.qmth.dp.examcloud.oe.util;
 package cn.com.qmth.dp.examcloud.oe.util;
 
 
-import java.io.BufferedInputStream;
-import java.io.BufferedReader;
-import java.io.DataInputStream;
-import java.io.DataOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.*;
 import java.nio.charset.Charset;
 import java.nio.charset.Charset;
 import java.text.SimpleDateFormat;
 import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.Enumeration;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.UUID;
+import java.util.*;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 import java.util.zip.ZipFile;
 import java.util.zip.ZipOutputStream;
 import java.util.zip.ZipOutputStream;
 
 
-import org.apache.commons.io.IOUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
 public class FileUtil {
 public class FileUtil {
+
     private static Logger log = LoggerFactory.getLogger(FileUtil.class);
     private static Logger log = LoggerFactory.getLogger(FileUtil.class);
 
 
     /**
     /**
@@ -258,6 +244,35 @@ public class FileUtil {
         return true;
         return true;
     }
     }
 
 
+    /**
+     * 写入内容到文件(覆盖)
+     */
+    public static void writeFile(String filePath, String content) {
+        File file = new File(filePath);
+        makeDirs(file.getParent());
+
+        try {
+            if (!file.exists()) {
+                file.createNewFile();
+            }
+        } catch (IOException e) {
+            log.error(e.getMessage(), e);
+            return;
+        }
+
+        if (content == null) {
+            content = "";
+        }
+
+        try (FileOutputStream fos = new FileOutputStream(file);
+             OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
+             BufferedWriter bw = new BufferedWriter(osw);) {
+
+            bw.write(content);
+        } catch (IOException e) {
+            log.error(e.getMessage(), e);
+        }
+    }
 
 
     /**
     /**
      * 解压文件
      * 解压文件
@@ -413,44 +428,46 @@ public class FileUtil {
             zipOutStream.closeEntry();
             zipOutStream.closeEntry();
         }
         }
     }
     }
+
     public static void deleteFolder(String path) {
     public static void deleteFolder(String path) {
 
 
-		File file = new File(path);
-		if (file.exists()) {
-			if (file.isFile()) {
-				deleteFile(path);
-			} else {
-				deleteDirectory(path);
-			}
-		}
-	}
-
-	public static void deleteFile(String path) {
-		File file = new File(path);
-		if (file.isFile() && file.exists()) {
-			file.delete();
-		}
-	}
-
-	public static void deleteDirectory(String path) {
-		if (!path.endsWith(File.separator)) {
-			path = path + File.separator;
-		}
-		File dirFile = new File(path);
-		if (!dirFile.exists() || !dirFile.isDirectory()) {
-			return;
-		}
-		File[] files = dirFile.listFiles();
-		if (files != null) {
-			for (int i = 0; i < files.length; i++) {
-				if (files[i].isFile()) {
-					deleteFile(files[i].getAbsolutePath());
-				} else {
-					deleteDirectory(files[i].getAbsolutePath());
-				}
-			}
-		}
-
-		dirFile.delete();
-	}
+        File file = new File(path);
+        if (file.exists()) {
+            if (file.isFile()) {
+                deleteFile(path);
+            } else {
+                deleteDirectory(path);
+            }
+        }
+    }
+
+    public static void deleteFile(String path) {
+        File file = new File(path);
+        if (file.isFile() && file.exists()) {
+            file.delete();
+        }
+    }
+
+    public static void deleteDirectory(String path) {
+        if (!path.endsWith(File.separator)) {
+            path = path + File.separator;
+        }
+        File dirFile = new File(path);
+        if (!dirFile.exists() || !dirFile.isDirectory()) {
+            return;
+        }
+        File[] files = dirFile.listFiles();
+        if (files != null) {
+            for (int i = 0; i < files.length; i++) {
+                if (files[i].isFile()) {
+                    deleteFile(files[i].getAbsolutePath());
+                } else {
+                    deleteDirectory(files[i].getAbsolutePath());
+                }
+            }
+        }
+
+        dirFile.delete();
+    }
+
 }
 }