/**
   * 根据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;
  }
  /**
   * 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 = "postpos",
      method = RequestMethod.POST,
      consumes = "application/json",
      produces = "application/json")
  public @ResponseBody ResultResponse postPosition(
      @RequestBody PostPositionRequest req,
      HttpServletResponse response,
      HttpServletRequest request) {
    ResultResponse rp = new ResultResponse(Cookie.RESPONSE_BAD_REQUEST);
    String strId = SecurityContextHolder.getContext().getAuthentication().getName();
    long userId =
        Cookie.checkCommonUser402(
            strId, response, request, SecurityContextHolder.getContext().getAuthentication());

    if (PostPositionRequest.checkParameters(req) <= 0) {
      return rp;
    }
    if (req.getLon() < 0.1 && req.getLat() < 0.1) {
      rp.setResult(Cookie.RESPONSE_SUCCESS);
      return rp;
    }
    City city = cityBo.findById(req.getCityId());
    if (city == null || city.getId() <= 0) {
      rp.setResult(Cookie.RESPONSE_SUCCESS);
      return rp;
    }
    int id = 0;
    if (city.getDeep() == Constant.DEEP_CITY) {
      id = city.getId();
    } else if (city.getDeep() == Constant.DEEP_DISTRICT) {
      id = city.getParentId();
    }
    req.setCityId(id);
    Position pos = positionBo.createIfNull(userId);
    if ((req.getCityId() > 0 && pos.getLastEffectCity() != req.getCityId())
        || (req.getCityId() <= 0
            && PositionHelper.distFrom(req.getLat(), req.getLon(), pos.getLat(), pos.getLon())
                > 30000)) {
      PositionLog log =
          new PositionLog(
              null,
              userId,
              pos.getLastPositionTime(),
              pos.getLastEffectLon(),
              pos.getLastEffectLat(),
              pos.getLastEffectCity());
      positionLogBo.save(log);
    }
    pos.setLastEffectCity(pos.getCity());
    pos.setLastEffectLat(pos.getLat());
    pos.setLastEffectLon(pos.getLon());
    pos.setCity(req.getCityId());
    pos.setLon(req.getLon());
    pos.setLat(req.getLat());
    pos.setPositionTime(System.currentTimeMillis());
    pos.setLastPositionTime(System.currentTimeMillis());
    pos.setUpdateTime(System.currentTimeMillis());
    positionBo.update(pos);
    User user = userBo.findById(userId);
    if (user != null && user.getInstallPlace() <= 0) {
      user.setInstallPlace(req.getCityId());
      user.setUpdateTime(System.currentTimeMillis());
      userBo.update(user);
    }
    rp.setResult(Cookie.RESPONSE_SUCCESS);
    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;
  }