whyclxw
2024-10-17 77dba770ab6ab03d25106d143b6443d4c77a4af7
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
package com.whyc.service;
 
import com.whyc.dto.FileDirPath;
import com.whyc.dto.Response;
import com.whyc.pojo.AnaylsisId;
import com.whyc.pojo.AnaysisData;
import com.whyc.pojo.BatttestdataId;
import com.whyc.pojo.UserInf;
import com.whyc.util.ActionUtil;
import com.whyc.util.AnalysisUtil;
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 javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
 
@Service
public class AnaysiService {
    @Autowired(required = false)
    private SubTablePageInfoService subService;
 
    //预估数据
    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){
                //3.生成预估的文件xls
                AnalysisUtil.analysisXlx(excelSourcePath,voltagePredictExePath,excelOutPutPath);
                //补前面的数据
                subService.insertBeforeData(battGroupId,testRecordCount,list);
 
                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,"查询预估数据");
    }
}