From 7d76b9e89baa29a1ae2e5e406d6d11dcca15325a Mon Sep 17 00:00:00 2001
From: cjl <276999030@qq.com>
Date: Fri, 18 Aug 2023 12:25:11 +0800
Subject: [PATCH] fix:实测补偿修复

---
 screen-api/src/main/java/com/moral/api/controller/SysTestController.java |  348 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 338 insertions(+), 10 deletions(-)

diff --git a/screen-api/src/main/java/com/moral/api/controller/SysTestController.java b/screen-api/src/main/java/com/moral/api/controller/SysTestController.java
index 8071454..de0360f 100644
--- a/screen-api/src/main/java/com/moral/api/controller/SysTestController.java
+++ b/screen-api/src/main/java/com/moral/api/controller/SysTestController.java
@@ -1,5 +1,6 @@
 package com.moral.api.controller;
 
+import cn.afterturn.easypoi.entity.ImageEntity;
 import cn.afterturn.easypoi.word.WordExportUtil;
 import com.alibaba.excel.EasyExcel;
 import com.alibaba.excel.ExcelWriter;
@@ -13,19 +14,28 @@
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.poi.xwpf.usermodel.XWPFDocument;
+import org.jfree.chart.ChartFactory;
+import org.jfree.chart.ChartUtils;
+import org.jfree.chart.JFreeChart;
+import org.jfree.chart.StandardChartTheme;
+import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
+import org.jfree.chart.plot.PiePlot;
+import org.jfree.data.general.DefaultPieDataset;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.Assert;
 import org.springframework.util.ResourceUtils;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.servlet.http.HttpServletResponse;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.OutputStream;
+import java.awt.*;
+import java.io.*;
 import java.lang.reflect.Field;
+import java.math.BigDecimal;
 import java.net.URLEncoder;
 import java.util.*;
+import java.util.List;
 import java.util.stream.Collectors;
 
 /**
@@ -123,12 +133,35 @@
         }
     }
 
+    @GetMapping("resultWordMonth")
+    public void resultWordMonth(HttpServletResponse response) {
+
+        String ks = "2020-06-01";
+        Date ksDate = DateUtils.convertDate(ks);
+        for(int i = 0;i<37;i++){
+            Date jsDate =  DateUtils.getLastDayOfCurrMonth(ksDate);;
+            String time = DateUtils.dateToDateString(ksDate,DateUtils.yyyy_MM_dd_EN);
+            String endTime = DateUtils.dateToDateString(jsDate,DateUtils.yyyy_MM_dd_EN);
+            List<SysTest> listAll = sysTestService.listAllAvg(time,endTime,null);
+            if(CollectionUtils.isEmpty(listAll)){
+                return;
+            }
+            List<SysTest> listDay = sysTestService.listAll(time,endTime,null);
+            String  fileName = "������������������.docx";
+            String time1 = DateUtils.dateToDateString(ksDate,"yyyy���MM���");
+            Long days = DateUtils.getQuotByDays(DateUtils.dateToDateString(listDay.get(0).getTime()),DateUtils.dateToDateString(listDay.get(listDay.size()-1).getTime()));
+            Map<String,Object> map = resultWeekMap(listAll,time1,listDay,days.intValue());
+            downloadWord(fileName, map, response,"������������������"+time1+".docx");
+           ksDate =  DateUtils.addMonths(ksDate,1);
+        }
+
+    }
 
     public static void downloadWord(String fileName, Map<String, Object> params, HttpServletResponse response,String newFileName) {
         String path = getPath(fileName);//��������� resources������
         try {
             //������������������
-            OutputStream out = new FileOutputStream("E:\\home\\week\\" + newFileName);
+            OutputStream out = new FileOutputStream("E:\\home\\mon\\" + newFileName);
             File rootFile = new File(ResourceUtils.getURL("classpath:").getPath());
             File file= new File(rootFile, path);
             XWPFDocument word = WordExportUtil.exportWord07(file.getPath(), params);
@@ -238,10 +271,245 @@
             resultMap.put(name+"TV", StringUtils.isNotEmpty(s.getVoc())?s.getVoc():"--");
             resultMap.put(name+"S", StringUtils.isNotEmpty(s.getSu())?s.getSu():"--");
         }
-        String bodyName = stringButterStrWeek(listAll);
-        resultMap.put("������������",bodyName);
+       // String bodyName = stringButterStrWeek(listAll);
+        HashMap<String, Integer> datas = new HashMap<>(3);
+        datas.put("������",10);
+        datas.put("������",20);
+        datas.put("������",40);
+        ImageEntity imageEntity = pieChart("������",datas, 500, 300);
+        resultMap.put("������������",imageEntity);
         return resultMap;
     }
+    private Map<String,Object> resultWeekMap(List<SysTest> listAll,String weekTime,List<SysTest> listDay,int days){
+        Map<String,Object> resultMap = new HashMap<>();
+        resultMap.put("������",weekTime);
+        String name = "���";
+        for(SysTest s : listAll){
+            if(s.getName().contains("���")){
+                name = "���";
+            }else if(s.getName().contains("���")){
+                name = "���";
+            }else {
+                name = "���";
+            }
+            resultMap.put(name+"AQI", StringUtils.isNotEmpty(s.getAqi())?s.getAqi():"--");
+            resultMap.put(name+"PM10", StringUtils.isNotEmpty(s.getPm10())?s.getPm10():"--");
+            resultMap.put(name+"PM2", StringUtils.isNotEmpty(s.getPm25())?s.getPm25():"--");
+            resultMap.put(name+"SO2", StringUtils.isNotEmpty(s.getSo2())?s.getSo2():"--");
+            resultMap.put(name+"NO2", StringUtils.isNotEmpty(s.getNo2())?s.getNo2():"--");
+            resultMap.put(name+"CO", StringUtils.isNotEmpty(s.getCo())?s.getCo():"--");
+            resultMap.put(name+"O3", StringUtils.isNotEmpty(s.getO3())?s.getO3():"--");
+            resultMap.put(name+"TV", StringUtils.isNotEmpty(s.getVoc())?s.getVoc():"--");
+            resultMap.put(name+"S", StringUtils.isNotEmpty(s.getSu())?s.getSu():"--");
+        }
+        // String bodyName = stringButterStrWeek(listAll);
+        /*HashMap<String, Integer> datas = new HashMap<>(3);
+        datas.put("������",10);
+        datas.put("������",20);
+        datas.put("������",40);
+        ImageEntity imageEntity = pieChart("������",datas, 500, 300);*/
+       // resultMap.put("������������",imageEntity);
+        resultMap.putAll(month(listDay,days+1));
+        return resultMap;
+    }
+
+    private Map<String,Object> month(List<SysTest> listDay, int days){
+        Map<String,Object> map = new HashMap<>();
+        map.put("���������",0);
+        map.put("���������",0);
+        map.put("������",0);
+        map.put("������",0);
+        map.put("������",0);
+        map.put("������",0);
+        map.put("������",0);
+        map.put("������",0);
+        map.put("������",0);
+        map.put("������",0);
+        map.put("������",0);
+        map.put("������",0);
+        map.put("������",0);
+        map.put("������",0);
+        map.put("������",0);
+        map.put("������",0);
+        map.put("������",0);
+        map.put("������",0);
+        map.put("������",0);
+        map.put("������",0);
+
+        Map<String,Integer> map1 = new HashMap<>();
+        Map<String,Integer> map11 = new HashMap<>();
+        Map<String,Integer> map111 = new HashMap<>();
+        Map<String,Integer> map2 = new HashMap<>();
+        Map<String,Integer> map22 = new HashMap<>();
+        Map<String,Integer> map222 = new HashMap<>();
+        List<Integer> list1 = Arrays.asList(0,0);
+        List<Integer> list2 = Arrays.asList(0,0);
+        List<Integer> list3 = Arrays.asList(0,0);
+        String name = "���";
+        String levelName = "���";
+        for(SysTest s : listDay){
+            if(s.getName().contains("���")){
+                name = "���";
+            }else if(s.getName().contains("���")){
+                name = "���";
+            }else {
+                name = "���";
+            }
+            if(Double.parseDouble(s.getAqi()) <= 50){
+                levelName = "���";
+            }else if(Double.parseDouble(s.getAqi()) <= 100){
+                levelName = "���";
+            }else if(Double.parseDouble(s.getAqi()) <= 150){
+                levelName = "���";
+            }else if(Double.parseDouble(s.getAqi()) <= 200){
+                levelName = "���";
+            }else if(Double.parseDouble(s.getAqi()) <= 300){
+                levelName = "���";
+            }else {
+                levelName = "���";
+            }
+
+            if(StringUtils.isNotEmpty(s.getSu()) && s.getName().contains("���")){
+                s.setSu(s.getSu().equals("-")?"���":s.getSu());
+                if(map1.containsKey(s.getSu())){
+                    map1.put(s.getSu(),map1.get(s.getSu())+1);
+                }else {
+                    map1.put(s.getSu(),1);
+                }
+            }
+            if(StringUtils.isNotEmpty(s.getSu())&& s.getName().contains("���")){
+                s.setSu(s.getSu().equals("-")?"���":s.getSu());
+                if(map11.containsKey(s.getSu())){
+                    map11.put(s.getSu(),map11.get(s.getSu())+1);
+                }else {
+                    map11.put(s.getSu(),1);
+                }
+            }
+            if(StringUtils.isNotEmpty(s.getSu())&&s.getName().contains("���")){
+                s.setSu(s.getSu().equals("-")?"���":s.getSu());
+                if(map111.containsKey(s.getSu())){
+                    map111.put(s.getSu(),map111.get(s.getSu())+1);
+                }else {
+                    map111.put(s.getSu(),1);
+                }
+            }
+            if(Double.parseDouble(s.getAqi()) <= 100){
+                if(map.containsKey(name+"������")){
+                    map.put(name+"������",Integer.parseInt(map.get(name+"������").toString())+1);
+                }else {
+                    map.put(name+"������",1);
+                }
+            }else {
+                if(map.containsKey(name+"������")){
+                    map.put(name+"������",Integer.parseInt(map.get(name+"������").toString())+1);
+                }else {
+                    map.put(name+"������",1);
+                }
+                if(StringUtils.isNotEmpty(s.getSu())&& s.getName().contains("���") && s.getSu().contains("PM2.5")){
+                    list1.set(0,list1.get(0)+1);
+                }else if(StringUtils.isNotEmpty(s.getSu())&& s.getName().contains("���") && s.getSu().contains("O3")){
+                    list1.set(1,list1.get(1)+1);
+                }
+                if(StringUtils.isNotEmpty(s.getSu())&& s.getName().contains("���") && s.getSu().contains("PM2.5")){
+                    list2.set(0,list2.get(0)+1);
+                }else if(StringUtils.isNotEmpty(s.getSu())&& s.getName().contains("���") && s.getSu().contains("O3")){
+                    list2.set(1,list2.get(1)+1);
+                }
+                if(StringUtils.isNotEmpty(s.getSu())&& s.getName().contains("���") && s.getSu().contains("PM2.5")){
+                    list3.set(0,list3.get(0)+1);
+                }else if(StringUtils.isNotEmpty(s.getSu())&& s.getName().contains("���") && s.getSu().contains("O3")){
+                    list3.set(1,list3.get(1)+1);
+                }
+            }
+            if(map.containsKey(name+levelName)){
+                map.put(name+levelName,Integer.parseInt(map.get(name+levelName).toString())+1);
+            }else {
+                map.put(name+levelName,1);
+            }
+            String level = levelName;
+            if(level.equals("���")){
+                level = "������������";
+            }else if(level.equals("���")){
+                level = "������������";
+            }else if(level.equals("���")){
+                level = "������������";
+            }else if(level.equals("���")){
+                level = "������������";
+            }
+            if(s.getName().contains("���")){
+                if(map2.containsKey(levelName)){
+                    map2.put(levelName,map2.get(levelName)+1);
+                }else {
+                    map2.put(levelName,1);
+                }
+            }else if(s.getName().contains("���")){
+                if(map22.containsKey(levelName)){
+                    map22.put(levelName,map22.get(levelName)+1);
+                }else {
+                    map22.put(levelName,1);
+                }
+            }else if(s.getName().contains("���")){
+                if(map222.containsKey(levelName)){
+                    map222.put(levelName,map222.get(levelName)+1);
+                }else {
+                    map222.put(levelName,1);
+                }
+            }
+
+        }
+
+        BigDecimal zb1 = BigDecimal.valueOf(Integer.parseInt(map.get("���������").toString())*100).divide(BigDecimal.valueOf(days),2,BigDecimal.ROUND_HALF_UP);
+        map.put("���������������",zb1.toString()+"%");
+        BigDecimal zb2 = BigDecimal.valueOf(Integer.parseInt(map.get("���������").toString())*100).divide(BigDecimal.valueOf(days),2,BigDecimal.ROUND_HALF_UP);
+        map.put("���������������",zb2.toString()+"%");
+        BigDecimal zb3 = BigDecimal.valueOf(Integer.parseInt(map.get("���������").toString())*100).divide(BigDecimal.valueOf(days),2,BigDecimal.ROUND_HALF_UP);
+        map.put("���������������",zb3.toString()+"%");
+        ImageEntity imageEntity2 = pieChart("���������������������������������",map2, 500, 300);
+        map.put("���������������",imageEntity2);
+        ImageEntity imageEntity22 = pieChart("���������������������������������",map22, 500, 300);
+        map.put("���������������",imageEntity22);
+        ImageEntity imageEntity222 = pieChart("���������������������������������",map222, 500, 300);
+        map.put("���������������",imageEntity222);
+        ImageEntity imageEntity1 = pieChart("������������������������������",map1, 500, 300);
+        map.put("���������������",imageEntity1);
+        ImageEntity imageEntity11 = pieChart("������������������������������",map11, 500, 300);
+        map.put("���������������",imageEntity11);
+        ImageEntity imageEntity111 = pieChart("������������������������������",map111, 500, 300);
+        map.put("���������������",imageEntity111);
+        StringBuffer buffer = new StringBuffer();
+        if(!list1.get(0).equals(0)){
+            BigDecimal num = BigDecimal.valueOf(list1.get(0)*100).divide(BigDecimal.valueOf(Integer.parseInt(map.get("���������").toString())),2,BigDecimal.ROUND_HALF_UP);
+            buffer.append("PM2.5���������"+num+"%������");
+        }
+        if(!list1.get(1).equals(0)){
+            BigDecimal num = BigDecimal.valueOf(list1.get(1)*100).divide(BigDecimal.valueOf(Integer.parseInt(map.get("���������").toString())),2,BigDecimal.ROUND_HALF_UP);
+            buffer.append("O3���������"+num+"%������");
+        }
+
+        map.put("���������������",buffer.toString().length()>0?"������������������"+buffer.toString():new StringBuffer());
+        StringBuffer buffer1 = new StringBuffer();
+        if(!list2.get(0) .equals(0)){
+            BigDecimal num = BigDecimal.valueOf(list2.get(0)*100).divide(BigDecimal.valueOf(Integer.parseInt(map.get("���������").toString())),2,BigDecimal.ROUND_HALF_UP);
+            buffer1.append("PM2.5���������"+num+"%������");
+        }
+        if(!list2.get(1) .equals(0)){
+            BigDecimal num = BigDecimal.valueOf(list2.get(1)*100).divide(BigDecimal.valueOf(Integer.parseInt(map.get("���������").toString())),2,BigDecimal.ROUND_HALF_UP);
+            buffer1.append("O3���������"+num+"%������");
+        }
+        map.put("���������������",buffer1.toString().length()>0?"������������������"+buffer1.toString():new StringBuffer());
+        StringBuffer buffer2 = new StringBuffer();
+        if(!list3.get(0) .equals(0)){
+            BigDecimal num = BigDecimal.valueOf(list3.get(0)*100).divide(BigDecimal.valueOf(Integer.parseInt(map.get("���������").toString())),2,BigDecimal.ROUND_HALF_UP);
+            buffer2.append("PM2.5���������"+num+"%������");
+        }
+        if(!list3.get(1).equals(0)){
+            BigDecimal num = BigDecimal.valueOf(list3.get(1)*100).divide(BigDecimal.valueOf(Integer.parseInt(map.get("���������").toString())),2,BigDecimal.ROUND_HALF_UP);
+            buffer2.append("O3���������"+num+"%������");
+        }
+        map.put("���������������",buffer2.toString().length()>0?"������������������"+buffer2.toString():new StringBuffer());
+        return map;
+    }
+
     private String stringButterStr(List<SysTest> listAll){
 
         // 0 ������1 ������ 2���������3���������
@@ -463,26 +731,86 @@
         return map;
 
     }
+    private static String tempImgPath="E:\\home\\image\\tempJfree.jpg";
+    private static byte[] imgToByte(){
+        File file = new File(tempImgPath);
+        byte[] buffer = null;
+        try {
+            FileInputStream fis = new FileInputStream(file);
+            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
+            byte[] b = new byte[1000];
+            int n;
+            while ((n = fis.read(b)) != -1) {
+                bos.write(b, 0, n);
+            }
+            fis.close();
+            bos.close();
+            buffer = bos.toByteArray();
+        } catch (IOException e) {
+            log.error(e.getMessage());
+        }
+        //������������������
+        //file.delete();
+        return buffer;
+    }
+    public ImageEntity pieChart(String title, Map<String, Integer> datas, int width, int height) {
+
+        //������������������
+        StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
+        //������������������
+        standardChartTheme.setExtraLargeFont(new Font("������", Font.BOLD, 20));
+        //���������������������
+        standardChartTheme.setRegularFont(new Font("������", Font.PLAIN, 15));
+        //���������������������
+        standardChartTheme.setLargeFont(new Font("������", Font.PLAIN, 15));
+        //������������������
+        ChartFactory.setChartTheme(standardChartTheme);
+
+        //������jfree���������������������������
+        DefaultPieDataset pds = new DefaultPieDataset();
+        datas.forEach(pds::setValue);
+        //���������������������������������������������������������������������tooltips������������������������
+        JFreeChart chart = ChartFactory.createPieChart(title, pds, true, false, false);
+        //���������������
+        chart.setTextAntiAlias(false);
+        PiePlot plot = (PiePlot) chart.getPlot();
+        plot.setNoDataMessage("������������");
+        //���������������������
+        plot.setIgnoreNullValues(true);
+        plot.setBackgroundAlpha(0f);
+        //������������������������
+        plot.setShadowPaint(new Color(255,255,255));
+        //���������������������(������{0})
+        plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({1})/{2}"));
+        try {
+            ChartUtils.saveChartAsJPEG(new File(tempImgPath), chart, width, height);
+        } catch (IOException e1) {
+            log.error("������������������������");
+        }
+        ImageEntity imageEntity = new ImageEntity(imgToByte(), width, height);
+        Assert.notNull(imageEntity.getData(),"������������������������������");
+        return imageEntity;
+    }
 
     public static void main(String[] args) {
-        /*String ks = "2020-06-01";
+        String ks = "2020-06-01";
         Date ksDate = DateUtils.convertDate(ks);
         for(int i = 0;i<37;i++){
             Date jsDate =  DateUtils.getLastDayOfCurrMonth(ksDate);
             System.out.println("���������"+DateUtils.dateToDateString(ksDate,DateUtils.yyyy_MM_dd_EN));
             System.out.println("���������"+DateUtils.dateToDateString(jsDate,DateUtils.yyyy_MM_dd_EN));
             ksDate =  DateUtils.addMonths(ksDate,1);
-        }*/
+        }
 
 
-        String ks = "2020-06-22";
+        /*String ks = "2020-06-22";
         Date ksDate = DateUtils.convertDate(ks);
         for(int i = 0;i<157;i++){
             Date jsDate =  DateUtils.addDays(ksDate,6);
             System.out.println("���������"+DateUtils.dateToDateString(ksDate,DateUtils.yyyy_MM_dd_EN));
             System.out.println("���������"+DateUtils.dateToDateString(jsDate,DateUtils.yyyy_MM_dd_EN));
             ksDate =  DateUtils.addDays(ksDate,7);
-        }
+        }*/
 
 
     }

--
Gitblit v1.8.0