@Override
  public int add(DBFriendGroupMapping mapping) {
    db = getTransactionDB();

    // 查找user
    DBUser user = userDao.findByAccount(mapping.getUser().getAccount());
    if (user == null) {
      return DBColumns.ERROR_USER_NOT_FOUND;
    }

    // 查找group
    DBFriendGroup fg = fgDao.findByAccount(mapping.getFriendGroup().getAccount());
    if (fg == null) {
      fgDao.add(mapping.getFriendGroup());
    }

    // 是否已经存在
    if (isExists(mapping.getUser().getAccount(), mapping.getFriendGroup().getAccount())) {
      return DBColumns.ERROR_FRIEND_STATE_EXISTS;
    }

    // 添加
    db.execSQL(
        "INSERT INTO "
            + DBColumns.FRIEND_STATE_TABLE_NAME
            + "("
            + DBColumns.FRIEND_STATE_ACCOUNT
            + ","
            + DBColumns.FRIEND_STATE_USER_ACCOUNT
            + ","
            + DBColumns.FRIEND_STATE_FRIEND_GROUP_ACCOUNT
            + ","
            + DBColumns.FRIEND_STATE_LOGIN_STATE
            + ","
            + DBColumns.FRIEND_STATE_LOGIN_CHANNEL
            + ","
            + DBColumns.FRIEND_STATE_POSITION
            + ","
            + DBColumns.FRIEND_STATE_REMARK
            + ") VALUES(?,?,?,?,?,?,?)",
        new String[] {
          mapping.getAccount(),
          mapping.getUser().getAccount(),
          mapping.getFriendGroup().getAccount(),
          mapping.getLoginState() + "",
          mapping.getLoginChannel() + "",
          mapping.getPosition() + "",
          mapping.getRemark()
        });

    return DBColumns.RESULT_OK;
  }
  @Override
  public DBFriendGroupMapping findByAccount(String account) {
    db = getTransactionDB();
    Cursor cursor =
        db.rawQuery(
            "SELECT * FROM "
                + DBColumns.FRIEND_STATE_TABLE_NAME
                + " WHERE "
                + DBColumns.FRIEND_STATE_ACCOUNT
                + "=?",
            new String[] {account});
    DBFriendGroupMapping mapping = null;
    if (cursor.moveToFirst()) {
      // 查找user
      String userAccount =
          cursor.getString(cursor.getColumnIndex(DBColumns.FRIEND_STATE_USER_ACCOUNT));
      DBUser user = userDao.findByAccount(userAccount);

      // 查找friendgroup
      String friendGroupAccount =
          cursor.getString(cursor.getColumnIndex(DBColumns.FRIEND_STATE_FRIEND_GROUP_ACCOUNT));
      DBFriendGroup friendGroup = fgDao.findByAccount(friendGroupAccount);

      mapping = DBObjectHelper.getDB3FriendGroupMapping(user, friendGroup, cursor);
    }
    cursor.close();

    return mapping;
  }
  @Override
  // public synchronized void update(String userAccount, XMPPUser contact) {
  public void update(String userAccount, XMPPUser contact) {
    db = getTransactionDB();

    synchronized (synchObj) {

      // 是否存在
      DBUser user = userDao.findByAccount(userAccount);
      if (user == null) {
        return;
      }

      DBUser ct = userDao.findByName(contact.getJid());
      if (ct == null) {
        add(userAccount, contact);

        return;
      }

      // 查找group,不存在会创建新的分组
      String group = contact.getGroup();
      DBFriendGroup fg = fgDao.find(userAccount, group);

      DBFriendGroupMapping mp = new DBFriendGroupMapping();
      mp.setUser(ct);
      mp.setFriendGroup(fg);
      mp.setLoginState(contact.getStatusMode());
      mp.setRemark(contact.getNickname());

      // 查找mapping中是否存在
      DBFriendGroupMapping mapping = find(userAccount, ct.getAccount());
      if (mapping != null) {
        // 更新
        mp.setAccount(mapping.getAccount());
        update(mp);
      } else {
        // 不存在,添加
        String acc = Integer.parseInt(getMaxAccount()) + 1 + "";
        mp.setAccount(acc);
        add(mp);
      }
    }
  }
  @Override
  // public synchronized void add(String account, XMPPUser contact) {
  public void add(String account, XMPPUser contact) {
    db = getTransactionDB();

    synchronized (synchObj) {

      // 查找group
      String group = contact.getGroup();
      DBFriendGroup fg = fgDao.find(account, group);

      // 查找contact
      String jid = contact.getJid();
      DBUser ct = userDao.findByName(jid);
      if (ct == null) {
        // 添加contact
        // 查找account
        String ua = userDao.findMaxAccount();
        ua = Integer.parseInt(ua) + 1 + "";
        ct = new DBUser();
        ct.setAccount(ua);
        ct.setNickname(jid);
        // ct.setState(contact.getStatusMode());
        ct.setDesp(contact.getStatusMessage());
        ct.setRegisterTime(new Date().getTime());
        userDao.add(ct);
      }

      // 是否已经存在,add(mapping)中会检查

      // 添加mapping
      DBFriendGroupMapping mapping = new DBFriendGroupMapping();
      mapping.setFriendGroup(fg);
      mapping.setUser(ct);
      mapping.setRemark(contact.getNickname());
      mapping.setLoginState(contact.getStatusMode());
      String ac = Integer.parseInt(getMaxAccount()) + 1 + "";
      mapping.setAccount(ac);
      add(mapping);
    }
  }
  @Override
  public DBFriendGroupMapping find(String userAccount, String contactAccount) {
    db = getTransactionDB();

    // 要查询的用户
    DBUser user = userDao.findByAccount(contactAccount);

    // select fs.* from friend_state fs,friend_group fg where
    // fs.[user_account]='100002' and fs.[friend_group_account]=fg.[account]
    // and fg.[user_account]='100000'
    Cursor cursor =
        db.rawQuery(
            "SELECT fs.* FROM "
                + DBColumns.FRIEND_STATE_TABLE_NAME
                + " fs,"
                + DBColumns.FRIEND_GROUP_TABLE_NAME
                + " fg WHERE fs."
                + DBColumns.FRIEND_STATE_USER_ACCOUNT
                + "=? AND fs."
                + DBColumns.FRIEND_STATE_FRIEND_GROUP_ACCOUNT
                + "=fg."
                + DBColumns.FRIEND_GROUP_ACCOUNT
                + " AND fg."
                + DBColumns.FRIEND_GROUP_USER_ACCOUNT
                + "=?",
            new String[] {contactAccount, userAccount});

    DBFriendGroupMapping mapping = null;
    if (cursor.moveToFirst()) {
      String friendGroupAccount =
          cursor.getString(cursor.getColumnIndex(DBColumns.FRIEND_STATE_FRIEND_GROUP_ACCOUNT));
      DBFriendGroup friendGroup = fgDao.findByAccount(friendGroupAccount);
      mapping = DBObjectHelper.getDB3FriendGroupMapping(user, friendGroup, cursor);
    }
    cursor.close();

    return mapping;
  }
 @Override
 public void close() {
   super.close();
   userDao.close();
   fgDao.close();
 }