/**
   * 分页获取我的最近浏览
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/v1.0/resource/userview", method = RequestMethod.GET)
  @ResponseBody
  public ResultJSON getMyViewList(HttpServletRequest request, HttpServletResponse response) {
    // 返回json的结果对象
    ResultJSON result = new ResultJSON();
    // 异常
    CustomException exception = (CustomException) request.getAttribute(CustomException.request_key);
    // 当前登录用户id
    Long currentUserId = (Long) request.getAttribute("currentUserId");
    // 返回
    Object data = null;
    try {
      if (currentUserId != null && exception == null) {
        // 获取文件服务器的访问url
        String resServiceLocal = (String) request.getAttribute("resServiceLocal");
        String currentResPath = (String) request.getAttribute("currentResPath");

        long userId = currentUserId;
        long unifyTypeId = 0;
        int page = 1;
        int prePage = 10;

        String _unifyTypeId = request.getParameter("unifyTypeId");
        String _page = request.getParameter("page");
        String _prePage = request.getParameter("perPage");
        String fileFormat = request.getParameter("fileFormat");
        if (StringUtils.isNotEmpty(_unifyTypeId)) {
          unifyTypeId = Long.parseLong(_unifyTypeId);
        }
        if (StringUtils.isNotEmpty(_page)) {
          page = Integer.parseInt(_page);
        }
        if (StringUtils.isNotEmpty(_prePage)) {
          prePage = Integer.parseInt(_prePage);
        }

        // 获取结果
        PageInfo info =
            userLogService.getMyViewLogFroResource(userId, unifyTypeId, fileFormat, page, prePage);
        Pagination _p = new PageInfoToPagination().transfer(info.getList());
        // 获取缩略图的最终url
        JPrepareContentViewUtil.convertToPurpose_view(
            _p.getList(), resServiceLocal, currentResPath);
        data = _p;
        exception = CustomException.SUCCESS;
      } else {
        exception = CustomException.INVALIDACCESSTOKEN;
      }
    } catch (Exception e) {
      exception = CustomException.getCustomExceptionByCode(e.getMessage());
      // 如果是普通的异常
      e.printStackTrace();
    } finally {
      result.setCode(exception.getCode());
      result.setMessage(exception.getMessage());
      result.setData(data == null ? "" : data);
      result.setSign("");
    }
    return result;
  }
  /**
   * 分页我获取的资源评论
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/v1.0/resource/userReviewComment", method = RequestMethod.GET)
  @ResponseBody
  public ResultJSON getMyReviewComment(HttpServletRequest request, HttpServletResponse response) {
    // 返回json的结果对象
    ResultJSON result = new ResultJSON();
    // 异常
    CustomException exception = (CustomException) request.getAttribute(CustomException.request_key);
    // 当前登录用户id
    Long currentUserId = (Long) request.getAttribute("currentUserId");
    // 返回
    Object data = null;
    try {
      if (currentUserId != null && exception == null) {
        // 获取文件服务器的访问url
        String resServiceLocal = (String) request.getAttribute("resServiceLocal");
        String currentResPath = (String) request.getAttribute("currentResPath");

        long userId = currentUserId;
        int reviewType = 0;
        int page = 1;
        int prePage = 10;

        String _reviewType = request.getParameter("reviewType");
        String _page = request.getParameter("page");
        String _prePage = request.getParameter("perPage");
        if (StringUtils.isNotEmpty(_reviewType)) {
          reviewType = Integer.parseInt(_reviewType);
        }
        if (StringUtils.isNotEmpty(_page)) {
          page = Integer.parseInt(_page);
        }
        if (StringUtils.isNotEmpty(_prePage)) {
          prePage = Integer.parseInt(_prePage);
        }
        Pagination page_result = assetService.getMyReviewComment(userId, reviewType, page, prePage);
        JPrepareContentViewUtil.convertToPurpose_Review(
            page_result.getList(), resServiceLocal, currentResPath);
        data = page_result;
        exception = CustomException.SUCCESS;
      } else {
        exception = CustomException.INVALIDACCESSTOKEN;
      }
    } catch (Exception e) {
      exception = CustomException.getCustomExceptionByCode(e.getMessage());
      // 如果是普通的异常
      if (exception.getStatus() == 500) {
        e.printStackTrace();
      }
    } finally {
      result.setCode(exception.getCode());
      result.setMessage(exception.getMessage());
      result.setData(data == null ? "" : data);
      result.setSign("");
    }
    return result;
  }
  /**
   * 将 JPrepareContentView 转换生成有效数据 注意网络资源的处理
   *
   * @param <T>
   * @param list
   * @param resUrlLocal 文件服务器的内网地址
   * @param currnetResUrl 浏览器上的当前的(可用内、外网文件服务器)地址
   */
  public static void convertToPurpose(
      List<JPrepareContentView> list, String resUrlLocal, String currnetResUrl) {

    // JPrepareContentView ResourceReview ResourceViewLog

    if (list != null && list.size() > 0) {
      for (int i = 0; i < list.size(); i++) {

        JPrepareContentView view = list.get(i);
        String imgPath = view.getImgPath();
        String size = view.getSize();
        // imgPath 以http开头 并且size 为0或空
        if ((StringUtils.isEmpty(size) || "0".equals(size))
            && (imgPath.startsWith("http") || imgPath.startsWith("HTTP"))) {
          // 设置文件后缀 html
          view.setFileSuffix("html");
        } else {
          // 设置文件后缀
          view.setFileSuffix(imgPath.substring(imgPath.lastIndexOf(".") + 1, imgPath.length()));
          // 组装缩略图路径(约定)
          imgPath =
              imgPath.substring(0, imgPath.lastIndexOf("."))
                  + ZhlResourceCenterWrap.THUMBNAILS_IMG_TYPE;
          // 判断是否存在
          String s = ZhlResourceCenterWrap.GetFileInfo(resUrlLocal, imgPath);
          if (StringUtils.isNotEmpty(s)) {
            HashMap m = JSONObject.parseObject(s, HashMap.class);
            if (m != null && ((Integer) m.get("FileSize") > 0)) {
              // 获取缩略图的地址(内网)
              imgPath = ZhlResourceCenterWrap.getWebThumbnail(resUrlLocal, imgPath);
              // 替换
              imgPath = imgPath.replace(resUrlLocal, currnetResUrl);
              view.setImgPath(imgPath);
            }
          } else {
            view.setImgPath("");
          }
        }
      }
    }
  }
  /**
   * 删除备课资源
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/v1.0/resource/prepareResource", method = RequestMethod.POST)
  @ResponseBody
  public ResultJSON removeMyPrepareResource(
      HttpServletRequest request, HttpServletResponse response) {
    // 返回json的结果对象
    ResultJSON result = new ResultJSON();
    // 异常
    CustomException exception = (CustomException) request.getAttribute(CustomException.request_key);
    // 当前登录用户id
    Long currentUserId = (Long) request.getAttribute("currentUserId");
    // 返回
    Object data = null;
    try {
      if (currentUserId != null && exception == null) {
        long userId = currentUserId;

        String _method = request.getParameter("_method");
        String resIds = request.getParameter("resIds");
        String fromFlags = request.getParameter("fromFlags");
        if (StringUtils.isNotEmpty(_method)
            && RequestMethod.DELETE.name().equalsIgnoreCase(_method)) {
          prepareService.removeMyPrepareContentResource(userId, resIds, fromFlags);
          exception = CustomException.SUCCESS;
        } else {
          exception = CustomException.PARAMSERROR;
        }
      } else {
        exception = CustomException.INVALIDACCESSTOKEN;
      }
    } catch (Exception e) {
      exception = CustomException.getCustomExceptionByCode(e.getMessage());
      // 如果是普通的异常
      if (exception.getStatus() == 500) {
        e.printStackTrace();
      }
    } finally {
      result.setCode(exception.getCode());
      result.setMessage(exception.getMessage());
      result.setData(data == null ? "" : data);
      result.setSign("");
    }
    return result;
  }
  /**
   * 登陆、注销
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping("/v1.0/users/login")
  @ResponseBody
  public ResultJSON Login(HttpServletRequest request, HttpServletResponse response) {

    // 返回json的结果对象
    ResultJSON result = new ResultJSON();
    // 异常
    CustomException exception = (CustomException) request.getAttribute(CustomException.request_key);
    // 当前登录用户id
    Long currentUserId = (Long) request.getAttribute("currentUserId");

    String _method = request.getParameter("_method");

    // 不同的子系统,使用不同的model参数
    String model = request.getParameter("model") == null ? " " : request.getParameter("model");

    // 注销
    if (StringUtils.isNotEmpty(_method) && HttpMethod.DELETE.name().equals(_method)) {

      try {

        if (currentUserId != null && exception == null) {
          String token = request.getHeader("Authorization");
          userService.logout(token);
          exception = CustomException.SUCCESS;
        } else {
          exception = CustomException.INVALIDACCESSTOKEN;
        }
      } catch (Exception e) {
        exception = CustomException.getCustomExceptionByCode(e.getMessage());
        // 如果是普通的异常
        if (exception.getStatus() == 500) {
          e.printStackTrace();
        }
      } finally {
        result.setCode(exception.getCode());
        result.setMessage(exception.getMessage());
        result.setData("");
        result.setSign("");
      }
    } else {
      String userName = request.getParameter("user_name");
      String userPwd = request.getParameter("user_pwd");
      // 返回用户的信息
      //            UserLoginResultInfo data = new UserLoginResultInfo();
      UserSimple user = null;
      try {
        // 用户登录
        SRegister reg = registerService.login(userName, userPwd);
        // 获取用户信息
        user = userService.getUserSimpleById(reg.getId(), model);

        // 如果头像不是系统头像,而是在文件服务中保存的头像的话,需要修改userimage 为 (文件服务中保存的)头像的可访问路径
        if (user.getUserImage() != null
            && user.getUserImage().trim().contains(ZhlResourceCenterWrap.userimage_upload_prefix)) {
          // 获取文件服务器的访问url
          String resServiceLocal = (String) request.getAttribute("resServiceLocal");
          String currentResPath = (String) request.getAttribute("currentResPath");
          String temp = ZhlResourceCenterWrap.getDownUrl(resServiceLocal, user.getUserImage());
          temp = temp.replace(resServiceLocal, currentResPath);
          user.setUserImage(temp);
        }

        //                // 成功,增加用户的在线信息
        //                Boolean repeatLoginVaildFlag = false;//
        // repeatLoginVaildFlag资源中心不允许一个用户重复登录
        //                JOnlineUsers online = jOnlineUsersService.getUserOnlines(reg.getId(),
        // request, repeatLoginVaildFlag);

        //                BeanUtils.copyProperties(data, user);
        //                data.setToken(user.getToken());
        exception = CustomException.SUCCESS;
      } catch (Exception e) {
        exception = CustomException.getCustomExceptionByCode(e.getMessage());
        // 如果是普通的异常
        if (exception.getStatus() == 500) {
          e.printStackTrace();
        }
      } finally {
        result.setCode(exception.getCode());
        result.setMessage(exception.getMessage());
        result.setData(user == null ? "" : user);
        result.setSign("");
      }
    }
    return result;
  }
  /**
   * 修改用户信息
   *
   * @return
   */
  @RequestMapping(value = "/v1.0/users/{id}", method = RequestMethod.POST)
  @ResponseBody
  public ResultJSON updateUserInfo(
      @PathVariable Long id, HttpServletRequest request, HttpServletResponse response) {
    // 返回json的结果对象
    ResultJSON result = new ResultJSON();
    // 异常
    CustomException exception = (CustomException) request.getAttribute(CustomException.request_key);
    // 当前登录用户id
    Long currentUserId = (Long) request.getAttribute("currentUserId");
    // 返回
    Object data = null;

    try {
      if (currentUserId != null && exception == null) {
        long userId = currentUserId;
        boolean male = false;
        long termId = 0;
        long subjectId = 0;
        String trueName = request.getParameter("trueName");
        String _termId = request.getParameter("termId");
        String _subjectId = request.getParameter("subjectId");
        String _male = request.getParameter("male");
        String _method = request.getParameter("_method");

        if (StringUtils.isNotEmpty(_male)
            && ("Y".equalsIgnoreCase(_male) || "true".equalsIgnoreCase(_male))) {
          male = true;
        }
        if (StringUtils.isNotEmpty(_termId)) {
          termId = Long.parseLong(_termId);
        }
        if (StringUtils.isNotEmpty(_subjectId)) {
          subjectId = Long.parseLong(_subjectId);
        }

        if (!RequestMethod.PATCH.name().equals(_method)) { // _method!=patch
          exception = CustomException.PARAMSERROR;
        } else {
          userService.updateUserInfo(userId, trueName, male, termId, subjectId);
          exception = CustomException.SUCCESS;
          data = "";
        }
      } else {
        exception = CustomException.INVALIDACCESSTOKEN;
      }

    } catch (Exception e) {
      exception = CustomException.getCustomExceptionByCode(e.getMessage());
      // 如果是普通的异常
      if (exception.getStatus() == 500) {
        e.printStackTrace();
      }
    } finally {
      result.setCode(exception.getCode());
      result.setMessage(exception.getMessage());
      result.setData(data);
      result.setSign("");
    }
    return result;
  }