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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whyc.dto.*;
import com.whyc.mapper.*;
import com.whyc.pojo.*;
import com.whyc.util.MathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.io.File;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.util.*;
 
@Service
public class FileParamService {
    @Autowired(required = false)
    private FileParamMapper mapper;
 
    @Autowired(required = false)
    private BattGroupDataMapper dataMapper;
 
    @Autowired(required = false)
    private FileParamMapper paramMapper;
 
    @Autowired(required = false)
    private FileInfoMapper infoMapper;
 
    @Autowired(required = false)
    private TestParamMapper testParamMapper;
 
    @Autowired(required = false)
    private BattgroupInfoMapper groupInfoMapper;
 
    //解析xml文件(传参一个文件)
    public Response getXmlValue(String filePath) {
        File file=new File(filePath);
        if(file.exists()) {
            if (file.isFile()) {
                FileInfo fileInfo = XmlFileOpreate.readXml(filePath);
                fileInfo.setFileUrl(filePath);
                fileInfo.setFileId("0");
                String fileName = filePath.substring(filePath.lastIndexOf("\\")+1);
                fileInfo.setFileName(fileName);
                fileInfo.setFileNameTmp(fileName+" tmp1.~xml");
                return new Response().setII(1,fileInfo!=null,fileInfo,"返回解析数据");
            }else{
                return new Response().set(1,false,"文件路径不正确");
            }
        }else{
            return new Response().set(1,false,"文件路径不正确");
        }
    }
    //解析xml文件(传参一个文件夹)
    public Response getXmlValueByPath(String filePath) {
        File file=new File(filePath);
        List list=new ArrayList();
        if(file.exists()){
            if(file.isFile()){
                FileInfo fileInfo=XmlFileOpreate.readXml(filePath);
                fileInfo.setFileUrl(filePath);
                list.add(fileInfo);
            }else{
                //3.如果是文件夹:获取文件夹下所有的文件
                List<File> allFile= FileOpreate.getAllFile(filePath);
                if(allFile!=null&&allFile.size()>0){
                    for (File f:allFile){
                        if(!f.getName().contains(".xml")){
                            continue;
                        }
                        FileInfo fileInfo=XmlFileOpreate.readXml(f.getPath());
                        fileInfo.setFileUrl(f.getPath());
                        list.add(fileInfo);
                    }
                }
            }
            return new Response().setII(1,list.size()>0,list,"返回解析数据");
        }else{
            return new Response().set(1,false,"文件路径不正确");
        }
    }
 
    //通过修改属性窗口值来修改文件值
    public Response updateXmlByFileParam(FileParam fileParam,String filePath) {
        //修改文件对应的数据库数据
        //paramMapper.updateParamByFileUrl(fileParam,filePath);
        updateParamByFileUrl(fileParam,filePath);
 
        Map<String,String> map=new HashMap<>();
        Class paramClass=fileParam.getClass();
        // 获取所有的属性数组
        Field[] fields = paramClass.getDeclaredFields();
        for (Field field:fields) {
            field.setAccessible(true);
            try {
                String paramName=field.getName();
                //获取属性值
                Object obj=field.get(fileParam);
                if(obj==null){
                    obj="null";
                }
                String paramValue=obj.toString();
                if(field.getType().toString().equals("class java.util.Date")){
                    if(!paramValue.equals("null")){
                        paramValue= ActionUtil.sdfwithALL.format(ActionUtil.df.parse(paramValue));
                    }
                }
                String xmlName=FileParamToXml.getNameByType(paramName);
                if(xmlName!=null&&!xmlName.isEmpty()){
                    map.put(xmlName,paramValue);
                }
            } catch (IllegalAccessException | ParseException e) {
                e.printStackTrace();
            }
        }
        boolean bl=false;
        if(map.size()>0){
            bl=XmlFileOpreate.writeXml(map,filePath);
        }
        return new Response().setII(1,bl,map,"修改文件");
    }
    //修改文件对应的数据库数据
    public void updateParamByFileUrl(FileParam fileParam,String filePath){
        //根据文件路径查出file_id
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("file_url",filePath);
        List<FileInfo> list=infoMapper.selectList(wrapper);
        if(list!=null&&list.size()>0){
            for (FileInfo info:list){
                //修改参数信息
                paramMapper.updateParam(fileParam,info.getFileId());
            }
        }
 
    }
 
    //通过修改属性窗口值来修改文件值
    public Response updateXmlByParamMap(Map<String,String> map,String filePath) {
        boolean bl=false;
        if(map.size()>0){
            bl=XmlFileOpreate.writeXml(map,filePath);
        }
        return new Response().setII(1,bl,map,"修改文件");
    }
    //查询数据中存在的电池标称电压类型
    public Response getMonVolStd() {
        List<Integer> list=mapper.getMonVolStd();
        return new Response().setII(1,list.size()>0,list,"返回所有的标称电压类型");
    }
    //按照筛选条件查询数据库信息(内阻评价:内阻越大越差),然后再根据内阻手动分页
    public Response getDataByCondition(Date testTime1, Date testTime2, String battVol,int pageCurr,int pageSize,int flag) {
        //PageHelper.startPage(pageCurr,pageSize);
        List<FileParam> list=mapper.getDataByCondition(testTime1,testTime2,Float.valueOf(battVol));
        List flagList=new ArrayList();//存放评价结果
        QueryWrapper testParamWrapper=new QueryWrapper();
        testParamWrapper.eq("enabled",1);
        TestParam testParam=testParamMapper.selectOne(testParamWrapper);
        float std=0f;
        int resFlag=0;//整组内阻标识,1差,2良,3优
        if(list!=null&&list.size()>0){
            for (FileParam fparam:list) {
                List<BattgroupInfo> binfInfoList=fparam.getBattInfoList();
                std=fparam.getBattRes();
                if(binfInfoList!=null&&binfInfoList.size()>0){
                    for (BattgroupInfo binfo:binfInfoList) {
                        List<BattgroupData> binfDataList=binfo.getBattDataList();
                        float maxStd=0f;
                        List listBv=new ArrayList();
                        List listBr=new ArrayList();
                        List listBs=new ArrayList();
                        List listCr=new ArrayList();
                        if(binfDataList!=null&&binfDataList.size()>0) {
                            for (BattgroupData bData : binfDataList) {
                                listBv.add(Float.valueOf(bData.getBv()));
                                listBr.add(Float.valueOf(bData.getBr()));
                                listBs.add(Float.valueOf(bData.getBs()));
                                listCr.add(Float.valueOf(bData.getCr()));
                                if( Float.parseFloat(bData.getBr())>=maxStd){
                                    maxStd= Float.parseFloat(bData.getBr());
                                }
                            }
                            binfo.setSdBasebv(MathUtil.getStandardDeviation(listBv));
                            binfo.setSdBasebr(MathUtil.getStandardDeviation(listBr));
                            binfo.setSdBasebs(MathUtil.getStandardDeviation(listBs));
                            binfo.setSdBasecr(MathUtil.getStandardDeviation(listCr));
                            resFlag=MathUtil.getflag(maxStd,std,testParam);
                            binfo.setBrJudge(MathUtil.getJudge(resFlag));
                        }
                        break;
                    }
                }
                if(flag==0){//为0是全部
                    flagList.add(fparam);
                }else{
                    if(flag==resFlag){
                        flagList.add(fparam);
                    }
                }
            }
        }
        //创建Page类
        Page page = new Page(pageCurr, pageSize);
        //为Page类中的total属性赋值
        int total = flagList.size();
        page.setTotal(total);
       //计算当前需要显示的数据下标起始值
        int startIndex = (pageCurr - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize,total);
        //从链表中截取需要显示的子链表,并加入到Page
        page.addAll(flagList.subList(startIndex,endIndex));
        //以Page创建PageInfo
        PageInfo pageInfo = new PageInfo<>(page);
        return new Response().setII(1,flagList.size()>0,pageInfo,"筛选数据库信息");
    }
 
    public FileParam getByFileId(String fileId) {
        QueryWrapper<FileParam> query = Wrappers.query();
        query.eq("file_id",fileId).last(" limit 1");
        return mapper.selectOne(query);
    }
    //删除基站下数据
    public Response deleteDataById(int stationId, int fileId) {
        //删除所有文件数据(指定机房id和文件id)
        //dataMapper.deleteDataAndInfo(stationId,fileId);
        deleteDataAndInfo(stationId,fileId);
        //删除所有文件参数
        paramMapper.deleteParamByFileId(fileId);
        //移除机房下挂在的文件
        infoMapper.deleteFileInStation(stationId,fileId);
        return new Response().set(1,"删除成功!");
    }
    //3根据(stationId,fileId)删除groupinfo和groupdata数据
    public void deleteDataAndInfo(int stationId,int fileId){
        //先查询出对应的电池组id然后删除
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("station_id",stationId);
        wrapper.eq("file_id",fileId);
        List<BattgroupInfo> list=groupInfoMapper.selectList(wrapper);
        if(list!=null&&list.size()>0){
            for (BattgroupInfo ginfo:list) {
                UpdateWrapper dataWrapper=new UpdateWrapper();
                dataWrapper.eq("battGroup_id",ginfo.getBattgroupId());
                dataMapper.delete(dataWrapper);
            }
        }
        //再删除groupinfo
        UpdateWrapper infoWrapper=new UpdateWrapper();
        infoWrapper.eq("station_id",stationId);
        infoWrapper.eq("file_id",fileId);
        groupInfoMapper.delete(infoWrapper);
    }
    //根据fileId获取参数信息
    public Response getParamByFileId(int fileId) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("file_id",fileId);
        wrapper.last("limit 1");
        FileParam fileParam=mapper.selectOne(wrapper);
        return new Response().setII(1,fileParam!=null,fileParam,"返回数据");
    }
 
    //根据fileId通过修改属性窗口值来修改数据库属性
    public Response updateFileParamByFileId(FileParam fileParam, String fileId) {
        int flag=paramMapper.updateParam(fileParam,fileId);
        return new Response().set(1,flag>0,"修改数据");
    }
    //更改台站下公用参数
    @Transactional
    public Response updateFileParamByStationId(FileParam fileParam, int stationId) {
        //查询基站下所有的文件id
        List<String> list=infoMapper.selectFileIds(stationId);
        int flag=0;
        for (String fileId:list) {
            flag+=paramMapper.updateParam(fileParam,fileId);
        }
        return new Response().set(1,flag>0,"修改数据");
    }
}