/**
  * 部门列表页面跳转
  *
  * @return
  */
 @RequestMapping(params = "update")
 public ModelAndView update(TSDepart depart, HttpServletRequest req) {
   List<TSDepart> departList = systemService.getList(TSDepart.class);
   req.setAttribute("departList", departList);
   if (StringUtil.isNotEmpty(depart.getId())) {
     depart = systemService.getEntity(TSDepart.class, depart.getId());
     req.setAttribute("depart", depart);
   }
   return new ModelAndView("system/depart/depart");
 }
  /**
   * 删除部门
   *
   * @return
   */
  @RequestMapping(params = "del")
  @ResponseBody
  public AjaxJson del(TSDepart depart, HttpServletRequest request) {
    AjaxJson j = new AjaxJson();
    depart = systemService.getEntity(TSDepart.class, depart.getId());
    message = "部门: " + depart.getDepartname() + "被删除 成功";
    // 删除部门之前更新与之相关的实体
    upEntity(depart);
    systemService.delete(depart);
    systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);

    j.setMsg(message);
    return j;
  }
 /**
  * 部门列表,树形展示
  *
  * @param request
  * @param response
  * @param treegrid
  * @return
  */
 @RequestMapping(params = "departgrid")
 @ResponseBody
 public List<TreeGrid> departgrid(
     TSDepart tSDepart,
     HttpServletRequest request,
     HttpServletResponse response,
     TreeGrid treegrid) {
   CriteriaQuery cq = new CriteriaQuery(TSDepart.class);
   // update-begin--Author:sun  Date:20130510 for:[089]解决部门名称查询异常
   if ("yes".equals(request.getParameter("isSearch"))) {
     treegrid.setId(null);
     tSDepart.setId(null);
   }
   // ----------------------------------------------------------------
   // update-begin--Author:yeshuai  Date:20130412 for:增加部门名称搜寻框
   if (null != tSDepart.getDepartname()) {
     org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, tSDepart);
   }
   // update-end--Author:yeshuai  Date:20130412 for:增加部门名称搜寻框
   // ----------------------------------------------------------------
   if (treegrid.getId() != null) {
     cq.eq("TSPDepart.id", treegrid.getId());
   }
   if (treegrid.getId() == null) {
     cq.isNull("TSPDepart");
   }
   cq.add();
   List<TreeGrid> departList = null;
   departList = systemService.getListByCriteriaQuery(cq, false);
   if (departList.size() == 0 && tSDepart.getDepartname() != null) {
     cq = new CriteriaQuery(TSDepart.class);
     TSDepart parDepart = new TSDepart();
     tSDepart.setTSPDepart(parDepart);
     org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, tSDepart);
     departList = systemService.getListByCriteriaQuery(cq, false);
   }
   // update-end--Author:sun  Date:20130510 for:[089]解决部门名称查询异常
   List<TreeGrid> treeGrids = new ArrayList<TreeGrid>();
   TreeGridModel treeGridModel = new TreeGridModel();
   treeGridModel.setTextField("departname");
   treeGridModel.setParentText("TSPDepart_departname");
   treeGridModel.setParentId("TSPDepart_id");
   treeGridModel.setSrc("description");
   treeGridModel.setIdField("id");
   treeGridModel.setChildList("TSDeparts");
   treeGrids = systemService.treegrid(departList, treeGridModel);
   return treeGrids;
 }
 // ----------------------------------------------------------------
 // add-begin--Author:liutao  Date:20130405 for:部门管理的添加处理逻辑
 // ----------------------------------------------------------------
 @RequestMapping(params = "add")
 public ModelAndView add(TSDepart depart, HttpServletRequest req) {
   List<TSDepart> departList = systemService.getList(TSDepart.class);
   req.setAttribute("departList", departList);
   if (StringUtil.isNotEmpty(depart.getId())) {
     TSDepart tspDepart = new TSDepart();
     TSDepart tsDepart = new TSDepart();
     depart = systemService.getEntity(TSDepart.class, depart.getId());
     tspDepart.setId(depart.getId());
     tspDepart.setDepartname(depart.getDepartname());
     tsDepart.setTSPDepart(tspDepart);
     req.setAttribute("depart", tsDepart);
   }
   return new ModelAndView("system/depart/depart");
 }
  /**
   * 添加部门
   *
   * @param ids
   * @return
   */
  @RequestMapping(params = "save")
  @ResponseBody
  public AjaxJson save(TSDepart depart, HttpServletRequest request) {
    // 设置上级部门
    String pid = request.getParameter("TSPDepart.id");
    if (pid.equals("")) {
      depart.setTSPDepart(null);
    }
    AjaxJson j = new AjaxJson();
    if (StringUtil.isNotEmpty(depart.getId())) {
      message = "部门: " + depart.getDepartname() + "被更新成功";
      userService.saveOrUpdate(depart);
      systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
    } else {
      message = "部门: " + depart.getDepartname() + "被添加成功";
      userService.save(depart);
      systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
    }

    return j;
  }
 public void upEntity(TSDepart depart) {
   List<TSUser> users = systemService.findByProperty(TSUser.class, "TSDepart.id", depart.getId());
   if (users.size() > 0) {
     for (TSUser tsUser : users) {
       // update-begin--Author:sun  Date:20130510 for[085]:解决部门删除时,人员没被删除的异常
       // tsUser.setTSDepart(null);
       // systemService.saveOrUpdate(tsUser);
       systemService.delete(tsUser);
       // update-end--Author:sun  Date:20130510 for[085]:解决部门删除时,人员没被删除的异常
     }
   }
 }