/**
   * 处理搜索请求
   *
   * @param session
   * @param model
   * @param str
   * @return
   */
  @RequestMapping(params = "method=search", method = RequestMethod.POST)
  public String search(HttpSession session, Model model, String str, Pager<Document> paraPage) {
    User user = (User) session.getAttribute("user");
    if (paraPage == null) {
      paraPage = new Pager<Document>();
    }
    if (user != null) {
      int totleCount = documentService.getCountByStr(str);
      Pager<Document> page =
          new Pager<Document>(totleCount, paraPage.getCurPage(), paraPage.getCountPerPage());
      List<Document> documentList =
          documentService.getByStr(page.getStartCount(), page.getCountPerPage(), str);
      if (documentList.isEmpty()) {
        model.addAttribute("msg", "搜索不到'" + str + "'相关内容");
      } else {
        page.settList(documentList);
        model.addAttribute("msg", "搜索'" + str + "'相关的结果");
        model.addAttribute("page", page);
      }
      model.addAttribute("user", user);
      return "document/search_result";

    } else {
      model.addAttribute("msg", "请登录");
      return "error";
    }
  }
  /**
   * 删除Controller
   *
   * @param session
   * @param model
   * @param docid
   * @return
   */
  @RequestMapping(params = "method=delete", method = RequestMethod.GET)
  public String delete(HttpSession session, Model model, Integer docid) {
    User user = (User) session.getAttribute("user");
    if (user != null) {
      Document document = documentService.getById(docid);

      documentService.delete(docid);
      // 回滚积分
      retPoint(user, document);

      model.addAttribute("user", user);
      return "redirect:document?method=list";
    } else return "error";
  }
 /**
  * 获取热门文档
  *
  * @return
  */
 private List<Document> getHotDoc() {
   List<Want> wantList = wantService.getByGroup();
   List<Document> hotDocList = new ArrayList<Document>();
   for (Want want : wantList) {
     hotDocList.add(documentService.getById(want.getDocid()));
   }
   return hotDocList;
 }
 /**
  * 修改页面转发
  *
  * @param session
  * @param model
  * @param docid
  * @return
  */
 @RequestMapping(params = "method=modify", method = RequestMethod.GET)
 public String modify(HttpSession session, Model model, Integer docid) {
   User user = (User) session.getAttribute("user");
   if (user != null) {
     Document document = documentService.getById(docid);
     model.addAttribute("document", document);
     model.addAttribute("user", user);
     return "document/modify";
   } else return "error";
 }
 /**
  * 修改主流程
  *
  * @param session
  * @param model
  * @param document
  * @return
  */
 @RequestMapping(params = "method=modify", method = RequestMethod.POST)
 public String modify(HttpSession session, Model model, Document document) {
   User user = (User) session.getAttribute("user");
   if (user != null) {
     document.setUserid(user.getUserid());
     documentService.update(document);
     model.addAttribute("user", user);
     model.addAttribute("msg", "更新成功!");
     return "redirect:document?method=list";
   } else return "error";
 }
  /**
   * 分页获取最新求译列表 登录后文档首页的转发
   *
   * @param session
   * @param model
   * @param paraPage
   * @return
   */
  @RequestMapping(params = "method=index", method = RequestMethod.GET)
  public String index(HttpSession session, Model model, Pager<Document> paraPage) {
    User user = (User) session.getAttribute("user");
    if (paraPage == null) paraPage = new Pager<Document>();
    if (user != null) {
      int totleCount = documentService.getAllCount();
      Pager<Document> page =
          new Pager<Document>(totleCount, paraPage.getCurPage(), paraPage.getCountPerPage());
      List<Document> documentList =
          documentService.getByPage(page.getStartCount(), page.getCountPerPage());
      page.settList(documentList);

      model.addAttribute("hotDocList", getHotDoc());
      model.addAttribute("hotComList", getHotCom());

      model.addAttribute("user", user);
      model.addAttribute("page", page);
      return "user/index";
    }
    return "error";
  }
  /**
   * vo分页获取文档列表
   *
   * @param session
   * @param model
   * @param curPage
   * @param countPerPage
   * @return
   */
  @RequestMapping(params = "method=list", method = RequestMethod.GET)
  public String list(HttpSession session, Model model, Pager<Document> paraPage, String target) {
    User user = (User) session.getAttribute("user");

    // 默认情况
    if (paraPage == null) {
      paraPage = new Pager<Document>();
    }
    if (user != null) {
      int totleCount = documentService.getAllCount(user.getUserid());

      Pager<Document> page =
          new Pager<Document>(totleCount, paraPage.getCurPage(), paraPage.getCountPerPage());

      List<Document> documentList =
          documentService.getByPage(page.getStartCount(), page.getCountPerPage(), user.getUserid());
      page.settList(documentList);
      model.addAttribute("user", user);
      model.addAttribute("page", page);
      model.addAttribute("target", target);
      return "user/vo_index";
    } else return "error";
  }
  /**
   * 显示详细内容
   *
   * @param session
   * @param model
   * @param docid
   * @return
   */
  @RequestMapping(params = "method=show", method = RequestMethod.GET)
  public String show(HttpSession session, Model model, int docid, Pager<Comment> paraPage) {
    User user = (User) session.getAttribute("user");
    if (user != null) {
      Document document = documentService.getById(docid);
      if (document != null) {
        if (paraPage == null) paraPage = new Pager<Comment>();
        int totleCount = commentService.getAllCount(docid);

        Pager<Comment> page =
            new Pager<Comment>(totleCount, paraPage.getCurPage(), paraPage.getCountPerPage());
        List<Comment> commentList =
            commentService.getByPage(page.getStartCount(), page.getCountPerPage(), docid);
        if (commentList.isEmpty()) model.addAttribute("msg", "当前还没人翻译,译一下");
        else {
          // 为comment设置User字段
          for (Comment comment : commentList) {
            comment.setUser(userService.getByKey(comment.getUserid()));
            comment.setLikeTime(loveService.CountByComment(comment.getComid()));
            // 如果当前session与该评论的user相同,则可以删除
            if (comment.getUserid().equals(user.getUserid())) comment.setCanDel("1");
          }

          Comment comment = commentService.getByDocAndAc(document.getDocid());
          // 还没接受则可以接受
          if (comment == null) {
            // 如果当前session与此篇文档的用户相同,则可以进行接受操作
            if (document.getUserid().equals(user.getUserid())) model.addAttribute("canAcc", "1");
          } else {
            comment.setUser(userService.getByKey(comment.getUserid()));
            comment.setLikeTime(loveService.CountByComment(comment.getComid()));
            if (comment.getUserid().equals(user.getUserid())) comment.setCanDel("1");
            model.addAttribute("accComment", comment);
          }

          page.settList(commentList);
          model.addAttribute("msg", "已有翻译");
        }
        model.addAttribute("page", page);
        User author = userService.getByKey(document.getUserid());
        model.addAttribute("user", user);
        model.addAttribute("document", document);
        model.addAttribute("author", author);
        model.addAttribute("wantCount", wantService.countByDoc(docid));
        return "document/show";
      }
    }
    return "error";
  }
  /**
   * 添加业务
   *
   * @param session
   * @param model
   * @param document
   * @return
   */
  @RequestMapping(params = "method=add", method = RequestMethod.POST)
  public String add(HttpSession session, Model model, Document document, String guestIdStr) {
    try {
      User user = (User) session.getAttribute("user");
      document.setUserid(user.getUserid());

      Integer docid = documentService.addRet(document);

      // 为文档分配积分
      distribPoint(user, document);

      // 添加邀请信息
      if (guestIdStr != "") invite(guestIdStr, docid);

      return "redirect:/document?method=list";
    } catch (Exception e) {
      System.out.println(e);
      return "error";
    }
  }
 /**
  * 获取其他用户document列表
  *
  * @param model
  * @param userid
  * @return
  */
 @RequestMapping(params = "method=guest_user_list", method = RequestMethod.GET)
 public String guestUserList(Model model, Integer userid) {
   List<Document> guestDocumentList = documentService.getByPage(0, maxCount, userid);
   model.addAttribute("guestDocumentList", guestDocumentList);
   return "templet/guest_user/document";
 }