#include "DaoAdmin.h"
#include "Crypto.h"
#include "database/DaoEntity.h"
#include "common/Snowflake.h"

bool DaoAdmin::insert_operator(DataFields& fields)
{
    // 数据库 id 自增
    fields.remove(DMOperator::FID_ID);
    DaoEntity dao(DMOperator::TABLENAME);
    return dao.insert_fields(fields);
}

bool DaoAdmin::update_operator_by_id(DataFields& fields)
{
    string id = fields.get_str(DMOperator::FID_ID);
    fields.remove(DMOperator::FID_ID);
    fields.remove(DMOperator::FID_CREATETIME);
    fields.remove(DMOperator::FID_UPDATETIME);
    fields.set(DMOperator::FID_PASSWD, Crypto::md5("123456"));
    DaoEntity dao(DMOperator::TABLENAME);
    return dao.update_fields(fields, "WHERE id='" + id + "'");
}


bool DaoAdmin::query_operator_page(PageInfo& pageinfo, vector<DataFields>& result)
{
    DaoEntity dao(DMOperator::TABLENAME);
    return dao.query_fields("*", "", pageinfo, result);
}

bool DaoAdmin::query_operator_by_name(string name, vector<DataFields>& result)
{
    string sql_c = "where username = '" + name + "';";
    DaoEntity dao(DMOperator::TABLENAME);
    return dao.query_fields("*", sql_c, result);
}

static void LogDaoResult(vector<DataFields>& vec_fields)
{
    for (int i = 0; i < vec_fields.size(); i++)
    {
        string s = "{";
        vec_fields[i].foreach_item([&](string key, string val)
            {
                s += (key + ":" + val + ", ");
            });
        s = s.substr(0, s.size() - 2);
        s += "}";
    }
}

bool DaoAdmin::query_role(vector<DataFields>& result)
{
    DaoEntity dao(DMRole::TABLENAME);
    dao.query_fields("*", "", result);
    LogDaoResult(result);
    return true;
}

bool DaoAdmin::update_role_by_id(DataFields& fields)
{
    string id = fields.get_str(DMRole::FID_ID);
    fields.remove(DMRole::FID_ID);
    string sql_c = "where " + DMRole::FID_ID + "='" + id + "'";
    DaoEntity dao(DMRole::TABLENAME);
    return dao.update_fields(fields, sql_c);
}


int DaoAdmin::insert_permission(DataFields& fields)
{
    DaoEntity dao(DMPermission::TABLENAME);
    return dao.insert_fields(fields);
}

bool DaoAdmin::query_permission(PageInfo& pageinfo, vector<DataFields>& result, string sql_c)
{
    DaoEntity dao(DMPermission::TABLENAME);
    return dao.query_fields("*", sql_c, pageinfo, result);
}

bool DaoAdmin::update_permission(DataFields& fields, const string& sql_c)
{
    fields.remove(DMPermission::FID_ID);
    DaoEntity dao(DMPermission::TABLENAME);
    return dao.update_fields(fields, sql_c);
}


bool DaoAdmin::query_admin_permission(string account, vector<DataFields>& result)
{
    string sql = "select role.role_name, role_permission.*, permission.func  from operator "
        "left join role on operator.role_name = role.role_name "
        "left join role_permission on role_permission.role_id = role.id "
        "left join permission on permission.id = role_permission.permission_id "
        "where operator.username = '" + account + "' order by permission_id; ";
    return DaoEntity::exec_once(sql, result);
}

int DaoAdmin::insert_policy(DataFields& fields)
{
    DaoEntity dao(DMPolicy::TABLENAME);
    return dao.insert_fields(fields);
}

bool DaoAdmin::query_policy(PageInfo& pageinfo, vector<DataFields>& result, string sql_c)
{
    DaoEntity dao(DMPolicy::TABLENAME);
    return dao.query_fields("*", sql_c, pageinfo, result);
}

bool DaoAdmin::update_policy(DataFields& fields, const string& sql_c)
{
    fields.remove(DMPolicy::FID_ID);
    fields.remove(DMPolicy::FID_UPDATE_TIME);
    DaoEntity dao(DMPolicy::TABLENAME);
    return dao.update_fields(fields, sql_c);
}

