/**
   * 用户登录
   *
   * @param userName
   * @param pwd
   * @param type 用户类型:1 商户 2 顾客
   * @return
   * @throws Exception
   */
  @ResponseBody
  @RequestMapping(value = "/login", produces = "text/plain;charset=UTF-8")
  public String login(
      @RequestParam(value = "name", required = false, defaultValue = "") String userName,
      @RequestParam(value = "pwd", required = false, defaultValue = "") String pwd,
      @RequestParam(value = "type", required = false, defaultValue = "2") int type)
      throws Exception {
    if (Utils.isEmpty(userName) || Utils.isEmpty(pwd))
      return JsonUtils.resultJson(-2, "用户名或密码不能为空", null);
    User user = userService.findByUserName(userName.trim());
    if (null == user) return JsonUtils.resultJson(-3, "用户尚未注册", null);
    if (Utils.isEmpty(user.getPwd()) || !user.getPwd().equalsIgnoreCase(pwd.trim()))
      return JsonUtils.resultJson(-4, "密码错误", null);

    if (type == 2) {
      if (!user.getType().equals(UserType.customer)) {
        return JsonUtils.resultJson(-5, "帐号密码错误", null);
      }
    }
    if (type == 1) {
      if (!user.getType().equals(UserType.merchants)) {
        return JsonUtils.resultJson(-5, "帐号密码错误", null);
      }
      if (user.getStatus() != 1) return JsonUtils.resultJson(-6, "帐号已被锁定", null);
    }
    initSession(user.getType(), user, false);

    List<Address> addrs = addressService.findByUserId(user.getId());
    List<Map<String, String>> resAddr = new LinkedList<Map<String, String>>();
    if (null != addrs && !addrs.isEmpty()) {
      for (Address addr : addrs) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("addr_id", addr.getId() + "");
        map.put("address", addr.getAddress());
        map.put("phone", addr.getPhone());
        map.put("name", addr.getName());
        map.put("lng", addr.getLocation()[0] + "");
        map.put("lat", addr.getLocation()[1] + "");
        map.put("def", addr.getId() == user.getAddressId() ? "1" : "0");
        resAddr.add(map);
      }
    }

    Map<String, Object> veResult = new HashMap<String, Object>();
    veResult.put("user_id", user.getId() + "");
    veResult.put("user_name", user.getUserName());
    veResult.put("nick_name", user.getNickName());
    veResult.put("phone", user.getPhone());
    veResult.put("user_type", "1");
    if (type == 1) veResult.put("merc_num", user.getMercNum() + "");
    veResult.put("addrs", resAddr);
    return JsonUtils.resultJson(1, "", veResult);
  }
  /**
   * 查询用户信息
   *
   * @return
   * @throws Exception
   */
  @ResponseBody
  @RequestMapping(value = "/userinfo", produces = "text/plain;charset=UTF-8")
  @Authorization(type = Constant.SESSION_USER)
  public String userInfo() throws Exception {
    Object obj = session.getAttribute(Constant.SESSION_USER);
    User user = userService.findOne(((User) obj).getId());

    List<Address> addrs = addressService.findByUserId(user.getId());
    List<Map<String, String>> resAddr = new LinkedList<Map<String, String>>();
    if (null != addrs && !addrs.isEmpty()) {
      for (Address addr : addrs) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("addr_id", addr.getId() + "");
        map.put("address", addr.getAddress());
        map.put("phone", addr.getPhone());
        map.put("name", addr.getName());
        map.put("lng", addr.getLocation()[0] + "");
        map.put("lat", addr.getLocation()[1] + "");
        map.put("def", addr.getId() == user.getAddressId() ? "1" : "0");
        resAddr.add(map);
      }
    }

    // 通过IP获取位置信息
    if (Utils.isEmpty(user.getCity()) || user.getCityCode() == 0) {
      String ip = getRemoteAddr();
      if (!Utils.isEmpty(ip)) {
        AddressBean addr = Common.ipWithBaidu(ip);
        if (null != addr) {
          user.setCity(addr.getCity());
          user.setProvince(addr.getProvince());
          user.setCityCode(addr.getCityCode());
        }
      }
    }

    Map<String, Object> veResult = new HashMap<String, Object>();
    veResult.put("user_id", user.getId() + "");
    veResult.put("user_name", user.getUserName());
    veResult.put("nick_name", user.getNickName());
    veResult.put("phone", user.getPhone());
    if (user.getType().equals(UserType.visitor)) veResult.put("user_type", "0");
    else veResult.put("user_type", "1");
    if (user.getType().equals(UserType.merchants)) veResult.put("merc_num", user.getMercNum() + "");
    veResult.put("addrs", resAddr);
    return JsonUtils.resultJson(1, "", veResult);
  }
  /**
   * 新增、编辑常用地址
   *
   * @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);
  }