whyclxw
2025-05-07 45ef9e340f9bb4b98fd88a758343470dfe125cb3
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
package com.whyc.service;
 
import com.whyc.dto.FileDirPath;
import com.whyc.dto.Response;
import com.whyc.dto.paramter.AnalysisDto;
import com.whyc.pojo.*;
import com.whyc.util.ActionUtil;
import com.whyc.util.AnalysisUtil;
import lombok.Data;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
 
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.*;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
@Service
public class AnaysiService {
    @Autowired(required = false)
    private SubTablePageInfoService subService;
 
    @Autowired(required = false)
    private BatttestdataInfService tinfService;
 
 
 
 
    //预估数据
    public String createAnaysisXls(int battGroupId, int testRecordCount) {
        UserInf uinf= ActionUtil.getUser();
        String uid=String.valueOf(uinf.getUId());
        //1.查出需要预估的数据
        List<AnaysisData> list=subService.getAnaysisData(battGroupId,testRecordCount);
        String excelName="测试数据"+"_"+battGroupId+"_"+testRecordCount+".xlsx";
        //2.生成解析的xls
        String root=exportDataToXls(uid,excelName,list);
        String voltagePredictExePath=root+ File.separator+"10小时率电压预估\\voltage_predict_process\\main.exe";
        String excelOutPutPath=root+ File.separator+"analysis"+File.separator+uid+ File.separator+"outPath"+File.separator+battGroupId+File.separator+testRecordCount;
        String excelSourcePath=root+ File.separator+"analysis"+File.separator+uid+File.separator+excelName;
        //4。将预估的数据存入数据库
        if(list!=null){
            int recordNum=list.size()+1;
            //验证分析表是否存在
            int flag=subService.judgeTable_anaysis(battGroupId,testRecordCount);
            int insertFlag=0;
            //检测表是存在后testRecordCount数据是否存在
            if(flag!=0){
                int sum=subService.getAnaysisIdByTestRecordCount(battGroupId,testRecordCount);
                if(sum==0) {
                    insertFlag=1;
                }
            }else{
                //表不存在则创建表
                subService.creatAnasys(battGroupId);
                insertFlag=1;
            }
            if(insertFlag!=0){
                //补前面的数据
                subService.insertBeforeData(battGroupId,testRecordCount,list);
                //3.生成预估的文件xls
                AnalysisUtil.analysisXlx(excelSourcePath,voltagePredictExePath,excelOutPutPath);
 
                storeAnaysisToSql(battGroupId,testRecordCount,recordNum,excelOutPutPath,insertFlag);
            }
        }
        return voltagePredictExePath;
    }
    //将预估的数据存入数据库
    private   void  storeAnaysisToSql(int battGroupId,int testRecordCount,int recordNum,String excelSourcePath,int insertFlag) {
        //获取文件夹下所有的数据
        List<String> fileList=getFileNameWithOutDirectory(excelSourcePath);
        List<AnaylsisId> groupVolList=new ArrayList<>();
        if(insertFlag!=0){
            if(fileList!=null){
                for (String fileName:fileList) {
 
                    if(fileName.contains("总电压")){
                        //解析组端电压单个文件
                        groupVolList=anaysisFileByGroupVol(testRecordCount,recordNum,excelSourcePath+File.separator+fileName);
                    }else{
                        String name=fileName.substring(0,fileName.indexOf("#"));
                        int monNum=Integer.valueOf(name);
                        //解析单个文件
                        List<AnaylsisId> idList=anaysisFileByNum(monNum,testRecordCount,recordNum,excelSourcePath+File.separator+fileName);
                        //将解析出的数据存入数据库表
                        subService.storeAnaysisToSqlWithNum(battGroupId,testRecordCount,idList);
                    }
                }
                //将解析的组端电压修改到数据库
                subService.updateAnaysisToSqlWithNum(battGroupId,testRecordCount,groupVolList);
            }
        }
    }
    //解析单体的单个文件
    private static List<AnaylsisId> anaysisFileByNum(int monNum,int testRecordCount, int recordNum, String fileRoot) {
        List<AnaylsisId> idList=new ArrayList<>();
        Workbook workbook = null;
        File file=new File(fileRoot);
        try {
            workbook = WorkbookFactory.create(file);
            //取第一个sheet表
            Sheet sheet = workbook.getSheetAt(0);
            int lastRowNum = sheet.getLastRowNum();
 
            /*Row rowTemp = sheet.getRow(0);
            short lastCellNum = rowTemp.getLastCellNum();*/
            DataFormatter dataFormatter = new DataFormatter(Locale.US);
            for (int i = 1; i < lastRowNum; i++) {
                Row row = sheet.getRow(i);
 
                AnaylsisId aIdDta=new AnaylsisId();
                aIdDta.setTestRecordCount(testRecordCount);
                aIdDta.setMonNum(monNum);
                Cell cell0 = row.getCell(0);
                cell0.setCellType(Cell.CELL_TYPE_NUMERIC);
                aIdDta.setRecordNum( (recordNum+(int)cell0.getNumericCellValue()));
 
                Cell cell1 = row.getCell(1);
                String formattedValue = dataFormatter.formatCellValue(cell1);
                aIdDta.setRecordTime(formattedValue);
 
                Cell cell2 = row.getCell(2);
                cell2 .setCellType(Cell.CELL_TYPE_NUMERIC);
                aIdDta.setMonVol(cell2.getNumericCellValue());
                idList.add(aIdDta);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        }
        return idList;
    }
 
    //解析组端电压单个文件
    private static List<AnaylsisId> anaysisFileByGroupVol(int testRecordCount, int recordNum, String fileRoot) {
        List<AnaylsisId> idList=new ArrayList<>();
        Workbook workbook = null;
        File file=new File(fileRoot);
        try {
            workbook = WorkbookFactory.create(file);
            //取第一个sheet表
            Sheet sheet = workbook.getSheetAt(0);
            int lastRowNum = sheet.getLastRowNum();
 
            /*Row rowTemp = sheet.getRow(0);
            short lastCellNum = rowTemp.getLastCellNum();*/
            DataFormatter dataFormatter = new DataFormatter(Locale.US);
            for (int i = 1; i < lastRowNum; i++) {
                Row row = sheet.getRow(i);
 
                AnaylsisId aIdDta=new AnaylsisId();
                aIdDta.setTestRecordCount(testRecordCount);
                Cell cell0 = row.getCell(0);
                cell0.setCellType(Cell.CELL_TYPE_NUMERIC);
                aIdDta.setRecordNum( (recordNum+(int)cell0.getNumericCellValue()));
 
                Cell cell1 = row.getCell(1);
                String formattedValue = dataFormatter.formatCellValue(cell1);
                aIdDta.setRecordTime(formattedValue);
 
                Cell cell2 = row.getCell(2);
                cell2 .setCellType(Cell.CELL_TYPE_NUMERIC);
                aIdDta.setGroupVol(cell2.getNumericCellValue());
                idList.add(aIdDta);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        }
        return idList;
    }
 
    //读取文件夹下的所有文件(不读取文件夹内的文件)
    public static List getFileNameWithOutDirectory(String filePath) {
        File folder = new File(filePath); // 文件夹路径
        List nameList=new ArrayList();
        File[] listOfFiles = folder.listFiles();
        if (listOfFiles != null) {
            for (File file : listOfFiles) {
                if (file.isFile()) {
                    nameList.add(file.getName());
                }
            }
        }
        return nameList;
    }
 
    //生成解析的xls
    private String exportDataToXls(String uid, String excelName,List<AnaysisData> list) {
        String fileDirName = FileDirPath.getFileDirName();
        String rootFace="";
        String[] titleNames=new String[]{"时间(HMS)","总电压(V)","总电流(A)","容量(AH)"};
        //创建单个sheet
        HSSFWorkbook wb = new HSSFWorkbook();
        //字体格式-加粗
        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setFillForegroundColor(IndexedColors.GOLD.getIndex());//添加前景色,内容看的清楚
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        HSSFFont font = wb.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        //font.setColor(HSSFFont.COLOR_RED);
        cellStyle.setFont(font);
 
        HSSFSheet sheet = wb.createSheet("电压数据表格1");
        int rownum = 0;
        int monSum =24;//单体总数
        HSSFRow row=sheet.createRow(rownum);
        if(titleNames.length>0){
            HSSFCell cell=null;
            for (int i=0;i<titleNames.length;i++) {
                cell=row.createCell(i);
                cell.setCellValue(titleNames[i]);
            }
            for (int j=0;j<monSum;j++) {
                cell=row.createCell(j+4);
                cell.setCellValue(j+1+"#");
            }
        }
        rootFace=fileDirName+File.separator+"analysis";
        File destfile = new File(rootFace);
        if(!destfile.exists()) {
            destfile.mkdir();
        }
        rootFace=fileDirName+File.separator+"analysis"+File.separator+uid;
        File destfile1 = new File(rootFace);
        if(!destfile1.exists()) {
            destfile1.mkdir();
        }
        //将选中的文件存入指定目录下打包下载
        if(list!=null&&list.size()>0){
            for (int i=0;i<list.size();i++) {
                AnaysisData data = list.get(i);
                rownum = rownum + 1;
                row = sheet.createRow(rownum);
                row.createCell(0).setCellValue(data.getRecordTime());
                row.createCell(1).setCellValue(data.getGroupVol());
                row.createCell(2).setCellValue(data.getGroupCurr());
                row.createCell(3).setCellValue(data.getTestCap());
                if (data.getMonVols() != null) {
                    List<Float> monVols = data.getMonVols();
                    for (int j = 0; j < monVols.size(); j++) {
                        row.createCell(4 + j).setCellValue(monVols.get(j));
                    }
                }
            }
        }
        FileOutputStream fileOut =null;
        excelName=rootFace+ File.separator+excelName;
        try {
            fileOut = new FileOutputStream(excelName);
            // 写入excel文件
            wb.write(fileOut);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fileOut != null){
                try {
                    fileOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return fileDirName;
    }
 
    //查询预估数据
    public Response getAnaysisXls(int battGroupId, int testRecordCount) {
        List<AnaylsisId> list=subService.getAnaysisXls(battGroupId,testRecordCount);
        return new Response().setII(1,list!=null,list,"查询预估数据");
    }
    //解析xls文件到放电数据中
    public Response<Object> anaysisXlsToTdata(int battGroupId,InputStream inputStream) throws IOException, InvalidFormatException, ParseException {
        Workbook workbook = null;
        workbook = WorkbookFactory.create(inputStream);
        inputStream.close();
        Sheet sheet =null;
        int numberOfSheets=workbook.getNumberOfSheets();
        int flag=0;
        //取第一个sheet表
        for (int i = 0; i < numberOfSheets; i++) {
            sheet = workbook.getSheetAt(i);
            String sheetName=sheet.getSheetName();
            if (sheetName.contains("电压数据表格")) {
                flag=1;
                break;
            }
        }
        if(flag==0){
            return new Response<>().set(1,false,"不存在包含电压数据表格的数据");
        }
        int lastRowNum = sheet.getLastRowNum();
        Row rowTemp = sheet.getRow(0);
        int lastCellNum = rowTemp.getLastCellNum();
        DataFormatter dataFormatter = new DataFormatter(Locale.US);
        List<XlsToTdata> list=new ArrayList<>();
        int testTimelong=0;
        int timeInterval=0;
        for (int i=1;i<lastRowNum;i++){
            XlsToTdata tData=new XlsToTdata();
            Row row = sheet.getRow(i);
            Cell cell0 = row.getCell(0);
            String formattedValue = dataFormatter.formatCellValue(cell0);
            tData.setRecordTime(ActionUtil.parseDurationDirectly(formattedValue));
 
            int testTimeLong=ActionUtil.convertToSeconds(formattedValue);
            tData.setTestTimelong(testTimeLong);
            if(i==2){
                timeInterval=testTimeLong-timeInterval;
            }
 
            Cell cell1 = row.getCell(1);
            cell1.setCellType(Cell.CELL_TYPE_NUMERIC);
            tData.setGroupVol((float) cell1.getNumericCellValue());
 
            Cell cell2 = row.getCell(2);
            cell2.setCellType(Cell.CELL_TYPE_NUMERIC);
            tData.setGroupCurr((float) cell2.getNumericCellValue());
 
            Cell cell3 = row.getCell(3);
            cell3.setCellType(Cell.CELL_TYPE_NUMERIC);
            tData.setTestCap((float) cell3.getNumericCellValue());
 
            List<Float> monVols=new ArrayList<>();
            for (int j=4;j<lastCellNum;j++){
                Cell cell = row.getCell(j);
                if(cell!=null){
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    String monVol= cell.getStringCellValue();
                    monVols.add(Float.valueOf(monVol));
                }
 
            }
            testTimelong= tData.getTestTimelong();
            tData.setMonVols(monVols);
            list.add(tData);
        }
 
        //1.获取当前最大的放电信息testRecordCount
        BatttestdataInf tinf=tinfService.getMaxRecord(battGroupId);
        int testRecordCount=0;
        if(tinf!=null) {
            testRecordCount = tinf.getTestRecordCount();
        }
 
 
        //3.将tinf补齐
        tinf=new BatttestdataInf();
        tinf.setBattGroupId(battGroupId);
        tinf.setDataNew((byte) 1);
        tinf.setDataAvailable((byte) 1);
        tinf.setTestType(3);
        tinf.setRecordTimeInterval(timeInterval);
        tinf.setTestRecordCount(testRecordCount+1);
        tinf.setTestRecordCountEx(testRecordCount+2);
        tinf.setRecordNum(list.size()+1);
        tinf.setTestStarttime(new Date());
        tinf.setRecordTime(new Date());
        tinf.setTestStarttype(3);
        tinf.setTestTimelong(testTimelong);
        tinf.setTestStoptype(1);
        tinfService.insertTinf(tinf);
 
        //2.将数据插入电池组数据
        subService.insertAnaysisXlsToTdata(battGroupId,testRecordCount+1,list);
        return new Response<>().set(1,true,"解析xls文件到放电数据中");
    }
    //查询当前Ai解析数据的程度
    public Response getAnalysisSocket(AnalysisDto dto) {
        //1.检测是否存在数据
        int sum=subService.getAnaysisIdByTestRecordCount(dto.getBattGroupId(),dto.getTestRecordCount());
        if(sum==0){
            return new Response().setII(1,true,-1,"查询当前Ai解析数据的程度");
        }
        //检测书否存在补充的数据
        int supple=subService.getAnaysisSupplement(dto.getBattGroupId(),dto.getTestRecordCount());
        if(supple==0){
            return new Response().setII(1,true,0,"查询当前Ai解析数据的程度");
        }
        //检测书否存在解析的数据
        int anaNum=subService.getAnaysisNum(dto.getBattGroupId(),dto.getTestRecordCount());
        return new Response().setII(1,true,anaNum,"查询当前Ai解析数据的程度");
    }
 
    //预估一个2小时后面的数据
    public Response get2To10Data(int battGroupId, int testRecordCount1, int testRecordCount2) {
        //查询上一次最后一次的数据
        //1.查出需要预估的数据
        List<PredictDataId> list2=subService.getPredictDataId(battGroupId,testRecordCount2);
        int maxRecordNum=list2.get(list2.size()-1).getRecordNum();
        //2.取出最后一笔数据
        List<PredictDataId> listLast=subService.getPredictDataIdLast(battGroupId,testRecordCount2,maxRecordNum);
        //3.取出预估对象的maxRecordNum之后的数据
        List<PredictDataId> list1=subService.getPredictDataIdAgain(battGroupId,testRecordCount1,maxRecordNum);
        //4.取出预估对象的maxRecordNum的数据
        List<PredictDataId> listAgain=subService.getPredictDataIdLast(battGroupId,testRecordCount1,maxRecordNum);
        Map<Integer,PredictDataId> mapDifference=new HashMap<>();
        for (int i=0;i<listLast.size();i++){
            PredictDataId dataLast=listLast.get(i);
            PredictDataId dataAgain=listAgain.get(i);
            PredictDataId data=new PredictDataId();
            data.setTestTimelong(dataLast.getTestTimelong()-dataAgain.getTestTimelong());
            data.setMonNum(dataLast.getMonNum());
            data.setTestCurr(dataLast.getTestCurr()-dataAgain.getTestCurr());
            data.setTestCap(dataLast.getTestCap()-dataAgain.getTestCap());
            data.setMonVol(dataLast.getMonVol()- dataAgain.getMonVol());
            data.setMonTmp(dataLast.getMonTmp()- dataAgain.getMonTmp());
            mapDifference.put(dataLast.getMonNum(),data);
        }
        //将数据插入inf表和id表
        //3.将tinf补齐
        BatttestdataInf tinf=new BatttestdataInf();
        tinf.setBattGroupId(battGroupId);
        tinf.setDataNew((byte) 1);
        tinf.setDataAvailable((byte) 1);
        tinf.setTestType(3);
        tinf.setRecordTimeInterval(0);
        tinf.setTestRecordCount(testRecordCount2+1);
        tinf.setTestRecordCountEx(testRecordCount2+2);
        tinf.setRecordNum(0);
        tinf.setTestStarttime(new Date());
        tinf.setRecordTime(new Date());
        tinf.setTestStarttype(3);
        tinf.setTestTimelong(0);
        tinf.setTestStoptype(1);
        tinfService.insertTinf(tinf);
 
        //2.将数据插入电池组数据
        subService.addPredictDataId(battGroupId,testRecordCount2,list2,list1,mapDifference);
        return new Response().set(1,true);
    }
}