/**
  * 删除地址
  *
  * @param addrId
  * @return
  */
 @ResponseBody
 @RequestMapping(value = "/removeaddress", produces = "text/plain;charset=UTF-8")
 @Authorization(type = Constant.SESSION_USER)
 public String removeAddress(
     @RequestParam(value = "addressid", required = false, defaultValue = "0") long addrId) {
   Object obj = session.getAttribute(Constant.SESSION_USER);
   User user = userService.findOne(((User) obj).getId());
   if (addrId > 0) {
     addressService.remove(addrId);
     if (addrId == user.getAddressId()) {
       user.setAddress("");
       user.setAddressId(0);
       user.setLocation(new double[] {0, 0});
       userService.save(user);
     }
   }
   return JsonUtils.resultJson(1, "", null);
 }
  /**
   * 新增、编辑常用地址
   *
   * @param addrId 地址ID
   * @param address 地址详细
   * @param lat 纬度
   * @param lng 经度
   * @param def 是否设置为默认(1默认 其它值不设为默认)
   * @return
   * @throws Exception
   */
  @ResponseBody
  @RequestMapping(value = "/editaddress", produces = "text/plain;charset=UTF-8")
  @Authorization(type = Constant.SESSION_USER)
  public String editAddress(
      @RequestParam(value = "addressid", required = false, defaultValue = "0") Long addrId,
      @RequestParam(value = "address", required = false, defaultValue = "") String address,
      @RequestParam(value = "lat", required = false, defaultValue = "0") Double lat,
      @RequestParam(value = "lng", required = false, defaultValue = "0") Double lng,
      @RequestParam(value = "default", required = false, defaultValue = "") String def,
      @RequestParam(value = "phone", required = false, defaultValue = "") String phone,
      @RequestParam(value = "name", required = false, defaultValue = "") String name)
      throws Exception {
    if (addrId == 0 && (lat <= 0 || lng <= 0)) return JsonUtils.resultJson(-2, "位置信息错误", null);
    if (addrId == 0 && Utils.isEmpty(address)) return JsonUtils.resultJson(-3, "请填写地址信息", null);
    boolean isadd = true;
    boolean isdef = false;
    if (addrId > 0) isadd = false;
    if (def.equals("1")) isdef = true;

    Object obj = session.getAttribute(Constant.SESSION_USER);
    User user = userService.findOne(((User) obj).getId());

    Address addObj = null;

    if (user.getType().equals(UserType.merchants)) { // 商户只有一个绑定地址。
      isdef = true;
      List<Address> oldAddress = addressService.findByUserId(user.getId());
      if (isadd && oldAddress.size() > 0) return JsonUtils.resultJson(4, "设置地址失败", null);
    }

    if (isadd) {
      addObj = new Address();
      addObj.setUserId(user.getId());

      // 获取位置信息
      if (Utils.isEmpty(user.getCity()) || user.getCityCode() == 0) {
        AddressBean addr = Common.geocoderWithBaidu(lng, lat);
        if (null != addr) {
          user.setCity(addr.getCity());
          user.setProvince(addr.getProvince());
          user.setCityCode(addr.getCityCode());
        }
      }

    } else {
      addObj = addressService.findOne(addrId);
      if (null == addObj) return JsonUtils.resultJson(5, "地址修改失败", null);
    }
    if (!Utils.isEmpty(address)) addObj.setAddress(address);
    if (lat > 0 && lng > 0) addObj.setLocation(new double[] {lng, lat});
    if (!Utils.isEmpty(phone)) addObj.setPhone(phone);
    if (!Utils.isEmpty(name)) addObj.setName(name);

    addressService.save(addObj);

    if (isdef) {
      user.setAddress(addObj.getAddress());
      user.setAddressId(addObj.getId());
      user.setLocation(addObj.getLocation());
      session.setAttribute(Constant.SESSION_USER, user);
    }
    userService.save(user);
    session.setAttribute(Constant.SESSION_USER, user);

    Map<String, String> reMap = new HashMap<String, String>();
    reMap.put("addr_id", addObj.getId() + "");
    reMap.put("address", addObj.getAddress());
    reMap.put("phone", addObj.getPhone());
    reMap.put("name", addObj.getName());
    reMap.put("lng", addObj.getLocation()[0] + "");
    reMap.put("lat", addObj.getLocation()[1] + "");
    reMap.put("def", isdef ? "1" : "0");

    return JsonUtils.resultJson(1, "", reMap);
  }