#include <QObject>
#include <QSqlQuery>

#include "C_BDM_FreeBasePlugin.h"
#include "../../MedinTuxTools-QT4/CGestIni.h"

//-------------------------------- C_BDM_FreeBasePlugin ---------------------------------------
C_BDM_FreeBasePlugin::C_BDM_FreeBasePlugin(QObject *parent)
    : C_BDM_InitPluginI(parent)
{m_position   = 0;
 m_pQSqlQuery = 0;
}
//-------------------------------- ~C_BDM_FreeBasePlugin ---------------------------------------
C_BDM_FreeBasePlugin::~C_BDM_FreeBasePlugin()
{if (m_pQSqlQuery) delete m_pQSqlQuery;
}

//-------------------------------- connect ---------------------------------------
int C_BDM_FreeBasePlugin::connect(const QString &params)
{QString errMess;
 if (BaseConnect(params, "BDM_FREEBASE", "BaseGetTest", &errMess )==0) return 0;
 m_pQSqlQuery = new QSqlQuery (QString::null , database() );
 setInitialisedSate(1);
 return 1;
}

//-------------------------------- drugsList_Count ---------------------------------------
long C_BDM_FreeBasePlugin::drugsList_Count()
{return countRecords("BDM_CIP", " WHERE CODE_ATC <>'Z' AND CODE_ATC <>''");
}

//-------------------------------- smrList_Count ---------------------------------------
int   C_BDM_FreeBasePlugin::smrList_Count()
{ return 0;
}
//-------------------------------- getSmrList_Start ---------------------------------------
int   C_BDM_FreeBasePlugin::getSmrList_Start()
{ return 0;
}
//-------------------------------- getSmrList_NextRecord ---------------------------------------
int   C_BDM_FreeBasePlugin::getSmrList_NextRecord (C_BDM_SmrListRecord  &rC_BDM_SmrListRecord)
{ return 0;
}

//-------------------------------- getDrugList_Start ---------------------------------------
int C_BDM_FreeBasePlugin::getDrugList_Start()
{   m_position = 0;
    if (!database().isOpen() && database().open()== FALSE)
       {outMessage( tr("ERREUR : C_BDM_FreeBasePlugin::getDrugList_Start() database can not be open"), __FILE__, __LINE__);  return 0;
       }
    QString     requete  =      "SELECT  NOM_COURT, NOM_LONG1, NOM_LONG2, CIP7, CODE_ATC, CIP_UCD "
                                "FROM BDM_CIP WHERE CODE_ATC <>'Z' AND CODE_ATC <>''";
    m_pQSqlQuery->exec( requete );
    outSQL_error( *m_pQSqlQuery, "ERREUR  : C_BDM_FreeBasePlugin::drugsList_Count()", requete, __FILE__, __LINE__);
    if ( !m_pQSqlQuery->isActive() ) return 0;
    else                             return 1;
}

//-------------------------------- getDrugList_NextRecord ---------------------------------------
int  C_BDM_FreeBasePlugin::getDrugList_NextRecord(C_BDM_DrugListRecord &rC_BDM_DrugListRecord)
{
    QStringList dciList;
    QString     libelle  = "";
    QString     libelle1 = "";
    QString     libelle2 = "";
    QString     cis_code = "";
    QString     cip_code = "";
    QString     ucd_code = "";
    QString     atc_code = "";
    QString     dci_1    = "";
    QString     dci_2    = "";
    QString     dci_3    = "";

  if (m_pQSqlQuery->next() && m_position < 20000)
     {libelle      = CGestIni::Utf8_Query(*m_pQSqlQuery, 0);
      libelle1     = CGestIni::Utf8_Query(*m_pQSqlQuery, 1);
      libelle2     = CGestIni::Utf8_Query(*m_pQSqlQuery, 2);
      if (libelle1.left(8)==libelle.left(8)) libelle = libelle1;
      libelle += libelle2;

      cip_code     = m_pQSqlQuery->value(3).toString(); // get_CIP_From_CIS(cis_code);
      cis_code     = get_CIS_From_CIP(cip_code);
      atc_code     = m_pQSqlQuery->value(4).toString();
      ucd_code     = m_pQSqlQuery->value(5).toString(); // cis_to_ucd(cis_code);
      dciList      = isThisValueInTable_ToList("afs_comp", "afs_comp_cis_code", cis_code, "afs_comp_libelle",0);
      dci_1        = "";
      dci_2        = "";
      dci_3        = "";
      if (dciList.count())
         {if (dciList.count()>0) dci_1 = dciList[0];
          if (dciList.count()>1) dci_2 = dciList[1];
          if (dciList.count()>2) dci_3 = dciList[2];
         }
      else
         {dci_1 = cip_to_libelle_atc(cip_code);
          if (dci_1.length()==0) outMessage(  tr("WARNING : C_BDM_FreeBasePlugin::getDrugList_NextRecord() DCI not found for : %1").arg(libelle));
         }
      rC_BDM_DrugListRecord.set_commercialName(libelle);
      rC_BDM_DrugListRecord.set_id(cip_code);
      rC_BDM_DrugListRecord.set_id_type("CIP");
      rC_BDM_DrugListRecord.set_owner("AF");
      rC_BDM_DrugListRecord.set_lang("fr");
      rC_BDM_DrugListRecord.set_atc(atc_code);
      rC_BDM_DrugListRecord.set_dci_1Name(dci_1);
      rC_BDM_DrugListRecord.set_dci_2Name(dci_2);
      rC_BDM_DrugListRecord.set_dci_3Name(dci_3);
      rC_BDM_DrugListRecord.set_ucd(ucd_code);
      rC_BDM_DrugListRecord.set_ucd_price(cip_to_price(cip_code));
      return 1;
    }  // endif (query.isActive() )
    return 0;
}

//----------------------------------------------------- cip_to_price ---------------------------------------------------------------
QString C_BDM_FreeBasePlugin::cip_to_price(const QString &cip)
{QString requete  =      QString("SELECT  PRIX_E FROM BDM_PRIX WHERE CIP7 ='%1'").arg(cip);
 QSqlQuery query (requete , database() );
 outSQL_error( query, "ERREUR  : cip_to_price()", requete, __FILE__, __LINE__);
 if (query.isActive() )
    {while (query.next()) {return query.value(0).toString();}
    }
 return QString::null;
}

//--------------------------- get_CIS_From_CIP -----------------------------------------------------------
QString C_BDM_FreeBasePlugin::get_CIS_From_CIP(const QString &cip)
{QStringList list = isThisValueInTable_ToList("afs_ciscip", "afs_ciscip_cip_code", cip,     "afs_ciscip_cis_code");
 if (list.count()) return list[0];
 return QString::null;
}
//----------------------------------------------------- cip_to_libelle_atc ---------------------------------------------------------------
QString C_BDM_FreeBasePlugin::cip_to_libelle_atc(const QString &cip)
{QString requete  =      QString("SELECT  CLASSE_ATC FROM BDM_CIP WHERE CIP7 ='%1'").arg(cip);
 QSqlQuery query (requete , database() );
 outSQL_error( query, "ERREUR  : cip_to_libelle_atc()", requete, __FILE__, __LINE__);
 if (query.isActive() )
    {while (query.next()) {return query.value(0).toString();}
    }
 return QString::null;
}
