whycxzp
2022-09-28 b08e3ef5207a78ef376c5978e9ab2e08272a8175
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
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.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whyc.dto.*;
import com.whyc.mapper.FileParamMapper;
import com.whyc.pojo.FileInfo;
import com.whyc.pojo.FileParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
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;
 
    //解析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);
                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) {
        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 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, int battVol,int pageCurr,int pageSize) {
        PageHelper.startPage(pageCurr,pageSize);
        List<FileParam> list=mapper.getDataByCondition(testTime1,testTime2,battVol);
        PageInfo pageInfo=new PageInfo(list);
        return new Response().setII(1,list.size()>0,pageInfo,"筛选数据库信息");
    }
 
    public FileParam getFactorsAndThreshold(String fileId) {
        QueryWrapper<FileParam> query = Wrappers.query();
        query.select("num","file_id","batt_vol","batt_res","volLowCoeK1","volHighCoeK2","resGoodCoeK3","resBadCoeK4","chainRes","factor_disabled").eq("file_id",fileId).last(" limit 1");
        return mapper.selectOne(query);
    }
 
    public void updateFactorsAndThreshold(FileParam param) {
        mapper.updateById(param);
    }
 
 
    public FileParam getByFileId(String fileId) {
        QueryWrapper<FileParam> query = Wrappers.query();
        query.eq("file_id",fileId).last(" limit 1");
        return mapper.selectOne(query);
    }
}