/*
 * @Description: 
 * @Version: 1.0
 * @Autor: lishengyin
 * @Date: 2022-04-07 15:23:35
 * @LastEditors: lishengyin
 * @LastEditTime: 2022-04-28 15:22:41
 */
#include "ApolloAppConfig.hpp"

/**
 * @description: objectToJson
 * @param {string&} str
 * @return {*}
 */    
void ApolloAppConfig::ObjectToJson(std::string& str){
}
/**
 * @description: jsonToObject
 * @param {string&} json
 * @return {*}
 */    
bool ApolloAppConfig::jsonToObject(const std::string& json) {
    return true;
}

/**
 * @description: jsonToObject
 * @param {Value&} object
 * @return {*}
 */    
bool ApolloAppConfig::jsonToObject(const rapidjson::Value& object){
    const auto end = object.MemberEnd();
    try
    {
        if (end == object.FindMember("k8s_IP") || !object["k8s_IP"].IsString()) {
            return false;
        } else {
            k8s_IP = object["k8s_IP"].GetString();
        }
        if (end == object.FindMember("k8s_port") || !object["k8s_port"].IsString()) {
            return false;
        } else {
            k8s_port = std::atoi(object["k8s_port"].GetString());
        }
        if (end == object.FindMember("netty_IP") || !object["netty_IP"].IsString()) {
            return false;
        } else {
            netty_IP = object["netty_IP"].GetString();
        }
        if (end == object.FindMember("netty_port") || !object["netty_port"].IsString()) {
            return false;
        } else {
            netty_port = std::atoi(object["netty_port"].GetString());
        }
        if (end == object.FindMember("Server_Port") || !object["Server_Port"].IsString()) {
            return false;
        } else {
            Server_Port = std::atoi(object["Server_Port"].GetString());
        }
        if(end == object.FindMember("ayncThreadNum") || !object["ayncThreadNum"].IsString()){
            return false;
        }else{
            ayncThreadNum = std::atoi(object["ayncThreadNum"].GetString());
        }
    }
    catch(const std::exception& e)
    {
        std::cerr << e.what() << '\n';
    }

    return true;
}

/**
 * @description: ObjectToSql
 * @param {string&} sql
 * @return {*}
 */
bool ApolloAppConfig::ObjectToSql(std::string& sql){
    SqlStream sqlStream("insert into InsServerDB.`InsServerConfig` (USERID,AYNCTHREADNUM, K8S_IP, K8S_PORT, NETTY_IP, NETTY_PORT, SERVER_PORT) \
        values  ('?','?','?','?', '?', '?', '?') \
        on duplicate key update AYNCTHREADNUM=VALUES(AYNCTHREADNUM), K8S_IP=VALUES(K8S_IP), K8S_PORT=VALUES(K8S_PORT), \
        NETTY_IP=VALUES(NETTY_IP),NETTY_PORT=VALUES(NETTY_PORT), SERVER_PORT=VALUES(SERVER_PORT);");
    
    sqlStream << USERID << ayncThreadNum << k8s_IP << k8s_port << netty_IP << netty_port << Server_Port;
    sql  = (string)sqlStream;

    return true;
}