/*
 * @Description: 
 * @Version: 1.0
 * @Autor: lishengyin
 * @Date: 2022-04-26 16:42:14
 * @LastEditors: Your Name lishengyin@sz-sunwin.com
 * @LastEditTime: 2022-06-16 22:14:08
 */
#include "KubeSphereServers.hpp"

/**
 * @description: ObjectToJson
 * @return {*}
 */
void KubeSphereServers::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("NAMESPACE");
    writer.String(NAMESPACE.c_str());

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

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

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

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

    writer.EndObject();
}

/**
 * @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 {
        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("namespace")) this->NAMESPACE = obj["namespace"].GetString();
        if(end != obj.FindMember("labels")) this->LABELS = objectToJson(obj["labels"]);
    }
    if (end == object.FindMember("spec") || !object["spec"].IsObject()){
        return false;
    }else{
        const rapidjson::Value& obj = object["spec"];
        if(end != obj.FindMember("clusterIP")) this->CLUSTERIP = obj["clusterIP"].GetString();
        if(end != obj.FindMember("type")) this->TYPE = obj["type"].GetString();
        if(end != obj.FindMember("ports")) this->PORTS = objectToJson(obj["ports"]);
    }
    return true;
}

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

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