/**
   * 找回密时,根据 手机号和 验证码 核查phonenumber的验证码是否正确 是否匹配 需要验证生成验证码的时间是否超时
   *
   * @param phonenumber captchacode
   * @return {@value} url: /api/v1/phoneCollect/checkPhonenumberInFindPasswd
   *     <p>找回密时,核实手机和验证码是否匹配 url :
   *     http://localhost/lr/api/v1/phoneCollect/checkPhonenumberInFindPasswd?phonenumber=13662127862&captchacode=3361
   *     <p>0 验证码超时: 需要重新获取验证码 调用这个
   *     http://localhost/lr/api/v1/phoneCollect/genCaptchacodeByPhoneInFindPasswd?phonenumber={phonemum}
   *     http://localhost/lr/api/v1/phoneCollect/genCaptchacodeByPhoneInFindPasswd?phonenumber=13662127862
   *     即本类下的 genCaptchacodeByPhoneInFindPasswd()方法
   */
  @RequestMapping(value = "/checkPhonenumberInFindPasswd", method = RequestMethod.GET)
  @ResponseBody
  public GeneralResponse checkPhonenumberInFindPasswd(
      @RequestParam("phonenumber") String phonenum,
      @RequestParam("captchacode") String captchacode) {
    GeneralResponse gp = new GeneralResponse();
    int returncode = 0;
    int errcode = -1;
    String errmsg = "比对不成功";
    int successcode = 1;
    String successmsg = "比对OK_";
    int overtimecode = 0;
    String overtimemsg = "验证码超时,需要重新获取验证码";
    int err2code = -2;
    String err2msg = "未知错误";
    int err3code = -3;
    String err3msg = "错误:没有找到手机号对应的用户";

    returncode = userPhoneTools.checkPhoneInFindPasswd(phonenum, captchacode);

    if (returncode == errcode) {
      gp.setRetCode(errcode);
      gp.setRetInfo(errmsg);
    } else if (returncode == successcode) {
      /** 1.生成临时验证码, 2.找到手机号对应的User, 存储入临时验证码 3.返回临时验证码,临时验证码存在RetInfo 中,显示。 */
      String uuid = UUID.randomUUID().toString();
      String tempToken =
          uuid.substring(0, 8)
              + uuid.substring(9, 13)
              + uuid.substring(14, 18)
              + uuid.substring(19, 23)
              + uuid.substring(24);
      User u = accountService.findUserByPhonenumber(phonenum);
      if (u == null) {
        gp.setRetCode(err3code);
        gp.setRetInfo(err3msg);
      } else {
        u.setTempToken(tempToken);
        u.setTempTokenDate(new Date());
        accountService.updateUser(u);
        successmsg = successmsg + "令牌是:" + tempToken;
        gp.setRetCode(successcode);
        gp.setRetInfo(successmsg);
      }
    } else if (returncode == overtimecode) {
      gp.setRetCode(overtimecode);
      gp.setRetInfo(overtimemsg);
    } else {
      gp.setRetCode(err2code);
      gp.setRetInfo(err2msg);
    }

    return gp;
  }
  /**
   * 短信登录时,根据 手机号和 验证码 核查phonenumber的验证码是否正确 是否匹配 需要验证生成验证码的时间是否超时 生成新的登录凭证,更新之前的登录凭证
   *
   * @param phonenumber captchacode
   * @return {@value} url: /api/v1/phoneCollect/checkPhonenumberInSmsLogin
   *     <p>短信登录时,核实手机和验证码是否匹配 url :
   *     http://localhost/lr/api/v1/phoneCollect/checkPhonenumberInSmsLogin?phonenumber=13662127862&captchacode=3361
   *     <p>0 验证码超时: 需要重新获取验证码 调用这个
   *     http://localhost/lr/api/v1/phoneCollect/genCaptchacodeByPhoneInSmsLogin?phonenumber={phonemum}
   *     http://localhost/lr/api/v1/phoneCollect/genCaptchacodeByPhoneInSmsLogin?phonenumber=13662127862
   *     即本类下的 genCaptchacodeByPhoneInSmsLogin()方法
   */
  @RequestMapping(value = "/checkPhonenumberInSmsLogin", method = RequestMethod.GET)
  @ResponseBody
  public GeneralResponse checkPhonenumberInSmsLogin(
      @RequestParam("phonenumber") String phonenum,
      @RequestParam("captchacode") String captchacode) {
    GeneralResponse gp = new GeneralResponse();
    int returncode = 0;
    int errcode = -1;
    String errmsg = "比对不成功";
    int successcode = 1;
    // String successmsg = "比对OK_";
    int overtimecode = 0;
    String overtimemsg = "验证码超时,需要重新获取验证码";
    int err2code = -2;
    String err2msg = "未知错误";
    int err3code = -3;
    String err3msg = "错误:没有找到手机号对应的用户";

    returncode = userPhoneTools.checkPhoneInFindPasswd(phonenum, captchacode);

    if (returncode == errcode) {
      gp.setRetCode(errcode);
      gp.setRetInfo(errmsg);
    } else if (returncode == successcode) {
      /** 1.生成短信登录令牌 2.找到手机号对应的User, 存储入短信登录令牌 3.返回短信登录令牌,临时验证码存在RetInfo中,显示。 */
      String uuid = UUID.randomUUID().toString();
      String smsToken1 =
          uuid.substring(0, 8)
              + uuid.substring(9, 13)
              + uuid.substring(14, 18)
              + uuid.substring(19, 23)
              + uuid.substring(24);
      String uuid2 = UUID.randomUUID().toString();
      String smsToken2 =
          uuid2.substring(0, 8)
              + uuid2.substring(9, 13)
              + uuid2.substring(14, 18)
              + uuid2.substring(19, 23)
              + uuid2.substring(24);
      String smsToken = smsToken1 + smsToken2;
      User u = accountService.findUserByPhonenumber(phonenum);
      if (u == null) {
        // 未找到用户
        gp.setRetCode(err3code);
        gp.setRetInfo(err3msg);
      } else {
        // u.setTempToken(tempToken);
        // u.setTempTokenDate(new Date());
        u.setSmstoken(smsToken);
        u.setSmsTokenDate(new Date());
        Integer tokenshowtimes = u.getSmstokenshowtimes();
        accountService.updateUser(u);
        // successmsg =smsToken;
        gp.setRetCode(successcode);
        gp.setRetInfo(smsToken); // 返回消息放置单一令牌
      }
    } else if (returncode == overtimecode) {
      gp.setRetCode(overtimecode);
      gp.setRetInfo(overtimemsg);
    } else {
      gp.setRetCode(err2code);
      gp.setRetInfo(err2msg);
    }

    return gp;
  }
  /**
   * 更改手机号时,根据 手机号和 验证码 ---- 确认新手机号 核查phonenumber的验证码是否正确 是否匹配 需要验证生成验证码的时间是否超时 验证手机号是否激活
   *
   * <p>满足 可以更改手机号
   *
   * <p>比对Phonenumber中的对象
   *
   * @param phonenumber captchacode
   *     <p>username={username}&digest={加密后的passwd}
   * @return {@value} url: /api/v1/phoneCollect/checkPhonenumberInChangePhone
   *     <p>核实手机和验证码是否匹配 ? url :
   *     http://localhost/lr/api/v1/phoneCollect/checkPhonenumberInChangePhone?username={username}&digest={加密后的passwd}&phonenumber=13662127862&captchacode=3361
   *     <p>返回 : GP
   */
  @RequestMapping(
      value = "/checkPhonenumberInChangePhone",
      method = RequestMethod.GET,
      produces = MediaTypes.JSON_UTF_8)
  public GeneralResponse checkPhonenumberInChangePhone(ServletRequest request) {
    System.out.println("in PhoneRestController() checkPhonenumberInChangePhone方法 ");
    // 1 匹配	0 不匹配
    int returncode = 0;
    // String phonetempToken  ="";
    String phonenumber = request.getParameter("phonenumber");
    String captchacode = request.getParameter("captchacode");
    if ("".equals(phonenumber)
        || "".equals(captchacode)
        || phonenumber == null
        || captchacode == null) {
    } else {
      phonenumber = phonenumber.trim();
      captchacode = captchacode.trim();
      Phonenumber p =
          accountService.findUserPhoneInPhonenumberByPhoneAndCode(phonenumber, captchacode);
      if (p == null) {
        returncode = 0; // 没有相关对象 ,不做比对 返回不匹配
      } else {
        Date startDate = p.getRegisterDate();
        int no_outtime = accountService.compareTimes(startDate, new Date(), Constants.SMS_Gap_Time);
        if (no_outtime == 1) {
          // 未超时
          if (p.getPhonestatus() == 0) { // 未激活状态 (应为新手机号)  再作比对
            // phonestatus; 0 ,未激活  not_activated ; 1,已激活 ; 2,解绑  <暂时不用>
            returncode = accountService.checkUserPhone(phonenumber, captchacode); // 对新的手机号 作比对
            // if(returncode == 1){
            // 匹配
            // String uuid = UUID.randomUUID().toString();
            // phonetempToken =
            // uuid.substring(0,8)+uuid.substring(9,13)+uuid.substring(14,18)+uuid.substring(19,23)+uuid.substring(24);
            // p.setPhonetempToken(phonetempToken);
            // accountService.updatePhonenumber(p);
            // }
          } else {
            returncode = 0; // 不做比对,表示验证码不匹配
          }
        } else {
          // 超时
          returncode = 0; // 不做比对,表示验证码不匹配
          // 重新生成新的验证码 和 生成时间
          accountService.updatePhonenumber(p);
        }
      }
    }

    GeneralResponse gp = new GeneralResponse();
    gp.setRetCode(returncode);
    if (returncode == 0) {
      // 0 不匹配
      gp.setRetInfo("验证码不匹配");
    } else if (returncode == 1) {
      // 1  匹配
      gp.setRetInfo("符合条件,可以更改手机号");
    } else {
      gp.setRetInfo("phone未知错误 1001");
    }

    // ----------------------------------
    /** 调整用户的手机号 1. 解绑旧手机号 (Phonenumber 对象) 2. 绑定新手机 (Phonenumber 对象) 3. 修改用户手机号 (User 对象) */
    if (returncode == 1) {
      Long userId = getCurrentUserId();
      User u = accountService.findUserByUserId(userId);

      String newphone = phonenumber;

      /** 1. 解绑旧手机 start * */
      String oldphone = u.getPhonenumber();
      int no_activated = 0;
      int phonestatus = no_activated; // 0 ,未激活  not_activated ; 1,已激活
      ChangePhoneStsInPhonenumber(oldphone, phonestatus);
      /** 1. 解绑旧手机 end * */

      /** 2. 绑定新手机start * */
      int activated = 1;
      phonestatus = activated; // 0 ,未激活  not_activated ; 1,已激活
      ChangePhoneStsInPhonenumber(newphone, phonestatus);
      /** 2. 绑定新手机end * */

      /** 3. 更改用户中的手机信息 start * */
      u.setPhonenumber(newphone);
      accountService.saveUser(u);
      /** 3. 更改用户中的手机信息 end * */
      gp.setRetInfo("验证符合条件,已更改用户手机号");
    }
    return gp;
  }