Kaynağa Gözat

/student/basestudentinfo/page 优化sql

大数据与最优化研究所 1 yıl önce
ebeveyn
işleme
ee2a6dcebe

+ 166 - 0
src/main/java/com/xjrsoft/common/utils/FileZipUtil.java

@@ -0,0 +1,166 @@
+package com.xjrsoft.common.utils;
+
+import java.io.*;
+import java.util.List;
+import java.util.Map;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+/**
+ * @author dzx
+ * @Description 文件压缩工具
+ * 2023/12/5
+ */
+public class FileZipUtil {
+    public static ByteArrayInputStream inputStream2ByteArray(InputStream in, boolean isClose) {
+        byte[] byteArray = null;
+        try {
+            int total = in.available();
+            byteArray = new byte[total];
+            in.read(byteArray);
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            if (isClose) {
+                try {
+                    in.close();
+                } catch (Exception e2) {
+                    //zou.log.Logger.getServerlog().error(" ر   ʧ  ");
+                }
+            }
+        }
+        return new ByteArrayInputStream(byteArray);
+    }
+
+    public static byte[] byteArray(InputStream in, boolean isClose) {
+        byte[] byteArray = null;
+        try {
+            int total = in.available();
+            byteArray = new byte[total];
+            in.read(byteArray);
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            if (isClose) {
+                try {
+                    in.close();
+                } catch (Exception e2) {
+                    //zou.log.Logger.getServerlog().error(" ر   ʧ  ");
+                }
+            }
+        }
+        return byteArray;
+    }
+
+    public static void zipFiles(File[] srcfile, File zipfile) {
+        byte[] buf = new byte[1024];
+        try {
+            // ZipOutputStream ࣺ    ļ    ļ  е ѹ
+            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
+            for (int i = 0; i < srcfile.length; i++) {
+                FileInputStream in = new FileInputStream(srcfile[i]);
+                out.putNextEntry(new ZipEntry(srcfile[i].getName()));
+                int len;
+                while ((len = in.read(buf)) > 0) {
+                    out.write(buf, 0, len);
+                }
+                out.closeEntry();
+                in.close();
+            }
+            out.close();
+        } catch (Exception e) {
+            // TODO Auto-generated catch block
+            //zou.log.Logger.getServerlog().error("ѹ  ʧ   "+e.toString());
+        }
+    }
+
+    public static void zipFiles(List<File> fileList, File zipfile) {
+        byte[] buf = new byte[1024];
+        try {
+            // ZipOutputStream ࣺ    ļ    ļ  е ѹ
+            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
+            for(File file : fileList) {
+                FileInputStream in = new FileInputStream(file);
+                out.putNextEntry(new ZipEntry(file.getName()));
+                int len;
+                while ((len = in.read(buf)) > 0) {
+                    out.write(buf, 0, len);
+                }
+                out.closeEntry();
+                in.close();
+            }
+            out.close();
+        } catch (Exception e) {
+            // TODO Auto-generated catch block
+
+            //zou.log.Logger.getServerlog().error("ѹ  ʧ   "+e.toString());
+        }
+    }
+
+
+    public static byte[] byteAryMap2Zip(Map<String,byte[]> byteAryMap) {
+        byte[] buf = new byte[1024];
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        try {
+            // ZipOutputStream ࣺ    ļ    ļ  е ѹ
+            ZipOutputStream out = new ZipOutputStream(baos);
+            for(String fileName : byteAryMap.keySet()) {
+                byte[] byteAry = byteAryMap.get(fileName);
+                ByteArrayInputStream in = new ByteArrayInputStream(byteAry);
+                out.putNextEntry(new ZipEntry(fileName));
+                int len;
+                while ((len = in.read(buf)) > 0) {
+                    out.write(buf, 0, len);
+                }
+                out.closeEntry();
+                in.close();
+            }
+            out.close();
+        } catch (Exception e) {
+            // TODO Auto-generated catch block
+
+            //zou.log.Logger.getServerlog().error("ѹ  ʧ   "+e.toString());
+        }
+        return baos.toByteArray();
+    }
+
+    public static boolean createFloder(File file) {
+        if (!file.exists()) {
+            file.mkdir();
+        }
+        return true;
+    }
+    public static boolean createFile(File file) {
+
+        if (!file.exists()) {
+            try {
+                file.createNewFile();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        return true;
+    }
+    public static String saveFile(String fileName,byte[] fileContent) {
+        File file1 = new File("D:\\     ļ \\flew\\");
+        File file = new File("D:\\     ļ \\flew\\"+fileName);
+        FileOutputStream out = null;
+        createFloder(file1);
+        createFile(file);
+        try {
+            out = new FileOutputStream(file);
+            out.write(fileContent);
+            out.flush();
+            out.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }finally {
+            try {
+                out.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        return file.getPath();
+    }
+}

+ 2 - 0
src/main/java/com/xjrsoft/module/system/service/IFileService.java

@@ -14,4 +14,6 @@ import com.xjrsoft.module.system.entity.File;
 public interface IFileService extends MPJBaseService<File> {
 
     boolean deleteFile(String encode);
+
+    Boolean downloadFileByZip(Long folderId);
 }

+ 29 - 0
src/main/java/com/xjrsoft/module/system/service/impl/FileServiceImpl.java

@@ -1,11 +1,18 @@
 package com.xjrsoft.module.system.service.impl;
 
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.github.yulichang.base.MPJBaseServiceImpl;
+import com.xjrsoft.common.utils.FileZipUtil;
 import com.xjrsoft.module.system.entity.File;
 import com.xjrsoft.module.system.mapper.FileMapper;
 import com.xjrsoft.module.system.service.IFileService;
 import org.springframework.stereotype.Service;
 
+import java.io.FileInputStream;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 /**
  * <p>
  * 文件关联关系表 服务实现类
@@ -21,4 +28,26 @@ public class FileServiceImpl extends MPJBaseServiceImpl<FileMapper, File> implem
     public boolean deleteFile(String encode) {
         return false;
     }
+
+    @Override
+    public Boolean downloadFileByZip(Long folderId) {
+        List<File> fileList = this.list(Wrappers.<File>query().lambda().eq(File::getFolderId, folderId));
+
+        //生命一个Map,将所有文件装进去,map的key是完整的文件名
+        Map<String, byte[]> byteAryMap = new HashMap<>();
+
+//        for (int i = 0; i < fileList.size(); i++) {
+//            java.io.File file = new java.io.File(fileList.get(i).getFileUrl());
+//            byte[] bytes = FileZipUtil.byteArray(new FileInputStream(file), true);
+//            byteAryMap.put(file.getName(), );
+//        }
+
+        //执行下面这段代码就可以拿到压缩之后的byte数组
+        byte[] resultBtyeAry_temp = FileZipUtil.byteAryMap2Zip(byteAryMap);
+
+
+        return true;
+    }
+
+
 }

+ 12 - 4
src/main/resources/mapper/student/BaseStudentSchoolRollMapper.xml

@@ -4,13 +4,21 @@
         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.xjrsoft.module.student.mapper.BaseStudentSchoolRollMapper">
     <select id="getMobilePage" parameterType="com.xjrsoft.module.student.dto.BaseStudentInfoPageDto" resultType="com.xjrsoft.module.student.vo.BaseStudentInfoPageVo">
-        SELECT t2.id,t2.name AS student_name,t1.student_id,t2.mobile AS phone,concat(t5.name, ' ', t5.mobile) AS teacher_name,t4.name AS class_name,
-        (SELECT mobile FROM base_student_family_member WHERE delete_mark = 0 AND user_id = t2.id AND is_guardian = 1) AS guardian_phone FROM base_student t1
+        SELECT t2.id,
+        t2.name AS student_name,
+        t1.student_id,
+        t2.mobile AS phone,
+        CONCAT(t5.name, ' ', t5.mobile) AS teacher_name,
+        t4.name AS class_name,
+        t6.mobile AS guardian_phone
+        FROM base_student t1
         INNER JOIN xjr_user t2 ON t1.user_id = t2.id
         INNER JOIN base_student_school_roll t3 ON t1.user_id = t3.user_id
         LEFT JOIN base_class t4 ON t3.class_id = t4.id
         LEFT JOIN xjr_user t5 ON t4.teacher_id = t5.id
-        WHERE t2.delete_mark = 0 AND t1.delete_mark = 0
+        LEFT JOIN base_student_family_member t6 ON t6.user_id = t2.id AND t6.delete_mark = 0 AND t6.is_guardian = 1
+        WHERE t2.delete_mark = 0
+        AND t1.delete_mark = 0
         <if test="dto.gradeId != null">
             and t4.grade_id = #{dto.gradeId}
         </if>
@@ -26,7 +34,7 @@
             or t5.name like concat('%', #{dto.keyWord}, '%')
             or t1.student_id like concat('%', #{dto.keyWord}, '%')
             or t2.mobile like concat('%', #{dto.keyWord}, '%')
-            or (SELECT mobile FROM base_student_family_member WHERE delete_mark = 0 AND user_id = t2.id AND is_guardian = 1 and mobile like concat('%', #{dto.keyWord}, '%'))
+            or t6.mobile LIKE concat('%', #{dto.keyWord}, '%')
             )
         </if>
     </select>