南昌市物联网技防平台-公安版
zengh
2021-06-04 c926acaadc3d98fd8ba8926466b842f1edb3aee3
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
package org.springblade.jfpt.animalheat.controller;
 
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import org.apache.commons.codec.Charsets;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.springblade.common.entity.ReportReturnData;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.common.entity.AnimalHeatExcel;
import org.springblade.jfpt.animalheat.entity.BladeAnimalHeat;
import org.springblade.jfpt.animalheat.service.AnimalHeatService;
import org.springblade.jfpt.animalheat.vo.AnimalHeatVo;
import org.springblade.jfpt.pie.ImageData;
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.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
 
/**
 * 体温监测控制层
 * @time 2021-2-25
 */
 
@RestController
@RequestMapping("animalHeat/animalHeat")
@AllArgsConstructor
public class AnimalHeatController {
 
    private final AnimalHeatService animalHeatService;
 
    /**
     * 获取体温数据统计
     * @param animalHeatVo 条件参数 开始时间,结束时间
     * @return
     */
    @GetMapping("/getAnimalStatis")
    public R getAnimalStatis(AnimalHeatVo animalHeatVo,HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Allow-Credentials","true");
        return R.data(animalHeatService.getAnimalStatis(animalHeatVo));
    }
 
 
    /**
     * 体温数据的分页数据--图表的点击事件
     * @param animalHeatVo 查询条件  包含时间状态type 0:本日  1:本周   2:本月
     * @param query  查询页码
     * @return
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    public R<IPage<BladeAnimalHeat>> page(AnimalHeatVo animalHeatVo, Query query,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Allow-Credentials","true");
        return R.data(animalHeatService.selectAnimalHeatPage(Condition.getPage(query), animalHeatVo));
    }
 
    /**
     * 查询本周每天的体温数据数量
     * @return
     */
    @GetMapping("/selWeekDayAnimalStatis")
    public R selWeekDayAnimalStatis(HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Allow-Credentials","true");
        return R.data(animalHeatService.selWeekDayAnimalStatis());
    }
 
    /**
     * 导出体温数据列表
     * @param animalHeatVo 条件
     * @param response 返回域
     */
    @GetMapping("/export-animalHeat")
    public void exportAnimalHeat(AnimalHeatVo animalHeatVo,HttpServletResponse response) throws Exception{
        List<AnimalHeatExcel> list = animalHeatService.exportAnimalHeat(animalHeatVo);
//        response.setContentType("application/vnd.ms-excel");
//        response.setCharacterEncoding(Charsets.UTF_8.name());
//        String fileName = URLEncoder.encode("体温检测数据" + DateUtil.time(), Charsets.UTF_8.name());
//        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
//        ExcelWriter writer = new ExcelWriterBuilder()
//            .autoCloseStream(true)
//            .file(response.getOutputStream())
//            .head(AnimalHeatExcel.class)
//            .build();
//        // xlsx文件上上限是104W行左右,这里如果超过104W需要分Sheet
//        WriteSheet writeSheet = new WriteSheet();
//        writeSheet.setSheetName("target");
//        long lastBatchMaxId = 0L;
//        int limit = 500;
//        for (; ; ) {
//            List<AnimalHeatExcel> list = animalHeatService.exportAnimalHeat(animalHeatVo);
//            List list = orderService.queryByScrollingPagination(paymentDateTimeStart, paymentDateTimeEnd, lastBatchMaxId, limit);
//            if (list.isEmpty()) {
//                writer.finish();
//                break;
//            } else {
//                lastBatchMaxId = list.stream().map(AnimalHeatExcel::getId).max(Long::compareTo).orElse(Long.MAX_VALUE);
//                writer.write(list, writeSheet);
//            }
//        }
//        EasyExcel.write(response.getOutputStream(), AnimalHeatExcel.class).sheet("体温检测数据表").doWrite(list);
        ExcelUtil.export(response, "体温检测数据" + DateUtil.time(), "体温检测数据表", list,AnimalHeatExcel.class );
    }
 
    /**
     * 导出体温数据列表
     * @param animalHeatVo 条件
     * @param response 返回域
     */
    @GetMapping("/getAnimalHeatStatis")
    public void getAnimalHeatStatis(AnimalHeatVo animalHeatVo,HttpServletResponse response){
        //获取统计数据
        List<AnimalHeatExcel> animalHeatExcels = animalHeatService.exportAnimalHeat(animalHeatVo);
        List<ReportReturnData> reportReturnData = animalHeatService.getAnimalHeatPie(animalHeatVo);
 
        //创建jfreechart 画图对象
        DefaultPieDataset pds = new DefaultPieDataset();
 
        //遍历统计数据,将数据保存到饼图对象中
        for (ReportReturnData returnData:reportReturnData) {
            pds.setValue(returnData.getType(),returnData.getCount());
        }
 
        //声明excelWriter对象
        ExcelWriter excelWriter  = null;
        try {
            // 分别是:显示图表的标题、需要提供对应图表的DateSet对象、是否显示图例、是否生成贴士以及是否生成URL链接
            JFreeChart chart = ChartFactory.createPieChart("体温数据统计图", pds, false, false, true);
            // 如果不使用Font,中文将显示不出来
            Font font = new Font("宋体", Font.BOLD, 24);
            // 设置图片标题的字体
            chart.getTitle().setFont(font);
            // 得到图块,准备设置标签的字体
            PiePlot plot = (PiePlot) chart.getPlot();
            // 设置标签字体
            plot.setLabelFont(font);
            plot.setStartAngle(new Float(3.14f / 2f));
            // 设置plot的前景色透明度
            plot.setForegroundAlpha(0.7f);
            // 设置plot的背景色透明度
            plot.setBackgroundAlpha(0.0f);
            // 设置标签生成器(默认{0})
            // {0}:key {1}:value {2}:百分比 {3}:sum
            plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({1}占{2})"));
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
 
            //创建图片数据集合,对象
            ArrayList<ImageData> imageDatas = new ArrayList<>();
            ImageData imageData = new ImageData();
            try {
                // 将图表写入到ByteArrayOutputStream流中
                ChartUtilities.writeChartAsJPEG(bos, chart, 800, 700);
                //将字节数组输出流写入到输入流中
                InputStream inputStream = new ByteArrayInputStream(bos.toByteArray());
                //excel 文件名称
                String fileName = "体温统计报表" + System.currentTimeMillis() + ".xlsx";
                //封装数据到图表
                imageData.setInputStream(inputStream);
                imageDatas.add(imageData);
 
                //设置响应体
                response.setContentType("application/vnd.ms-excel");
                response.setCharacterEncoding(Charsets.UTF_8.name());
                fileName = URLEncoder.encode(fileName, Charsets.UTF_8.name());
                response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
                //写入
                excelWriter  = EasyExcel.write(response.getOutputStream()).build();
 
                //shell0
                WriteSheet writeSheet = EasyExcel.writerSheet(0, "图表").head(ImageData.class).build();
                excelWriter.write(imageDatas, writeSheet);
 
                //shell1
                WriteSheet writeSheet0 = EasyExcel.writerSheet(1, "数据列表").head(AnimalHeatExcel.class).build();
                excelWriter.write(animalHeatExcels, writeSheet0);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //完成后会关闭流
            excelWriter.finish();
        }
    }
 
}