/**
* 加入群
* @UserFunction(method = GET|POST)
* @CheckLogin
*/
function join_group(Integer $id)
{
    $db = new DataBase(DB_DNS, DB_USER, DB_PASSWORD);
    $user_id = getCurrentUserId();
    $group = $db->fetch('SELECT * FROM `group`  WHERE `id` = ?;', $id);
    if ($group) {
        if ($group['number'] >= $group['max_number']) {
            throw new ProException('group $id reach limit', 202);
        }
        if ($db->fetchColumn('SELECT count(*) FROM `group_user` WHERE `user_id`=? AND `group_id`=?', $user_id, $id) != 0) {
            throw new ProException('you have to join the $id group', 203);
        }
        $db->exec('INSERT INTO `group_user`(`group_id`, `user_id`, `role`) VALUES(?,?,0);', $id, $user_id);
        $db->exec('UPDATE `group` SET `number` = `number`+1  WHERE `id` = ?;', $id);
    } else {
        throw new ProException('group $id is not exists', 201);
    }
}
/**
* 处理好友邀请
* @UserFunction(method = GET|POST)
* @CheckLogin
*/
function process_request_friend(Integer $id, Boolean $is_access)
{
    $currentUserId = getCurrentUserId();
    $db = new DataBase(DB_DNS, DB_USER, DB_PASSWORD);
    $my_friend_status = $db->fetch('SELECT `status` FROM `friend` WHERE `user_id` = ? AND `friend_id` = ?', $currentUserId, $id);
    $other_friend_status = $db->fetch('SELECT `status` FROM `friend` WHERE `user_id` = ? AND `friend_id` = ?', $id, $currentUserId);
    if ($my_friend_status && $other_friend_status && $my_friend_status['status'] == 3 && $other_friend_status['status'] == 2) {
        if ($is_access->val) {
            $db->exec('UPDATE `friend` SET `status` = 1 WHERE `user_id`=? AND `friend_id`=?;', $currentUserId, $id);
            $db->exec('UPDATE `friend` SET `status` = 1 WHERE `user_id`=? AND `friend_id`=?;', $id, $currentUserId);
            $user_name_1 = $db->fetchColumn('SELECT `username` FROM `user` WHERE `id` = ?', $currentUserId);
            $user_name_2 = $db->fetchColumn('SELECT `username` FROM `user` WHERE `id` = ?', $id);
            //向融云IM server发送消息
            //$other_id = $id->val;
            ServerAPI::getInstance()->messagePublish($currentUserId, array($id->val), 'RC:InfoNtf', json_encode(array('message' => '你已添加了' . $user_name_1 . ',现在可以开始聊天了。')));
            ServerAPI::getInstance()->messagePublish($id->val, array($currentUserId), 'RC:InfoNtf', json_encode(array('message' => '你已添加了' . $user_name_2 . ',现在可以开始聊天了。')));
        } else {
            $db->exec('DELETE FROM `friend` WHERE `user_id`=? AND `friend_id`=?;', $currentUserId, $id);
            $db->exec('UPDATE `friend` SET `status` = 4 WHERE `user_id`=? AND `friend_id`=?;', $id, $currentUserId);
        }
    } else {
        throw new ProException('unknow error', 306);
    }
}
/**
* 发送激活码
* @UserFunction
*/
function send_activatiton_code(Mobile $mobile)
{
    $db = new DataBase(DB_DNS, DB_USER, DB_PASSWORD);
    $code = 2000;
    if ($db->fetchColumn('SELECT count(*) FROM `activation_code` WHERE `mobile` = ?', $mobile) == 0) {
        $db->insert('INSERT INTO `activation_code` (`mobile`, `code`) values(?,?)', $mobile, $code);
    } else {
        $timestamp = $db->fetchColumn('SELECT `timestamp` FROM `activation_code` WHERE `mobile` = ?', $mobile);
        if (time() - $timestamp > 60000) {
            $db->exec('UPDATE `activation_code` SET `code` = ? WHERE `mobile` = ?', $code, $mobile);
        } else {
            throw new ProException('Activation code is sent too frequently', 107);
        }
    }
}