lxw
2022-10-19 7f782442a9655e27becb247e4025886c9bc39cb3
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
package com.whyc.service;
 
import cn.afterturn.easypoi.entity.ImageEntity;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.google.common.collect.Maps;
import com.whyc.dto.BattGroupStationInfoWithFileParam;
import com.whyc.dto.Response;
import com.whyc.pojo.BattgroupData;
import com.whyc.pojo.FileParam;
import com.whyc.pojo.StationInfo;
import com.whyc.util.MathUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Decoder;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
 
@Service
public class ExcelExportService {
 
    @Autowired
    private FileParamService fileParamService;
 
    @Autowired
    private BattGroupDataService battGroupDataService;
 
    @Autowired
    private StationInfoService stationInfoService;
 
    public void resTestReport(BattGroupStationInfoWithFileParam info, HttpServletResponse response) throws IOException {
        HashMap<String, Object> map = Maps.newHashMap();
        //总体评价:内阻/电压/连接条/?容量
        int resGoodCount = 0;
        int volGoodCount = 0;
        int chainResGoodCount = 0;
        FileParam fileParam = info.getFileParam();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
        fileParam.setTestTimeStr(format.format(fileParam.getTestTime()));
        List<BattgroupData> dataList = info.getDataList();
        for(BattgroupData data : dataList){
            data.setMonNumStr("#"+data.getMonNum());
            if(!data.isHighResFlag1() && !data.isHighResFlag2()){
                resGoodCount +=1;
                data.setResEvaluation("优");
            }else if(data.isHighResFlag1()){
                data.setResEvaluation("差");
            }else{
                data.setResEvaluation("很差");
            }
            if(!data.isLowVolFlag() && !data.isHighVolFlag()){
                volGoodCount +=1;
            }
            if(!data.isHighChainResFlag()){
                chainResGoodCount +=1;
            }
        };
        info.setResGoodCount(resGoodCount);
        info.setVolGoodCount(volGoodCount);
        info.setChainResGoodCount(chainResGoodCount);
 
        map.put("dto", info);
        //图片
        BASE64Decoder decoder = new BASE64Decoder();
        if(!info.getVolPic().equals("")) {
            ImageEntity volImage = new ImageEntity();
            volImage.setData(decoder.decodeBuffer(info.getVolPic().substring(info.getVolPic().indexOf(",") + 1)));
            volImage.setColspan(6);
            volImage.setRowspan(17);
 
            map.put("volImage",volImage);
        }
        if(!info.getResPic().equals("")) {
            ImageEntity resImage = new ImageEntity();
            resImage.setData(decoder.decodeBuffer(info.getResPic().substring(info.getResPic().indexOf(",") + 1)));
            resImage.setColspan(6);
            resImage.setRowspan(16);
 
            map.put("resImage", resImage);
        }
 
        //map.put("volImage","volImage");
        //map.put("resImage","resImage");
 
        //获取导出模板地址
        ClassPathResource classPathResource = new ClassPathResource("excel_templates/res_test_report_single_template.xls");
        String path = classPathResource.getPath();
        TemplateExportParams templateExportParams1 = new TemplateExportParams(path);
        Workbook wb = ExcelExportUtil.exportExcel(templateExportParams1, map);
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));
        String fileName = "内阻计测试"+time+".xls";
        try {
            //response.setContentType("application/octet-stream;charset=utf-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
            response.flushBuffer();
            wb.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public void resTestCompareReport(String fileId, String fileId2, BattGroupStationInfoWithFileParam dto, HttpServletResponse response) throws IOException {
        Map<String,Object> map = new HashMap<>();
        Response comparedList = battGroupDataService.getComparedList(fileId, fileId2);
        List<BattgroupData> dataList = (List<BattgroupData>) comparedList.getData();
        List<BattgroupData> dataList2 = (List<BattgroupData>) comparedList.getData2();
        Map<String,List<String>> data3 = (Map<String, List<String>>) comparedList.getData3();
        //变化率
        List<String> resChangeRateList = data3.get("resChangeRate");
        List<String> volChangeRateList = data3.get("volChangeRate");
        map.put("resChangeRateList",resChangeRateList.subList(0,resChangeRateList.size()-1));
        map.put("volChangeRateList",volChangeRateList.subList(0,volChangeRateList.size()-1));
 
        map.put("volBalanceRateChangeRate",volChangeRateList.get(volChangeRateList.size()-1));
        map.put("resBalanceRateChangeRate",resChangeRateList.get(resChangeRateList.size()-1));
        //均一性:res/vol
        List<String> resBalanceRateList = data3.get("resBalanceRate");
        List<String> volBalanceRateList = data3.get("volBalanceRate");
        //均一性:chainRes/?容量/bs(电导)
        List<Float> chainResList = dataList2.stream().map(BattgroupData::getCr).collect(Collectors.toList());
        List<Float> bsList = dataList2.stream().map(BattgroupData::getBs).collect(Collectors.toList());
        String chainResBalanceRate = MathUtil.getStandardDeviation(chainResList) * 10000 / 100 + "%";
        String bsBalanceRate = MathUtil.getStandardDeviation(bsList) * 10000 / 100 + "%";
        //获取FileParam和StationInfo
        StationInfo stationInfo = stationInfoService.getByFileId(fileId2);
        FileParam param = fileParamService.getByFileId(fileId2);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        param.setTestTimeStr(format.format(param.getTestTime()));
 
        FileParam paramBase = fileParamService.getByFileId(fileId);
        String testTimeStrBase = format.format(paramBase.getTestTime());
        //图片
        BASE64Decoder decoder = new BASE64Decoder();
        ImageEntity volImage = new ImageEntity();
        volImage.setData(decoder.decodeBuffer(dto.getVolPic().substring(dto.getVolPic().indexOf(",")+1)));
        volImage.setColspan(6);
        volImage.setRowspan(17);
 
        ImageEntity resImage = new ImageEntity();
        resImage.setData(decoder.decodeBuffer(dto.getResPic().substring(dto.getResPic().indexOf(",")+1)));
        resImage.setColspan(6);
        resImage.setRowspan(16);
 
        map.put("volImage",volImage);
        map.put("resImage",resImage);
 
        map.put("fileParam",param);
        map.put("stationInfo",stationInfo);
        //总体评价:内阻/电压/连接条/?容量
        int resGoodCount = 0;
        int volGoodCount = 0;
        int chainResGoodCount = 0;
        for(BattgroupData data : dataList2){
            data.setMonNumStr("#"+data.getMonNum());
            if(!data.isHighResFlag1() && !data.isHighResFlag2()){
                resGoodCount +=1;
                data.setResEvaluation("优");
            }else if(data.isHighResFlag1()){
                data.setResEvaluation("差");
            }else{
                data.setResEvaluation("很差");
            }
            if(!data.isLowVolFlag() && !data.isHighVolFlag()){
                volGoodCount +=1;
            }
            if(!data.isHighChainResFlag()){
                chainResGoodCount +=1;
            }
        };
        map.put("resGoodCount",resGoodCount);
        map.put("volGoodCount",volGoodCount);
        map.put("chainResGoodCount",chainResGoodCount);
 
        map.put("dataList",dataList);
        map.put("dataList2",dataList2);
 
        map.put("stdDevBv",volBalanceRateList.get(1));
        map.put("stdDevBr",resBalanceRateList.get(1));
        map.put("stdDevCr",chainResBalanceRate);
        map.put("stdDevBs",bsBalanceRate);
        map.put("testTimeStrBase",testTimeStrBase);
 
        map.put("stdDevBvBase",volBalanceRateList.get(0));
        map.put("stdDevBrBase",resBalanceRateList.get(0));
 
 
        //获取导出模板地址
        ClassPathResource classPathResource = new ClassPathResource("excel_templates/res_test_report_compare_template.xls");
        String path = classPathResource.getPath();
        TemplateExportParams templateExportParams1 = new TemplateExportParams(path);
        templateExportParams1.setColForEach(true);
        Workbook wb = ExcelExportUtil.exportExcel(templateExportParams1, map);
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));
        String fileName = "内阻计测试"+time+".xls";
        try {
            //response.setContentType("application/octet-stream;charset=utf-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
            response.flushBuffer();
            wb.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}