136 lines
3.7 KiB
C++
136 lines
3.7 KiB
C++
//
|
|
// Created by dongl on 23-5-5.
|
|
//
|
|
|
|
#include "UserFriendsService.h"
|
|
|
|
UserFriendsService::~UserFriendsService() {}
|
|
|
|
// 好友群组 添加删除 任务组 决策接口
|
|
mp::sri *UserFriendsService::friendImProve(mp::body* body) {
|
|
sri_clear();
|
|
|
|
auto subcommand = body->subcommand();
|
|
|
|
// 搜索
|
|
if (subcommand == mp::MP_SUB_TYPE::MP_SEARCH_FRIENDS_ACCOUNT) {
|
|
fetchUser(strtol(body->account().c_str(), nullptr, 0), strtol(body->data().c_str(), nullptr, 0));
|
|
}
|
|
// 添加
|
|
else if (subcommand == mp::MP_SUB_TYPE::MP_ADD_FRIENDS_ACCOUNT) {
|
|
add_contact_person(body);
|
|
}
|
|
// 拉黑
|
|
else if (subcommand == mp::MP_SUB_TYPE::MP_ADD_BLACK_LIST) {
|
|
|
|
}
|
|
// 删除
|
|
else if (subcommand == mp::MP_SUB_TYPE::MP_REMOVE_FRIEND) {
|
|
|
|
}
|
|
// 获取好友列表
|
|
else if (subcommand == mp::MP_SUB_TYPE::MP_GET_FRIENDS) {
|
|
// 此时包传来的 是 客户端 取到用户的帐号
|
|
fetchUserFriends(strtol(body->account().c_str(), nullptr, 0), body->data());
|
|
}
|
|
|
|
return sri;
|
|
}
|
|
|
|
// 添加好友函数
|
|
mp::sri* UserFriendsService::add_contact_person(mp::body *body) {
|
|
// 查看添加目标的权限类型
|
|
char type = userFriendsDb.select_add_type(body->target());
|
|
|
|
// 直接添加
|
|
if (type == '0') {
|
|
add_friends(body);
|
|
}
|
|
// 回答问题
|
|
else if (type == '1') {
|
|
if ("") {
|
|
add_friends(body);
|
|
} else {
|
|
sri->set_subcommand(mp::MP_SUB_TYPE::MP_ADD_FAIL);
|
|
sri->set_msg("答案错误");
|
|
}
|
|
}
|
|
// 账户审核
|
|
else if (type == '2') {
|
|
|
|
}
|
|
|
|
return sri;
|
|
}
|
|
|
|
// 直接添加好友
|
|
void UserFriendsService::add_friends(mp::body *body) {
|
|
auto [state1, msg1] = userFriendsDb.add_friends(body->source(), body->target());
|
|
auto [state2, msg2] = userFriendsDb.add_friends(body->target(), body->source());
|
|
if (state1 && state2) {
|
|
sri->set_subcommand(mp::MP_SUB_TYPE::MP_ADD_SUCCESS);
|
|
sri->set_msg("添加成功");
|
|
} else {
|
|
sri->set_subcommand(mp::MP_SUB_TYPE::MP_ADD_FAIL);
|
|
sri->set_msg("添加失败,请重试");
|
|
}
|
|
}
|
|
|
|
// 查账户好友名单 列表
|
|
void UserFriendsService::fetchUserFriends(uint64_t account, const std::string &data) {
|
|
sri_clear();
|
|
// page
|
|
uint8_t page_begin = data.c_str()[0];
|
|
uint8_t page_end = data.c_str()[1];
|
|
|
|
auto friends = userFriendsDb.select_friends_all(account);
|
|
if (friends.has_value()) {
|
|
sri->set_data(friends.value());
|
|
sri->set_msg("好友获取成功");
|
|
sri->set_subcommand(mp::MP_SUB_TYPE::MP_ADD_SUCCESS);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* MP_REQUEST_FRIENDS MP_SEARCH_FRIENDS_ACCOUNT
|
|
* 搜索指定账户信息 搜索可添加好友
|
|
* @param user 带查用户
|
|
* @param account user是否为account的好友
|
|
*/
|
|
void UserFriendsService::fetchUser(uint64_t user, uint64_t account) {
|
|
// 查询用户存在与否
|
|
auto userinfo = isExistUser(user);
|
|
auto is_friend = isExistFriend(user, account);
|
|
|
|
if (userinfo.has_value()) {
|
|
if (is_friend) {
|
|
sri->set_msg("此账户已是的好友");
|
|
sri->set_subcommand(mp::MP_SUB_TYPE::MP_SEARCH_FAIL);
|
|
return;
|
|
}
|
|
sri->set_username(userinfo.value().username);
|
|
sri->set_account(userinfo.value().account);
|
|
sri->set_email(userinfo.value().email);
|
|
sri->set_msg("搜索成功");
|
|
sri->set_subcommand(mp::MP_SUB_TYPE::MP_SEARCH_SUCCESS);
|
|
} else {
|
|
sri->set_msg("无此用户");
|
|
sri->set_subcommand(mp::MP_SUB_TYPE::MP_SEARCH_FAIL);
|
|
}
|
|
}
|
|
|
|
bool UserFriendsService::isExistFriend(uint64_t user, uint64_t account) {
|
|
return userFriendsDb.select_friend(account, user);
|
|
}
|
|
|
|
std::optional<PoUser> UserFriendsService::isExistUser(uint64_t user) {
|
|
return userFriendsDb.select_user(user);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|