Ver Fonte

自动化考场问题解决

zhangbin há 1 ano atrás
pai
commit
f560a32750

+ 32 - 0
nbjk-admin/src/test/java/com/miaxis/test/FileSortExample.java

@@ -0,0 +1,32 @@
+package com.miaxis.test;
+
+import java.io.File;
+import java.text.Collator;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Locale;
+
+public class FileSortExample {
+
+    public static void main(String[] args) {
+        File folder = new File("F:/河南省");
+        File[] files = folder.listFiles();
+        for (File file : files) {
+            System.out.println(file.getName());
+        }
+        System.out.println("-------------------");
+
+        if (files != null) {
+            // 使用Collator类创建一个特定语言环境的排序器(这里使用中文)
+            Collator collator = Collator.getInstance(Locale.CHINA);
+            // 使用Comparator.comparing方法结合Lambda表达式按文件名的拼音首字母排序
+            Arrays.sort(files, Comparator.comparing(file -> file.getName(), collator));
+
+            // 输出排序后的文件名
+            for (File file : files) {
+                System.out.println(file.getName());
+            }
+        }
+    }
+
+}

+ 121 - 147
nbjk-admin/src/test/java/com/miaxis/test/ReadFileNameTest.java

@@ -33,9 +33,11 @@ public class ReadFileNameTest {
     @Autowired
     private IExamInfoService examInfoService;
 
-
+    /**
+     * 用于通过文件夹导出考场下的视频信息
+     */
     @Test
-    public  void test() {
+    public void test() {
         String folderPath = "H:/河南省"; // 替换为你的文件夹路径
         String excelFilePath = "H:/file.xlsx"; // 替换为你的Excel文件路径
 
@@ -84,167 +86,139 @@ public class ReadFileNameTest {
         }
     }
 
-        private  void traverseFolders(File folder, Sheet sheet, int rowIndex) {
-            String province = folder.getName();
-            String provinceId = "41";
-            String city = null;
-            String cityId = null;
-            String county = null;
-            String countyId = null;
-            String exam = null;
-
-            String video = null;
-            if (folder.isDirectory()) {
-                File[] files = folder.listFiles();
-                for (int i = 0; i < files.length; i++) {    //市级
-                   // System.out.println(files[i].getName());
-                    city = files[i].getName();
-                    List<DictCity> dictCityList = dictCityService.getDictCityByName(city);
-
-                    for (int j = 0; j < dictCityList.size(); j++) {
-                        cityId = dictCityList.get(j).getCode();
-                        if(cityId.startsWith("41")){
-                            break;
-                        }
+    private void traverseFolders(File folder, Sheet sheet, int rowIndex) {
+        String province = folder.getName();
+        String provinceId = "41";
+        String city = null;
+        String cityId = null;
+        String county = null;
+        String countyId = null;
+        String exam = null;
+
+        String video = null;
+        if (folder.isDirectory()) {
+            File[] files = folder.listFiles();
+            for (int i = 0; i < files.length; i++) {    //市级
+                // System.out.println(files[i].getName());
+                city = files[i].getName();
+                List<DictCity> dictCityList = dictCityService.getDictCityByName(city);
+
+                for (int j = 0; j < dictCityList.size(); j++) {
+                    cityId = dictCityList.get(j).getCode();
+                    if (cityId.startsWith("41")) {
+                        break;
                     }
+                }
 
 
-                    if(files[i].isDirectory()) {
-                        File[] files2 = files[i].listFiles();
-                        for (int j = 0; j < files2.length; j++) {   //区级
-                         //   System.out.println("    "+files2[j].getName());
-                            county = files2[j].getName();
-                            List<DictCity> dictCity2List = dictCityService.getDictCityByName(county);
+                if (files[i].isDirectory()) {
+                    File[] files2 = files[i].listFiles();
+                    for (int j = 0; j < files2.length; j++) {   //区级
+                        //   System.out.println("    "+files2[j].getName());
+                        county = files2[j].getName();
+                        List<DictCity> dictCity2List = dictCityService.getDictCityByName(county);
 
-                            for (int x = 0; x < dictCity2List.size(); x++) {
-                                countyId = dictCity2List.get(x).getCode();
-                                if(countyId.startsWith(cityId)){
-                                    break;
-                                }
+                        for (int x = 0; x < dictCity2List.size(); x++) {
+                            countyId = dictCity2List.get(x).getCode();
+                            if (countyId.startsWith(cityId)) {
+                                break;
                             }
+                        }
 
-                            if(files2[j].isDirectory()) {
-                                File[] files3 = files2[j].listFiles();
-                                for (int k = 0; k < files3.length; k++) { //考场
-                                 //   System.out.println("        "+files3[k].getName());
-                                    exam = files3[k].getName();
-                                    ExamInfo examInfo = new ExamInfo();
-                                    examInfo.setName(exam);
-                                    ExamInfo examInfo2 = null;
-                                    List<ExamInfo> list = examInfoService.selectOldExamInfoList(examInfo);
-                                    if(list!=null && list.size()>0) {
-                                        examInfo2 = list.get(0);
-                                    }
-                                    if(files3[k].isDirectory()){
-                                        File[] files4 = files3[k].listFiles();
-                                        for (int l = 0; l < files4.length; l++) {
-                                         //   System.out.println("            "+files4[l].getName());
-                                            video = files4[l].getName();
-                                            video = video.substring(0,video.indexOf("."));
-                                            //写入excel
-                                            Row row = sheet.createRow(rowIndex);
-                                            row.createCell(0).setCellValue("");
-                                            row.createCell(1).setCellValue(video);
-
-                                            if (video.indexOf("试看")>=0) {
-                                                row.createCell(2).setCellValue(1);
-                                            } else if (video.indexOf("1")>=0){
-                                                row.createCell(2).setCellValue(1+1);
-                                            } else if (video.indexOf("2")>=0) {
-                                                row.createCell(2).setCellValue(2+1);
-                                            } else if (video.indexOf("3")>=0) {
-                                                row.createCell(2).setCellValue(3+1);
-                                            } else if (video.indexOf("4")>=0) {
-                                                row.createCell(2).setCellValue(4+1);
-                                            } else if (video.indexOf("5")>=0) {
-                                                row.createCell(2).setCellValue(5+1);
-                                            } else if (video.indexOf("6")>=0) {
-                                                row.createCell(2).setCellValue(6+1);
-                                            } else if (video.indexOf("7")>=0) {
-                                                row.createCell(2).setCellValue(7+1);
-                                            } else if (video.indexOf("A")>=0) {
-                                                row.createCell(2).setCellValue(1+1);
-                                            } else if (video.indexOf("B")>=0) {
-                                                row.createCell(2).setCellValue(2+1);
-                                            } else if (video.indexOf("C")>=0) {
-                                                row.createCell(2).setCellValue(3+1);
-                                            } else if (video.indexOf("D")>=0) {
-                                                row.createCell(2).setCellValue(4+1);
-                                            } else if (video.indexOf("E")>=0) {
-                                                row.createCell(2).setCellValue(5+1);
-                                            }
-
-
-                                            row.createCell(3).setCellValue("");
-                                            row.createCell(4).setCellValue("");
-                                            row.createCell(5).setCellValue("3");
-                                            row.createCell(6).setCellValue("1");
-                                            row.createCell(7).setCellValue("1");
-                                            row.createCell(8).setCellValue(provinceId);
-                                            row.createCell(9).setCellValue(province);
-                                            row.createCell(10).setCellValue(cityId);
-                                            row.createCell(11).setCellValue(city);
-                                            row.createCell(12).setCellValue(countyId);
-                                            row.createCell(13).setCellValue(county);
-                                            row.createCell(14).setCellValue("");
-                                            row.createCell(15).setCellValue("");
-                                            if(examInfo2!=null) {
-                                                row.createCell(16).setCellValue(examInfo2.getId());
-                                                row.createCell(17).setCellValue(examInfo2.getName());
-                                            } else {
-                                                row.createCell(16).setCellValue("");
-                                                row.createCell(17).setCellValue("");
-                                            }
-                                            if (video.indexOf("试看")>=0) {
-                                                row.createCell(18).setCellValue("1");
-                                            } else {
-                                                row.createCell(18).setCellValue("2");
-                                            }
-
-
-                                            System.out.println(province+"   "+city+"    "+county+"  "+exam+"    "+video);
-                                            rowIndex++;
+                        if (files2[j].isDirectory()) {
+                            File[] files3 = files2[j].listFiles();
+                            for (int k = 0; k < files3.length; k++) { //考场
+                                //   System.out.println("        "+files3[k].getName());
+                                exam = files3[k].getName();
+                                ExamInfo examInfo = new ExamInfo();
+                                examInfo.setName(exam);
+                                ExamInfo examInfo2 = null;
+                                List<ExamInfo> list = examInfoService.selectOldExamInfoList(examInfo);
+                                if (list != null && list.size() > 0) {
+                                    examInfo2 = list.get(0);
+                                }
+                                if (files3[k].isDirectory()) {
+                                    File[] files4 = files3[k].listFiles();
+                                    for (int l = 0; l < files4.length; l++) {
+                                        //   System.out.println("            "+files4[l].getName());
+                                        video = files4[l].getName();
+                                        video = video.substring(0, video.indexOf("."));
+                                        //写入excel
+                                        Row row = sheet.createRow(rowIndex);
+                                        row.createCell(0).setCellValue("");
+                                        row.createCell(1).setCellValue(video);
+
+                                        if (video.indexOf("试看") >= 0) {
+                                            row.createCell(2).setCellValue(1);
+                                        } else if (video.indexOf("1") >= 0) {
+                                            row.createCell(2).setCellValue(1 + 1);
+                                        } else if (video.indexOf("2") >= 0) {
+                                            row.createCell(2).setCellValue(2 + 1);
+                                        } else if (video.indexOf("3") >= 0) {
+                                            row.createCell(2).setCellValue(3 + 1);
+                                        } else if (video.indexOf("4") >= 0) {
+                                            row.createCell(2).setCellValue(4 + 1);
+                                        } else if (video.indexOf("5") >= 0) {
+                                            row.createCell(2).setCellValue(5 + 1);
+                                        } else if (video.indexOf("6") >= 0) {
+                                            row.createCell(2).setCellValue(6 + 1);
+                                        } else if (video.indexOf("7") >= 0) {
+                                            row.createCell(2).setCellValue(7 + 1);
+                                        } else if (video.indexOf("A") >= 0) {
+                                            row.createCell(2).setCellValue(1 + 1);
+                                        } else if (video.indexOf("B") >= 0) {
+                                            row.createCell(2).setCellValue(2 + 1);
+                                        } else if (video.indexOf("C") >= 0) {
+                                            row.createCell(2).setCellValue(3 + 1);
+                                        } else if (video.indexOf("D") >= 0) {
+                                            row.createCell(2).setCellValue(4 + 1);
+                                        } else if (video.indexOf("E") >= 0) {
+                                            row.createCell(2).setCellValue(5 + 1);
                                         }
-                                    }
 
 
+                                        row.createCell(3).setCellValue("");
+                                        row.createCell(4).setCellValue("");
+                                        row.createCell(5).setCellValue("3");
+                                        row.createCell(6).setCellValue("1");
+                                        row.createCell(7).setCellValue("1");
+                                        row.createCell(8).setCellValue(provinceId);
+                                        row.createCell(9).setCellValue(province);
+                                        row.createCell(10).setCellValue(cityId);
+                                        row.createCell(11).setCellValue(city);
+                                        row.createCell(12).setCellValue(countyId);
+                                        row.createCell(13).setCellValue(county);
+                                        row.createCell(14).setCellValue("");
+                                        row.createCell(15).setCellValue("");
+                                        if (examInfo2 != null) {
+                                            row.createCell(16).setCellValue(examInfo2.getId());
+                                            row.createCell(17).setCellValue(examInfo2.getName());
+                                        } else {
+                                            row.createCell(16).setCellValue("");
+                                            row.createCell(17).setCellValue("");
+                                        }
+                                        if (video.indexOf("试看") >= 0) {
+                                            row.createCell(18).setCellValue("1");
+                                        } else {
+                                            row.createCell(18).setCellValue("2");
+                                        }
+
+
+                                        System.out.println(province + "   " + city + "    " + county + "  " + exam + "    " + video);
+                                        rowIndex++;
+                                    }
                                 }
+
+
                             }
                         }
                     }
                 }
-//                if (files != null) {
-//                    for (File file : files) {
-//                        if (file.isDirectory()) {
-//                            // 获取文件夹路径
-//                            String folderPath = file.getPath();
-//
-//                            // 分割文件夹路径
-//                            String[] folderNames = folderPath.split("\\\\");
-//                            int numLevels = folderNames.length;
-//
-//                            // 创建新行
-//                            Row row = sheet.createRow(rowIndex);
-//                            for (int i = 0; i < numLevels; i++) {
-//                                row.createCell(i).setCellValue(folderNames[i]);
-//                            }
-//
-//                            // 获取mp4文件
-//                            File[] mp4Files = file.listFiles((dir, name) -> name.toLowerCase().endsWith(".mp4"));
-//                            if (mp4Files != null && mp4Files.length > 0) {
-//                                String mp4FileName = mp4Files[0].getName();
-//                                row.createCell(numLevels).setCellValue(mp4FileName);
-//                            }
-//
-//                            rowIndex++;
-//
-//                            // 递归遍历下一级文件夹
-//                            traverseFolders(file, sheet, rowIndex);
-//                        }
-//                    }
-                }
             }
 
+        }
+    }
+
 }
 
 

+ 174 - 0
nbjk-admin/src/test/java/com/miaxis/test/ReadFileNameTest2.java

@@ -0,0 +1,174 @@
+package com.miaxis.test;
+
+import com.miaxis.NbjkApplication;
+import com.miaxis.dict.domain.DictCity;
+import com.miaxis.dict.service.IDictCityService;
+import com.miaxis.exam.domain.ExamInfo;
+import com.miaxis.exam.service.IExamInfoService;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.text.Collator;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Locale;
+
+@ActiveProfiles("dev")
+@SpringBootTest(classes = NbjkApplication.class)
+@RunWith(SpringRunner.class)
+public class ReadFileNameTest2 {
+
+
+    @Autowired
+    private IDictCityService dictCityService;
+
+
+    /**
+     * 用于通过文件夹导出考场信息
+     */
+    @Test
+    public void test() {
+        String folderPath = "F:/河南省"; // 替换为你的文件夹路径
+        String excelFilePath = "F:/exam_info.xlsx"; // 替换为你的Excel文件路径
+
+        try {
+            Workbook workbook = new XSSFWorkbook();
+            Sheet sheet = workbook.createSheet("Folders");
+
+            // 创建表头
+            Row headerRow = sheet.createRow(0);
+            headerRow.createCell(0).setCellValue("id");
+            headerRow.createCell(1).setCellValue("name");
+            headerRow.createCell(2).setCellValue("image");
+            headerRow.createCell(3).setCellValue("province_id");
+            headerRow.createCell(4).setCellValue("province");
+            headerRow.createCell(5).setCellValue("city_id");
+            headerRow.createCell(6).setCellValue("city");
+            headerRow.createCell(7).setCellValue("county_id");
+            headerRow.createCell(8).setCellValue("county");
+            headerRow.createCell(9).setCellValue("price");
+
+
+            // 获取根文件夹
+            File rootFolder = new File(folderPath);
+
+            // 递归遍历文件夹
+            traverseFolders(rootFolder, sheet, 1);
+
+            // 将工作簿写入Excel文件
+            FileOutputStream outputStream = new FileOutputStream(excelFilePath);
+            workbook.write(outputStream);
+            workbook.close();
+            outputStream.close();
+
+            System.out.println("Excel文件生成成功!");
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private void traverseFolders(File folder, Sheet sheet, int rowIndex) {
+        String province = folder.getName();
+        String provinceId = "41";
+        String city = null;
+        String cityId = null;
+        String county = null;
+        String countyId = null;
+        String exam = null;
+
+        if (folder.isDirectory()) {
+            File[] files = this.sortFiles(folder.listFiles());
+            //File[] files = folder.listFiles();
+            for (int i = 0; i < files.length; i++) {    //市级
+                // System.out.println(files[i].getName());
+                city = files[i].getName();
+                List<DictCity> dictCityList = dictCityService.getDictCityByName(city);
+
+                for (int j = 0; j < dictCityList.size(); j++) {
+                    cityId = dictCityList.get(j).getCode();
+                    if (cityId.startsWith("41")) {
+                        break;
+                    }
+                }
+
+
+                if (files[i].isDirectory()) {
+                    File[] files2 = this.sortFiles(files[i].listFiles());
+                    //File[] files2 = files[i].listFiles();
+                    for (int j = 0; j < files2.length; j++) {   //区级
+                        //   System.out.println("    "+files2[j].getName());
+                        county = files2[j].getName();
+                        List<DictCity> dictCity2List = dictCityService.getDictCityByName(county);
+
+                        for (int x = 0; x < dictCity2List.size(); x++) {
+                            countyId = dictCity2List.get(x).getCode();
+                            if (countyId.startsWith(cityId)) {
+                                break;
+                            }
+                        }
+
+                        if (files2[j].isDirectory()) {
+                            File[] files3 = this.sortFiles(files2[j].listFiles());
+                            //File[] files3 = files2[j].listFiles();
+                            for (int k = 0; k < files3.length; k++) { //考场
+                                //   System.out.println("        "+files3[k].getName());
+                                exam = files3[k].getName();
+
+                                //写入excel
+                                Row row = sheet.createRow(rowIndex);
+                                row.createCell(0).setCellValue("");
+                                row.createCell(1).setCellValue(exam);
+                                row.createCell(2).setCellValue("");
+                                row.createCell(3).setCellValue(provinceId);
+                                row.createCell(4).setCellValue(province);
+                                row.createCell(5).setCellValue(cityId);
+                                row.createCell(6).setCellValue(city);
+                                row.createCell(7).setCellValue(countyId);
+                                row.createCell(8).setCellValue(county);
+                                row.createCell(9).setCellValue(1);
+
+                               // System.out.println(province + "   " + city + "    " + county + "  " + exam);
+                                rowIndex++;
+
+                            }
+                        }
+                    }
+                }
+            }
+
+        }
+    }
+
+
+    public File[] sortFiles(File[] files) {
+
+        if (files != null) {
+            // 使用Collator类创建一个特定语言环境的排序器(这里使用中文)
+            Collator collator = Collator.getInstance(Locale.CHINA);
+            // 使用Comparator.comparing方法结合Lambda表达式按文件名的拼音首字母排序
+            Arrays.sort(files, Comparator.comparing(file -> file.getName(), collator));
+
+            // 输出排序后的文件名
+            for (File file : files) {
+                System.out.println(file.getName());
+            }
+        }
+        return files;
+    }
+
+
+}
+
+

+ 9 - 0
nbjk-admin/src/test/java/com/miaxis/test/ReadFileNameTest3.java

@@ -0,0 +1,9 @@
+package com.miaxis.test;
+
+public class ReadFileNameTest3 {
+
+    public static void main(String[] args) {
+
+    }
+
+}

+ 112 - 0
nbjk-admin/src/test/java/com/miaxis/test/Test2.java

@@ -0,0 +1,112 @@
+package com.miaxis.test;
+
+import net.sourceforge.pinyin4j.PinyinHelper;
+
+import java.io.*;
+import java.nio.file.Files;
+import java.text.Collator;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Locale;
+
+public class Test2 {
+
+    public static void main(String[] args) throws IOException {
+
+        String province = "河南省2";
+        String city = null;
+        String county = null;
+        String exam = null;
+        String video = null;
+
+        File root = new File("F:/河南省");
+        File[] files = root.listFiles(); //市级
+
+        sortFiles(files);
+
+        for (int i = 0; i < files.length; i++) {
+            city = files[i].getName();
+            File[] files2 = files[i].listFiles(); //区级
+            sortFiles(files2);
+
+            for (int j = 0; j < files2.length; j++) {
+                county = files2[j].getName();
+                File[] files3 = files2[j].listFiles(); //考场
+                sortFiles(files3);
+
+
+                for (int k = 0; k < files3.length; k++) {
+                    exam = files3[k].getName();
+                    File[] files4 = files3[k].listFiles(); //视频
+                    sortFiles(files4);
+
+                    // 替换为您想要创建的文件夹路径
+                    String folderPath = "F:/" + province + "/" + city + "/" + county + "/" + exam;
+                    File folder = new File(folderPath);
+                    boolean success = folder.mkdirs();
+                    if (success) {
+                        System.out.println("文件夹创建成功!");
+                    } else {
+                        System.out.println("文件夹创建失败或文件夹已存在。");
+                    }
+
+                    for (int l = 0; l < files4.length; l++) {
+                        if (l != 0) {
+                            break;
+                        }
+                        video = files4[0].getName();
+                        String videoPath = "F:/河南省/" + city + "/" + county + "/" + exam + "/" + video;
+                        FileInputStream fis = new FileInputStream(videoPath);
+                        String destinationFilePath = "F:/" + province + "/" + city + "/" + county + "/" + exam + "/" + video;
+
+                        FileOutputStream fos = new FileOutputStream(destinationFilePath);
+
+                        // 创建一个缓冲区,用于从源文件读取内容
+                        byte[] buffer = new byte[1024];
+                        int bytesRead;
+
+                        // 从源文件读取内容,并将其写入到目标文件中
+                        while ((bytesRead = fis.read(buffer)) != -1) {
+                            fos.write(buffer, 0, bytesRead);
+                        }
+                        System.out.println("文件复制成功!");
+
+                    }
+                    System.out.println(province + "   " + city + "    " + county + "  " + " " + exam + "   " + video);
+                }
+            }
+
+        }
+
+
+    }
+
+
+    public static File[] sortFiles(File[] files) {
+        if (files != null) {
+            Collator collator = Collator.getInstance(Locale.CHINA);
+            // 使用Comparator.comparing方法结合Lambda表达式按文件名的拼音首字母排序
+            //Arrays.sort(files, Comparator.comparing(file -> file.getName(), collator));
+            // 使用Pinyin4j将汉字转换为拼音,并按拼音进行排序
+            Arrays.sort(files, Comparator.comparing(file -> getPinyin(file.getName()), collator));
+        }
+        return files;
+    }
+
+    // 使用Pinyin4j将汉字转换为拼音
+    private static String getPinyin(String hanzi) {
+        StringBuilder pinyin = new StringBuilder();
+        for (char ch : hanzi.toCharArray()) {
+            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(ch);
+            if (pinyinArray != null && pinyinArray.length > 0) {
+                pinyin.append(pinyinArray[0]);
+            } else {
+                // 如果无法获取拼音,则原样输出汉字
+                pinyin.append(ch);
+            }
+        }
+        return pinyin.toString();
+    }
+
+
+}

+ 6 - 0
nbjk-common/pom.xml

@@ -273,6 +273,12 @@
             <version>2.16.0</version>
         </dependency>
 
+        <dependency>
+            <groupId>com.belerweb</groupId>
+            <artifactId>pinyin4j</artifactId>
+            <version>2.5.0</version>
+        </dependency>
+
     </dependencies>
 
 </project>