/*
     * 创建事件 业务部分写在pullEvent()方法中,这个方法会被定时调用。
     */
    @Override
    protected Event pullEvent() {
      Event event = Event.createDataEvent("/notify/message");

      int count = SessionManager.getInstance().getSessionCount();
      if (count >= 1) {
        Session[] sessions = SessionManager.getInstance().getSessions();
        String userId = sessions[0].getEvent().getField("user_id");

        WebApplicationContext context = SpringContextHolder.getContext();
        NotifyMessageService notifyService =
            (NotifyMessageService) context.getBean("notifyService");

        NotifyMessage entity = new NotifyMessage();
        entity.setIs_query(false);
        entity.setIs_read(false);
        entity.setReceiver_id(Integer.parseInt(userId));
        Map<Integer, List<NotifyMessage>> map = notifyService.modifyAndQueryMapList(entity);
        try {
          String data = buildText(map);
          event.setField("data", data);
        } catch (Exception ex) {
          LogHelper.getLogger().error("构建事务通知信息时出错", ex);
        }

        // 在线用户
        event.setField("userCount", OnlineUser.getInstance().gainUserCount());
      }

      return event;
    }
 /**
  * @Description 标记已读
  *
  * @param id
  * @return JsonResult
  */
 @RequestMapping("/read")
 public @ResponseBody JsonResult read(Integer id) {
   NotifyMessage entity = new NotifyMessage();
   entity.setId(id);
   entity.setIs_read(true);
   return notifyMessageService.modify(entity);
 }
 /**
  * @Description 获取列表数据
  *
  * @param model
  * @author davidwan
  */
 @RequestMapping("/homelist")
 public @ResponseBody Map<Integer, List<NotifyMessage>> homeList(
     Boolean is_init, HttpServletRequest request) {
   NotifyMessage entity = new NotifyMessage();
   entity.setIs_read(false);
   entity.setIs_query(!is_init);
   entity.setReceiver_id(getCurrentUserId());
   Map<Integer, List<NotifyMessage>> map = notifyMessageService.modifyAndQueryMapList(entity);
   return map;
 }
    private String buildText(Map<Integer, List<NotifyMessage>> map)
        throws UnsupportedEncodingException {
      if (map == null) {
        return "";
      }

      StringBuffer buffer = new StringBuffer();
      List<NotifyMessage> list = null;
      String senderName = null;
      for (Integer key : map.keySet()) {
        list = map.get(key);
        if (list != null && !list.isEmpty()) {
          for (NotifyMessage item : list) {
            senderName =
                StringUtils.isBlank(item.getSender_real_name()) ? "系统" : item.getSender_real_name();
            buffer
                .append(key)
                .append("#")
                .append(item.getId())
                .append("#")
                .append(item.getUrl())
                .append("#")
                .append(item.getCreate_time())
                .append("#")
                .append(URLEncoder.encode(senderName, "utf-8"))
                .append("#")
                .append(URLEncoder.encode(item.getContent(), "utf-8"))
                .append("#")
                .append(item.getReceiver_id())
                .append("^");
          }
        }
        buffer.append("|");
      }

      return buffer.toString();
    }
 /**
  * @Description 标记当前用户所有未读提醒为已读
  *
  * @param entity
  * @return JsonResult
  */
 @RequestMapping("/readall")
 public @ResponseBody JsonResult readAll(NotifyMessage entity) {
   entity.getMap().put("receiver_id", getCurrentUserId());
   entity.setIs_read(true);
   return notifyMessageService.modify(entity);
 }