lxw
2022-09-20 50481636b267ac8a516672db66b6206d378baf26
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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
    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);
                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) {
       List<FileParam> list=mapper.getDataByCondition(testTime1,testTime2,battVol);
       return new Response().setII(1,list.size()>0,list,"筛选数据库信息");
    }
 
 
}