/**
 * 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 "SMFRegistrationDocumentApiImpl.h"

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

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

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

void SMFRegistrationDocumentApiImpl::create_smf_context_non3gpp(
    const std::string &ueId, const int32_t &pduSessionId,
    const SmfRegistration &smfRegistration,
    Pistache::Http::ResponseWriter &response) {
  MYSQL_RES *res = NULL;
  MYSQL_ROW row;
  const std::string select_SmfRegistration =
      "SELECT * from SmfRegistrations WHERE ueid=" + ueId +
      " AND subpduSessionId=" + std::to_string(pduSessionId);
  std::string query;
  nlohmann::json j;

  if (mysql_real_query(mysql_WitcommUDRDB, select_SmfRegistration.c_str(),
                       (unsigned long)select_SmfRegistration.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;
  }
  if (mysql_num_rows(res)) {
    query =
        "update SmfRegistrations set smfInstanceId='" +
        smfRegistration.getSmfInstanceId() + "'" +
        ",pduSessionId=" + std::to_string(smfRegistration.getPduSessionId()) +
        (smfRegistration.smfSetIdIsSet()
             ? ",smfSetId='" + smfRegistration.getSmfSetId() + "'"
             : "") +
        (smfRegistration.supportedFeaturesIsSet()
             ? ",supportedFeatures='" + smfRegistration.getSupportedFeatures() +
                   "'"
             : "") +
        (smfRegistration.dnnIsSet() ? ",dnn='" + smfRegistration.getDnn() + "'"
                                    : "") +
        (smfRegistration.emergencyServicesIsSet()
             ? (smfRegistration.isEmergencyServices() ? ",emergencyServices=1"
                                                      : ",emergencyServices=0")
             : "") +
        (smfRegistration.pcscfRestorationCallbackUriIsSet()
             ? ",pcscfRestorationCallbackUri='" +
                   smfRegistration.getPcscfRestorationCallbackUri() + "'"
             : "") +
        (smfRegistration.pgwFqdnIsSet()
             ? ",pgwFqdn='" + smfRegistration.getPgwFqdn() + "'"
             : "") +
        (smfRegistration.epdgIndIsSet()
             ? (smfRegistration.isEpdgInd() ? ",epdgInd=1" : ",epdgInd=0")
             : "") +
        (smfRegistration.deregCallbackUriIsSet()
             ? ",deregCallbackUri='" + smfRegistration.getDeregCallbackUri() +
                   "'"
             : "") +
        (smfRegistration.registrationTimeIsSet()
             ? ",registrationTime='" + smfRegistration.getRegistrationTime() +
                   "'"
             : "");

    if (smfRegistration.registrationReasonIsSet()) {
      to_json(j, smfRegistration.getRegistrationReason());
      query += ",registrationReason='" + j.dump() + "'";
    }
    if (smfRegistration.contextInfoIsSet()) {
      to_json(j, smfRegistration.getContextInfo());
      query += ",contextInfo='" + j.dump() + "'";
    }

    to_json(j, smfRegistration.getSingleNssai());
    query += ",singleNssai='" + j.dump() + "'";
    to_json(j, smfRegistration.getPlmnId());
    query += ",plmnId='" + j.dump() + "'";
    query += " where ueid=" + ueId +
             " AND subpduSessionId=" + std::to_string(pduSessionId);
  } else {
    query =
        "insert into SmfRegistrations set ueid=" + ueId +
        ",subpduSessionId=" + std::to_string(pduSessionId) +
        ",pduSessionId=" + std::to_string(smfRegistration.getPduSessionId()) +
        ",smfInstanceId='" + smfRegistration.getSmfInstanceId() + "'" +
        (smfRegistration.smfSetIdIsSet()
             ? ",smfSetId='" + smfRegistration.getSmfSetId() + "'"
             : "") +
        (smfRegistration.supportedFeaturesIsSet()
             ? ",supportedFeatures='" + smfRegistration.getSupportedFeatures() +
                   "'"
             : "") +
        (smfRegistration.dnnIsSet() ? ",dnn='" + smfRegistration.getDnn() + "'"
                                    : "") +
        (smfRegistration.emergencyServicesIsSet()
             ? (smfRegistration.isEmergencyServices() ? ",emergencyServices=1"
                                                      : ",emergencyServices=0")
             : "") +
        (smfRegistration.pcscfRestorationCallbackUriIsSet()
             ? ",pcscfRestorationCallbackUri='" +
                   smfRegistration.getPcscfRestorationCallbackUri() + "'"
             : "") +
        (smfRegistration.pgwFqdnIsSet()
             ? ",pgwFqdn='" + smfRegistration.getPgwFqdn() + "'"
             : "") +
        (smfRegistration.epdgIndIsSet()
             ? (smfRegistration.isEpdgInd() ? ",epdgInd=1" : ",epdgInd=0")
             : "") +
        (smfRegistration.deregCallbackUriIsSet()
             ? ",deregCallbackUri='" + smfRegistration.getDeregCallbackUri() +
                   "'"
             : "") +
        (smfRegistration.registrationTimeIsSet()
             ? ",registrationTime='" + smfRegistration.getRegistrationTime() +
                   "'"
             : "");

    if (smfRegistration.registrationReasonIsSet()) {
      to_json(j, smfRegistration.getRegistrationReason());
      query += ",registrationReason='" + j.dump() + "'";
    }
    if (smfRegistration.contextInfoIsSet()) {
      to_json(j, smfRegistration.getContextInfo());
      query += ",contextInfo='" + j.dump() + "'";
    }

    to_json(j, smfRegistration.getSingleNssai());
    query += ",singleNssai='" + j.dump() + "'";
    to_json(j, smfRegistration.getPlmnId());
    query += ",plmnId='" + j.dump() + "'";
  }

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

  to_json(j, smfRegistration);
  response.send(Pistache::Http::Code::Created, j.dump());
}
void SMFRegistrationDocumentApiImpl::delete_smf_context(
    const std::string &ueId, const int32_t &pduSessionId,
    Pistache::Http::ResponseWriter &response) {
  const std::string query =
      "DELETE from SmfRegistrations WHERE ueid=" + ueId +
      " AND subpduSessionId=" + std::to_string(pduSessionId);

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

  response.send(Pistache::Http::Code::No_Content, "");
}
void SMFRegistrationDocumentApiImpl::query_smf_registration(
    const std::string &ueId, const int32_t &pduSessionId,
    const Pistache::Optional<std::vector<std::string>> &fields,
    const Pistache::Optional<std::string> &supportedFeatures,
    Pistache::Http::ResponseWriter &response) {
  MYSQL_RES *res = NULL;
  MYSQL_ROW row;
  MYSQL_FIELD *field = nullptr;

  nlohmann::json j;

  SmfRegistration smfregistration;
  const std::string query =
      "SELECT * from SmfRegistrations WHERE ueid=" + ueId +
      " AND subpduSessionId=" + std::to_string(pduSessionId);

  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("smfInstanceId", field->name)) {
        smfregistration.setSmfInstanceId(row[i]);
      } else if (!strcmp("smfSetId", field->name) && row[i] != NULL) {
        smfregistration.setSmfSetId(row[i]);
      } else if (!strcmp("supportedFeatures", field->name) && row[i] != NULL) {
        smfregistration.setSupportedFeatures(row[i]);
      } else if (!strcmp("pduSessionId", field->name)) {
        std::string s = row[i];
        std::stringstream ss;
        int32_t a;
        ss << s;
        ss >> a;
        smfregistration.setPduSessionId(a);
      } else if (!strcmp("singleNssai", field->name)) {
        Snssai singlenssai;
        nlohmann::json::parse(row[i]).get_to(singlenssai);
        smfregistration.setSingleNssai(singlenssai);
      } else if (!strcmp("dnn", field->name) && row[i] != NULL) {
        smfregistration.setDnn(row[i]);
      } else if (!strcmp("emergencyServices", field->name) && row[i] != NULL) {
        if (strcmp(row[i], "0"))
          smfregistration.setEmergencyServices(true);
        else
          smfregistration.setEmergencyServices(false);
      } else if (!strcmp("pcscfRestorationCallbackUri", field->name) &&
                 row[i] != NULL) {
        smfregistration.setPcscfRestorationCallbackUri(row[i]);
      } else if (!strcmp("plmnId", field->name)) {
        PlmnId plmnid;
        nlohmann::json::parse(row[i]).get_to(plmnid);
        smfregistration.setPlmnId(plmnid);
      } else if (!strcmp("pgwFqdn", field->name) && row[i] != NULL) {
        smfregistration.setPgwFqdn(row[i]);
      } else if (!strcmp("epdgInd", field->name) && row[i] != NULL) {
        if (strcmp(row[i], "0"))
          smfregistration.setEpdgInd(true);
        else
          smfregistration.setEpdgInd(false);
      } else if (!strcmp("deregCallbackUri", field->name) && row[i] != NULL) {
        smfregistration.setDeregCallbackUri(row[i]);
      } else if (!strcmp("registrationReason", field->name) && row[i] != NULL) {
        RegistrationReason registrationreason;
        nlohmann::json::parse(row[i]).get_to(registrationreason);
        smfregistration.setRegistrationReason(registrationreason);
      } else if (!strcmp("registrationTime", field->name) && row[i] != NULL) {
        smfregistration.setRegistrationTime(row[i]);
      } else if (!strcmp("contextInfo", field->name) && row[i] != NULL) {
        ContextInfo contextinfo;
        nlohmann::json::parse(row[i]).get_to(contextinfo);
        smfregistration.setContextInfo(contextinfo);
      }
    }
    to_json(j, smfregistration);
    response.send(Pistache::Http::Code::Ok, j.dump());
  } else {
    std::cout << "SmfRegistration no data！" << std::endl;
  }

  mysql_free_result(res);
}

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