/*
 * @Description: 
 * @Version: 1.0
 * @Autor: lishengyin
 * @Date: 2022-04-26 15:35:49
 * @LastEditors: lishengyin
 * @LastEditTime: 2022-04-26 17:15:54
 */
#include "KubeSpherePods.hpp"



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

}

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

/**
 * @description: jsonToObject
 * @param {Value&} object
 * @return {*}
 */    
bool KubeSpherePods::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();
        this->PHASE = object["status"]["phase"].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();
        }

        //todo CONTAINERS

        if(object["status"]["conditions"].IsArray()){
            const rapidjson::Value& obj = object["status"]["conditions"];
            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();
                }
                writer.EndObject();
            }
            writer.EndArray();
            this->CONDITIONS = strBuf.GetString();
        }
    }
    return true;
}

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