/**
   * 获取用户头像的专用上传链接
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/v1.0/users/userImageUploadUrl", method = RequestMethod.GET)
  @ResponseBody
  public ResultJSON getUserImageUrl(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");
        String hostLocal = (String) request.getAttribute("hostLocal");

        long userId = currentUserId;
        JUser user = userService.getUserById(userId);
        long schoolId = user.getSchoolid();
        // 组装上传路径
        String uploadPath = ZhlResourceCenterWrap.getUserImageUploadPath(userId, schoolId);
        // 获取上传文件路径
        String uploadUrl =
            ZhlResourceCenterWrap.getUploadUrlConvert(
                uploadPath, resServiceLocal, currentResPath, hostLocal, userId);

        HashMap<String, String> map = new HashMap<String, String>();
        map.put("uploadUrl", uploadUrl);
        map.put("uploadPath", uploadPath);
        data = map;
        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;
  }
  /**
   * 修改用户信息
   *
   * @return
   */
  @RequestMapping(value = "/v1.0/users/userimage/{userid}", method = RequestMethod.POST)
  @ResponseBody
  public ResultJSON updateUserImage(
      @PathVariable Long userid, 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 userImage = request.getParameter("userImage");
        userService.updateUserImage(userId, userImage);
        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 == null ? "" : data);
      result.setSign("");
    }
    return result;
  }
  /**
   * 获取用户信息
   *
   * @return
   */
  @RequestMapping(value = "/v1.0/users/{id}", method = RequestMethod.GET)
  @ResponseBody
  public ResultJSON getUserInfo(
      @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");

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

    // 返回
    Object data = null;

    try {
      if (currentUserId != null && exception == null) {
        data = userService.getUserSimpleById(id, model);
        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;
  }
  /**
   * 登陆、注销
   *
   * @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;
  }
  /**
   * 跳转到自主学习中心 教师学生通用接口
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/v1.0/autoLearning")
  public ModelAndView autoLearning(HttpServletRequest request, HttpServletResponse response) {

    /** 默认使用这个 */
    String host = "http://fd.zhihaole.net/";
    String sub = "0101";
    String term = "GZ";

    // 拦截器读取配置文件  写入request
    String currentFDHost = (String) request.getAttribute("currentFdHost");

    // 返回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;
        SRegister record = registerService.getRegister(userId);
        String userName = record.getName();
        Long roleId = record.getRoleid();
        String userPwd = PWDEncrypt.getPWD(record.getPwd());

        HashMap<String, String> map = userService.getUserTermAndSubject(userId);
        if (map != null) {

          sub = map.get("subjectcode");
          term = map.get("termcode");

          if (sub.contains(",")) {
            sub = sub.split(",")[0];
          }
        }

        String params =
            "user="******"&pass="******"&page=1"
                + "&grd="
                + term.toUpperCase()
                + "&sub="
                + sub
                + "&ST="
                + (1 == roleId ? "S" : 2 == roleId ? "T" : 5 == roleId ? "J" : "S");
        String sign = MD5.MD5(params + "&key=9k8i78jug6hd93kjf84h");
        String s = params + "&sign=" + sign;
        byte[] sbytes;
        sbytes = xxtea.encrypt(s.getBytes("utf-8"), "9k8i78jug6hd93kjf84h".getBytes());
        s = Base64.encode(sbytes, 0, sbytes.length);
        s = URLEncoder.encode(s, "utf-8");

        String str =
            ((currentFDHost != null && !"".equals(currentFDHost) && currentFDHost.length() > 0)
                    ? currentFDHost
                    : host)
                + "eblogin.do?s=";
        String url = str + s;
        System.out.println("----autoLearning-----" + url);
        response.sendRedirect(url);

      } else {
        exception = CustomException.INVALIDACCESSTOKEN;
      }
    } catch (Exception e) {
      exception = CustomException.getCustomExceptionByCode(e.getMessage());
      // 如果是普通的异常
      if (exception.getStatus() == 500) {
        e.printStackTrace();
      }
    } finally {

    }
    return null;
  }
  /**
   * 修改用户信息
   *
   * @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;
  }