/**
   * 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);
  }
  /**
   * 导出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) {

      }
    }
  }