Browse Source

新建查询中增加根据目录查询和导出图片功能

zhuoning 4 years ago
parent
commit
7720d272b8

+ 2 - 0
huimv-video/huimv-video-manager/src/main/java/com/huimv/manager/controller/ImageCatalogController.java

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.huimv.manager.service.IImageCatalogSerivce;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.CrossOrigin;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
@@ -16,6 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
  * @Author : ZhuoNing
  * @Create : 2020-12-25
  **/
+@CrossOrigin
 @RestController
 @RequestMapping("/catalog")
 public class ImageCatalogController {

+ 36 - 18
huimv-video/huimv-video-manager/src/main/java/com/huimv/manager/controller/ImageManagerController.java

@@ -28,6 +28,7 @@ import javax.servlet.http.HttpServletResponse;
 import java.awt.image.BufferedImage;
 import java.io.*;
 //import java.sql.Timestamp;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 
@@ -93,13 +94,13 @@ public class ImageManagerController {
     public JSONObject listImage(@RequestParam(value = "startDate", required = true) String startDate, @RequestParam(value = "endDate", required = true) String endDate, @RequestParam(value = "catalog", required = false) String catalog, @RequestParam(value = "pageNo", required = true) int pageNo, @RequestParam(value = "pageSize", required = true) int pageSize) {
         log.info("startDate>>" + startDate);
         log.info("endDate>>" + endDate);
-        log.info("catalog>>"+catalog);
+        log.info("catalog>>" + catalog);
         log.info("pageNo>>" + pageNo);
         log.info("pageSize>>" + pageSize);
         // 搜索记录时,catalog是非必要字段;
         // 导出图片时,catalog是必要字段;
         //
-        List<ImagePathEntity> imageList = imageService.getImageList(startDate, endDate, pageNo, pageSize,catalog);
+        List<ImagePathEntity> imageList = imageService.getImageList(startDate, endDate, pageNo, pageSize, catalog);
         JSONArray imageJa = new JSONArray();
         for (ImagePathEntity imageEntity : imageList) {
 //            JSONObject dataJo = (JSONObject) JSONObject.toJSON(imageEntity);
@@ -118,7 +119,14 @@ public class ImageManagerController {
             imageJa.add(imageJo);
         }
         // 读取记录数
-        int total = imageService.getTotal(startDate, endDate);
+        int total = 0;
+        if(catalog != null && !catalog.trim().isEmpty()){
+            total= imageService.getTotal(startDate, endDate,catalog);
+        }else{
+            total= imageService.getTotal(startDate, endDate);
+        }
+
+
         JSONObject outJo = new JSONObject();
         outJo.put("data", imageJa);
         outJo.put("total", total);
@@ -138,10 +146,10 @@ public class ImageManagerController {
     public Result getPreviousImage(@RequestParam(value = "imageId", required = true) int imageId) {
         //
         ImagePathEntity imagePathEntity = imageService.getOneImage("previous", imageId);
-        if(imagePathEntity == null){
-            return new Result(10001,"未查询到任何记录.",false);
-        }else{
-            return new Result(ResultCode.SUCCESS,(JSONObject)JSONObject.toJSON(imagePathEntity));
+        if (imagePathEntity == null) {
+            return new Result(10001, "未查询到任何记录.", false);
+        } else {
+            return new Result(ResultCode.SUCCESS, (JSONObject) JSONObject.toJSON(imagePathEntity));
         }
     }
 
@@ -158,10 +166,10 @@ public class ImageManagerController {
     public Result getNextImage(@RequestParam(value = "imageId", required = true) int imageId) {
         //
         ImagePathEntity imagePathEntity = imageService.getOneImage("next", imageId);
-        if(imagePathEntity == null){
-            return new Result(10001,"未查询到任何记录.",false);
-        }else{
-            return new Result(ResultCode.SUCCESS,(JSONObject)JSONObject.toJSON(imagePathEntity));
+        if (imagePathEntity == null) {
+            return new Result(10001, "未查询到任何记录.", false);
+        } else {
+            return new Result(ResultCode.SUCCESS, (JSONObject) JSONObject.toJSON(imagePathEntity));
         }
     }
 
@@ -306,7 +314,6 @@ public class ImageManagerController {
         out.write(FileUtils.readFileToByteArray(zipFile));
         out.flush();
         out.close();
-
         // 输出客户端结束后,删除压缩包
         if (zipFile.exists()) {
             zipFile.delete();
@@ -314,20 +321,27 @@ public class ImageManagerController {
     }
 
     @RequestMapping("/exportImage")
-    public void exportImage(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "startDate", required = false) String startDate, @RequestParam(value = "endDate", required = false) String endDate, @RequestParam(value = "imageIds", required = false) String imageIds) throws Exception {
+    public void exportImage(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "startDate", required = false) String startDate, @RequestParam(value = "endDate", required = false) String endDate, @RequestParam(value = "imageIds", required = false) String imageIds, @RequestParam(value = "catalog", required = false) String catalog) throws Exception {
 //        if(imageIds == null || imageIds.trim().isEmpty()){
 //            log.error("输入参数[imageIds]不能为空.");
 //            return ;
 //        }
         log.info("输入参数[startDate]>>" + startDate);
         log.info("输入参数[endDate]>>" + endDate);
+        log.info("输入参数[catalog]>>" + catalog);
         log.info("输入参数[imageIds]>>" + imageIds);
         if (startDate == null && endDate == null && imageIds == null) {
             log.error("日期区间或指定图片导出二选一");
             return;
         }
-        // 读取图片文件
-        List<ImagePathEntity> imageList = imageService.getExportImageList(startDate, endDate, imageIds);
+        List<ImagePathEntity> imageList = new ArrayList<>();
+        if (catalog != null && !catalog.trim().isEmpty()) {
+            // 读取图片文件
+            imageList = imageService.getExportImageList(startDate, endDate, imageIds,catalog);
+        } else {
+            // 读取图片文件
+            imageList = imageService.getExportImageList(startDate, endDate, imageIds);
+        }
         if (imageList.size() == 0) {
             log.error("未读取到任何需要导出的图片.");
             return;
@@ -335,8 +349,10 @@ public class ImageManagerController {
 //        System.out.println("imageList.size>>"+imageList.size());
         int i = 0;
         File[] fileList = new File[imageList.size()];
+        String camera = "";
         for (ImagePathEntity imagePathEntity : imageList) {
             File imageFile = new File(imagePathEntity.getImgPath());
+            camera = imagePathEntity.getName();
             fileList[i++] = imageFile;
         }
         //定义根路径
@@ -348,11 +364,14 @@ public class ImageManagerController {
             file.mkdir();
         }
         String name = "图片压缩包下载";
-        String fileName = name + new Date().getTime();
+        if (catalog != null && !catalog.trim().isEmpty()) {
+            name = camera;
+        }
+        String fileName = name +"_"+ dateUtil.formatLongTimeForTime(new Date().getTime());
         String zipFileName = fileName + ".zip";
         File zipFile = null;
         String path = rootPath + "temp_download";
-// 打成压缩包
+        // 打成压缩包
         zipFile = new File(path + "/" + zipFileName);
         FileOutputStream zipFos = new FileOutputStream(zipFile);
         ArchiveOutputStream archOut = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipFos);
@@ -377,7 +396,6 @@ public class ImageManagerController {
         out.write(FileUtils.readFileToByteArray(zipFile));
         out.flush();
         out.close();
-
         // 输出客户端结束后,删除压缩包
         if (zipFile.exists()) {
             zipFile.delete();

+ 16 - 0
huimv-video/huimv-video-manager/src/main/java/com/huimv/manager/dao/repo/ImagePathRepo.java

@@ -48,6 +48,9 @@ public interface ImagePathRepo extends JpaRepository<ImagePathEntity, Integer>,
     @Query(nativeQuery = true, value = "SELECT count(id) FROM image_path WHERE DATE_FORMAT(create_date,'%Y-%m-%d')>=DATE_FORMAT(:startDate,'%Y-%m-%d') AND DATE_FORMAT(create_date,'%Y-%m-%d')<=DATE_FORMAT(:endDate,'%Y-%m-%d')")
     int getTotal(String startDate, String endDate);
 
+    @Query(nativeQuery = true, value = "SELECT count(id) FROM image_path WHERE DATE_FORMAT(create_date,'%Y-%m-%d')>=DATE_FORMAT(:startDate,'%Y-%m-%d') AND DATE_FORMAT(create_date,'%Y-%m-%d')<=DATE_FORMAT(:endDate,'%Y-%m-%d') AND name=:catalog")
+    int getTotal(String startDate, String endDate,String catalog);
+
     /**
      * @Method      : getExportImageList
      * @Description : 读取导出的图片
@@ -62,6 +65,19 @@ public interface ImagePathRepo extends JpaRepository<ImagePathEntity, Integer>,
     List<ImagePathEntity> getExportImageListByStartAndEndDate(String startDate, String endDate);
 
     /**
+     * @Method      : getExportImageListByStartAndEndDate
+     * @Description : 读取指定目录的图片
+     * @Params      : [startDate, endDate, catalog]
+     * @Return      : java.util.List<com.huimv.manager.dao.entity.ImagePathEntity>
+     * 
+     * @Author      : ZhuoNing
+     * @Date        : 2021/1/26       
+     * @Time        : 19:03
+     */
+    @Query(nativeQuery = true, value = "SELECT * FROM image_path WHERE DATE_FORMAT(create_date,'%Y-%m-%d')>=DATE_FORMAT(:startDate,'%Y-%m-%d') AND DATE_FORMAT(create_date,'%Y-%m-%d')<=DATE_FORMAT(:endDate,'%Y-%m-%d') AND name=:catalog")
+    List<ImagePathEntity> getExportImageListByStartAndEndDate(String startDate, String endDate,String catalog);
+
+    /**
      * @Method      : getExportImageListById
      * @Description : 
      * @Params      : [imageIdList]

+ 7 - 1
huimv-video/huimv-video-manager/src/main/java/com/huimv/manager/service/IImageService.java

@@ -52,7 +52,7 @@ public interface IImageService {
      * @Date        : 2021/1/22       
      * @Time        : 16:48
      */
-    int getTotal(String startDate, String endDate);
+    int getTotal(String startDate, String endDate,String catalog);
 
     /**
      * @Method      : getExportImageList
@@ -89,4 +89,10 @@ public interface IImageService {
      * @Time        : 19:42
      */
     ImagePathEntity getOneImageDetail(String imageId);
+
+
+    List<ImagePathEntity> getExportImageList(String startDate, String endDate, String imageIds, String catalog);
+
+
+    int getTotal(String startDate, String endDate);
 }

+ 24 - 3
huimv-video/huimv-video-manager/src/main/java/com/huimv/manager/service/impl/ImageServiceImpl.java

@@ -73,10 +73,7 @@ public class ImageServiceImpl implements IImageService {
      */
     @Override
     public List<ImagePathEntity> getImageList(String startDate, String endDate, int pageNo, int pageSize, String catalog) {
-        System.out.println("pageNo>>" + pageNo);
-        System.out.println("pageSize>>" + pageSize);
         int start = (pageNo - 1) * pageSize;
-        System.out.println("start>>" + start);
         if (catalog != null && !catalog.isEmpty()) {
             // 按照日期区间,读取图片列表
             return imagePathRepo.listImage(startDate, endDate, start, pageSize,catalog);
@@ -96,6 +93,12 @@ public class ImageServiceImpl implements IImageService {
      * @Time : 16:50
      */
     @Override
+    public int getTotal(String startDate, String endDate,String catalog) {
+        // 根据条件读取总记录数
+        return imagePathRepo.getTotal(startDate, endDate,catalog);
+    }
+
+    @Override
     public int getTotal(String startDate, String endDate) {
         // 根据条件读取总记录数
         return imagePathRepo.getTotal(startDate, endDate);
@@ -120,6 +123,24 @@ public class ImageServiceImpl implements IImageService {
     }
 
     @Override
+    public List<ImagePathEntity> getExportImageList(String startDate, String endDate, String imageIds,String catalog) {
+        // 以指定图片导出优先条件
+        if (imageIds == null || imageIds.trim().isEmpty()) {
+            // 用startDate,endDate读取需要导出的图片
+            return imagePathRepo.getExportImageListByStartAndEndDate(startDate, endDate,catalog);
+        } else {
+            // 指定需要导出的图片
+            String[] idArray = imageIds.split(",");
+            List imageIdList = new ArrayList();
+            for (String id : idArray) {
+                imageIdList.add(id);
+            }
+            // 读取导出图片
+            return imagePathRepo.getExportImageListById(imageIdList);
+        }
+    }
+
+    @Override
     public ImagePathEntity getOneImage(String type, int imageId) {
         if (type.trim().equalsIgnoreCase("next")) {
             // 读取下一张照片

+ 8 - 0
huimv-video/huimv-video-manager/src/main/java/com/huimv/manager/utils/DateUtil.java

@@ -26,6 +26,14 @@ public class DateUtil {
         return time;
     }
 
+    public String formatLongTimeForTime(Long dateLong){
+        Date date =new Date(dateLong);//以1429339937748为bai毫秒数du实例化zhi一个Date对象dao
+        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");//设置转化格式
+        String time=sdf.format(date);//将Date对象转化为yyyy-MM-dd形式的字符串zhuan
+        System.out.println(time);//输出字符串
+        return time;
+    }
+
     public String formatTimestamp(Timestamp timestamp) {
         DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         //方法一