Commit c88a1206fb8bd5788351e62dddb80e23df0d3378

Authored by 潘钊
2 parents 4e610d94 4c4f471e

Merge branch 'minhang' of http://222.66.0.204:8090/panzhaov5/bsth_control into minhang

src/main/java/com/bsth/controller/DeviceGpsController.java
1 1 package com.bsth.controller;
2 2  
  3 +import java.io.BufferedOutputStream;
3 4 import java.io.BufferedReader;
4 5 import java.io.File;
5 6 import java.io.FileInputStream;
6 7 import java.io.IOException;
7 8 import java.io.InputStreamReader;
  9 +import java.io.OutputStream;
  10 +import java.text.SimpleDateFormat;
8 11 import java.util.ArrayList;
  12 +import java.util.Date;
9 13 import java.util.HashMap;
10 14 import java.util.List;
11 15 import java.util.Map;
12 16  
13 17 import javax.servlet.http.HttpServletRequest;
  18 +import javax.servlet.http.HttpServletResponse;
14 19  
15 20 import org.apache.commons.lang.StringEscapeUtils;
  21 +import org.apache.poi.hssf.usermodel.HSSFCell;
  22 +import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  23 +import org.apache.poi.hssf.usermodel.HSSFRow;
  24 +import org.apache.poi.hssf.usermodel.HSSFSheet;
  25 +import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  26 +import org.springframework.beans.BeanUtils;
16 27 import org.springframework.beans.factory.annotation.Autowired;
17 28 import org.springframework.util.StringUtils;
18 29 import org.springframework.web.bind.annotation.PathVariable;
... ... @@ -27,6 +38,7 @@ import com.bsth.data.gpsdata.GpsRealData;
27 38 import com.fasterxml.jackson.core.JsonParseException;
28 39 import com.fasterxml.jackson.databind.JsonMappingException;
29 40 import com.fasterxml.jackson.databind.ObjectMapper;
  41 +import com.fasterxml.jackson.databind.util.BeanUtil;
30 42  
31 43 @RestController
32 44 @RequestMapping("devicegps")
... ... @@ -101,8 +113,8 @@ public class DeviceGpsController {
101 113 }
102 114  
103 115 @SuppressWarnings("unchecked")
104   - @RequestMapping(value = "/open/filter", method = RequestMethod.POST)
105   - public Map<String, Object> filter(@RequestParam(value = "json")String json) {
  116 + @RequestMapping(value = "/opened", method = RequestMethod.POST)
  117 + public Map<String, Object> opened(@RequestParam(value = "json")String json) {
106 118 json = StringEscapeUtils.unescapeHtml(json);
107 119 Map<String, Object> res = new HashMap<>();
108 120 List<Map<String, Object>> data = null;
... ... @@ -127,4 +139,122 @@ public class DeviceGpsController {
127 139  
128 140 return res;
129 141 }
  142 +
  143 + @SuppressWarnings("unchecked")
  144 + @RequestMapping(value = "/export", method = RequestMethod.POST)
  145 + public void export(@RequestParam(value = "json")String json, HttpServletResponse response) {
  146 + json = StringEscapeUtils.unescapeHtml(json);
  147 + List<Map<String, Object>> data = null;
  148 + OutputStream out = null;
  149 + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  150 + try {
  151 + data = new ObjectMapper().readValue(json, List.class);
  152 + for (Map<String, Object> info : data) {
  153 + Map<String, Object> detail = (Map<String, Object>)info.get("detail");
  154 + if (detail != null) {
  155 + info.put("lineId", detail.get("lineId"));
  156 + info.put("valid", (int)detail.get("valid") == 0 ? "有效" : "无效");
  157 + info.put("timestamp", sdf.format(new Date((Long)detail.get("timestamp"))));
  158 + info.put("version", detail.get("version"));
  159 + } else {
  160 + info.put("lineId", "");
  161 + info.put("valid", "");
  162 + info.put("timestamp", "");
  163 + info.put("version", "");
  164 + }
  165 + }
  166 +
  167 + List<Header> head = new ArrayList<>();
  168 + head.add(new Header("线路编码", "lineCode"));
  169 + head.add(new Header("线路名称", "lineName"));
  170 + head.add(new Header("内部编码", "inCode"));
  171 + head.add(new Header("识别码", "deviceId"));
  172 + head.add(new Header("线路ID", "lineId"));
  173 + head.add(new Header("GPS", "valid"));
  174 + head.add(new Header("report", "timestamp"));
  175 + head.add(new Header("版本", "version"));
  176 +
  177 + // 清空response
  178 + response.reset();
  179 + // 设置response的Header
  180 + response.addHeader("Content-Disposition", "attachment;filename=" + System.currentTimeMillis() + ".xls");
  181 + response.setContentType("application/vnd.ms-excel;charset=UTF-8");
  182 + out = new BufferedOutputStream(response.getOutputStream());
  183 + excel(head, data, out);
  184 + out.flush();
  185 + } catch (JsonParseException e) {
  186 + // TODO Auto-generated catch block
  187 + e.printStackTrace();
  188 + } catch (JsonMappingException e) {
  189 + // TODO Auto-generated catch block
  190 + e.printStackTrace();
  191 + } catch (IOException e) {
  192 + // TODO Auto-generated catch block
  193 + e.printStackTrace();
  194 + } finally {
  195 + try {
  196 + if (out != null) out.close();
  197 + } catch (IOException e) {
  198 + e.printStackTrace();
  199 + }
  200 + }
  201 + }
  202 +
  203 + private void excel(List<Header> head, List<Map<String, Object>> data, OutputStream out) throws IOException {
  204 + // 声明一个工作薄
  205 + HSSFWorkbook wb = new HSSFWorkbook();
  206 + // 生成一个表格
  207 + HSSFSheet sheet = wb.createSheet("1");
  208 + // 产生表格标题行
  209 + HSSFRow row = sheet.createRow(0);
  210 + for (int i = 0;i < head.size();i++) {
  211 + HSSFCell cell = row.createCell(i);
  212 + HSSFRichTextString text = new HSSFRichTextString(head.get(i).getDescribe());
  213 + cell.setCellValue(text);
  214 + }
  215 +
  216 + int rownum = 1;
  217 + for (Map<String, Object> map : data) {
  218 + HSSFRow r = sheet.createRow(rownum);
  219 + for (int i = 0;i < head.size();i++) {
  220 + HSSFCell cell = r.createCell(i);
  221 + HSSFRichTextString text = new HSSFRichTextString(String.valueOf(map.get(head.get(i).getField())));
  222 + cell.setCellValue(text);
  223 + }
  224 + rownum++;
  225 + }
  226 +
  227 + wb.write(out);
  228 + wb.close();
  229 + }
  230 +
  231 + final class Header {
  232 + private String describe;
  233 + private String field;
  234 +
  235 + Header(){
  236 +
  237 + }
  238 +
  239 + Header(String describe, String field) {
  240 + this.describe = describe;
  241 + this.field = field;
  242 + }
  243 +
  244 + public String getDescribe() {
  245 + return describe;
  246 + }
  247 +
  248 + public void setDescribe(String describe) {
  249 + this.describe = describe;
  250 + }
  251 +
  252 + public String getField() {
  253 + return field;
  254 + }
  255 +
  256 + public void setField(String field) {
  257 + this.field = field;
  258 + }
  259 + }
130 260 }
... ...
src/main/resources/static/pages/device/connection_search.html
... ... @@ -71,6 +71,9 @@
71 71 </div>
72 72 </div>
73 73 </div>
  74 + <form id="export_form" action="/devicegps/export" method="post" style="display:none;">
  75 + <input type="text" id="json" name="json">
  76 + </form>
74 77 <div class="tab-pane fade" id="tab2">
75 78 <div class="row">
76 79 <div class="col-md-12">
... ... @@ -98,6 +101,8 @@
98 101 <label class="checkbox-inline">
99 102 <input type="radio" name="filterOption" value="5">版本不等于
100 103 <input type="text" id="version" name="version" size="5">
  104 + &nbsp;
  105 + <a id="export_btn" class="btn btn-circle blue" href="#" data-pjax> 导出</a>
101 106 </label>
102 107 </div>
103 108 </div>
... ... @@ -227,7 +232,7 @@
227 232 </script>
228 233 <script>
229 234 $(function(){
230   - var opened;
  235 + var opened,filtered;
231 236 $('#myTab li:eq(0) a').tab('show');
232 237 $('#fileinput').fileinput({
233 238 'showPreview' : false,
... ... @@ -247,10 +252,11 @@ $(function(){
247 252 $(this).html(moment(parseInt($(this).html())).format('YYYY-M-D HH:mm:ss'));
248 253 });
249 254 opened = new Array();
  255 + filtered = new Array();
250 256 for (var j = 0,len = r.data.length;j < len;j++) {
251 257 var item = r.data[j];
252   - delete item.detail;
253 258 opened.push(item);
  259 + filtered.push(item);
254 260 }
255 261 }
256 262 $(this).fileinput('clear').fileinput('enable');
... ... @@ -265,15 +271,19 @@ $(function(){
265 271 $('input[type=radio][name=filterOption]').change(function() {
266 272 importDeviceSearch();
267 273 });
  274 +
  275 + $('#export_btn').on('click', function(e){
  276 + exportResult();
  277 + });
268 278  
269 279 function importDeviceSearch(){
270 280 if (!opened) return false;
271 281 var params = { json : JSON.stringify(opened) };
272   - $post('/devicegps/open/filter', params, function(r){
273   - debugger;
  282 + $post('/devicegps/opened', params, function(r){
274 283 if(r.errCode) layer.msg(r.msg);
275 284 else {
276   - var filtered = new Array(), val = $('input[type=radio][name=filterOption]:checked').val(), version = $('#version').val(), i = 0,item = null;
  285 + var val = $('input[type=radio][name=filterOption]:checked').val(), version = $('#version').val(), i = 0,item = null;
  286 + filtered = new Array();
277 287 switch(val) {
278 288 case '1':
279 289 filtered = r.data;
... ... @@ -313,6 +323,13 @@ $(function(){
313 323 }
314 324 });
315 325 }
  326 +
  327 + function exportResult() {
  328 + if (!filtered) return false;
  329 + $('#json').val(JSON.stringify(filtered));
  330 + var form = $('#export_form');
  331 + form.submit();
  332 + }
316 333  
317 334 var page = 0, initPagination;
318 335 var icheckOptions = {
... ...