/**
   * 向hql中设置orderBy条件(可以处理以有order by的hql和没有order by的hql)
   *
   * @param hql hql语句
   * @param page 分页和排序参数
   * @return
   */
  protected String createHqlAddOrderBy(final String hql, final Page<T> page) {
    String newHql = hql;
    if (page.isOrderBySetted()) {
      String[] orderByArray = StringUtils.split(page.getOrderBy(), ',');
      String[] orderArray = StringUtils.split(page.getOrder(), ',');

      Assert.isTrue(orderByArray.length == orderArray.length, "分页多重排序参数中,排序字段与排序方向的个数不相等");

      String orderByStr = "";
      if (StringUtils.contains(newHql, "order by")) {
        for (int i = 0; i < orderByArray.length; i++) {
          if ((i + 1) == orderByArray.length) {
            orderByStr += getAlias(hql) + "." + orderByArray[i].trim() + " " + orderArray[i].trim();
          } else {
            orderByStr +=
                getAlias(hql) + "." + orderByArray[i].trim() + " " + orderArray[i].trim() + ", ";
          }
        }
        newHql = StringUtils.substringBefore(newHql, "order by") + "order by " + orderByStr;
      } else {
        orderByStr = " order by ";
        for (int i = 0; i < orderByArray.length; i++) {
          if ((i + 1) == orderByArray.length) {
            orderByStr += getAlias(hql) + "." + orderByArray[i].trim() + " " + orderArray[i].trim();
          } else {
            orderByStr +=
                getAlias(hql) + "." + orderByArray[i].trim() + " " + orderArray[i].trim() + ", ";
          }
        }
        newHql += orderByStr;
      }
    }
    log.debug("newHql =" + newHql);
    return newHql;
  }
  /** 原生SQL分页查询 */
  public void findPageByJdbc(Page<T> page, String sql, Object... values) {
    SQLQuery sqlQuery = getSession().createSQLQuery(sql);
    if (values != null) {
      for (int i = 0; i < values.length; i++) {
        sqlQuery.setString(i, values[i].toString());
      }
    }

    page.setTotalCount(countSql(sql, values));

    List<T> results =
        sqlQuery
            .addEntity(entityClass)
            .setFirstResult(page.getFirst() - 1)
            .setMaxResults(page.getPageSize())
            .list();
    page.setResult(results);
  }
 @Override
 @Action("work-group")
 public String list() throws Exception {
   if (page.getPageSize() > 1) {
     page = workGroupManager.getAllWorkGroupByBranchesId(page, branchesId);
     //			ApiFactory.getBussinessLogService().log("工作组管理",
     //					"查看工作组列表",ContextUtils.getSystemId("acs"));
     this.renderText(PageUtils.pageToJson(page));
     return null;
   }
   return "work-group";
 }
  public Page<T> findPage(final Page<T> page, final String hql, final Object... values) {
    Assert.notNull(page, "page不能为空");

    String newHql = createHqlAddOrderBy(hql, page);

    Query q = createQuery(newHql, values);

    if (page.isAutoCount()) {
      int pageNo = page.getPageNo();
      int pageSize = page.getPageSize();
      long totalCount = countHqlResult(newHql, values);
      long z = totalCount / pageSize;
      long y = totalCount % pageSize;
      long c = pageNo - z;
      page.setTotalCount(totalCount);
      if (y == 0 && c == 1) { // 该页没有数据转到上一页
        page.setPageNo(Integer.valueOf(pageNo - 1));
      }
    }

    setPageParameter(q, page);
    List result = q.list();
    page.setResult(result);
    return page;
  }
  /**
   * 按Criterion分页查询.
   *
   * @param page 分页参数.包括pageSize、firstResult、orderBy、asc、autoCount. 其中firstResult可直接指定,也可以指定pageNo.
   *     autoCount指定是否动态获取总结果数.
   * @param criterion 数量可变的Criterion.
   * @return 分页查询结果.附带结果列表及所有查询时的参数.
   */
  public Page<T> findByCriteria(Page page, Criterion... criterion) {
    Assert.notNull(page);

    Criteria c = createCriteria(criterion);

    if (page.isAutoCount()) {
      page.setTotalCount(countQueryResult(page, c));
    }

    if (page.isOrderBySetted()) {
      if (page.getOrder().endsWith("asc")) {
        c.addOrder(Order.asc(page.getOrderBy()));
      } else {
        c.addOrder(Order.desc(page.getOrderBy()));
      }
    }
    // hibernate的firstResult的序号从0开始
    c.setFirstResult(page.getFirst() - 1);
    c.setMaxResults(page.getPageSize());
    page.setResult(c.list());
    return page;
  }
 /**
  * 工作组用户列表
  *
  * @return
  * @throws Exception
  */
 @Action("work-group-getUserByWorkGroup")
 public String getUserByWorkGroup() throws Exception {
   containBranches = departmentManager.containBranches();
   if (userPage.getPageSize() <= 1) {
     User user = userManager.getUserById(ContextUtils.getUserId());
     if (!roleManager.hasSystemAdminRole(user) && roleManager.hasBranchAdminRole(user)) {
       List<BranchAuthority> branchesList =
           branchAuthorityManager.getBranchByUser(ContextUtils.getUserId());
       for (BranchAuthority branches : branchesList) {
         if (StringUtils.isNotEmpty(manageBranchesIds)) {
           manageBranchesIds += ",";
         }
         manageBranchesIds += branches.getBranchesId();
       }
     }
     return "work-group-users";
   } else {
     if (workGroupId != null) {
       userPage = userInfoManager.queryUsersByWorkGroup(userPage, workGroupId);
     }
     renderHtml(PageUtils.pageToJson(userPage));
     return null;
   }
 }
 /** 设置分页参数到Query对象,辅助函数. */
 protected Query setPageParameter(final Query q, final Page<T> page) {
   // hibernate的firstResult的序号从0开始
   q.setFirstResult(page.getFirst() - 1);
   q.setMaxResults(page.getPageSize());
   return q;
 }