cjl
2023-11-15 28776b56db3bbd2fbbfd64394e40aa11a6b7ea29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
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;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.moral.api.dto.SysTestDTO;
import com.moral.api.entity.SysTest;
import com.moral.api.service.SysTestService;
import com.moral.util.DateUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
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.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;
 
/**
 * @ClassName SysTestController
 * @Description TODO
 * @Author @lizijie
 * @Date 2023-08-01 13:45
 * @Version 1.0
 */
@Slf4j
@Api(tags = {"测试补偿数据导出"})
@RestController
@RequestMapping("/systest")
public class SysTestController {
    @Autowired
    private SysTestService sysTestService;
 
 
    @GetMapping("resultWord")
    public void adresultWordd(HttpServletResponse response,String time,String endTime) {
        String ks = "2023-08-06";
        for(int i =1;i<2;i++){
            Date ksDate = DateUtils.convertDate(ks);
            List<SysTest> listAll = sysTestService.listAll(ks,null,null);
            if(CollectionUtils.isEmpty(listAll)){
                return;
            }
            String endTimeStr = DateUtils.dateToDateString(ksDate,DateUtils.yyyyMMdd_EN);
            String  fileName = "空气质量日报.docx";
            Map<String,Object> map = resultMap(listAll);
            downloadWord(fileName, map, response,"空气质量日报"+endTimeStr+".docx");
            ks = DateUtils.dateToDateString(DateUtils.addDays(ksDate,1),DateUtils.yyyy_MM_dd_EN);
        }
    }
 
 
    @GetMapping("excelM")
    public void excelM(HttpServletResponse response,String time,String endTime) {
        // for(int i = 495;i<1095;i++){
        String ks = "2020-06-01";
        Date ksDate = DateUtils.convertDate(ks);
        List<SysTest> result = new ArrayList<>();
        for(int i = 0;i<37;i++){
            Date jsDate =  DateUtils.getLastDayOfCurrMonth(ksDate);
            List<SysTest> listAll = sysTestService.listAllAvg(DateUtils.dateToDateString(ksDate,DateUtils.yyyy_MM_dd_EN),DateUtils.dateToDateString(jsDate,DateUtils.yyyy_MM_dd_EN),null);
            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);
            result.addAll(listAll);
        }
 
        if(CollectionUtils.isEmpty(result)){
            return;
        }
        String path = "D:\\空气质量月数据.xls";
        WriteSheet build = EasyExcel.writerSheet("因子信息").head(SysTestDTO.class).build();
        // 创建 easyexcel 的写出类构造器 参数 告诉构造器,我的excel将来要写到哪里,以及execl中数据是基于哪个java对象模板创建的
        ExcelWriter excelWriter = EasyExcel.write(path, SysTestDTO.class).build();
        excelWriter.write(result, build);
        excelWriter.finish();
    }
 
    @GetMapping("excelDay")
    public void excelDay(HttpServletResponse response,String time,String endTime) {
 
        List<SysTest> listAll = sysTestService.listAll(time,endTime,null);
 
        if(CollectionUtils.isEmpty(listAll)){
            return;
        }
        String path = "D:\\空气质量日数据.xls";
        WriteSheet build = EasyExcel.writerSheet("因子信息").head(SysTestDTO.class).build();
        // 创建 easyexcel 的写出类构造器 参数 告诉构造器,我的excel将来要写到哪里,以及execl中数据是基于哪个java对象模板创建的
        ExcelWriter excelWriter = EasyExcel.write(path, SysTestDTO.class).build();
        excelWriter.write(listAll, build);
        excelWriter.finish();
    }
 
    @GetMapping("resultWordWeek")
    public void resultWordWeek(HttpServletResponse response) {
        String ks = "2023-06-19";
        Date ksDate = DateUtils.convertDate(ks);
        int num = 157;
        for(int i = 0;i<8;i++){
            Date jsDate =  DateUtils.addDays(ksDate,6);
            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;
            }
            String  fileName = "空气质量周报.docx";
            String time1 = DateUtils.dateToDateString(ksDate,"yyyy.MM.dd");
            String endTime1= DateUtils.dateToDateString(jsDate,"yyyy.MM.dd");
            Map<String,Object> map = resultWeekMap(listAll,num,time1+"-"+endTime1);
            downloadWord(fileName, map, response,"空气质量周报"+time+".docx");
            ksDate =  DateUtils.addDays(ksDate,7);
            num++;
        }
    }
 
    @GetMapping("resultWordMonth")
    public void resultWordMonth(HttpServletResponse response) {
 
        String ks = "2023-07-01";
        Date ksDate = DateUtils.convertDate(ks);
        for(int i = 0;i<1;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\\week1\\" + newFileName);
            File rootFile = new File(ResourceUtils.getURL("classpath:").getPath());
            File file= new File(rootFile, path);
            XWPFDocument word = WordExportUtil.exportWord07(file.getPath(), params);
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(newFileName, "utf-8"));
            //OutputStream out = response.getOutputStream();
            word.write(out);
            out.flush();
            out.close();
        } catch (Exception e) {
            log.error ("导出失败,请联系网站管理员!", e);
        }
    }
 
 
    public static String getPath(String filename) {
        filename = "/word/" + filename;
        return filename;
    }
 
 
    /**
     实体类转Map
     */
    public static Map<String, Object> entityToMap(Object object) {
        Map<String, Object> map = new HashMap<>();
        for (Field field : object.getClass().getDeclaredFields()) {
            try {
                boolean flag = field.isAccessible();
                field.setAccessible(true);
                Object o = field.get(object);
                map.put(field.getName(), o);
                field.setAccessible(flag);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }
 
    private Map<String,Object> resultMap(List<SysTest> listAll){
        String first = "2020-06-22";
        String firstYear = "2020";
 
        Map<String,Object> resultMap = new HashMap<>();
        Date endTime =listAll.get(0).getTime();
        String endTimeStr = DateUtils.dateToDateString(endTime,DateUtils.yyyy_MM_dd_EN);
        List<String> endTimeList= Arrays.asList(endTimeStr.split("-"));
        if(Integer.parseInt(firstYear)<Integer.parseInt(endTimeList.get(0))){
            firstYear = endTimeList.get(0);
            first = endTimeList.get(0)+"-01-01";
        }
        String firstM = String.valueOf(Integer.parseInt(endTimeList.get(1)));
        String firstD = String.valueOf(Integer.parseInt(endTimeList.get(2)));
        Long day = DateUtils.getQuotByDays(first,endTimeStr)+1  ;
        resultMap.put("年",firstYear);
        resultMap.put("月",firstM);
        resultMap.put("日",firstD);
        resultMap.put("期",day.intValue());
        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 = stringButterStr(listAll);
 
        resultMap.put("主要内容",bodyName);
        return resultMap;
    }
    private Map<String,Object> resultWeekMap(List<SysTest> listAll,int week,String weekTime){
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("周",week);
        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("主要内容",bodyName);
        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高,低
        int type = 0;
        // TODO: 2023-08-01 第一次循环比较最高值或者最低值
        int aqiHigh = Integer.parseInt(listAll.get(0).getAqi());
        int aqiMiddle = Integer.parseInt(listAll.get(1).getAqi());
        int aqiLow = Integer.parseInt(listAll.get(2).getAqi());
        List<Integer> numList = Arrays.asList(aqiHigh,aqiMiddle,aqiLow);
        StringBuffer stringBuffer = new StringBuffer();
        Set<Integer> stringSet = new HashSet<>(numList);
        List<Integer> listTest = stringSet.stream().collect(Collectors.toList());
        Collections.sort(listTest);
        int aqiHighResult = listTest.get(listTest.size()-1);
        int aqiLowResult = listTest.get(0);
        if(stringSet.size() == 1){
            type = 0;
            stringBuffer.append("整体空气质量(AQI)基本相同,无太大区别。");
            return stringBuffer.toString();
        }else if(stringSet.size() == 3){
            type = 3;
        }else {
            int count = Collections.frequency(numList, listTest.get(0));
            if(count == 1){
                type = 2;
            }else {
                type = 1;
            }
        }
        int sizeHighResult = 0;
        int sizeLowResult = 0;
        String highName = "沙岗村";
        String lowName = "沙岗村";
        //1 两高 2两低
        if(type == 1){
            SysTest ResultAqi = listAll.stream().min(Comparator.comparing(SysTest::getAqi)).get();
            if(ResultAqi.getName().contains("沙岗村")){
                lowName = "沙岗村";
                highName = "友爱村、中间村";
            }else if(ResultAqi.getName().contains("友爱村")){
                lowName = "友爱村";
                highName = "沙岗村、中间村";
            } else if(ResultAqi.getName().contains("中间村")){
                lowName = "中间村";
                highName = "沙岗村、友爱村";
            }
 
        }else if(type == 2){
            SysTest ResultAqi = listAll.stream().max(Comparator.comparing(SysTest::getAqi)).get();
            if(ResultAqi.getName().contains("沙岗村")){
                highName = "沙岗村";
                lowName = "友爱村、中间村";
            }else if(ResultAqi.getName().contains("友爱村")){
                highName = "沙岗村";
                lowName = "友爱村、中间村";
            } else if(ResultAqi.getName().contains("中间村")){
                highName = "沙岗村";
                lowName = "友爱村、中间村";
            }
        }else if(type == 3){
            if(aqiHigh == aqiHighResult){
                sizeHighResult = 0;
            }
            if(aqiMiddle == aqiHighResult){
                sizeHighResult = 1;
            }
            if(aqiLow == aqiHighResult){
                sizeHighResult = 2;
            }
            if(aqiHigh == aqiLowResult){
                sizeLowResult = 0;
            }
            if(aqiMiddle == aqiLowResult){
                sizeLowResult = 1;
            }
            if(aqiLow == aqiLowResult){
                sizeLowResult = 2;
            }
            SysTest HighResult = listAll.get(sizeHighResult);
            SysTest LowResult = listAll.get(sizeLowResult);
 
            if(HighResult.getName().contains("友爱村")){
                highName = "友爱村";
            }else if(HighResult.getName().contains("中尖村")){
                highName = "中尖村";
            }
            if(LowResult.getName().contains("友爱村")){
                lowName = "友爱村";
            }else if(LowResult.getName().contains("中尖村")){
                lowName = "中尖村";
            }
        }
        SysTest result0 = listAll.get(0);
        SysTest result1 = listAll.get(1);
        SysTest result2 = listAll.get(2);
        Map<String,String> pm10 = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getPm10(),result1.getPm10(),result2.getPm10());
        Map<String,String> pm25 = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getPm25(),result1.getPm25(),result2.getPm25());
        Map<String,String> so = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getSo2(),result1.getSo2(),result2.getSo2());
        Map<String,String> no = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getNo2(),result1.getNo2(),result2.getNo2());
        Map<String,String> co = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getCo(),result1.getCo(),result2.getCo());
        Map<String,String> o3 = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getO3(),result1.getO3(),result2.getO3());
        stringBuffer.append(highName+"整体空气质量(AQI)较高,"+lowName+"整体空气质量(AQI)较低。");
        if(pm10.isEmpty()&& pm25.isEmpty()&&so.isEmpty()&& no.isEmpty()&& co.isEmpty()&& o3.isEmpty()){
            return stringBuffer.toString();
        }
        stringBuffer.append("其中:");
        if(!pm10.isEmpty()){
            stringBuffer.append("PM10较高的是:"+pm10.get("high")+";较低的是:"+pm10.get("low")+"。");
        }
        if(!pm25.isEmpty()){
            stringBuffer.append("PM2.5较高的是:"+pm25.get("high")+";较低的是:"+pm25.get("low")+"。");
        }
        if(!so.isEmpty()){
            stringBuffer.append("SO2较高的是:"+so.get("high")+";较低的是:"+so.get("low")+"。");
        }
        if(!no.isEmpty()){
            stringBuffer.append("NO2较高的是:"+no.get("high")+";较低的是:"+no.get("low")+"。");
        }
        if(!co.isEmpty()){
            stringBuffer.append("CO较高的是:"+co.get("high")+";较低的是:"+co.get("low")+"。");
        }
        if(!o3.isEmpty()){
            stringBuffer.append("O3较高的是:"+o3.get("high")+";较低的是:"+o3.get("low")+"。");
        }
        return stringBuffer.toString();
 
    }
 
    private String stringButterStrWeek(List<SysTest> listAll){
        StringBuffer stringBuffer = new StringBuffer();
        SysTest result0 = listAll.get(0);
        SysTest result1 = listAll.get(1);
        SysTest result2 = listAll.get(2);
        Map<String,String> pm10 = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getPm10(),result1.getPm10(),result2.getPm10());
        Map<String,String> pm25 = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getPm25(),result1.getPm25(),result2.getPm25());
        Map<String,String> so = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getSo2(),result1.getSo2(),result2.getSo2());
        Map<String,String> no = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getNo2(),result1.getNo2(),result2.getNo2());
        Map<String,String> co = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getCo(),result1.getCo(),result2.getCo());
        Map<String,String> o3 = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getO3(),result1.getO3(),result2.getO3());
        if(pm10.isEmpty()&& pm25.isEmpty()&&so.isEmpty()&& no.isEmpty()&& co.isEmpty()&& o3.isEmpty()){
            return stringBuffer.toString();
        }
        stringBuffer.append("其中:");
        if(!pm10.isEmpty()){
            stringBuffer.append("PM10较高的是:"+pm10.get("high")+";较低的是:"+pm10.get("low")+"。");
        }
        if(!pm25.isEmpty()){
            stringBuffer.append("PM2.5较高的是:"+pm25.get("high")+";较低的是:"+pm25.get("low")+"。");
        }
        if(!so.isEmpty()){
            stringBuffer.append("SO2较高的是:"+so.get("high")+";较低的是:"+so.get("low")+"。");
        }
        if(!no.isEmpty()){
            stringBuffer.append("NO2较高的是:"+no.get("high")+";较低的是:"+no.get("low")+"。");
        }
        if(!co.isEmpty()){
            stringBuffer.append("CO较高的是:"+co.get("high")+";较低的是:"+co.get("low")+"。");
        }
        if(!o3.isEmpty()){
            stringBuffer.append("O3较高的是:"+o3.get("high")+";较低的是:"+o3.get("low")+"。");
        }
        return stringBuffer.toString();
    }
    private Map<String,String> nameCode(String name1,String name2,String name3,String nums1,String nums2,String nums3){
        double num1 = StringUtils.isNotEmpty(nums1)?Double.parseDouble(nums1):0;
        double num2 = StringUtils.isNotEmpty(nums2)?Double.parseDouble(nums2):0;
        double num3 = StringUtils.isNotEmpty(nums3)?Double.parseDouble(nums3):0;
        Map<String,String> map = new HashMap<>();
        if(num1 ==num2 && num2 ==num3){
            return map;
        }
        name1 = name1.substring(3,name1.length());
        name2 = name2.substring(3,name2.length());
        name3 = name3.substring(3,name3.length());
        if(num1 ==num2 && num2 < num3){
            map.put("high",name3);
            map.put("low",name1+"、"+name2);
            return map;
        }else if(num1 ==num2 && num2 > num3){
            map.put("low",name3);
            map.put("high",name1+"、"+name2);
            return map;
        }
        if(num1 == num3 && num2 < num3){
            map.put("low",name2);
            map.put("high",name1+"、"+name3);
            return map;
        }else if(num1 == num3 && num2 > num3){
            map.put("high",name2);
            map.put("low",name1+"、"+name3);
            return map;
        }
        if(num2 == num3 && num1 < num2){
            map.put("low",name1);
            map.put("high",name2+"、"+name3);
            return map;
        }else if(num2 == num3 && num1 > num2){
            map.put("high",name1);
            map.put("low",name2+"、"+name3);
            return map;
        }
        List<Double> numList = Arrays.asList(num1,num2,num3);
        int high = numList.indexOf(Collections.max(numList));
        int low = numList.indexOf(Collections.min(numList));
        if(high == 0){
            map.put("high",name1);
        }else if(high == 1){
            map.put("high",name2);
        }else if(high == 2){
            map.put("high",name3);
        }
        if(low == 0){
            map.put("low",name1);
        }else if(low == 1){
            map.put("low",name2);
        }else if(low == 2){
            map.put("low",name3);
        }
        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";
        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 = "2023-06-22";
        for(int i =1;i<46;i++){
            Date ksDate = DateUtils.convertDate(ks);
            System.out.println("开始:"+DateUtils.dateToDateString(ksDate,DateUtils.yyyy_MM_dd_EN));
 
            ks = DateUtils.dateToDateString(DateUtils.addDays(ksDate,1),DateUtils.yyyy_MM_dd_EN);
            System.out.println("结束:"+ks);
        }
 
        /*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);
        }*/
 
 
    }
 
 
}