欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  类库下载

站内信,群发与全部发送。Gson解析result

程序员文章站 2022-03-06 16:51:45
...
/**
* 发送站内信
*/
@Permission(Module.TZGL)
@RequestMapping(value = "/sendznx", method = RequestMethod.POST)
@ResponseBody
public String sendZnx(HttpSession session, Model model,
@RequestParam(defaultValue = "") String[] userList,
@RequestParam(defaultValue = "") String content) throws Exception {
int rows = 0; //记录发送数量
String msg = ""; //记录返回信息
System.out.println("length--->"+userList.length);
System.out.println("content--->"+content);
try{
if(userList.length == 0){ //给全体成员发送系统通知
//全体成员发送
try{
String result = Jsoup.connect(NMApi.url() + "/message/userlist").timeout(10000)
.get()
.text();
//--->使用Gson把请求回来的数据转成MAP结构
Map data = new Gson().fromJson(result,new TypeToken>() {}.getType());
//--->把MAP中需要的Object类型数据取出来,并转成List
List list = (List)data.get("RecList");
//--->使用Gson把List转成Json
String str = new Gson().toJson(list);
//--->再次使用Gson把Json转成需要的类型
List userInfoList = new Gson().fromJson(str,new TypeToken>() {}.getType());
try{
for(UserInfo userInfo : userInfoList){
WebMessage webMessage = new WebMessage();
webMessage.setClientId(userInfo.getIcid());
webMessage.setReceiveTime(new Date());
webMessage.setType("系统通知");
webMessage.setContent(content);
webMessage.setStatus(0);
webMessageService.add(webMessage);
rows ++;
}
msg = "发送站内信成功,成功条数:"+rows;
}catch (Exception e){
e.printStackTrace();
return "群发站内信出错";
}
} catch (Exception e) {
e.printStackTrace();
return "查询用户列表出错";
}
}else{
for (int i = 0;i WebMessage webMessage = new WebMessage();
webMessage.setClientId(Integer.parseInt(userList[i]));
webMessage.setReceiveTime(new Date());
webMessage.setType("管理员消息");
webMessage.setContent(content);
webMessage.setStatus(0);
webMessageService.add(webMessage);
rows ++;
}
msg = "发送站内信成功,成功条数:"+rows;
//发件记录
try {
User user = (User)session.getAttribute("user");
MessageLog messageLog = new MessageLog();
messageLog.setSendTime(new Date());
messageLog.setType("站内信");
messageLog.setSender(user.getUnitName());
messageLog.setReceiver(userList.toString());
messageLog.setContent(content);
messageLog.setResult(msg);
messageLog.setError("");
int num = messageLogService.add(messageLog);
System.out.println("生成记录--->"+num);
}catch (Exception e){
e.printStackTrace();
return "生成发信记录出错";
}
}
} catch (Exception e) {
e.printStackTrace();
return "系统出错";
}
return msg;
}
相关标签: java Gson