From 6b41868dbc883d7baf35130d688d6671fc61d767 Mon Sep 17 00:00:00 2001
From: cjl <909710561@qq.com>
Date: Tue, 02 Apr 2024 11:42:43 +0800
Subject: [PATCH] fix:补充提交

---
 screen-api/src/main/java/com/moral/api/controller/CruiserController.java |  272 +++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 216 insertions(+), 56 deletions(-)

diff --git a/screen-api/src/main/java/com/moral/api/controller/CruiserController.java b/screen-api/src/main/java/com/moral/api/controller/CruiserController.java
index c7ca622..23c3e71 100644
--- a/screen-api/src/main/java/com/moral/api/controller/CruiserController.java
+++ b/screen-api/src/main/java/com/moral/api/controller/CruiserController.java
@@ -4,10 +4,11 @@
 import cn.hutool.poi.word.WordUtil;
 import com.deepoove.poi.XWPFTemplate;
 import com.deepoove.poi.config.Configure;
+import com.deepoove.poi.data.PictureRenderData;
 import com.deepoove.poi.data.RowRenderData;
-import com.deepoove.poi.data.TextRenderData;
 import com.deepoove.poi.policy.HackLoopTableRenderPolicy;
 import com.moral.api.entity.Dustld;
+import com.moral.api.exception.BusinessException;
 import com.moral.api.pojo.bo.ExcelBO;
 import com.moral.api.pojo.dto.historySecondCruiser.HistorySecondCruiserResultDTO;
 import com.moral.api.pojo.dust.DustForm;
@@ -16,6 +17,7 @@
 import com.moral.api.service.SpecialDeviceService;
 import com.moral.constant.ResponseCodeEnum;
 import com.moral.constant.ResultMessage;
+import com.moral.util.DateUtils;
 import com.moral.util.WebUtils;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiImplicitParam;
@@ -25,17 +27,16 @@
 import org.apache.commons.io.FileUtils;
 import org.apache.poi.xwpf.usermodel.XWPFDocument;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.system.ApplicationHome;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.ObjectUtils;
 import org.springframework.util.ResourceUtils;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.multipart.MultipartHttpServletRequest;
-
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.*;
-import java.net.URL;
 import java.net.URLEncoder;
 import java.text.ParseException;
 import java.util.ArrayList;
@@ -152,19 +153,21 @@
         return ObjectUtils.isEmpty(daily) ? ResultMessage.ok() : ResultMessage.ok(daily);
     }
 
+
+
     /**
      * ���������������������
-     * @param id
+     * @param params
      * @return
      */
-    @GetMapping("loadDaily")
-    public ResultMessage loadDaily(Integer id){
-
-        DailyVo dailyVo = specialDeviceService.loadDaily(id);
-        if (dailyVo==null){
-            return ResultMessage.fail(ResponseCodeEnum.TARGET_IS_NULL.getCode(), ResponseCodeEnum.TARGET_IS_NULL.getMsg());
+    @PostMapping("loadDaily")
+    public void loadDaily( @RequestBody Map<String,Object> params,HttpServletResponse response) throws IOException {
+        List<Integer> ids = (List<Integer>) params.get("ids");
+        List<DailyVo> dailyVos = specialDeviceService.loadDaily(ids);
+        if (ObjectUtils.isEmpty(dailyVos)){
+            throw new BusinessException("���������������������");
         }
-        return new ResultMessage(ResponseCodeEnum.SUCCESS.getCode(), ResponseCodeEnum.SUCCESS.getMsg(),dailyVo);
+        dailyDocx(dailyVos,response);
     }
 
     /**
@@ -235,23 +238,19 @@
      */
     @PostMapping("dailyDustlds")
     @Transactional
-    public ResultMessage  dailyDustlds(@RequestBody Map<String,Object> params, HttpServletResponse response) throws IOException {
+    public void dailyDustlds(@RequestBody Map<String,Object> params, HttpServletResponse response,HttpServletRequest request) throws IOException {
 
 //        Map<String, Object> params = WebUtils.getParametersStartingWith(request, null);
         if (!params.containsKey("mac") || !params.containsKey("startTime") || !params.containsKey("endTime")){
-            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
+            return;
         }
-        System.out.println(CruiserController.class.getResource("/word/���������������������.docx").getPath());
-        URL resource = getClass().getResource("/word/���������������������.docx");
-        String path = resource.getPath();
-        System.out.println(path);
         Map<String, Object> map = dustldService.dailyDustlds(params);
         if (ObjectUtils.isEmpty(map)){
-            return ResultMessage.fail(ResponseCodeEnum.TARGET_IS_NULL.getCode(), ResponseCodeEnum.TARGET_IS_NULL.getMsg());
+            throw new BusinessException("���������������������");
         }
-        test(map,response);
+        test(map,response,request);
 
-        return ResultMessage.ok();
+
     }
 
 
@@ -314,7 +313,7 @@
     }
 
 
-    public  void test( Map<String, Object> params,HttpServletResponse response) throws IOException {
+    public  void test( Map<String, Object> params,HttpServletResponse response,HttpServletRequest request) throws IOException {
 
         try {
             //InputStream  inputStream = WordUtil.class.getClassLoader().getResourceAsStream("word/���������������������.docx");
@@ -347,38 +346,39 @@
                 maps.put("table2", dustForm.getRoad());
                 maps.put("table3", dustForm.getValue());
                 detailList.add(maps);
-               /* RowRenderData row0 = RowRenderData.build(i+1+"",dustForm.getRoad(), dustForm.getValue()+"");
-                rsList1.add(row0);*/
             }
-
-            /*for (int i = 0; i < mapArrayList.size(); i++) {
+            List<Map> detailList2 = new ArrayList<>();
+            Map maps = new HashMap();
+           /* maps.put("table4", "");
+            maps.put("table5", "");
+            maps.put("table6", "");
+            maps.put("table7", map.get("date1"));
+            maps.put("table8", map.get("date2"));
+            detailList2.add(maps);*/
+            for (int i = 0; i < mapArrayList.size(); i++) {
                 Map<String, Object> map1 = mapArrayList.get(i);
-                RowRenderData row = RowRenderData.build(i + 1 + "", map1.get("road").toString(), map1.get("value1").toString(), map1.get("value2").toString(), map1.get("value3").toString());
-                rsList3.add(row);
+                maps = new HashMap();
+                maps.put("time4", i+1);
+                maps.put("time5", map1.get("road").toString());
+                maps.put("time6", map1.get("value1").toString());
+                maps.put("time7", map1.get("value2").toString());
+                maps.put("time8", map1.get("value3").toString());
+                detailList2.add(maps);
             }
-            if (!ObjectUtils.isEmpty(mapArrayList)){
-                map.put("table3", new MiniTableRenderData(rsList3));
-            }
-            map.put("table1", new MiniTableRenderData(header1, rsList1));*/
             String data = params.get("data").toString();
             map.put("data",data);
             map.put("time",params.get("time").toString());
             map.put("date1",params.get("date1").toString());
             map.put("date2",params.get("date2").toString());
-            //map.put("detailList",detailList);
-            /*File docxFile = new File("���������������������.docx");
-            FileUtils.copyToFile(inputStream,docxFile);
-            inputStream.close();*/
-            //XWPFDocument compile = WordExportUtil.exportWord07(filePath,map);
             HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
-            Configure config = Configure.newBuilder().bind("detailList", policy).build();
-
+            Configure config = Configure.newBuilder().bind("detailList", policy).bind("detailList2", policy).build();
             XWPFTemplate compile = XWPFTemplate.compile(filePath,config).render(new HashMap<String, Object>(){{
                 put("time",map.get("time"));
                 put("data",map.get("data"));
-                put("data1",map.get("data1"));
-                put("data2",map.get("data2"));
+                put("time1",map.get("date1").toString());
+                put("time2",map.get("date2").toString());
                 put("detailList", detailList);
+                put("detailList2", detailList2);
             }});
 
             response.setContentType("multipart/form-data");
@@ -390,22 +390,6 @@
             out.flush();
             out.close();
             compile.close();
-
-
-
-
-/*
-            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
-            compile.write(byteArrayOutputStream);
-            byte[] bytes = byteArrayOutputStream.toByteArray();
-            response.setCharacterEncoding("utf-8");
-            response.setContentType("multipart/form-data");
-            response.setHeader("Content-Disposition", "attachment;filename=���������������������.docx");
-
-            ServletOutputStream outputStream = response.getOutputStream();
-            outputStream.write(bytes);
-            outputStream.flush();
-            outputStream.close();*/
         } catch (IOException e) {
             log.error ("������������������������������������������", e);
         } catch (Exception e) {
@@ -415,4 +399,180 @@
     }
 
 
+
+    public  void dailyDocx( List<DailyVo> dailyVos,HttpServletResponse response) throws IOException {
+//        String path = getPath(fileName);
+        //������jar���������������
+        ApplicationHome applicationHome = new ApplicationHome(getClass());
+        //���jar������������������������������upload������������������������������������
+        String path = applicationHome.getSource().getParentFile().toString() + "\\static\\img";
+        try {
+            //InputStream  inputStream = WordUtil.class.getClassLoader().getResourceAsStream("word/���������������������.docx");
+            InputStream stream = WordUtil.class.getClassLoader().getResourceAsStream("word/���������������������������.docx");
+            //������������������
+            File file = new File("word/���������������������������.docx");
+            //���������������������������������������������������������������������������������������������
+            FileUtils.copyInputStreamToFile(stream, file);
+            //��������������������������������������������� ������������������������
+            String filePath = file.getAbsolutePath();
+
+
+
+            //������������������������������������������������������{{}}������������������������,map������������key������������������������������������������
+            HashMap<String, Object> map = new HashMap<>();
+            //������������������������
+            //RowRenderData header1 = RowRenderData.build(new TextRenderData("6495ED", "������"), new TextRenderData("6495ED", "������"),new TextRenderData("6495ED", "���������������������/������������"));
+
+            //���������������������������������������������������������list������������������������������foreach������������������
+            List<Map> detailList1 = new ArrayList<>();
+            List<Map> detailList2 = new ArrayList<>();
+            List<Map> detailList3 = new ArrayList<>();
+            List<Map> detailList4 = new ArrayList<>();
+            List<Map> detailList5 = new ArrayList<>();
+            List<Map> detailList6 = new ArrayList<>();
+            List<Map> detailList7 = new ArrayList<>();
+            List<Map> detailList8 = new ArrayList<>();
+            StringBuilder builder = new StringBuilder();
+            List<Map> pic1 = new ArrayList<>();
+            List<Map> pic2 = new ArrayList<>();
+            List<Map> pic3 = new ArrayList<>();
+            List<Map> pic4 = new ArrayList<>();
+            List<Map> pic5 = new ArrayList<>();
+            List<Map> pic6 = new ArrayList<>();
+            List<Map> pic7 = new ArrayList<>();
+            List<Map> pic8 = new ArrayList<>();
+
+//            picMap.put("urlImg", Pictures.ofUrl(str, PictureType.JPEG).size(width, height).create());
+//            picMap.put("urlImg",new PictureRenderData(500, 500, "G:\\home\\3d423e3cb05d7edc35c38e3173af2a0d.jpg"));
+//            picMap.put("urlImg1",new PictureRenderData(500, 500, "G:\\home\\3d423e3cb05d7edc35c38e3173af2a0d.jpg"));
+//            picMap.put("urlImg", Pictures.ofUrl("G:\\home\\3d423e3cb05d7edc35c38e3173af2a0d.jpg", PictureType.JPEG).size(500, 500).create());
+//            picMap.put("urlImg1", Pictures.ofUrl("G:\\home\\3d423e3cb05d7edc35c38e3173af2a0d.jpg", PictureType.JPEG).size(500, 500).create());
+            int i =1;
+            for (DailyVo dailyVo : dailyVos) {
+                Map picMap1 = new HashMap();
+                Map picMap2 = new HashMap();
+                Map picMap3 = new HashMap();
+                Map picMap4 = new HashMap();
+                Map picMap5 = new HashMap();
+                Map picMap6 = new HashMap();
+                Map picMap7 = new HashMap();
+                Map picMap8 = new HashMap();
+                List<String> images = dailyVo.getImages();
+                if (!ObjectUtils.isEmpty(images)){
+
+                    picMap1.put("urlImg"+i,new PictureRenderData(500, 500, path+"\\"+images.get(0)));
+//                    picMap1.put("urlImg"+i,new PictureRenderData(500, 500, path+"\\0a4ab467-304d-4871-948f-55aa7e820975.png"));
+                    pic1.add(picMap1);
+                    picMap2.put("urlImg"+i,new PictureRenderData(500, 500, path+"\\"+images.get(1)));
+//                    picMap2.put("urlImg"+i,new PictureRenderData(500, 500, path+"\\0a4ab467-304d-4871-948f-55aa7e820975.png"));
+                    pic2.add(picMap2);
+                    picMap3.put("urlImg"+i,new PictureRenderData(500, 500, path+"\\"+images.get(2)));
+                    pic3.add(picMap3);
+                    picMap4.put("urlImg"+i,new PictureRenderData(500, 500, path+"\\"+images.get(3)));
+                    pic4.add(picMap4);
+                    picMap5.put("urlImg"+i,new PictureRenderData(500, 500, path+"\\"+images.get(4)));
+                    pic5.add(picMap5);
+                    picMap6.put("urlImg"+i,new PictureRenderData(500, 500, path+"\\"+images.get(5)));
+                    pic6.add(picMap6);
+                    picMap7.put("urlImg"+i,new PictureRenderData(500, 500, path+"\\"+images.get(6)));
+                    pic7.add(picMap7);
+                    picMap8.put("urlImg"+i,new PictureRenderData(500, 500, path+"\\"+images.get(7)));
+                    pic8.add(picMap8);
+                    i++;
+                }
+                detailList1.add(dailyVo.getCode());
+                detailList2.add(dailyVo.getCode());
+                detailList3.add(dailyVo.getCode());
+                detailList4.add(dailyVo.getCode());
+                detailList5.add(dailyVo.getCode());
+                detailList6.add(dailyVo.getCode());
+                detailList7.add(dailyVo.getCode());
+                detailList8.add(dailyVo.getCode());
+                builder.append("("+dailyVo.getCode().get("mac")+")������������������������������������������������������������������������������������������7���������������������PM2.5���������������"+dailyVo.getCode().get("avgPm25")+"ug/m3���PM10���������������"+dailyVo.getCode().get("avgPm10")+"ug/m3���NO2���������������"
+                        +dailyVo.getCode().get("avgNO2")+"ug/m3���CO���������������"+dailyVo.getCode().get("avgCO")+"mg/m3���SO2���������������"+dailyVo.getCode().get("avgSO2")+"ug/m3���O3���������������"+dailyVo.getCode().get("avgO3")+"ug/m3���VOCs������������"+dailyVo.getCode().get("avgVOC")+"mg/m3���"+"\n"+"    ");
+            }
+/*            for (HashMap<String, Object> list : lists) {
+                detailList1.add(list);
+                detailList2.add(list);
+                detailList3.add(list);
+                detailList4.add(list);
+                detailList5.add(list);
+                detailList6.add(list);
+                detailList7.add(list);
+                detailList8.add(list);
+            builder.append("("+list.get("mac")+")������������������������������������������������������������������������������������������7���������������������PM2.5���������������"+list.get("avgPm25")+"ug/m3���PM10���������������"+list.get("avgPm10")+"ug/m3���NO2���������������"
+                    +list.get("avgNO2")+"ug/m3���CO���������������"+list.get("avgCO")+"mg/m3���SO2���������������"+list.get("avgSO2")+"ug/m3���O3���������������"+list.get("avgO3")+"ug/m3���VOCs������������"+list.get("avgVOC")+"mg/m3���"+"\n"+"    ");
+            }*/
+/*            List<Map> pic = new ArrayList<>();
+            for (MultipartFile multipartFile : files) {
+                Map picMap = new HashMap();
+                InputStream inputStream = multipartFile.getInputStream();
+                picMap.put("urlImg", Pictures.ofStream(inputStream, PictureType.JPEG).size(500, 500).create());
+                pic.add(picMap);
+            }*/
+
+
+            String name = dailyVos.get(0).getName();
+            String type = dailyVos.get(0).getType();
+            map.put("code",builder);
+            map.put("time1",DateUtils.getCurCNDate());
+            map.put("name",name);
+            map.put("type",type);
+//            map.put("pho",new PictureRenderData(100, 100, "G:\\home\\3d423e3cb05d7edc35c38e3173af2a0d.jpg"));
+//            map.put("pho1",new PictureRenderData(500, 500, path+"\\0a4ab467-304d-4871-948f-55aa7e820975.png"));
+
+            HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
+            Configure config = Configure.newBuilder().bind("detailList1", policy).bind("detailList2", policy).bind("detailList3", policy).bind("detailList4", policy)
+                    .bind("detailList5", policy).bind("detailList6", policy).bind("detailList7", policy).bind("detailList8", policy).build();
+            XWPFTemplate compile = XWPFTemplate.compile(filePath,config).render(new HashMap<String, Object>(){{
+                put("code",map.get("code"));
+                put("time1",map.get("time1"));
+//                put("pho", map.get("pho"));
+//                put("pho1", map.get("pho1"));
+                put("name",map.get("name"));
+                put("type",map.get("type"));
+                put("detailList1", detailList1);
+                put("detailList2", detailList2);
+                put("detailList3", detailList3);
+                put("detailList4", detailList4);
+                put("detailList5", detailList5);
+                put("detailList6", detailList6);
+                put("detailList7", detailList7);
+                put("detailList8", detailList8);
+                put("picList1", pic1);
+                put("picList2", pic2);
+                put("picList3", pic3);
+                put("picList4", pic4);
+                put("picList5", pic5);
+                put("picList6", pic6);
+                put("picList7", pic7);
+                put("picList8", pic8);
+            }});
+
+
+
+
+            response.setContentType("multipart/form-data");
+            // ���������������
+            response.addHeader("Content-Disposition", "attachment;fileName=ce.docx" );
+//            response.setHeader("Content-Disposition", "attachment; filename=\"" + name+type+"������������" + "\"");
+            response.setCharacterEncoding("UTF-8");
+            //���������������
+/*            File file1 = new File("G:\\test\\test2.docx");
+            FileOutputStream out = new FileOutputStream(file1);
+            compile.write(out);*/
+            //���������������
+            OutputStream out = response.getOutputStream();
+            compile.write(out);
+            out.flush();
+            out.close();
+            compile.close();
+        } catch (IOException e) {
+            log.error ("������������������������������������������", e);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+
+    }
+
 }

--
Gitblit v1.8.0