143 lines
4.0 KiB
C++
143 lines
4.0 KiB
C++
//
|
|
// Created by dongl on 23-4-23.
|
|
//
|
|
|
|
#include "UserDB.h"
|
|
#include "template_table/im_user.h"
|
|
|
|
/// 0505 19:34 im_user 重定义
|
|
|
|
// select user info
|
|
std::tuple<bool, PoUser> UserDB::select_user(uint64_t account, const std::string& by_field) {
|
|
conn = LinkDB::safe_grab();
|
|
auto query = conn->query("select * from im_user where %2:field=%1:account;");
|
|
query.template_defaults[1] = account;
|
|
query.template_defaults[2] = by_field.c_str();
|
|
query.parse();
|
|
|
|
std::vector<im_user> user;
|
|
query.storein(user);
|
|
|
|
return {!user.empty(), user.empty() ? PoUser() :
|
|
PoUser(user[0].account, user[0].username, user[0].password, user[0].password_salt, user[0].client_info.c_str())};
|
|
}
|
|
|
|
// select key account is existed by account
|
|
bool UserDB::select_user_exist(uint64_t account) {
|
|
auto [exist, PoUser] = select_user(account, "account");
|
|
return exist;
|
|
}
|
|
|
|
// insert user info
|
|
bool UserDB::insert_user(uint64_t account, const std::string &password, const std::string &password_salt,
|
|
const std::string &client_info) {
|
|
conn = LinkDB::safe_grab();
|
|
auto query = conn->query();
|
|
mysqlpp::String info(client_info);
|
|
|
|
query.insert(im_user(account, "用户"+ std::to_string(account), password, password_salt, info));
|
|
return query.exec();
|
|
}
|
|
|
|
// insert user friends info
|
|
bool UserDB::insert_user_friends(uint64_t account, uint64_t friends) {
|
|
// 生成附属表字段 并添加自己为好友 以后官方通知通过此接口发送
|
|
conn = LinkDB::safe_grab();
|
|
auto q = conn->query("insert into im_user_friends (account, friends, restrictions) "
|
|
"values (%1:account, JSON_OBJECT(%2:key, "
|
|
"JSON_OBJECT('belong_grouping', 0, 'add_time', %3:time, 'add_source', 0)), 0)");
|
|
|
|
q.template_defaults[1] = account;
|
|
q.template_defaults[2] = friends == 0 ? account : friends;
|
|
q.template_defaults[3] = time(nullptr);
|
|
q.parse();
|
|
|
|
return q.exec();
|
|
}
|
|
|
|
bool UserDB::remove_user(uint64_t account) {
|
|
conn = LinkDB::safe_grab();
|
|
auto query = conn->query("delete from im_user where account=%0:account");
|
|
query.template_defaults["account"] = account;
|
|
query.parse();
|
|
|
|
return query.exec();
|
|
}
|
|
|
|
|
|
bool UserDB::test(uint64_t account) {
|
|
// 生成附属表字段 并添加自己为好友 以后官方通知通过此接口发送
|
|
conn = LinkDB::safe_grab();
|
|
auto q = conn->query("insert into im_user_friends (account, friends, restrictions) "
|
|
"values (%1:account, JSON_OBJECT(%2:key, "
|
|
"JSON_OBJECT('belong_grouping', 0, 'add_time', %3:time, 'add_source', 0)), 0)");
|
|
|
|
q.template_defaults[1] = account;
|
|
q.template_defaults[2] = account;
|
|
q.template_defaults[3] = time(nullptr);
|
|
q.parse();
|
|
|
|
return q.exec();
|
|
}
|
|
|
|
|
|
// 号码池 取号
|
|
std::optional<uint64_t> UserDB::fetch_account() {
|
|
conn = LinkDB::safe_grab();
|
|
auto query = conn->query("select account from account_pool limit 0,1");
|
|
|
|
auto ret = query.store();
|
|
|
|
if (ret.num_rows() < 1) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
return std::strtol(ret[0][0], nullptr,0);
|
|
}
|
|
|
|
// 号池内删除帐号
|
|
bool UserDB::remove_pool_account(uint64_t account) {
|
|
conn = LinkDB::safe_grab();
|
|
auto query = conn->query("delete from account_pool where account=%1:account");
|
|
query.template_defaults[1] = account;
|
|
query.parse();
|
|
|
|
return query.exec();
|
|
}
|
|
|
|
// 帐号 绑定邮箱
|
|
bool UserDB::bind_email(uint64_t account, const std::string &email) {
|
|
conn = LinkDB::safe_grab();
|
|
auto query = conn->query("update im_user set email=%2q:email where account=%1:account");
|
|
query.template_defaults[1] = account;
|
|
query.template_defaults[2] = email.c_str();
|
|
|
|
query.parse();
|
|
return query.exec();
|
|
}
|
|
|
|
bool UserDB::bind_phone(uint64_t account, const std::string &phone) {
|
|
conn = LinkDB::safe_grab();
|
|
auto query = conn->query("update im_user set phone=%2q:email where account=%1:account");
|
|
query.template_defaults[1] = account;
|
|
query.template_defaults[2] = phone.c_str();
|
|
|
|
query.parse();
|
|
return query.exec();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|