/*
 * @Description: 
 * @Version: 1.0
 * @Autor: lishengyin
 * @Date: 2022-04-06 17:07:05
 * @LastEditors: lishengyin
 * @LastEditTime: 2022-04-24 11:05:27
 */
#include "Disposition.hpp"
#include "Sectorpower.hpp"

namespace ins
{  
    /**
     * @description: 获取ptr
     * @param {*}
     * @return {*}
     */    
    std::shared_ptr<Disposition> Disposition::getPtr(){
        static shared_ptr<Disposition> m_Disposition = nullptr;
        if(m_Disposition == nullptr) m_Disposition = std::shared_ptr<Disposition>(new Disposition);
        return m_Disposition;
    }

    /**
     * @description: 释放
     * @param {*}
     * @return {*}
     */    
    void Disposition::Destroy(){
        
    }

    /**
     * @description: 获取userId
     * @param {*}
     * @return {*}
     */    
    int Disposition::getUserId(){
        if(this->usr == "admin") return 1;
        else if(this->usr == "admin2") return 2;
        return 0;
    }   

    /**
     * @description: 更新数据
     * @param {*}
     * @return {*}
     */    
    bool Disposition::Update(){
        std::string sql = "SELECT AYNCTHREADNUM,K8S_IP,K8S_PORT,NETTY_IP,NETTY_PORT,SERVER_PORT FROM InsServerDB.`InsServerConfig`";
        InsUniversalData::Ptr data = std::make_shared<InsUniversalData>();
        data->format = InsDataFormat::INS_DATA_FORMAT_SQL;
        data->data = sql;
        std::shared_ptr<InsPacket> packet = InsPacket::CreateNew(ins::InsPacketTypes::INS_ROUTINE_PACKET, ins::InsPacketTypes::INS_MYSQL_PACKET, data,\
            InsPacket::MakePackTaskPtr([this](InsPacket::Ptr packet){
                if(!packet->getSqlResult().empty()){
                    for(auto &line:packet->getSqlResult()){
                        if(line.size() != 6) return;
                        this->ayncThreadNum = std::atoi(line[0].c_str());
                        this->k8s_IP = line[1];
                        this->k8s_port = std::atoi(line[2].c_str());
                        this->netty_IP = line[3];
                        this->netty_port = std::atoi(line[4].c_str());
                        this->serverPort = std::atoi(line[5].c_str());
                    }
                }else WarnL << "Inquiry less than data" << endl;
            }));
        return Sectorpower::getPtr()->enqueue(packet) == OK ? true : false;
    }
} // namespace ins
