/**
  * 终端信息列表页面跳转
  *
  * @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");
 }
 /**
  * 添加终端信息
  *
  * @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;
 }
  /**
   * easyui AJAX请求数据
   *
   * @param request
   * @param response
   * @param dataGrid
   * @param user
   * @throws Exception
   */
  @SuppressWarnings("unchecked")
  @RequestMapping(params = "datagrid")
  public void datagrid(
      TerminalInfoEntity terminalInfo,
      HttpServletRequest request,
      HttpServletResponse response,
      DataGrid dataGrid,
      String groupname)
      throws Exception {
    CriteriaQuery cq = new CriteriaQuery(TerminalInfoEntity.class, dataGrid);
    // 查询条件组装器,fuzzy search
    String name = terminalInfo.getName();
    if (StringUtil.isNotEmpty(name)) {
      terminalInfo.setName("*" + name + "*");
    }
    org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(
        cq, terminalInfo, request.getParameterMap());
    this.terminalInfoService.getDataGridReturn(cq, true);

    List<TerminalInfoEntity> terminals = dataGrid.getResults();
    List<TerminalInfoPage> results = new ArrayList<TerminalInfoPage>();
    // 添加地理位置
    for (TerminalInfoEntity t : terminals) {
      if (StringUtil.isNotEmpty(t.getGroupid())) {
        TerminalInfoPage page = new TerminalInfoPage();
        MyBeanUtils.copyBeanNotNull2Bean(t, page);
        TSTerritory self = systemService.get(TSTerritory.class, t.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));
        results.add(page);
      }
    }
    dataGrid.setResults(results);
    TagUtil.datagrid(response, dataGrid);
  }
  /**
   * 删除终端信息
   *
   * @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;
  }
  /**
   * 导出excel
   *
   * @param request
   * @param response
   */
  @SuppressWarnings("unchecked")
  @RequestMapping(params = "exportXls")
  public void exportXls(
      TerminalInfoEntity terminalInfo,
      HttpServletRequest request,
      HttpServletResponse response,
      DataGrid dataGrid) {
    response.setContentType("application/vnd.ms-excel");
    String codedFileName = null;
    OutputStream fOut = null;
    try {
      codedFileName = "终端信息";
      // 根据浏览器进行转码,使其支持中文文件名
      if (BrowserUtils.isIE(request)) {
        response.setHeader(
            "content-disposition",
            "attachment;filename=" + java.net.URLEncoder.encode(codedFileName, "UTF-8") + ".xls");
      } else {
        String newtitle = new String(codedFileName.getBytes("UTF-8"), "ISO8859-1");
        response.setHeader("content-disposition", "attachment;filename=" + newtitle + ".xls");
      }
      // 产生工作簿对象
      HSSFWorkbook workbook = null;
      CriteriaQuery cq = new CriteriaQuery(TerminalInfoEntity.class, dataGrid);
      org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(
          cq, terminalInfo, request.getParameterMap());

      List<TerminalInfoEntity> courses = systemService.getListByCriteriaQuery(cq, false);
      List<TerminalInfoPage> pages = new ArrayList<TerminalInfoPage>();
      // 转换代码为汉字
      for (TerminalInfoEntity t : courses) {
        TerminalInfoPage p = new TerminalInfoPage();
        MyBeanUtils.copyBeanNotNull2Bean(t, p);

        // 组织结构
        TSTerritory te = systemService.get(TSTerritory.class, t.getGroupid());
        if (te != null) {
          p.setGroupname(te.getTerritoryName());
        }

        // 状态
        TSType tp =
            systemService.getType(
                p.getStatus() == null ? null : p.getStatus().toString(), SystemType.TERMINAL_STATE);
        if (tp != null) {
          p.setStatusname(tp.getTypename());
        }

        pages.add(p);
      }

      TSUser user = ResourceUtil.getSessionUserName();
      workbook =
          ExcelExportUtil.exportExcel(
              new ExcelTitle("终端状态", "导出人:" + user.getRealName(), "导出信息"),
              TerminalInfoPage.class,
              pages);
      fOut = response.getOutputStream();
      workbook.write(fOut);
    } catch (Exception e) {
    } finally {
      try {
        fOut.flush();
        fOut.close();
      } catch (IOException e) {

      }
    }
  }
  @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");
  }