作者 王勇

下载excel,功能,基础架子

@@ -111,6 +111,7 @@ @@ -111,6 +111,7 @@
111 <artifactId>poi-ooxml</artifactId> 111 <artifactId>poi-ooxml</artifactId>
112 <version>4.1.0</version> 112 <version>4.1.0</version>
113 </dependency> 113 </dependency>
  114 + <!-- excel导出 end-->
114 <dependency> 115 <dependency>
115 <groupId>org.projectlombok</groupId> 116 <groupId>org.projectlombok</groupId>
116 <artifactId>lombok</artifactId> 117 <artifactId>lombok</artifactId>
@@ -2,7 +2,6 @@ package com.sunyo.wlpt.cgonms.provide.controller; @@ -2,7 +2,6 @@ package com.sunyo.wlpt.cgonms.provide.controller;
2 2
3 import com.alibaba.fastjson.JSON; 3 import com.alibaba.fastjson.JSON;
4 import com.alibaba.fastjson.serializer.SerializerFeature; 4 import com.alibaba.fastjson.serializer.SerializerFeature;
5 -import com.google.common.collect.Lists;  
6 5
7 import com.sunyo.wlpt.cgonms.provide.domain.*; 6 import com.sunyo.wlpt.cgonms.provide.domain.*;
8 import com.sunyo.wlpt.cgonms.provide.feign.GetCgoAsmFeign; 7 import com.sunyo.wlpt.cgonms.provide.feign.GetCgoAsmFeign;
@@ -22,7 +21,6 @@ import java.io.IOException; @@ -22,7 +21,6 @@ import java.io.IOException;
22 21
23 import javax.annotation.Resource; 22 import javax.annotation.Resource;
24 import javax.servlet.http.HttpServletRequest; 23 import javax.servlet.http.HttpServletRequest;
25 -import java.io.IOException;  
26 import java.text.SimpleDateFormat; 24 import java.text.SimpleDateFormat;
27 import java.util.Date; 25 import java.util.Date;
28 import java.util.List; 26 import java.util.List;
@@ -93,16 +91,12 @@ public class NmsController { @@ -93,16 +91,12 @@ public class NmsController {
93 @RequestParam(value = "flightNo", required = false) String flightNo, 91 @RequestParam(value = "flightNo", required = false) String flightNo,
94 HttpServletRequest request) { 92 HttpServletRequest request) {
95 93
96 - /**  
97 - * 1.取token作爲websocket的sid  
98 - * 2.每個可以返回給前端的消息通過websocket發送回去  
99 - */ 94 + // 获取token
100 String sid = request.getHeader("Authorization"); 95 String sid = request.getHeader("Authorization");
101 log.info("token的值:" + sid); 96 log.info("token的值:" + sid);
102 final String startTime = sdf.format(new Date()); 97 final String startTime = sdf.format(new Date());
103 - ResultWs resultStart = new ResultWs("数据开始获取,开始时间:"+ startTime);  
104 -  
105 - sendMsgByWebsocket("开始时间:" + startTime, sid); 98 + String resultStart = JSON.toJSONString(new ResultWs("数据开始获取,开始时间:" + startTime));
  99 + sendMsgByWebsocket(resultStart, sid);
106 ResultJson resultJson = new ResultJson(); 100 ResultJson resultJson = new ResultJson();
107 101
108 /** 102 /**
@@ -116,7 +110,7 @@ public class NmsController { @@ -116,7 +110,7 @@ public class NmsController {
116 //控制index,查询数据的时候,保证索引的位置不出错 110 //控制index,查询数据的时候,保证索引的位置不出错
117 int index = 0; 111 int index = 0;
118 try { 112 try {
119 - // 如果数据大于50个,则每次获取前50条。逐次获取 113 + /*如果数据大于50个,则每次获取前50条。逐次获取*/
120 if (resultList != null && !resultList.isEmpty() && resultList.size() > THREAD_ACCOUNT) { 114 if (resultList != null && !resultList.isEmpty() && resultList.size() > THREAD_ACCOUNT) {
121 while (index < resultList.size()) { 115 while (index < resultList.size()) {
122 //如果剩余未处理的数量 大于 等于线程数量,则遍历次数为线程数量 116 //如果剩余未处理的数量 大于 等于线程数量,则遍历次数为线程数量
@@ -130,8 +124,7 @@ public class NmsController { @@ -130,8 +124,7 @@ public class NmsController {
130 resultList.set(i, result); 124 resultList.set(i, result);
131 125
132 int temp = i + index; 126 int temp = i + index;
133 - ResultWs resultWs = new ResultWs();  
134 - resultWs = new ResultWs(sid, "获取数据,第" + temp + "条", result); 127 + ResultWs resultWs = new ResultWs("获取数据,第" + temp + "条", "201");
135 String resultJs = JSON.toJSONString(resultWs, SerializerFeature.DisableCircularReferenceDetect); 128 String resultJs = JSON.toJSONString(resultWs, SerializerFeature.DisableCircularReferenceDetect);
136 129
137 sendMsgByWebsocket(resultJs, sid); 130 sendMsgByWebsocket(resultJs, sid);
@@ -150,8 +143,7 @@ public class NmsController { @@ -150,8 +143,7 @@ public class NmsController {
150 result = threadJob(resultList.get(i + index), latch, threadPool); 143 result = threadJob(resultList.get(i + index), latch, threadPool);
151 144
152 int temp = i + index; 145 int temp = i + index;
153 - ResultWs resultWs = new ResultWs();  
154 - resultWs = new ResultWs(sid, "获取数据,第" + temp + "条", result); 146 + ResultWs resultWs = new ResultWs("获取数据,第" + temp + "条", "201");
155 147
156 String resultJs = JSON.toJSONString(resultWs, SerializerFeature.DisableCircularReferenceDetect); 148 String resultJs = JSON.toJSONString(resultWs, SerializerFeature.DisableCircularReferenceDetect);
157 sendMsgByWebsocket(resultJs, sid); 149 sendMsgByWebsocket(resultJs, sid);
@@ -171,8 +163,7 @@ public class NmsController { @@ -171,8 +163,7 @@ public class NmsController {
171 result = threadJob(resultList.get(i), latch, threadPool); 163 result = threadJob(resultList.get(i), latch, threadPool);
172 164
173 resultList.set(i, result); 165 resultList.set(i, result);
174 - ResultWs resultWs = new ResultWs();  
175 - resultWs = new ResultWs(sid, "获取数据,第" + i + "条", result); 166 + ResultWs resultWs = new ResultWs("获取数据,第" + i + "条", "201");
176 String resultJs = JSON.toJSONString(resultWs, SerializerFeature.DisableCircularReferenceDetect); 167 String resultJs = JSON.toJSONString(resultWs, SerializerFeature.DisableCircularReferenceDetect);
177 168
178 sendMsgByWebsocket(resultJs, sid); 169 sendMsgByWebsocket(resultJs, sid);
@@ -182,22 +173,15 @@ public class NmsController { @@ -182,22 +173,15 @@ public class NmsController {
182 } 173 }
183 } catch (Exception e) { 174 } catch (Exception e) {
184 e.printStackTrace(); 175 e.printStackTrace();
185 - log.error("获取目录文件出错", e); 176 + log.error("获取数据出错", e);
186 } 177 }
187 -  
188 -  
189 - System.out.println("出港信息,获取完毕");  
190 - System.out.println("index的值为:" + index);  
191 final String endTime = sdf.format(new Date()); 178 final String endTime = sdf.format(new Date());
192 - 179 + System.out.println("出港信息,获取完毕,index的值为:" + index);
193 System.out.println("结束时间:" + endTime); 180 System.out.println("结束时间:" + endTime);
194 - String resultJs = JSON.toJSONString(new ResultWs(sid, "获取数据,完毕", resultList)); 181 + String resultJs = JSON.toJSONString(new ResultWs("获取数据,结束时间:" + endTime, resultList, "200"));
195 sendMsgByWebsocket(resultJs, sid); 182 sendMsgByWebsocket(resultJs, sid);
196 - sendMsgByWebsocket("出港信息,获取完毕,结束时间:" + endTime, sid);  
197 -  
198 resultJson.setCode("200"); 183 resultJson.setCode("200");
199 resultJson.setData(resultList); 184 resultJson.setData(resultList);
200 -  
201 return resultJson; 185 return resultJson;
202 } 186 }
203 187
@@ -246,16 +230,11 @@ public class NmsController { @@ -246,16 +230,11 @@ public class NmsController {
246 * 区域描述,中文 230 * 区域描述,中文
247 */ 231 */
248 BasArea basArea = basAreaService.getBasAreaInfo(ta.getArea()); 232 BasArea basArea = basAreaService.getBasAreaInfo(ta.getArea());
  233 +
249 //航班目的区域,中文描述,所属洲 234 //航班目的区域,中文描述,所属洲
250 result.setAreaDescChn(basArea.getAreaDescChn()); 235 result.setAreaDescChn(basArea.getAreaDescChn());
251 236
252 - /**  
253 - * 6.根据 理货信息 中的运单号,获取预配表中的相应数据  
254 - *  
255 - * 发货人名称  
256 - * 收货人名称  
257 - * 航班目的站  
258 - */ 237 + // 6.根据 理货信息 中的运单号,获取预配表中的相应数据
259 List<PrepareMaster> pm = prepareMasterService.getPrepareMasterInfo(result.getWaybillNoMaster()); 238 List<PrepareMaster> pm = prepareMasterService.getPrepareMasterInfo(result.getWaybillNoMaster());
260 if (pm != null && pm.size() > 0) { 239 if (pm != null && pm.size() > 0) {
261 //发货人名称 240 //发货人名称
@@ -266,7 +245,7 @@ public class NmsController { @@ -266,7 +245,7 @@ public class NmsController {
266 result.setAimStation(pm.get(0).getDestinationStation()); 245 result.setAimStation(pm.get(0).getDestinationStation());
267 } 246 }
268 /** 247 /**
269 - * 获取数据仓库的对应数据 248 + * //获取数据仓库的对应数据
270 249
271 // if (result.getWaybillNoMaster() != null && result.getWaybillNoMaster() != "") {} 250 // if (result.getWaybillNoMaster() != null && result.getWaybillNoMaster() != "") {}
272 ResultExitData dataWareHouseInfo=new ResultExitData(); 251 ResultExitData dataWareHouseInfo=new ResultExitData();
@@ -277,9 +256,7 @@ public class NmsController { @@ -277,9 +256,7 @@ public class NmsController {
277 result.setTotalGrossWeightMeasure(dataWareHouseInfo.getTotalGrossWeightMeasure()); 256 result.setTotalGrossWeightMeasure(dataWareHouseInfo.getTotalGrossWeightMeasure());
278 */ 257 */
279 258
280 - /**  
281 - * 获取运输工具的对应数据  
282 - */ 259 + //获取运输工具的对应数据
283 ResultExitData transportInfo = new ResultExitData(); 260 ResultExitData transportInfo = new ResultExitData();
284 transportInfo = getTransportFeign.getInfo(result.getFlightNo(), result.getFlightDate()); 261 transportInfo = getTransportFeign.getInfo(result.getFlightNo(), result.getFlightDate());
285 //设置航班公司 262 //设置航班公司
@@ -295,10 +272,7 @@ public class NmsController { @@ -295,10 +272,7 @@ public class NmsController {
295 //设置航班计划时间 272 //设置航班计划时间
296 result.setFlightPlanTime(transportInfo.getFlightPlanTime()); 273 result.setFlightPlanTime(transportInfo.getFlightPlanTime());
297 274
298 - /**  
299 - * 获取代理人的相关数据  
300 - */  
301 -// if (result.getWaybillNoMaster() != null && result.getWaybillNoMaster() != "") { } 275 + //获取代理人的相关数据
302 ResultExitData asmInfo = new ResultExitData(); 276 ResultExitData asmInfo = new ResultExitData();
303 asmInfo = getCgoAsmFeign.getInfo(result.getWaybillNoMaster()); 277 asmInfo = getCgoAsmFeign.getInfo(result.getWaybillNoMaster());
304 //设置结算代理人 278 //设置结算代理人
@@ -322,6 +296,11 @@ public class NmsController { @@ -322,6 +296,11 @@ public class NmsController {
322 return result; 296 return result;
323 } 297 }
324 298
  299 + /**
  300 + * 由于前台传过来的时间参数是字符串,故转一下格式
  301 + *
  302 + * @param binder 转换器
  303 + */
325 @InitBinder 304 @InitBinder
326 public void initBinder(WebDataBinder binder) { 305 public void initBinder(WebDataBinder binder) {
327 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 306 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
@@ -329,12 +308,15 @@ public class NmsController { @@ -329,12 +308,15 @@ public class NmsController {
329 binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); 308 binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
330 } 309 }
331 310
  311 + /**
  312 + * @param msg JSON字符串
  313 + * @param sid 唯一识别值
  314 + */
332 private void sendMsgByWebsocket(String msg, String sid) { 315 private void sendMsgByWebsocket(String msg, String sid) {
333 try { 316 try {
334 WebSocketServer.sendInfo(msg, sid); 317 WebSocketServer.sendInfo(msg, sid);
335 } catch (IOException e) { 318 } catch (IOException e) {
336 e.printStackTrace(); 319 e.printStackTrace();
337 -  
338 } 320 }
339 } 321 }
340 } 322 }
@@ -12,6 +12,7 @@ import org.springframework.web.bind.annotation.RestController; @@ -12,6 +12,7 @@ import org.springframework.web.bind.annotation.RestController;
12 import javax.servlet.http.HttpServletResponse; 12 import javax.servlet.http.HttpServletResponse;
13 import java.io.*; 13 import java.io.*;
14 import java.text.SimpleDateFormat; 14 import java.text.SimpleDateFormat;
  15 +import java.util.Date;
15 import java.util.List; 16 import java.util.List;
16 import java.util.UUID; 17 import java.util.UUID;
17 18
@@ -40,6 +41,7 @@ public class ExitExcel { @@ -40,6 +41,7 @@ public class ExitExcel {
40 } 41 }
41 42
42 private void exportExcel(List<ResultExitData> exitInfoList, HttpServletResponse response) { 43 private void exportExcel(List<ResultExitData> exitInfoList, HttpServletResponse response) {
  44 + try {
43 // 创建工作簿 45 // 创建工作簿
44 HSSFWorkbook workbook = new HSSFWorkbook(); 46 HSSFWorkbook workbook = new HSSFWorkbook();
45 // 创建表 47 // 创建表
@@ -79,83 +81,84 @@ public class ExitExcel { @@ -79,83 +81,84 @@ public class ExitExcel {
79 }; 81 };
80 HSSFCell cell; 82 HSSFCell cell;
81 // 设置表头 83 // 设置表头
82 - for (int i = 0; i < head.length; i++) { 84 + for (Integer i = 0; i < head.length; i++) {
83 cell = row.createCell(i); 85 cell = row.createCell(i);
84 cell.setCellValue(head[i]); 86 cell.setCellValue(head[i]);
85 cell.setCellStyle(cellStyle); 87 cell.setCellStyle(cellStyle);
86 // 设置单元格宽度 88 // 设置单元格宽度
  89 + // 256*width+184
87 sheet.setColumnWidth(i, 4000); 90 sheet.setColumnWidth(i, 4000);
88 } 91 }
89 // 设置表格内容 92 // 设置表格内容
90 - for (int i = 0; i < exitInfoList.size(); i++) { 93 + for (Integer i = 0; i < exitInfoList.size(); i++) {
91 row = sheet.createRow(i + 1); 94 row = sheet.createRow(i + 1);
92 - ResultExitData ResultExitData = exitInfoList.get(i); 95 + ResultExitData resultExitData = exitInfoList.get(i);
93 // 这里是内容设置,替换则自己的数据即可 96 // 这里是内容设置,替换则自己的数据即可
94 - String[] excelTitle = new String[40]; 97 + String[] excelTitle = new String[27];
95 98
96 //取出数据,运单号 99 //取出数据,运单号
97 - excelTitle[0] = ResultExitData.getWaybillNoMaster(); 100 + excelTitle[0] = resultExitData.getWaybillNoMaster();
98 //件数 101 //件数
99 - excelTitle[1] = ResultExitData.getTallyTotalPiece(); 102 + excelTitle[1] = resultExitData.getTallyTotalPiece();
100 //重量 103 //重量
101 - excelTitle[2] = ResultExitData.getTallyTotalWeight(); 104 + excelTitle[2] = resultExitData.getTallyTotalWeight();
102 //计费重量 105 //计费重量
103 - excelTitle[3] = String.valueOf(ResultExitData.getTotalGrossWeightMeasure()); 106 + excelTitle[3] = String.valueOf(resultExitData.getTotalGrossWeightMeasure());
104 //体积 107 //体积
105 - excelTitle[4] = String.valueOf(ResultExitData.getVolumeMeasure()); 108 + excelTitle[4] = String.valueOf(resultExitData.getVolumeMeasure());
106 //品名 109 //品名
107 - excelTitle[5] = ResultExitData.getSdCargoName(); 110 + excelTitle[5] = resultExitData.getSdCargoName();
108 //二级品类 111 //二级品类
109 - excelTitle[6] = ResultExitData.getTwoTypeName(); 112 + excelTitle[6] = resultExitData.getTwoTypeName();
110 //一级品类 113 //一级品类
111 - excelTitle[7] = ResultExitData.getTypeName(); 114 + excelTitle[7] = resultExitData.getTypeName();
112 //货物目的站 115 //货物目的站
113 - excelTitle[8] = ResultExitData.getAimStation(); 116 + excelTitle[8] = resultExitData.getAimStation();
114 //货物目的国家/地区 117 //货物目的国家/地区
115 - excelTitle[9] = ResultExitData.getCountry(); 118 + excelTitle[9] = resultExitData.getCountry();
116 //所属洲 119 //所属洲
117 - excelTitle[10] = ResultExitData.getAreaDescChn(); 120 + excelTitle[10] = resultExitData.getAreaDescChn();
118 //航空公司 121 //航空公司
119 - excelTitle[11] = ResultExitData.getAirCompany(); 122 + excelTitle[11] = resultExitData.getAirCompany();
120 //航班号 123 //航班号
121 - excelTitle[12] = ResultExitData.getFlightNo(); 124 + excelTitle[12] = resultExitData.getFlightNo();
122 //航班日期 125 //航班日期
123 - excelTitle[13] = sdfShort.format(ResultExitData.getFlightDate()); 126 + excelTitle[13] = isNullShort(resultExitData.getFlightDate());
124 //航班时间 127 //航班时间
125 - excelTitle[14] = sdfLong.format(ResultExitData.getFlightTime()); 128 + excelTitle[14] = isNullLong(resultExitData.getFlightTime());
126 //航班计划日期 129 //航班计划日期
127 - excelTitle[15] = sdfShort.format(ResultExitData.getFlightPlanDate()); 130 + excelTitle[15] = isNullShort(resultExitData.getFlightPlanDate());
128 //航班计划时间 131 //航班计划时间
129 - excelTitle[16] = sdfLong.format(ResultExitData.getFlightPlanTime()); 132 + excelTitle[16] = isNullLong(resultExitData.getFlightPlanTime());
130 //航段 133 //航段
131 - excelTitle[17] = ResultExitData.getSegment(); 134 + excelTitle[17] = resultExitData.getSegment();
132 //机号 135 //机号
133 - excelTitle[18] = ResultExitData.getCfNo(); 136 + excelTitle[18] = resultExitData.getCfNo();
134 //机型 137 //机型
135 - excelTitle[19] = ResultExitData.getCfTp(); 138 + excelTitle[19] = resultExitData.getCfTp();
136 //最大业载 139 //最大业载
137 excelTitle[20] = ""; 140 excelTitle[20] = "";
138 //运单发货人 141 //运单发货人
139 - excelTitle[21] = ResultExitData.getShipperName(); 142 + excelTitle[21] = resultExitData.getShipperName();
140 //运单收货人 143 //运单收货人
141 - excelTitle[22] = ResultExitData.getConsigneeName(); 144 + excelTitle[22] = resultExitData.getConsigneeName();
142 //订舱代理人(代理人全称) 145 //订舱代理人(代理人全称)
143 - excelTitle[23] = ResultExitData.getOrderName(); 146 + excelTitle[23] = resultExitData.getOrderName();
144 //结算代理人(代理人全程) 147 //结算代理人(代理人全程)
145 - excelTitle[24] = ResultExitData.getCountName(); 148 + excelTitle[24] = resultExitData.getCountName();
146 //自定义项 149 //自定义项
147 excelTitle[25] = ""; 150 excelTitle[25] = "";
148 151
149 - for (int j = 0; j < excelTitle.length; j++) { 152 + for (Integer j = 0; j < excelTitle.length; j++) {
150 row.createCell(j).setCellValue(excelTitle[j]); 153 row.createCell(j).setCellValue(excelTitle[j]);
151 } 154 }
152 } 155 }
153 156
154 - String file_name= UUID.randomUUID().toString().replaceAll("-", ""); 157 + String file_name = UUID.randomUUID().toString().replaceAll("-", "");
155 // 设置文件名 158 // 设置文件名
156 - String title = file_name+".xls"; 159 + String title = file_name + ".xls";
157 160
158 - String filePath = disc+":" + File.separator + dir + File.separator + title; 161 + String filePath = disc + ":" + File.separator + dir + File.separator + title;
159 /** 162 /**
160 * 创建文件夹 163 * 创建文件夹
161 */ 164 */
@@ -175,5 +178,54 @@ public class ExitExcel { @@ -175,5 +178,54 @@ public class ExitExcel {
175 } catch (IOException e) { 178 } catch (IOException e) {
176 e.printStackTrace(); 179 e.printStackTrace();
177 } 180 }
  181 + } catch (Exception e) {
  182 + e.printStackTrace();
  183 + }
  184 + }
  185 +
  186 + /**
  187 + * 时间戳转换成字符串
  188 + */
  189 + public static String getDateToLong(long time) {
  190 + Date d = new Date(time);
  191 + SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  192 + return sf.format(d);
  193 + }
  194 +
  195 + /**
  196 + * 时间戳转换成字符串
  197 + */
  198 + public static String getDateToShort(long time) {
  199 + Date d = new Date(time);
  200 + SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
  201 + return sf.format(d);
  202 + }
  203 +
  204 + /**
  205 + * 判断传递来的时间,是否为空,为null,则返回"",不为空,则规范格式 "yyyy-MM-dd"
  206 + *
  207 + * @param time Date类型时间
  208 + * @return
  209 + */
  210 + public static String isNullShort(Date time) {
  211 + if (null == time) {
  212 + return "";
  213 + } else {
  214 + return getDateToShort(time.getTime());
  215 + }
  216 + }
  217 +
  218 + /**
  219 + * 判断传递来的时间,是否为空,为null,则返回"",不为空,则规范格式 "yyyy-MM-dd HH:mm:ss"
  220 + *
  221 + * @param time Date类型时间
  222 + * @return
  223 + */
  224 + public static String isNullLong(Date time) {
  225 + if (null == time) {
  226 + return "";
  227 + } else {
  228 + return getDateToLong(time.getTime());
  229 + }
178 } 230 }
179 } 231 }
@@ -20,33 +20,30 @@ public class ResultWs<T> implements Serializable { @@ -20,33 +20,30 @@ public class ResultWs<T> implements Serializable {
20 20
21 T data; 21 T data;
22 22
  23 +
23 String status; 24 String status;
24 25
25 public ResultWs() { 26 public ResultWs() {
26 } 27 }
27 28
28 - public ResultWs(String sid, T data) {  
29 - this.sid = sid;  
30 - this.data = data; 29 + public ResultWs(String message, String status) {
  30 + this.message = message;
  31 + this.status = status;
31 } 32 }
32 33
33 - public ResultWs(String sid, String message, T data) {  
34 - this.sid = sid; 34 + public ResultWs(String message, T data) {
35 this.message = message; 35 this.message = message;
36 this.data = data; 36 this.data = data;
37 } 37 }
38 38
39 - public ResultWs(String sid, T data, String status) {  
40 - this.sid = sid;  
41 - this.data = data;  
42 - this.status = status;  
43 - }  
44 39
45 - public ResultWs(String message, String status) { 40 + public ResultWs(String message, T data, String status) {
46 this.message = message; 41 this.message = message;
  42 + this.data = data;
47 this.status = status; 43 this.status = status;
48 } 44 }
49 45
  46 +
50 public ResultWs(String message) { 47 public ResultWs(String message) {
51 this.message = message; 48 this.message = message;
52 } 49 }
1 package com.sunyo.wlpt.cgonms.provide.websocket; 1 package com.sunyo.wlpt.cgonms.provide.websocket;
2 2
3 3
  4 +import cn.hutool.json.JSON;
  5 +import cn.hutool.json.JSONObject;
  6 +import cn.hutool.json.JSONUtil;
4 import cn.hutool.log.Log; 7 import cn.hutool.log.Log;
5 import cn.hutool.log.LogFactory; 8 import cn.hutool.log.LogFactory;
6 import com.sunyo.wlpt.cgonms.provide.controller.NmsController; 9 import com.sunyo.wlpt.cgonms.provide.controller.NmsController;
  10 +import com.sunyo.wlpt.cgonms.provide.response.ResultJson;
7 import org.springframework.stereotype.Component; 11 import org.springframework.stereotype.Component;
8 12
  13 +import javax.annotation.Resource;
9 import javax.websocket.*; 14 import javax.websocket.*;
10 import javax.websocket.server.PathParam; 15 import javax.websocket.server.PathParam;
11 import javax.websocket.server.ServerEndpoint; 16 import javax.websocket.server.ServerEndpoint;
@@ -20,6 +25,9 @@ import java.util.concurrent.atomic.AtomicInteger; @@ -20,6 +25,9 @@ import java.util.concurrent.atomic.AtomicInteger;
20 @Component 25 @Component
21 public class WebSocketServer { 26 public class WebSocketServer {
22 27
  28 + @Resource
  29 + NmsController nmsController;
  30 +
23 static Log log = LogFactory.get(WebSocketServer.class); 31 static Log log = LogFactory.get(WebSocketServer.class);
24 32
25 //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。 33 //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
@@ -45,9 +53,9 @@ public class WebSocketServer { @@ -45,9 +53,9 @@ public class WebSocketServer {
45 log.info("有新窗口开始监听:" + sid + ",当前在线人数为" + getOnlineCount()); 53 log.info("有新窗口开始监听:" + sid + ",当前在线人数为" + getOnlineCount());
46 this.sid = sid; 54 this.sid = sid;
47 try { 55 try {
48 - String openMessage= "{\n" +  
49 - " \"sid\":\""+sid+"\",\n" +  
50 - " \"session\":\""+session+"\",\n" + 56 + String openMessage = "{\n" +
  57 + " \"sid\":\"" + sid + "\",\n" +
  58 + " \"session\":\"" + session + "\",\n" +
51 " \"message\": \"连接成功\",\n" + 59 " \"message\": \"连接成功\",\n" +
52 " \"data\":{\"flightdate\":\"2015-02-05\",\"flightNo\":\"CV987\"}\n" + 60 " \"data\":{\"flightdate\":\"2015-02-05\",\"flightNo\":\"CV987\"}\n" +
53 "}"; 61 "}";
@@ -70,19 +78,15 @@ public class WebSocketServer { @@ -70,19 +78,15 @@ public class WebSocketServer {
70 /** 78 /**
71 * 收到客户端消息后调用的方法 79 * 收到客户端消息后调用的方法
72 * 80 *
73 - * @param message 客户端发送过来的消息,以json形式传递接收,  
74 - * 如:{  
75 - * "sid":"aaa",  
76 - * "session":"",  
77 - * "message": "",  
78 - * "data":{"flightdate":"2015-02-05","flightNo":"CV987"}  
79 - *  
80 - *  
81 - * } 81 + * @param message 客户端发送过来的消息,以json形式传递接收
82 */ 82 */
83 @OnMessage 83 @OnMessage
84 public void onMessage(String message, Session session) { 84 public void onMessage(String message, Session session) {
  85 +// JSONObject jsonObj = new JSONObject(message);
  86 +// String flightNo = jsonObj.get("flightNo").toString();
  87 +// String flightDate = jsonObj.get("flightDate").toString();
85 88
  89 +// nmsController.getData()
86 log.info("收到来自窗口" + sid + "的信息:" + message); 90 log.info("收到来自窗口" + sid + "的信息:" + message);
87 //群发消息 91 //群发消息
88 for (WebSocketServer item : webSocketSet) { 92 for (WebSocketServer item : webSocketSet) {