Commit c88a1206fb8bd5788351e62dddb80e23df0d3378
Merge branch 'minhang' of http://222.66.0.204:8090/panzhaov5/bsth_control into minhang
Showing
2 changed files
with
154 additions
and
7 deletions
src/main/java/com/bsth/controller/DeviceGpsController.java
| 1 | package com.bsth.controller; | 1 | package com.bsth.controller; |
| 2 | 2 | ||
| 3 | +import java.io.BufferedOutputStream; | ||
| 3 | import java.io.BufferedReader; | 4 | import java.io.BufferedReader; |
| 4 | import java.io.File; | 5 | import java.io.File; |
| 5 | import java.io.FileInputStream; | 6 | import java.io.FileInputStream; |
| 6 | import java.io.IOException; | 7 | import java.io.IOException; |
| 7 | import java.io.InputStreamReader; | 8 | import java.io.InputStreamReader; |
| 9 | +import java.io.OutputStream; | ||
| 10 | +import java.text.SimpleDateFormat; | ||
| 8 | import java.util.ArrayList; | 11 | import java.util.ArrayList; |
| 12 | +import java.util.Date; | ||
| 9 | import java.util.HashMap; | 13 | import java.util.HashMap; |
| 10 | import java.util.List; | 14 | import java.util.List; |
| 11 | import java.util.Map; | 15 | import java.util.Map; |
| 12 | 16 | ||
| 13 | import javax.servlet.http.HttpServletRequest; | 17 | import javax.servlet.http.HttpServletRequest; |
| 18 | +import javax.servlet.http.HttpServletResponse; | ||
| 14 | 19 | ||
| 15 | import org.apache.commons.lang.StringEscapeUtils; | 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 | import org.springframework.beans.factory.annotation.Autowired; | 27 | import org.springframework.beans.factory.annotation.Autowired; |
| 17 | import org.springframework.util.StringUtils; | 28 | import org.springframework.util.StringUtils; |
| 18 | import org.springframework.web.bind.annotation.PathVariable; | 29 | import org.springframework.web.bind.annotation.PathVariable; |
| @@ -27,6 +38,7 @@ import com.bsth.data.gpsdata.GpsRealData; | @@ -27,6 +38,7 @@ import com.bsth.data.gpsdata.GpsRealData; | ||
| 27 | import com.fasterxml.jackson.core.JsonParseException; | 38 | import com.fasterxml.jackson.core.JsonParseException; |
| 28 | import com.fasterxml.jackson.databind.JsonMappingException; | 39 | import com.fasterxml.jackson.databind.JsonMappingException; |
| 29 | import com.fasterxml.jackson.databind.ObjectMapper; | 40 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 41 | +import com.fasterxml.jackson.databind.util.BeanUtil; | ||
| 30 | 42 | ||
| 31 | @RestController | 43 | @RestController |
| 32 | @RequestMapping("devicegps") | 44 | @RequestMapping("devicegps") |
| @@ -101,8 +113,8 @@ public class DeviceGpsController { | @@ -101,8 +113,8 @@ public class DeviceGpsController { | ||
| 101 | } | 113 | } |
| 102 | 114 | ||
| 103 | @SuppressWarnings("unchecked") | 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 | json = StringEscapeUtils.unescapeHtml(json); | 118 | json = StringEscapeUtils.unescapeHtml(json); |
| 107 | Map<String, Object> res = new HashMap<>(); | 119 | Map<String, Object> res = new HashMap<>(); |
| 108 | List<Map<String, Object>> data = null; | 120 | List<Map<String, Object>> data = null; |
| @@ -127,4 +139,122 @@ public class DeviceGpsController { | @@ -127,4 +139,122 @@ public class DeviceGpsController { | ||
| 127 | 139 | ||
| 128 | return res; | 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,6 +71,9 @@ | ||
| 71 | </div> | 71 | </div> |
| 72 | </div> | 72 | </div> |
| 73 | </div> | 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 | <div class="tab-pane fade" id="tab2"> | 77 | <div class="tab-pane fade" id="tab2"> |
| 75 | <div class="row"> | 78 | <div class="row"> |
| 76 | <div class="col-md-12"> | 79 | <div class="col-md-12"> |
| @@ -98,6 +101,8 @@ | @@ -98,6 +101,8 @@ | ||
| 98 | <label class="checkbox-inline"> | 101 | <label class="checkbox-inline"> |
| 99 | <input type="radio" name="filterOption" value="5">版本不等于 | 102 | <input type="radio" name="filterOption" value="5">版本不等于 |
| 100 | <input type="text" id="version" name="version" size="5"> | 103 | <input type="text" id="version" name="version" size="5"> |
| 104 | + | ||
| 105 | + <a id="export_btn" class="btn btn-circle blue" href="#" data-pjax> 导出</a> | ||
| 101 | </label> | 106 | </label> |
| 102 | </div> | 107 | </div> |
| 103 | </div> | 108 | </div> |
| @@ -227,7 +232,7 @@ | @@ -227,7 +232,7 @@ | ||
| 227 | </script> | 232 | </script> |
| 228 | <script> | 233 | <script> |
| 229 | $(function(){ | 234 | $(function(){ |
| 230 | - var opened; | 235 | + var opened,filtered; |
| 231 | $('#myTab li:eq(0) a').tab('show'); | 236 | $('#myTab li:eq(0) a').tab('show'); |
| 232 | $('#fileinput').fileinput({ | 237 | $('#fileinput').fileinput({ |
| 233 | 'showPreview' : false, | 238 | 'showPreview' : false, |
| @@ -247,10 +252,11 @@ $(function(){ | @@ -247,10 +252,11 @@ $(function(){ | ||
| 247 | $(this).html(moment(parseInt($(this).html())).format('YYYY-M-D HH:mm:ss')); | 252 | $(this).html(moment(parseInt($(this).html())).format('YYYY-M-D HH:mm:ss')); |
| 248 | }); | 253 | }); |
| 249 | opened = new Array(); | 254 | opened = new Array(); |
| 255 | + filtered = new Array(); | ||
| 250 | for (var j = 0,len = r.data.length;j < len;j++) { | 256 | for (var j = 0,len = r.data.length;j < len;j++) { |
| 251 | var item = r.data[j]; | 257 | var item = r.data[j]; |
| 252 | - delete item.detail; | ||
| 253 | opened.push(item); | 258 | opened.push(item); |
| 259 | + filtered.push(item); | ||
| 254 | } | 260 | } |
| 255 | } | 261 | } |
| 256 | $(this).fileinput('clear').fileinput('enable'); | 262 | $(this).fileinput('clear').fileinput('enable'); |
| @@ -265,15 +271,19 @@ $(function(){ | @@ -265,15 +271,19 @@ $(function(){ | ||
| 265 | $('input[type=radio][name=filterOption]').change(function() { | 271 | $('input[type=radio][name=filterOption]').change(function() { |
| 266 | importDeviceSearch(); | 272 | importDeviceSearch(); |
| 267 | }); | 273 | }); |
| 274 | + | ||
| 275 | + $('#export_btn').on('click', function(e){ | ||
| 276 | + exportResult(); | ||
| 277 | + }); | ||
| 268 | 278 | ||
| 269 | function importDeviceSearch(){ | 279 | function importDeviceSearch(){ |
| 270 | if (!opened) return false; | 280 | if (!opened) return false; |
| 271 | var params = { json : JSON.stringify(opened) }; | 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 | if(r.errCode) layer.msg(r.msg); | 283 | if(r.errCode) layer.msg(r.msg); |
| 275 | else { | 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 | switch(val) { | 287 | switch(val) { |
| 278 | case '1': | 288 | case '1': |
| 279 | filtered = r.data; | 289 | filtered = r.data; |
| @@ -313,6 +323,13 @@ $(function(){ | @@ -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 | var page = 0, initPagination; | 334 | var page = 0, initPagination; |
| 318 | var icheckOptions = { | 335 | var icheckOptions = { |