/*
 * @Description: 
 * @Version: 1.0
 * @Autor: lishengyin
 * @Date: 2022-04-26 16:42:14
 * @LastEditors: lishengyin
 * @LastEditTime: 2022-04-26 17:21:15
 */
#include "KubeSphereServers.hpp"


/**
 * @description: objectToJson
 * @param {string&} str
 * @return {*}
 */    
void KubeSphereServers::objectToJson(std::string& str) {
}

/**
 * @description: jsonToObject
 * @param {string&} json
 * @return {*}
 */    
bool KubeSphereServers::jsonToObject(const std::string& json) {
    return true;
}

/**
 * @description: jsonToObject
 * @param {Value&} object
 * @return {*}
 */    
bool KubeSphereServers::jsonToObject(const rapidjson::Value& object){
    const auto end = object.MemberEnd();
    if (end == object.FindMember("metadata") || !object["metadata"].IsObject()) {
        return false;
    } else {
        this->NAME = object["metadata"]["name"].GetString();
        this->UID = object["metadata"]["uid"].GetString();

        if(object["metadata"]["labels"].IsObject()){
            const rapidjson::Value& obj = object["metadata"]["labels"];
            rapidjson::StringBuffer strBuf;
            rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
            writer.StartObject();
            for(auto iter = obj.MemberBegin(); iter != obj.MemberEnd(); iter++){
                writer.Key(iter->name.GetString());
                writer.String(iter->value.GetString());
            }
            writer.EndObject();
            this->LABELS = strBuf.GetString();
        }
    }

    if (end == object.FindMember("spec") || !object["spec"].IsObject()){
        return false;
    }else{
        this->CLUSTERIP = object["spec"]["clusterIP"].GetString();
        this->TYPE = object["spec"]["type"].GetString();

        if(object["spec"]["ports"].IsArray()){
            const rapidjson::Value& obj = object["spec"]["ports"];
            rapidjson::StringBuffer strBuf;
            rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
            writer.StartArray();
            for(size_t i = 0; i < obj.Size(); i++){
                writer.StartObject();
                for(auto iter = obj[i].MemberBegin(); iter != obj[i].MemberEnd(); iter++){
                    writer.Key(iter->name.GetString());
                    if(iter->value.IsString()) writer.String(iter->value.GetString());
                    else if(iter->value.IsNull()) writer.Null();
                    else if(iter->value.IsInt()) writer.Int(iter->value.GetInt());
                } 
                writer.EndObject();
            }
            writer.EndArray();
            this->PORTS = strBuf.GetString();
        }
    }
    
    return true;
}

/**
 * @description: ObjectToSql
 * @param {string&} sql
 * @return {*}
 */    
bool KubeSphereServers::ObjectToSql(std::string& sql) {
    SqlStream sqlStream("insert into InsServerDB.`InsKsServers` (NAME, NAMESPACE, UID, LABELS, PORTS, CLUSTERIP, TYPE) \
        values  ('?','?','?','?','?','?','?')\
        on duplicate key update NAME=VALUES(NAME), NAMESPACE=VALUES(NAMESPACE),\
        UID=VALUES(UID),LABELS=VALUES(LABELS) ,PORTS=VALUES(PORTS), CLUSTERIP=VALUES(CLUSTERIP),\
        TYPE=VALUES(TYPE)");
        
    sqlStream << NAME << NAMESPACE << UID << LABELS << PORTS << CLUSTERIP << TYPE;
    sql = (std::string)sqlStream;
    return true;
}
