Commit 217f4da1b8670fbc94de4b417baca1b2f9250f52
1 parent
cbfe66b5
车载设备查询加入导出
Showing
2 changed files
with
140 additions
and
4 deletions
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; | |
| 16 | 26 | import org.springframework.beans.factory.annotation.Autowired; |
| 17 | 27 | import org.springframework.util.StringUtils; |
| 18 | 28 | import org.springframework.web.bind.annotation.PathVariable; |
| ... | ... | @@ -101,8 +111,8 @@ public class DeviceGpsController { |
| 101 | 111 | } |
| 102 | 112 | |
| 103 | 113 | @SuppressWarnings("unchecked") |
| 104 | - @RequestMapping(value = "/open/filter", method = RequestMethod.POST) | |
| 105 | - public Map<String, Object> filter(@RequestParam(value = "json")String json) { | |
| 114 | + @RequestMapping(value = "/opened", method = RequestMethod.POST) | |
| 115 | + public Map<String, Object> opened(@RequestParam(value = "json")String json) { | |
| 106 | 116 | json = StringEscapeUtils.unescapeHtml(json); |
| 107 | 117 | Map<String, Object> res = new HashMap<>(); |
| 108 | 118 | List<Map<String, Object>> data = null; |
| ... | ... | @@ -127,4 +137,115 @@ public class DeviceGpsController { |
| 127 | 137 | |
| 128 | 138 | return res; |
| 129 | 139 | } |
| 140 | + | |
| 141 | + @SuppressWarnings("unchecked") | |
| 142 | + @RequestMapping(value = "/export", method = RequestMethod.POST) | |
| 143 | + public void export(@RequestParam(value = "json")String json, HttpServletResponse response) { | |
| 144 | + json = StringEscapeUtils.unescapeHtml(json); | |
| 145 | + List<Map<String, Object>> data = null; | |
| 146 | + OutputStream out = null; | |
| 147 | + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | |
| 148 | + try { | |
| 149 | + data = new ObjectMapper().readValue(json, List.class); | |
| 150 | + for (Map<String, Object> info : data) { | |
| 151 | + GpsEntity gps = gpsRealData.findByDeviceId((String)info.get("deviceId")); | |
| 152 | + info.put("lineId", gps.getLineId()); | |
| 153 | + info.put("valid", gps.getValid() == 0 ? "有效" : "无效"); | |
| 154 | + info.put("timestamp", sdf.format(new Date(gps.getTimestamp()))); | |
| 155 | + info.put("version", gps.getVersion()); | |
| 156 | + } | |
| 157 | + | |
| 158 | + List<Header> head = new ArrayList<>(); | |
| 159 | + head.add(new Header("线路编码", "lineCode")); | |
| 160 | + head.add(new Header("线路名称", "lineName")); | |
| 161 | + head.add(new Header("内部编码", "inCode")); | |
| 162 | + head.add(new Header("识别码", "deviceId")); | |
| 163 | + head.add(new Header("线路ID", "lineId")); | |
| 164 | + head.add(new Header("GPS", "valid")); | |
| 165 | + head.add(new Header("report", "timestamp")); | |
| 166 | + head.add(new Header("版本", "version")); | |
| 167 | + | |
| 168 | + // 清空response | |
| 169 | + response.reset(); | |
| 170 | + // 设置response的Header | |
| 171 | + response.addHeader("Content-Disposition", "attachment;filename=" + System.currentTimeMillis() + ".xls"); | |
| 172 | + response.setContentType("application/vnd.ms-excel;charset=UTF-8"); | |
| 173 | + out = new BufferedOutputStream(response.getOutputStream()); | |
| 174 | + excel(head, data, out); | |
| 175 | + out.flush(); | |
| 176 | + } catch (JsonParseException e) { | |
| 177 | + // TODO Auto-generated catch block | |
| 178 | + e.printStackTrace(); | |
| 179 | + } catch (JsonMappingException e) { | |
| 180 | + // TODO Auto-generated catch block | |
| 181 | + e.printStackTrace(); | |
| 182 | + } catch (IOException e) { | |
| 183 | + // TODO Auto-generated catch block | |
| 184 | + e.printStackTrace(); | |
| 185 | + } finally { | |
| 186 | + try { | |
| 187 | + if (out != null) out.close(); | |
| 188 | + } catch (IOException e) { | |
| 189 | + e.printStackTrace(); | |
| 190 | + } | |
| 191 | + } | |
| 192 | + } | |
| 193 | + | |
| 194 | + private void excel(List<Header> head, List<Map<String, Object>> data, OutputStream out) throws IOException { | |
| 195 | + // 声明一个工作薄 | |
| 196 | + HSSFWorkbook wb = new HSSFWorkbook(); | |
| 197 | + // 生成一个表格 | |
| 198 | + HSSFSheet sheet = wb.createSheet("1"); | |
| 199 | + // 产生表格标题行 | |
| 200 | + HSSFRow row = sheet.createRow(0); | |
| 201 | + for (int i = 0;i < head.size();i++) { | |
| 202 | + HSSFCell cell = row.createCell(i); | |
| 203 | + HSSFRichTextString text = new HSSFRichTextString(head.get(i).getDescribe()); | |
| 204 | + cell.setCellValue(text); | |
| 205 | + } | |
| 206 | + | |
| 207 | + int rownum = 1; | |
| 208 | + for (Map<String, Object> map : data) { | |
| 209 | + HSSFRow r = sheet.createRow(rownum); | |
| 210 | + for (int i = 0;i < head.size();i++) { | |
| 211 | + HSSFCell cell = r.createCell(i); | |
| 212 | + HSSFRichTextString text = new HSSFRichTextString(String.valueOf(map.get(head.get(i).getField()))); | |
| 213 | + cell.setCellValue(text); | |
| 214 | + } | |
| 215 | + rownum++; | |
| 216 | + } | |
| 217 | + | |
| 218 | + wb.write(out); | |
| 219 | + wb.close(); | |
| 220 | + } | |
| 221 | + | |
| 222 | + final class Header { | |
| 223 | + private String describe; | |
| 224 | + private String field; | |
| 225 | + | |
| 226 | + Header(){ | |
| 227 | + | |
| 228 | + } | |
| 229 | + | |
| 230 | + Header(String describe, String field) { | |
| 231 | + this.describe = describe; | |
| 232 | + this.field = field; | |
| 233 | + } | |
| 234 | + | |
| 235 | + public String getDescribe() { | |
| 236 | + return describe; | |
| 237 | + } | |
| 238 | + | |
| 239 | + public void setDescribe(String describe) { | |
| 240 | + this.describe = describe; | |
| 241 | + } | |
| 242 | + | |
| 243 | + public String getField() { | |
| 244 | + return field; | |
| 245 | + } | |
| 246 | + | |
| 247 | + public void setField(String field) { | |
| 248 | + this.field = field; | |
| 249 | + } | |
| 250 | + } | |
| 130 | 251 | } | ... | ... |
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 | + | |
| 105 | + <a id="export_btn" class="btn btn-circle blue" href="#" data-pjax> 导出</a> | |
| 101 | 106 | </label> |
| 102 | 107 | </div> |
| 103 | 108 | </div> |
| ... | ... | @@ -265,12 +270,15 @@ $(function(){ |
| 265 | 270 | $('input[type=radio][name=filterOption]').change(function() { |
| 266 | 271 | importDeviceSearch(); |
| 267 | 272 | }); |
| 273 | + | |
| 274 | + $('#export_btn').on('click', function(e){ | |
| 275 | + exportResult(); | |
| 276 | + }); | |
| 268 | 277 | |
| 269 | 278 | function importDeviceSearch(){ |
| 270 | 279 | if (!opened) return false; |
| 271 | 280 | var params = { json : JSON.stringify(opened) }; |
| 272 | - $post('/devicegps/open/filter', params, function(r){ | |
| 273 | - debugger; | |
| 281 | + $post('/devicegps/opened', params, function(r){ | |
| 274 | 282 | if(r.errCode) layer.msg(r.msg); |
| 275 | 283 | else { |
| 276 | 284 | var filtered = new Array(), val = $('input[type=radio][name=filterOption]:checked').val(), version = $('#version').val(), i = 0,item = null; |
| ... | ... | @@ -313,6 +321,13 @@ $(function(){ |
| 313 | 321 | } |
| 314 | 322 | }); |
| 315 | 323 | } |
| 324 | + | |
| 325 | + function exportResult() { | |
| 326 | + if (!opened) return false; | |
| 327 | + $('#json').val(JSON.stringify(opened)); | |
| 328 | + var form = $('#export_form'); | |
| 329 | + form.submit(); | |
| 330 | + } | |
| 316 | 331 | |
| 317 | 332 | var page = 0, initPagination; |
| 318 | 333 | var icheckOptions = { | ... | ... |