lxw
2022-09-16 547b6ac144d0745d9b55526c062b2fcaeb80fe4e
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.whyc.dto.FileOpreate;
import com.whyc.dto.Response;
import com.whyc.dto.XmlFileOpreate;
import com.whyc.mapper.*;
import com.whyc.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.io.File;
import java.util.ArrayList;
import java.util.List;
 
@Service
public class StationInfoService {
    @Autowired(required = false)
    private StationInfoMapper mapper;
 
    @Autowired(required = false)
    private FileInfoMapper infoMapper;
 
    @Autowired(required = false)
    private BattgroupInfoMapper groupInfoMapper;
 
    @Autowired(required = false)
    private BattgroupDataMapper dataMapper;
 
    @Autowired(required = false)
    private FileParamMapper paramMapper;
    //添加台站
    public Response addStation(String stationName1, String stationName2, String stationName3) {
        String stationName="";
        boolean bl=true;
        String msg="";
        StationInfo s=new StationInfo();
        if(stationName1!=null&&!stationName1.isEmpty()){
            s.setStationName1(stationName1);
            stationName=stationName+stationName1;
        }
        if(stationName2!=null&&!stationName2.isEmpty()){
            s.setStationName2(stationName2);
            stationName=stationName+"-"+stationName2;
        }else{
            s.setStationName2("-");
        }
        if(stationName3!=null&&!stationName3.isEmpty()){
            s.setStationName3(stationName3);
            stationName=stationName+"-"+stationName3;
        }else{
            s.setStationName3("-");
        }
        //查询stationName是否存在
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("station_name",stationName);
        int count=mapper.selectCount(wrapper);
        if(count>0){
            bl=false;
            msg="台站存在,添加失败";
        }else{
            s.setStationId("0");
            s.setStationName(stationName);
            int flag=mapper.insert(s);
            bl=flag>0;
            msg="台站添加成功";
        }
        return new Response().set(1,bl,msg);
    }
    //台站下添加文件
    @Transactional
    public Response addFileInStation(String stationName,String filePath) {
        File file=new File(filePath);
        boolean flag=true;
        //1.查询出最大的台站id(只有机房下添加了文件才会生成机房id)和最大文件id
        int stationIdMax=mapper.selectMaxId();
        int fileIdMax=infoMapper.selectMaxId();
        List list=new ArrayList();
        if(file.exists()){
            if(file.isFile()){
                if(!filePath.contains(".xml")){
                    return new Response().set(1,false,"台站下添加文件失败,文件不是指定的xml文件");
                }
                //2.解析文件并补全文件id
                FileInfo fileInfo=XmlFileOpreate.readXml(filePath);
                fileInfo.setFileUrl(file.getPath());
                fileInfo.setFileName(file.getName());
                //4.将单文件的数据存入数据库
                FileInfo returnFileInfo=insertFileInDataBase(stationName,stationIdMax,fileIdMax,fileInfo);
                list.add(returnFileInfo);
            }else{
                //3.如果是文件夹:获取文件夹下所有的文件
                List<File> allFile= FileOpreate.getAllFile(filePath);
                //4.将文件路径下的所有文件解析并存入数据库
                list=insertAllFileInDataBase(stationName,stationIdMax,fileIdMax,allFile);
            }
        }else{
            flag=false;
        }
        return new Response().setII(1,flag,list,flag?"台站下添加文件成功":"台站下添加文件失败");
    }
    //4.将文件路径下的所有文件解析并存入数据库
    private List insertAllFileInDataBase(String stationName, int stationIdMax, int fileIdMax, List<File> allFile) {
        List list=new ArrayList();
        if(allFile!=null&&allFile.size()>0){
            for (int i=0;i< allFile.size();i++){
                File file=allFile.get(i);
                if(!file.getName().contains(".xml")){
                    continue;
                }
                //2.解析文件并补全文件id
                FileInfo fileInfo=XmlFileOpreate.readXml(file.getPath());
                fileInfo.setFileUrl(file.getPath());
                fileInfo.setFileName(file.getName());
                //4.将单文件的数据存入数据库
                FileInfo returnFileInfo=insertFileInDataBase(stationName,stationIdMax,fileIdMax+i,fileInfo);
                list.add(returnFileInfo);
            }
        }
        return list;
    }
 
    //4.将单文件的数据存入数据库
    private FileInfo insertFileInDataBase(String stationName,int stationIdMax,int fileIdMax, FileInfo fileInfo) {
        int count=0;
        int stationId=0;
        //查询最大的电池组号
        int battGroupId=groupInfoMapper.selectMaxId();
        if(fileInfo!=null){
            //查询站点的机房是否存在stationId
            QueryWrapper wrapper=new QueryWrapper();
            stationId=mapper.selectIdByName(stationName);
            if(stationId!=0){
                fileInfo.setStationId(String.valueOf(stationId));
            }else{
                fileInfo.setStationId(String.valueOf(stationIdMax+1));
            }
            fileInfo.setFileId(String.valueOf(fileIdMax+1));
            count=infoMapper.insert(fileInfo);
            //将解析出来的文件信息插入数据库
            FileParam fparam=fileInfo.getFileParam();
            //去除groupinfo
            List<BattgroupInfo> battgroupInfoList=fileInfo.getBattInfoList();
            if(count>0&&fparam!=null){
                fparam.setFileId(String.valueOf(fileIdMax+1));
                paramMapper.insert(fparam);
                //将电池组信息插入数据库
                if(battgroupInfoList.size()>0){
                    for(int i=0;i<battgroupInfoList.size();i++){
                        BattgroupInfo battgroupInfo=battgroupInfoList.get(i);
                        battgroupInfo.setBattgroupId(String.valueOf(battGroupId+1+i));
                        battgroupInfo.setStationId(String.valueOf(stationIdMax+1));
                        battgroupInfo.setFileId(String.valueOf(fileIdMax+1));
                        battgroupInfo.setBattgroupNum(fparam.getGroupNum());
                        int groupCount=groupInfoMapper.insert(battgroupInfo);
 
                        List<BattgroupData> battgroupDataList=battgroupInfo.getBattDataList();
                        //插入放电数据
                        if(groupCount>0&&battgroupDataList.size()>0){
                            for (int j=0;j<battgroupDataList.size();j++){
                                BattgroupData battgroupData=battgroupDataList.get(j);
                                battgroupData.setBattgroupId(String.valueOf(battGroupId+1+i));
                            }
                        }
                        dataMapper.insertBatchSomeColumn(battgroupDataList);
                    }
                }
            }
        }
        if(count>0){
            if(stationId==0){
                //操作成功给台站赋予stationId
                UpdateWrapper uwrapper=new UpdateWrapper();
                uwrapper.set("station_id",stationIdMax+1);
                uwrapper.eq("station_name",stationName);
                mapper.update(null,uwrapper);
            }
        }
        return  fileInfo;
    }
}