/**
* Nudsf_DataRepository
* Nudsf Data Repository Service. © 2021, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TSDSI, TTA, TTC). All rights reserved. 
*
* The version of the OpenAPI document: 1.1.0-alpha.2
* 
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

#include "BlockCRUDApiImpl.h"

namespace org {
namespace openapitools {
namespace server {
namespace api {

using namespace org::openapitools::server::model;

BlockCRUDApiImpl::BlockCRUDApiImpl(std::shared_ptr<Pistache::Rest::Router> rtr)
    : BlockCRUDApi(rtr)
    {
       
     }
//void BlockCRUDApiImpl::create_or_modify_block(const std::string &realmId, const std::string &storageId, const std::string &recordId, const std::string &blockId, const AnyType &body, const Pistache::Optional<bool> &getPrevious, const Pistache::Optional<Pistache::Http::Header::Raw> &ifNoneMatch, const Pistache::Optional<Pistache::Http::Header::Raw> &ifMatch, const Pistache::Optional<std::string> &supportedFeatures, Pistache::Http::ResponseWriter &response) {
 //   response.send(Pistache::Http::Code::Ok, "Do some magic\n");
//}
void BlockCRUDApiImpl::delete_block(const std::string &realmId, const std::string &storageId, const std::string &recordId, const std::string &blockId, const Pistache::Optional<bool> &getPrevious, const Pistache::Optional<Pistache::Http::Header::Raw> &ifMatch, const Pistache::Optional<std::string> &supportedFeatures, Pistache::Http::ResponseWriter &response) {
    response.send(Pistache::Http::Code::Ok, "Do some magic BlockCRUDApiImpl::delete_block\n");
}
void BlockCRUDApiImpl::get_block(const std::string &realmId, const std::string &storageId, const std::string &recordId, const std::string &blockId, const Pistache::Optional<Pistache::Http::Header::Raw> &ifNoneMatch, const Pistache::Optional<Pistache::Http::Header::Raw> &ifModifiedSince, const Pistache::Optional<std::string> &supportedFeatures, Pistache::Http::ResponseWriter &response) {
    const std::string change_dabatase = "use "+ std::string("_" ) +realmId ;
	printf("------------%s------------------\n",change_dabatase.c_str());
    if(mysql_query(mysql_block, change_dabatase.c_str())){
        Logger::udsf_server().error("mysql_real_query failure！SQL(%s) error:%s\n",change_dabatase.c_str(), mysql_error(mysql_block));
        return;
    }
    
    const std::string chose_block = "select "+blockId+ " from  "+ std::string("_" )+ storageId + " where imsi="+ recordId;
    if(mysql_query(mysql_block, chose_block.c_str())){
        Logger::udsf_server().error("mysql_real_query failure！SQL(%s) error:%s\n", chose_block,mysql_error(mysql_block));
        return;
    }

    MYSQL_RES *result = mysql_store_result(mysql_block);
    if (NULL ==     result)
	{
		printf("mysql_store_result failure！SQL(%s) error(%d):%s\n", chose_block,mysql_errno(mysql_block), mysql_error(mysql_block));
		return;
	}
    int num_rows = mysql_num_rows(result);//2
	printf("got:%d row:\n", (int)num_rows);
    int num_fields = mysql_num_fields(result);//3
	printf("number of fields:%d\n", (int)num_fields);
    
    nlohmann::json json;

    MYSQL_ROW row;
    while (row = mysql_fetch_row(result))
		{
			unsigned long *lengths = mysql_fetch_lengths(result);
			for (int i = 0; i < num_fields; i++)
			{
				char *filed = row[i];
				unsigned int field_length = lengths[i];
                json =  {{blockId, filed}};
				printf("column[%d],length[%d],data[%s]\n", i, field_length, filed ? filed : "null");
			}
			printf("\n");
		}
	mysql_free_result(result);
    response.send(Pistache::Http::Code::Ok, json.dump());
}
void BlockCRUDApiImpl::get_block_list(const std::string &realmId, const std::string &storageId, const std::string &recordId, const Pistache::Optional<std::string> &supportedFeatures, Pistache::Http::ResponseWriter &response) {
    const std::string change_dabatase = "use "+ std::string("_" ) +realmId ;
	printf("------------%s------------------\n",change_dabatase.c_str());
    if(mysql_query(mysql_block, change_dabatase.c_str())){
        Logger::udsf_server().error("mysql_real_query failure！SQL(%s) error:%s\n",change_dabatase.c_str(), mysql_error(mysql_block));
        return;
    }
    
    const std::string chose_block = "select OPc,_key from  "+ std::string("_" )+ storageId + " where imsi="+ recordId;
    if(mysql_query(mysql_block, chose_block.c_str())){
        Logger::udsf_server().error("mysql_real_query failure！SQL(%s) error:%s\n", chose_block,mysql_error(mysql_block));
        return;
    }

    MYSQL_RES *result = mysql_store_result(mysql_block);
    if (NULL ==  result)
	{
		printf("mysql_store_result failure！SQL(%s) error(%d):%s\n", chose_block,mysql_errno(mysql_block), mysql_error(mysql_block));
		return;
	}
    int num_rows = mysql_num_rows(result);//2
	printf("got:%d row:\n", (int)num_rows);
    int num_fields = mysql_num_fields(result);//3
	printf("number of fields:%d\n", (int)num_fields);
    
    nlohmann::json json;
    MYSQL_FIELD *field = nullptr;
    MYSQL_ROW row;
    while (row = mysql_fetch_row(result))
		{
			unsigned long *lengths = mysql_fetch_lengths(result);
			for (int i = 0; i < num_fields; i++)
			{
				char *filed = row[i];
				unsigned int field_length = lengths[i];
                field = mysql_fetch_field(result);
				json[field->name] = filed;
                printf("column[%d],length[%d],field[%s]:data[%s]\n", i, field_length, field->name,filed ? filed : "null");
			}
			printf("\n");
		}
	mysql_free_result(result);
    
    response.send(Pistache::Http::Code::Ok, json.dump());
}

}
}
}
}

