DeviceGpsController.java
8.4 KB
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
package com.bsth.controller;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.bsth.data.gpsdata.GpsEntity;
import com.bsth.data.gpsdata.GpsRealData;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.BeanUtil;
@RestController
@RequestMapping("devicegps")
public class DeviceGpsController {
@Autowired
GpsRealData gpsRealData;
@RequestMapping(value = "/real/line/{lineCode}")
public List<GpsEntity> findByLineCode(@PathVariable("lineCode") String lineCode) {
return gpsRealData.getByLine(lineCode);
}
@RequestMapping(value = "/real/all")
public List<GpsEntity> findByLineCodes() {
return new ArrayList<>(gpsRealData.all());
}
@RequestMapping(value = "/open", method = RequestMethod.POST)
public Map<String, Object> open(@RequestParam(value = "_txt_", required = false) MultipartFile file, HttpServletRequest request) {
Map<String, Object> res = new HashMap<>();
File rf = new File(request.getServletContext().getRealPath("/temp"), System.currentTimeMillis() + "");
File rd = rf.getParentFile();
if (!rd.exists()) {
rd.mkdirs();
}
BufferedReader reader = null;
try {
file.transferTo(rf);
reader = new BufferedReader(new InputStreamReader(new FileInputStream(rf), "GBK"));
String line = null;
List<Map<String, Object>> data = new ArrayList<>();
while ((line = reader.readLine()) != null) {
if (!StringUtils.isEmpty(line)) {
String temp[] = line.split(",");
if (temp.length != 4) {
res.put("errCode", 1);
res.put("msg", "txt文档格式错误,请检查");
return res;
}
Map<String, Object> info = new HashMap<>();
info.put("lineName", temp[0]);
info.put("lineCode", temp[1]);
info.put("inCode", temp[2]);
info.put("deviceId", temp[3]);
info.put("detail", gpsRealData.get(temp[3]));
data.add(info);
}
}
// 删除临时文件
rf.delete();
res.put("errCode", 0);
res.put("data", data);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (reader != null) reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return res;
}
@SuppressWarnings("unchecked")
@RequestMapping(value = "/opened", method = RequestMethod.POST)
public Map<String, Object> opened(@RequestParam(value = "json")String json) {
json = StringEscapeUtils.unescapeHtml(json);
Map<String, Object> res = new HashMap<>();
List<Map<String, Object>> data = null;
try {
data = new ObjectMapper().readValue(json, List.class);
for (Map<String, Object> info : data) {
info.put("detail", gpsRealData.findByDeviceId((String)info.get("deviceId")));
}
res.put("errCode", 0);
res.put("data", data);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
@SuppressWarnings("unchecked")
@RequestMapping(value = "/export", method = RequestMethod.POST)
public void export(@RequestParam(value = "json")String json, HttpServletResponse response) {
json = StringEscapeUtils.unescapeHtml(json);
List<Map<String, Object>> data = null;
OutputStream out = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
data = new ObjectMapper().readValue(json, List.class);
for (Map<String, Object> info : data) {
Map<String, Object> detail = (Map<String, Object>)info.get("detail");
if (detail != null) {
info.put("lineId", detail.get("lineId"));
info.put("valid", (int)detail.get("valid") == 0 ? "有效" : "无效");
info.put("timestamp", sdf.format(new Date((Long)detail.get("timestamp"))));
info.put("version", detail.get("version"));
} else {
info.put("lineId", "");
info.put("valid", "");
info.put("timestamp", "");
info.put("version", "");
}
}
List<Header> head = new ArrayList<>();
head.add(new Header("线路编码", "lineCode"));
head.add(new Header("线路名称", "lineName"));
head.add(new Header("内部编码", "inCode"));
head.add(new Header("识别码", "deviceId"));
head.add(new Header("线路ID", "lineId"));
head.add(new Header("GPS", "valid"));
head.add(new Header("report", "timestamp"));
head.add(new Header("版本", "version"));
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + System.currentTimeMillis() + ".xls");
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
out = new BufferedOutputStream(response.getOutputStream());
excel(head, data, out);
out.flush();
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (out != null) out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void excel(List<Header> head, List<Map<String, Object>> data, OutputStream out) throws IOException {
// 声明一个工作薄
HSSFWorkbook wb = new HSSFWorkbook();
// 生成一个表格
HSSFSheet sheet = wb.createSheet("1");
// 产生表格标题行
HSSFRow row = sheet.createRow(0);
for (int i = 0;i < head.size();i++) {
HSSFCell cell = row.createCell(i);
HSSFRichTextString text = new HSSFRichTextString(head.get(i).getDescribe());
cell.setCellValue(text);
}
int rownum = 1;
for (Map<String, Object> map : data) {
HSSFRow r = sheet.createRow(rownum);
for (int i = 0;i < head.size();i++) {
HSSFCell cell = r.createCell(i);
HSSFRichTextString text = new HSSFRichTextString(String.valueOf(map.get(head.get(i).getField())));
cell.setCellValue(text);
}
rownum++;
}
wb.write(out);
wb.close();
}
final class Header {
private String describe;
private String field;
Header(){
}
Header(String describe, String field) {
this.describe = describe;
this.field = field;
}
public String getDescribe() {
return describe;
}
public void setDescribe(String describe) {
this.describe = describe;
}
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
}
}