/*
 * @Description: 
 * @Version: 1.0
 * @Autor: lishengyin
 * @Date: 2022-04-26 11:19:06
 * @LastEditors: lishengyin
 * @LastEditTime: 2022-04-26 15:29:42
 */
#include "KubeSphereNodes.hpp"


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

}

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

/**
 * @description: jsonToObject
 * @param {Value&} object
 * @return {*}
 */    
bool KubeSphereNodes::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("status") || !object["status"].IsObject()){
        return false;
    }else{
        if(object["status"]["capacity"].IsObject()){
            const rapidjson::Value& obj = object["status"]["capacity"];
            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->CAPACITY = strBuf.GetString();
        }
        if(object["status"]["allocatable"].IsObject()){
            const rapidjson::Value& obj = object["status"]["allocatable"];
            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->ALLOCATABLE = strBuf.GetString();
        }
        
        if(object["status"]["addresses"].IsArray()){
            const rapidjson::Value& objs = object["status"]["addresses"];
            rapidjson::StringBuffer strBuf;
            rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);

            writer.StartArray();
            for(size_t i = 0; i < objs.Size(); i++){
                writer.StartObject();
                for(auto iter = objs[i].MemberBegin(); iter != objs[i].MemberEnd(); iter++){
                    writer.Key(iter->name.GetString());
                    writer.String(iter->value.GetString());
                }
                writer.EndObject();
            }
            writer.EndArray();
            this->ADDRESSES = strBuf.GetString();
        }
    }
    return true;
}

/**
 * @description: ObjectToSql
 * @param {string&} sql
 * @return {*}
 */    
bool KubeSphereNodes::ObjectToSql(std::string& sql){
    SqlStream sqlStream("insert into InsServerDB.`InsKsNodes` (KSNODEID, UID, NAME, CREATIONTIMESTAMP, LABELS, CAPACITY, ALLOCATABLE,\
        CONDITIONS, ADDRESSES) \
        values  ('?','?','?','?','?','?','?','?','?')\
        on duplicate key update KSNODEID=VALUES(KSNODEID), UID=VALUES(UID),\
        NAME=VALUES(NAME) ,CREATIONTIMESTAMP=VALUES(CREATIONTIMESTAMP), LABELS=VALUES(LABELS),\
        LABELS=VALUES(LABELS) ,CAPACITY=VALUES(CAPACITY), ALLOCATABLE=VALUES(ALLOCATABLE)");

    sqlStream << KSNODESID << UID << NAME << CREATIONTIMESTAMP << LABELS << CAPACITY << ALLOCATABLE << CONDITIONS \
        << ADDRESSES;

    sql = (std::string)sqlStream;
    return true;
}