/**
  * 添加终端信息
  *
  * @param ids
  * @return
  */
 @RequestMapping(params = "save")
 @ResponseBody
 public AjaxJson save(TerminalInfoEntity terminalInfo, HttpServletRequest request) {
   AjaxJson j = new AjaxJson();
   if (StringUtil.isNotEmpty(terminalInfo.getId())) {
     message = "终端信息更新成功";
     TerminalInfoEntity t =
         terminalInfoService.get(TerminalInfoEntity.class, terminalInfo.getId());
     try {
       MyBeanUtils.copyBeanNotNull2Bean(terminalInfo, t);
       terminalInfoService.saveOrUpdate(t);
       systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
     } catch (Exception e) {
       e.printStackTrace();
       message = "终端信息更新失败";
     }
   } else {
     message = "终端信息添加成功";
     terminalInfo.setStatus(Integer.valueOf(SystemType.TERMINAL_STATE_0));
     terminalInfoService.save(terminalInfo);
     systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
   }
   j.setMsg(message);
   return j;
 }
 /**
  * 终端信息列表页面跳转
  *
  * @return
  */
 @RequestMapping(params = "addorupdate")
 public ModelAndView addorupdate(TerminalInfoEntity terminalInfo, HttpServletRequest req) {
   if (StringUtil.isNotEmpty(terminalInfo.getId())) {
     try {
       terminalInfo =
           terminalInfoService.getEntity(TerminalInfoEntity.class, terminalInfo.getId());
       TerminalInfoPage page = new TerminalInfoPage();
       MyBeanUtils.copyBeanNotNull2Bean(terminalInfo, page);
       TSTerritory self = systemService.get(TSTerritory.class, page.getGroupid());
       String territoryName = "";
       List<TSTerritory> ts = new ArrayList<TSTerritory>();
       TSTerritory parent = self.getTSTerritory();
       ts.add(self);
       String pid = parent.getId();
       // 当父组织机构不为根机构时,查找父机构
       while (!"1".equals(pid)) {
         parent = systemService.get(TSTerritory.class, pid);
         ts.add(parent);
         parent = parent.getTSTerritory();
         pid = parent.getId();
       }
       // 按照添加顺序逆序
       Collections.reverse(ts);
       for (TSTerritory te : ts) {
         territoryName += "-" + te.getTerritoryName();
       }
       page.setGroupname(territoryName.substring(1));
       req.setAttribute("terminalInfoPage", page);
     } catch (Exception e) {
       logger.error("获取终端信息错误");
       e.printStackTrace();
     }
   }
   return new ModelAndView("vod/terminalinfo/terminalInfo");
 }
  /**
   * 删除终端信息
   *
   * @return
   */
  @RequestMapping(params = "del")
  @ResponseBody
  public AjaxJson del(TerminalInfoEntity terminalInfo, HttpServletRequest request) {
    AjaxJson j = new AjaxJson();
    terminalInfo = systemService.getEntity(TerminalInfoEntity.class, terminalInfo.getId());
    message = "终端信息删除成功";
    terminalInfoService.delete(terminalInfo);
    systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);

    j.setMsg(message);
    return j;
  }
  /**
   * @param terminalInfo
   * @param request
   * @param response
   * @param meetingType
   * @param excepts
   * @param appointmentStarttime
   * @param appointmentDuration
   * @return
   */
  @RequestMapping(params = "wetheruesedterminal")
  @ResponseBody
  public AjaxJson wetheruesedterminal(
      String id,
      String meetingType,
      String excepts,
      String appointmentStarttime,
      String appointmentDuration) {

    AjaxJson j = new AjaxJson();
    // 当前选择的终端分组可用
    message = "true";
    if (StringUtil.isNotEmpty(id)) {
      List<TerminalInfoEntity> terminals =
          terminalInfoService.getConflicts(
              id, meetingType, appointmentStarttime, appointmentDuration);
      if (null != terminals && terminals.size() > 0) {
        // 当前选择的终端分组不可用
        message = "false";
      }
    }
    j.setMsg(message);
    return j;
  }
  @RequestMapping(params = "whouesed")
  public ModelAndView whouesed(
      HttpServletRequest req,
      String id,
      String meetingType,
      String excepts,
      String appointmentStarttime,
      String appointmentDuration) {
    // 获取改组当中所有冲突的终端列表
    List<TerminalInfoEntity> terminals =
        terminalInfoService.getConflicts(
            id, meetingType, appointmentStarttime, appointmentDuration);
    // 直播会议
    if (SystemType.APP_MEETING_TYPE_1.equals(meetingType)) {
      List<MeetingInfoEntity> meetings = new ArrayList<MeetingInfoEntity>();
      for (TerminalInfoEntity t : terminals) {
        List<AuthGroupTerminalEntity> gts =
            systemService.findByProperty(AuthGroupTerminalEntity.class, "terminalid", t.getId());
        for (AuthGroupTerminalEntity agt : gts) {
          // 获取分组id
          String groupid = agt.getAuthid();
          if (StringUtil.isNotEmpty(groupid)) {
            List<AppointmentChannelInfoEntity> channels =
                systemService.findByProperty(
                    AppointmentChannelInfoEntity.class, "authortiyGroupCid", groupid);
            for (AppointmentChannelInfoEntity c : channels) {
              String meetingid = c.getMeetingid();
              MeetingInfoEntity meeting = systemService.get(MeetingInfoEntity.class, meetingid);
              if (null != meeting
                  && !meetings.contains(meeting)
                  && Integer.valueOf(SystemType.MEETING_STATE_4) != meeting.getMeetingstate()) {
                meetings.add(meeting);
              } else continue;
            }
          } else continue;
        }
      }
      req.setAttribute("conflictMeetings", meetings);

    } else if (SystemType.APP_MEETING_TYPE_3.equals(meetingType)
        && StringUtil.isNotEmpty(appointmentStarttime)
        && StringUtil.isNotEmpty(appointmentDuration)) { // 预约会议
      List<AppointmentMeetingInfoEntity> meetings = new ArrayList<AppointmentMeetingInfoEntity>();
      for (TerminalInfoEntity t : terminals) {
        List<AuthGroupTerminalEntity> gts =
            systemService.findByProperty(AuthGroupTerminalEntity.class, "terminalid", t.getId());
        for (AuthGroupTerminalEntity agt : gts) {
          // 获取分组id
          String groupid = agt.getAuthid();
          if (StringUtil.isNotEmpty(groupid)) {
            List<AppointmentChannelInfoEntity> channels =
                systemService.findByProperty(
                    AppointmentChannelInfoEntity.class, "authortiyGroupCid", groupid);
            for (AppointmentChannelInfoEntity c : channels) {
              String meetingid = c.getAppointmentid();
              AppointmentMeetingInfoEntity meeting =
                  systemService.get(AppointmentMeetingInfoEntity.class, meetingid);
              if (null != meeting && !meetings.contains(meeting)) {
                meetings.add(meeting);
              } else continue;
            }
          } else continue;
        }
      }
      req.setAttribute("conflictMeetings", meetings);
    }
    return new ModelAndView("vod/meetinginfo/conflictMeetingInfo");
  }