/*
 * @Description: 
 * @Version: 1.0
 * @Autor: lishengyin
 * @Date: 2022-04-26 11:19:06
 * @LastEditors: Your Name lishengyin@sz-sunwin.com
 * @LastEditTime: 2022-06-16 22:11:41
 */
#include "KubeSphereNodes.hpp"

/**
 * @description: ObjectToJson
 * @return {*}
 */
void KubeSphereNodes::ObjectToJson(rapidjson::Writer<rapidjson::StringBuffer>& writer){
    writer.StartObject();

    writer.Key("UID");
    writer.String(UID.c_str());

    writer.Key("NAME");
    writer.String(NAME.c_str());

    writer.Key("CREATIONTIMESTAMP");
    writer.String(CREATIONTIMESTAMP.c_str());

    writer.Key("LABELS");
    writer.String(LABELS.c_str());

    writer.Key("CAPACITY");
    writer.String(CAPACITY.c_str());

    writer.Key("ALLOCATABLE");
    writer.String(ALLOCATABLE.c_str());

    writer.Key("CONDITIONS");
    writer.String(CONDITIONS.c_str());

    writer.Key("ADDRESSES");
    writer.String(ADDRESSES.c_str());

    writer.EndObject();
}


/**
 * @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 {
        const rapidjson::Value& obj = object["metadata"];
        if(end != obj.FindMember("name")) this->NAME = obj["name"].GetString();
        if(end != obj.FindMember("uid")) this->UID = obj["uid"].GetString();
        if(end != obj.FindMember("creationTimestamp")) this->CREATIONTIMESTAMP = obj["creationTimestamp"].GetString();
        if(end != obj.FindMember("labels")) this->LABELS = objectToJson(obj["labels"]);
    }
    if (end == object.FindMember("status") || !object["status"].IsObject()){
        return false;
    }else{
        const rapidjson::Value& obj = object["status"];
        if(end != obj.FindMember("capacity")) this->CAPACITY = this->objectToJson(obj["capacity"]);
        if(end != obj.FindMember("allocatable")) this->ALLOCATABLE = this->objectToJson(obj["allocatable"]);
        if(end != obj.FindMember("addresses")) this->ADDRESSES = this->objectToJson(obj["addresses"]);
        if(end != obj.FindMember("conditions")) this->CONDITIONS = this->objectToJson(obj["conditions"]);
    }
    return true;
}

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

/**
 * @description: ObjectToSql后处理
 * @param {string&} sql
 * @return {*}
 */ 
bool KubeSphereNodes::ObjectToSqlBack(std::string & sql){
    SqlStream sqlStream("UPDATE InsServerDB.`InsKsNodes` SET ISDELETE = 1 WHERE UPDATED_TIME != '?'");
    sqlStream << DATATIME;
    sql = (std::string)sqlStream;
    return true;
}

