/*
 * @Description: 
 * @Version: 1.0
 * @Autor: lishengyin
 * @Date: 2022-04-26 15:35:49
 * @LastEditors: lishengyin
 * @LastEditTime: 2022-06-27 15:39:13
 */
#include "KubeSphereDeployments.hpp"

/**
 * @description: objectToJson
 * @return {*}
 */
void KubeSphereDeployments::ObjectToJson(rapidjson::Writer<rapidjson::StringBuffer>& writer){

    writer.StartObject();

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

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

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

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

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

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

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

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

    writer.EndObject();
}

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

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

    return true;
}

/**
 * @description: jsonToObject
 * @param {Value&} object
 * @return {*}
 */    
bool KubeSphereDeployments::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 = this->objectToJson(obj["labels"]);
    }
    if(end == object.FindMember("status") || !object["status"].IsObject()){
        return false;
    }else{
        const rapidjson::Value& obj = object["status"];
        //if(end != obj.FindMember("phase")) this->PHASE = obj["phase"].GetString();
        if(end != obj.FindMember("conditions")) this->CONDITIONS = this->objectToJson(obj["conditions"]);
    }
    // if(end == object.FindMember("spec") || !object["spec"].IsObject()){
    //     return false;
    // }else{
    //     const rapidjson::Value& obj = object["spec"];
    //     if(end != obj.FindMember("containers")) this->CONTAINERS = this->objectToJson(obj["containers"]);
    // }
    return true;
}


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

    sqlStream << UID  << USERID << NAME << NAMESPACE << LABELS << CONTAINERS << PHASE << CONDITIONS << DATATIME;
    sql = (std::string)sqlStream;
    return true;
}

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