/*
 * @Description: 
 * @Version: 1.0
 * @Autor: lishengyin
 * @Date: 2022-04-26 11:50:14
 * @LastEditors: Your Name lishengyin@sz-sunwin.com
 * @LastEditTime: 2022-06-16 22:05:53
 */
#ifndef _KUBESPHERERESOURCESMSG_HPP_
#define _KUBESPHERERESOURCESMSG_HPP_


#include "K8sServerBase.hpp"
#include <vector>

template<typename T>
class KubeSphereResourcesMsg:public MsgBase
{
public:
    vector<T> items;
public:
    KubeSphereResourcesMsg(){}
    ~KubeSphereResourcesMsg(){}

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

    }

    /**
     * @description: jsonToObject
     * @param {string&} json
     * @return {*}
     */    
    bool jsonToObject(const std::string& json) override{
        rapidjson::Document doc;
        if (doc.Parse<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) {
            return false;
        }
        // get members
        const auto end = doc.MemberEnd();
        
        if (end != doc.FindMember("items")) {
        if(doc["items"].IsArray()){
                const rapidjson::Value& objs = doc["items"];
                for (size_t i = 0; i < objs.Size(); ++i) {
                    const rapidjson::Value& obj = objs[i];
                    T item;
                    if(!(item.jsonToObject(obj))){
                        return false;
                    }
                    items.push_back(item);
                }
            }
        }
        return true;
    }

    /**
     * @description: jsonToObject
     * @param {Value&} object
     * @return {*}
     */    
    bool jsonToObject(const rapidjson::Value& object) override{
        return true;
    }

    /**
     * @description: ObjectToSql
     * @param {string&} sql
     * @return {*}
     */    
    virtual bool ObjectToSql(std::string& sql) override{
        if(this->items.empty()) return false;
        for(auto iter = this->items.begin(); iter != this->items.end(); iter++){
            iter->USERID = this->USERID;
            iter->DATATIME = this->DATATIME;
            std::string isql;
            if(!iter->ObjectToSql(isql)) return false;
            sql = isql + ";\r\n";
        }
        return true;
    }

     /**
     * @description: objectToSql后处理
     * @param {string&} sql
     * @return {*}
     */    
    virtual bool ObjectToSqlBack(std::string & sql) {
        if(this->items.empty()) return false;
        for(auto iter = this->items.begin(); iter != this->items.end(); iter++){
            iter->USERID = this->USERID;
            iter->DATATIME = this->DATATIME;
            std::string isql;
            if(!iter->ObjectToSqlBack(isql)) return false;
            sql = isql + ";\r\n";
        }
        return true;
    } 
};





#endif