/**
 * Nudr_DataRepository API OpenAPI file
 * Unified Data Repository Service. © 2020, 3GPP Organizational Partners (ARIB,
 * ATIS, CCSA, ETSI, TSDSI, TTA, TTC). All rights reserved.
 *
 * The version of the OpenAPI document: 2.1.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 "SessionManagementSubscriptionDataApiImpl.h"

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

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

SessionManagementSubscriptionDataApiImpl::
    SessionManagementSubscriptionDataApiImpl(
        std::shared_ptr<Pistache::Rest::Router> rtr, MYSQL *mysql)
    : SessionManagementSubscriptionDataApi(rtr) {
  mysql_WitcommUDRDB = mysql;
}

void SessionManagementSubscriptionDataApiImpl::query_sm_data(
    const std::string &ueId, const std::string &servingPlmnId,
    const Pistache::Optional<Snssai> &singleNssai,
    const Pistache::Optional<std::string> &dnn,
    const Pistache::Optional<std::vector<std::string>> &fields,
    const Pistache::Optional<std::string> &supportedFeatures,
    const Pistache::Optional<Pistache::Http::Header::Raw> &ifNoneMatch,
    const Pistache::Optional<Pistache::Http::Header::Raw> &ifModifiedSince,
    Pistache::Http::ResponseWriter &response) {
  // response.send(Pistache::Http::Code::Ok, "query_sm_data\n");
  // servingPlmnId  pattern: "^[0-9]{5,6}$"
  MYSQL_RES *res = NULL;
  MYSQL_ROW row;
  MYSQL_FIELD *field = nullptr;

  nlohmann::json j;

  SessionManagementSubscriptionData sessionmanagementsubscriptiondata;
  const std::string query =
      "select * from SessionManagementSubscriptionData WHERE ueid=" + ueId +
      " and servingPlmnid=" + servingPlmnId;

  if (mysql_real_query(mysql_WitcommUDRDB, query.c_str(),
                       (unsigned long)query.size())) {
    std::cout << "mysql_real_query failure！" << std::endl;
    return;
  }

  res = mysql_store_result(mysql_WitcommUDRDB);
  if (res == NULL) {
    std::cout << "mysql_store_result failure！" << std::endl;
    return;
  }

  row = mysql_fetch_row(res);

  if (row != NULL) {
    for (int i = 0; field = mysql_fetch_field(res); i++) {
      if (!strcmp("singleNssai", field->name)) {
        Snssai singlenssai;
        nlohmann::json::parse(row[i]).get_to(singlenssai);
        sessionmanagementsubscriptiondata.setSingleNssai(singlenssai);
      } else if (!strcmp("dnnConfigurations", field->name) && row[i] != NULL) {
        std ::map<std ::string, DnnConfiguration> dnnconfigurations;
        nlohmann::json::parse(row[i]).get_to(dnnconfigurations);
        sessionmanagementsubscriptiondata.setDnnConfigurations(
            dnnconfigurations);
      } else if (!strcmp("internalGroupIds", field->name) && row[i] != NULL) {
        std ::vector<std ::string> internalgroupIds;
        nlohmann::json::parse(row[i]).get_to(internalgroupIds);
        sessionmanagementsubscriptiondata.setInternalGroupIds(internalgroupIds);
      } else if (!strcmp("sharedVnGroupDataIds", field->name) &&
                 row[i] != NULL) {
        std ::map<std ::string, std ::string> sharedvngroupdataids;
        nlohmann::json::parse(row[i]).get_to(sharedvngroupdataids);
        sessionmanagementsubscriptiondata.setSharedVnGroupDataIds(
            sharedvngroupdataids);
      } else if (!strcmp("sharedDnnConfigurationsId", field->name) &&
                 row[i] != NULL) {
        sessionmanagementsubscriptiondata.setSharedDnnConfigurationsId(row[i]);
      } else if (!strcmp("odbPacketServices", field->name) && row[i] != NULL) {
        OdbPacketServices odbpacketservices;
        nlohmann::json::parse(row[i]).get_to(odbpacketservices);
        sessionmanagementsubscriptiondata.setOdbPacketServices(
            odbpacketservices);
      } else if (!strcmp("traceData", field->name) && row[i] != NULL) {
        TraceData tracedata;
        nlohmann::json::parse(row[i]).get_to(tracedata);
        sessionmanagementsubscriptiondata.setTraceData(tracedata);
      } else if (!strcmp("sharedTraceDataId", field->name) && row[i] != NULL) {
        sessionmanagementsubscriptiondata.setSharedTraceDataId(row[i]);
      } else if (!strcmp("expectedUeBehavioursList", field->name) &&
                 row[i] != NULL) {
        std ::map<std ::string, ExpectedUeBehaviourData>
            expecteduebehaviourslist;
        nlohmann::json::parse(row[i]).get_to(expecteduebehaviourslist);
        sessionmanagementsubscriptiondata.setExpectedUeBehavioursList(
            expecteduebehaviourslist);
      } else if (!strcmp("suggestedPacketNumDlList", field->name) &&
                 row[i] != NULL) {
        std ::map<std ::string, SuggestedPacketNumDl> suggestedpacketnumdllist;
        nlohmann::json::parse(row[i]).get_to(suggestedpacketnumdllist);
        sessionmanagementsubscriptiondata.setSuggestedPacketNumDlList(
            suggestedpacketnumdllist);
      } else if (!strcmp("3gppChargingCharacteristics", field->name) &&
                 row[i] != NULL) {
        sessionmanagementsubscriptiondata.setR3gppChargingCharacteristics(
            row[i]);
      }
    }
    to_json(j, sessionmanagementsubscriptiondata);
    response.send(Pistache::Http::Code::Ok, j.dump());
  } else {
    std::cout << "SessionManagementSubscriptionData no data！" << std::endl;
  }

  mysql_free_result(res);
}

} // namespace api
} // namespace server
} // namespace openapitools
} // namespace org
