/**
   * 用户登录
   *
   * @param uname
   * @param pwd
   * @return
   */
  @RequestMapping(value = "/user/login")
  public void userLogin(
      String uname,
      String pwd,
      String code,
      HttpServletRequest request,
      HttpServletResponse response) {
    // TODO 验证码系统
    try {
      PrintWriter out = response.getWriter();
      UserInfo user = this.userService.getUserInfoByName(uname);
      if (user != null) {

        if (pwd.equals(user.getPassword())) {
          WebUtil.setLoginCookie(user.getId() + "", response);
          out.print("0"); // 登陆成功
          return;
        } else {
          out.print("1"); // 密码错误
          return;
        }
      } else {
        out.print("2"); // 用户不存在
        return;
      }

    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  @RequestMapping(value = "/user/follow")
  public void followUser(
      String fuid, String r, HttpServletRequest request, HttpServletResponse response) {
    String uid = WebUtil.getLoginCookie(request);

    this.userService.createRelationship(
        Integer.parseInt(uid), Integer.parseInt(fuid), UserRelations.FOLLOW, null);

    if ("1".equals(r)) { // 喜欢关系
      this.userService.createRelationship(
          Integer.parseInt(uid), Integer.parseInt(fuid), UserRelations.LOVE, null);
    }
  }
  /**
   * 用户注册
   *
   * @param uid
   * @param name
   * @param age
   * @param sex
   * @param create_time
   * @param last_login_date
   * @param STATUS
   * @param request
   */
  @RequestMapping(value = "/user/reg")
  public String userRegister(
      String uid,
      String age,
      String sex,
      String email,
      String pwd,
      HttpServletRequest request,
      HttpServletResponse response) {
    try {
      if (request instanceof MultipartHttpServletRequest) {
        request = (MultipartHttpServletRequest) request;
      } else {

      }

      System.out.println("in user reg.." + uid + ":" + pwd + ":" + age);

      UserInfo user = new UserInfo();
      user.setName(uid);
      user.setAge(Integer.parseInt(age));
      user.setPassword(pwd);
      user.setCreateTime(new Date());
      user.setLastLoginDate(new Date());
      user.setHeadImg("/res/img/head.jpg");

      userService.registerUser(user, null);

      WebUtil.setLoginCookie(user.getId() + "", response);

      return "/user/list.do?page=0&size=10";

    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return "/pages/user/login.jsp";
  }
  /**
   * 列表页
   *
   * @param request
   * @param response
   */
  @RequestMapping(value = "/user/list")
  public String list(
      String page, String size, HttpServletRequest request, HttpServletResponse response) {

    String uid = WebUtil.getLoginCookie(request);

    if (uid == null || "".equals(uid)) {
      return "/pages/user/login.jsp";
    }

    UserInfo user;
    try {
      user = this.userService.getUserInfoById(Integer.parseInt(uid));

      Map<String, Feed> feeds =
          this.feedService.getUserFeed(
              user.getId(), null, null, Integer.parseInt(page), Integer.parseInt(size), null);

      Collection<FeedView> fvCollection = new ArrayList<FeedView>(feeds.size());

      for (Feed feed : feeds.values()) {
        if (feed != null) {
          fvCollection.add(this.feedViewBuilder(feed));
        }
      }

      request.setAttribute("feeds", fvCollection);
      request.setAttribute("user", user);

      return "/pages/user/list.jsp";

    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return "/pages/user/login.jsp";
  }
 /**
  * 用户登出
  *
  * @param request
  * @param response
  */
 @RequestMapping(value = "/user/logout")
 public void userLogout(HttpServletRequest request, HttpServletResponse response) {
   WebUtil.removeLoginCookie(response);
 }