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

    DBFriendGroupMapping mp = findByAccount(mapping.getAccount());
    if (mp == null) {
      return DBColumns.ERROR_FRIEND_STATE_NOT_FOUND;
    }

    db.execSQL(
        "DELETE FROM "
            + DBColumns.FRIEND_STATE_TABLE_NAME
            + " WHERE "
            + DBColumns.FRIEND_STATE_ACCOUNT
            + "=?",
        new Object[] {mapping.getAccount()});

    return DBColumns.RESULT_OK;
  }
  @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 int update(DBFriendGroupMapping mapping) {
    db = getTransactionDB();

    // 查找是否存在
    DBFriendGroupMapping mp = findByAccount(mapping.getAccount());
    if (mp == null) {
      return DBColumns.ERROR_FRIEND_STATE_NOT_FOUND;
    }

    // 更新
    db.execSQL(
        "UPDATE "
            + DBColumns.FRIEND_STATE_TABLE_NAME
            + " SET "
            + 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
            + "=? WHERE "
            + DBColumns.FRIEND_STATE_ACCOUNT
            + "=?",
        new Object[] {
          mapping.getUser().getAccount(),
          mapping.getFriendGroup().getAccount(),
          mapping.getLoginState(),
          mapping.getLoginChannel(),
          mapping.getPosition(),
          mapping.getRemark(),
          mapping.getAccount()
        });

    return DBColumns.RESULT_OK;
  }
  @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 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;
  }