/**
  * 车库中精准定位为司机发定位短信 Accepts a POST request with an XML message parameter
  *
  * @param message serialized Message object
  * @return a string with the result of the POST
  */
 @RequestMapping(
     value = "sendpossms",
     method = RequestMethod.POST,
     consumes = "application/json",
     produces = "application/json")
 public @ResponseBody ResultResponse sendPosSms(
     @RequestBody IdRequest req, HttpServletResponse response, HttpServletRequest request) {
   ResultResponse rp = new ResultResponse();
   rp.setResult(Cookie.RESPONSE_BAD_REQUEST);
   String strId = SecurityContextHolder.getContext().getAuthentication().getName();
   Cookie.checkCommonUser(
       strId, response, request, SecurityContextHolder.getContext().getAuthentication());
   try {
     User user = userBo.findById(req.getId());
     if (user != null) {
       int smsLocate = PositionHelper.checkAvailable(user.getTelephone().toString());
       positionBo.updateSmsLocateState(smsLocate, req.getId());
     } else {
       UnregisterUser unUser = unregisterUserBo.findById(req.getId());
       if (unUser != null) {
         int smsLocate = PositionHelper.checkAvailable(unUser.getTelephone().toString());
         if (smsLocate != unUser.getSmsLocate()) {
           unregisterUserBo.updateSmsLocateState(smsLocate, req.getId());
         }
       }
     }
     rp.setResult(Cookie.RESPONSE_SUCCESS);
   } catch (Exception e) {
     e.printStackTrace();
     rp.setResult(Cookie.RESPONSE_SERVER_QUERY_ERROR);
   }
   return rp;
 }
  /**
   * 根据orderId获得运营商定位 Accepts a POST request with an XML message parameter
   *
   * @param message serialized Message object
   * @return a string with the result of the POST
   */
  @RequestMapping(
      value = "getsmsposbyorderid",
      method = RequestMethod.POST,
      consumes = "application/json",
      produces = "application/json")
  public @ResponseBody PositionResponse getSmsPosByOrderId(
      @RequestBody IdRequest req, HttpServletResponse response, HttpServletRequest request) {
    PositionResponse rp = new PositionResponse();
    rp.setResult(Cookie.RESPONSE_BAD_REQUEST);
    String strId = SecurityContextHolder.getContext().getAuthentication().getName();
    long userId =
        Cookie.checkCommonUser(
            strId, response, request, SecurityContextHolder.getContext().getAuthentication());
    try {
      Order order = orderBo.findById(req.getId());
      if (order == null) {
        return rp;
      }
      if (System.currentTimeMillis() > order.getExpireTime()) {
        rp.setResult(Cookie.RESPONSE_TIMEOUT);
        return rp;
      }
      if (!order.getCargoUserId().equals(userId)) {
        rp.setResult(Cookie.RESPONSE_PERMISSION_DENIED);
        return rp;
      }
      User user = userBo.findById(order.getTruckUserId());
      if (user != null) {
        Position position = positionBo.createIfNull(order.getTruckUserId());
        rp.setCarrier(PositionHelper.CHINA_MOBILE);
        //				rp.setCarrier(PositionHelper.validateMobile(user.getTelephone().toString()));
        if (position.getPositionTime() < (System.currentTimeMillis() - TimeUtils.ONE_MINUTE * 15)) {
          PositionInfo info = positionBo.getSmsLocation(1, userId, user.getTelephone(), user);
          rp.setLat(info.getLat());
          rp.setLon(info.getLon());
          rp.setSmsLocate(info.getType());
          rp.setPositionTime(System.currentTimeMillis());
          rp.setSmsLocateReplyNumber(
              PositionHelper.getLocateReplyNumber(user.getTelephone().toString()));
          rp.setResult(Cookie.RESPONSE_SUCCESS);
          return rp;
        } else {
          rp.setLat(position.getLat());
          rp.setLon(position.getLon());
          rp.setPositionTime(System.currentTimeMillis());
          rp.setResult(Cookie.RESPONSE_SUCCESS);
          return rp;
        }
      } else {
        rp.setIsRegister(0);
        UnregisterUser unregisterUser = unregisterUserBo.findById(order.getTruckUserId());
        if (unregisterUser != null) {
          rp.setCarrier(PositionHelper.CHINA_MOBILE);
          //
          //	rp.setCarrier(PositionHelper.validateMobile(unregisterUser.getTelephone().toString()));
          if (unregisterUser.getPositionTime()
              < (System.currentTimeMillis() - TimeUtils.ONE_MINUTE * 15)) {
            PositionInfo info = unregisterUserBo.getSmsLocation(unregisterUser, 1, userId);
            rp.setLat(info.getLat());
            rp.setLon(info.getLon());
            rp.setSmsLocate(info.getType());
            rp.setPositionTime(System.currentTimeMillis());
            rp.setSmsLocateReplyNumber(
                PositionHelper.getLocateReplyNumber(unregisterUser.getTelephone().toString()));
            rp.setResult(Cookie.RESPONSE_SUCCESS);
            return rp;
          } else {
            rp.setLat(unregisterUser.getLat());
            rp.setLon(unregisterUser.getLon());
            rp.setPositionTime(System.currentTimeMillis());
            rp.setResult(Cookie.RESPONSE_SUCCESS);
          }
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
      rp.setResult(Cookie.RESPONSE_SERVER_QUERY_ERROR);
    }
    return rp;
  }
  /**
   * 根据用户id获得运营商定位 Accepts a POST request with an XML message parameter
   *
   * @param message serialized Message object
   * @return a string with the result of the POST
   */
  @RequestMapping(
      value = "getsmspos",
      method = RequestMethod.POST,
      consumes = "application/json",
      produces = "application/json")
  public @ResponseBody GetSmsLocateResponse getSmsPos(
      @RequestBody IdTypeRequest req, HttpServletResponse response, HttpServletRequest request) {
    GetSmsLocateResponse rp = new GetSmsLocateResponse();
    rp.setResult(Cookie.RESPONSE_BAD_REQUEST);
    String strId = SecurityContextHolder.getContext().getAuthentication().getName();
    long userId =
        Cookie.checkCommonUser(
            strId, response, request, SecurityContextHolder.getContext().getAuthentication());
    try {
      Profile profile = profileBo.findById(userId);
      if (profile.getMemberLocateCount() <= 0) {
        rp.setResult(Cookie.RESPONSE_NOT_ENOUGH);
        return rp;
      }
      rp.setCount(profile.getMemberLocateCount());
      User user = userBo.findById(req.getId());
      if (user != null) {
        //				int carrier = PositionHelper.validateMobile(user.getTelephone().toString());
        Position pos = positionBo.createIfNull(req.getId());
        rp.setCarrier(PositionHelper.CHINA_MOBILE);
        if (pos.getPositionTime() < (System.currentTimeMillis() - TimeUtils.ONE_MINUTE * 5)) {
          PositionInfo info =
              positionBo.getSmsLocation(req.getType(), userId, user.getTelephone(), user);
          rp.setLat(info.getLat());
          rp.setLon(info.getLon());
          rp.setSmsLocate(info.getType());
          rp.setPositionTime(System.currentTimeMillis());
          rp.setSmsLocateReplyNumber(
              PositionHelper.getLocateReplyNumber(user.getTelephone().toString()));
          if (info.getType() == Constant.SMS_LOCATE_SUCCESS) {
            profileBo.detractMemberLocateCount(userId);
            rp.setCount(profile.getMemberLocateCount() - 1);
          }
          rp.setResult(Cookie.RESPONSE_SUCCESS);
          return rp;
        } else {
          rp.setLat(pos.getLat());
          rp.setLon(pos.getLon());
          rp.setPositionTime(System.currentTimeMillis());
          rp.setResult(Cookie.RESPONSE_SUCCESS);
          profileBo.detractMemberLocateCount(userId);
          rp.setCount(profile.getMemberLocateCount() - 1);
          return rp;
        }
      } else {
        UnregisterUser unregisterUser = unregisterUserBo.findById(req.getId());
        //				int carrier = PositionHelper.validateMobile(unregisterUser.getTelephone().toString());
        rp.setCarrier(PositionHelper.CHINA_MOBILE);
        if (unregisterUser != null) {
          rp.setCount(profile.getMemberLocateCount());
          if (unregisterUser.getPositionTime()
              < (System.currentTimeMillis() - TimeUtils.ONE_MINUTE * 5)) {
            PositionInfo info =
                unregisterUserBo.getSmsLocation(unregisterUser, req.getType(), userId);
            rp.setLat(info.getLat());
            rp.setLon(info.getLon());
            rp.setSmsLocate(info.getType());
            rp.setPositionTime(System.currentTimeMillis());
            rp.setSmsLocateReplyNumber(
                PositionHelper.getLocateReplyNumber(unregisterUser.getTelephone().toString()));
            if (info.getType() == Constant.SMS_LOCATE_SUCCESS) {
              profileBo.detractMemberLocateCount(userId);
              rp.setCount(profile.getMemberLocateCount() - 1);
            }
            rp.setResult(Cookie.RESPONSE_SUCCESS);
            return rp;
          } else {
            rp.setLat(unregisterUser.getLat());
            rp.setLon(unregisterUser.getLon());
            rp.setPositionTime(System.currentTimeMillis());
            rp.setResult(Cookie.RESPONSE_SUCCESS);
            profileBo.detractMemberLocateCount(userId);
            rp.setCount(profile.getMemberLocateCount() - 1);
          }
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
      rp.setResult(Cookie.RESPONSE_SERVER_QUERY_ERROR);
    }
    return rp;
  }