#include "datamanager.h"
#include <QDebug>
#include <QCoreApplication>
#include <QFile>
DataManager::DataManager(QObject * parent ):QObject(parent)
{
    init();
};
DataManager::~DataManager()
{

}
 DataManager& DataManager::instance()
{
     static DataManager dataManager;
     return dataManager;

}

void DataManager::init()
{
    QString  sFileName = QCoreApplication::applicationDirPath()+"/medicalRecode.db";
    //bool bexist = QFile::exists(sFileName);
    //if(bexist)
    {
        SqlCore::init("QSQLITE","user","passdwd",sFileName);
    }
    initTable();
}
void DataManager::initTable()
{

    //病例信息
    {
        /*
            病历管理
               检查号
               类型： 住院或门诊
               姓名
               性别 男女
               出生日期
               年龄
               左右利Laterality  左右
               检查日期  inspection
               其他信息
          */

           QMap<QString, QString> map;
           map.insert("f_CheckNum", "TEXT  PRIMARY KEY");
           map.insert("f_Type", "TEXT NOT NULL");
           map.insert("f_Name", "TEXT NOT NULL");
           map.insert("f_Sex", "TEXT NOT NULL");
           map.insert("f_Birthday", "TEXT NOT NULL");
           map.insert("f_Year", "TEXT NOT NULL");
           map.insert("f_Laterality", "TEXT NOT NULL");
            map.insert("f_InspectDate", "TEXT NOT NULL");
           map.insert("f_Other", "TEXT NOT NULL");
        SqlGenerate sqlGener;
        QString sql = sqlGener.createTable("t_RecodManage",map);
        qDebug()<<sql<<endl;
        m_sqlcore.execute(sql);
    }
    //医院信息
    {
        /*
           医院名称
           用户名称
           科室
           用户密码
           用户人数
           确认密码
           数据名称
        */
        QMap<QString, QString> map;
        map.insert("f_Id", "TEXT  PRIMARY KEY");
        map.insert("f_HostpitalName", "TEXT  ");
        map.insert("f_UserName", "TEXT NOT NULL");
        map.insert("f_Section", "TEXT NOT NULL");
        map.insert("f_UserPasswd", "TEXT NOT NULL");
        map.insert("f_UserNum", "TEXT NOT NULL");
        map.insert("f_RePasswd", "TEXT NOT NULL");
        map.insert("f_DataName", "TEXT NOT NULL");

     SqlGenerate sqlGener;
     QString sql = sqlGener.createTable("t_Hostpital",map);
     qDebug()<<sql<<endl;
     m_sqlcore.execute(sql);
    }

}

//更新医院信息
bool   DataManager::updateHospitalInfo(QString sHostpitalName,
                                       QString UserName,
                                       QString sSection,
                                       QString UserPasswd,
                                       QString UserNum,
                                       QString RePasswd,
                                       QString DataName)

{

    QMap<QString, QVariant> map;
    map.insert("f_Id", 0);
    map.insert("f_HostpitalName", sHostpitalName);
    map.insert("f_UserName", UserName);
    map.insert("f_Section", sSection);
    map.insert("f_UserPasswd", UserPasswd);
    map.insert("f_UserNum", UserNum);
    map.insert("f_RePasswd", RePasswd);
    map.insert("f_DataName",DataName);

    SqlGenerate sqlGener;
    QString sql = sqlGener.insertData("t_Hostpital",map);
    qDebug()<<sql<<endl;
    return  m_sqlcore.execute(sql);

}

bool  DataManager::HospitalInfo(QString& sHostpitalName,
                   QString& UserName,
                   QString& sSection,
                   QString& UserPasswd,
                   QString& UserNum,
                   QString& RePasswd,
                   QString& DataName)
{

    QStringList listName;
    listName<<"f_HostpitalName";
    listName<<"f_UserName";
    listName<<"f_Section";
    listName<<"f_UserPasswd";
    listName<<"f_UserNum";
    listName<<"f_RePasswd";
    listName<<"f_DataName";
    SqlGenerate sqlGener;
    QString sql = sqlGener.selectWhere("t_Hostpital",listName);

     bool ret =false;
      auto query(m_sqlcore.select(sql));
      if (query.isNull())
      {

          return ret;
      }
      while (query->next())
      {
          int i =0;
          sHostpitalName = query->value(i++).toString();;
          UserName = query->value(i++).toString();
           sSection= query->value(i++).toString();
           UserPasswd = query->value(i++).toString();
          UserNum = query->value(i++).toString();
          RePasswd = query->value(i++).toString();
           DataName = query->value(i++).toString();

      }

      query->finish();
      query->clear();

      return true;
}
bool   DataManager::updateMedicRecord(
           QString CheckNum ,
           QString Type ,
           QString  _Name ,
           QString Sex,
           QString Birthday,
           QString Year,
           QString Laterality,
           QString InspectDate,
           QString Other)
{
     QMap<QString, QVariant> map;
    map.insert("f_CheckNum", CheckNum);
    map.insert("f_Type", Type);
    map.insert("f_Name", _Name);
    map.insert("f_Sex", Sex);
    map.insert("f_Birthday", Birthday);
    map.insert("f_Year", Year);
    map.insert("f_Laterality", Laterality);
    map.insert("f_InspectDate", InspectDate);
    map.insert("f_Other", Other);
    SqlGenerate sqlGener;
    QString sql = sqlGener.insertData("t_RecodManage",map);
    qDebug()<<sql<<endl;
    return m_sqlcore.execute(sql);
}
int  DataManager::getMedicRecordCount( QString sName)
{
    int ret  = -1;
     QString querySql = QString("select count(*) from t_RecodManage where f_Name LIKE '%") + sName + "%';";
      //qDebug() << "querySql = " << querySql << endl;
      auto query(m_sqlcore.select(querySql));
      if (!query.isNull())
      {
          if (query->first())
          {
              ret = query->value(0).toInt();
          }
          query->finish();
          query->clear();
      }

      return ret;


}
uint  DataManager::getMedicRecordList(const QString& fileName,
                                 QList<MedicRecord>& filesInfo,
                                 int& curPage,
                                 int perPageCnt)
{
    uint ret = 0;
      filesInfo.clear();
      QString tmpFileName = fileName;     
      QString querySql = QString("SELECT f_CheckNum,f_Type,f_Name, f_Sex,f_Birthday,f_Year,f_Laterality,f_InspectDate,f_Other from t_RecodManage where f_Name LIKE '%") + tmpFileName + "%'";
      querySql += QString(" LIMIT %1 offset %2;").arg(perPageCnt)
                                                 .arg(curPage * perPageCnt);

      auto query(m_sqlcore.select(querySql));
      if (query.isNull())
      {

          return 0;
      }
      while (query->next())
      {
          MedicRecord  info;
          info.m_CheckNum= query->value(0).toString();
          info.m_Type = query->value(1).toString();
          info.m_Name = query->value(2).toString();
          info.m_Sex = query->value(3).toString();
          info.m_Birthday = query->value(4).toString();
          info.m_Year = query->value(5).toString();
          info.m_Laterality = query->value(6).toString();
          info.m_InspectDate= query->value(7).toString();
          info.m_Other = query->value(8).toString();

          filesInfo.append(info);
          ++ret;
      }
      if (ret > 0)
      {
          ++curPage;
      }

      query->finish();
      query->clear();

      return ret;
}
 bool  DataManager::deleteNumRecode(QString checkNum)
 {
    SqlGenerate sqlGener;

   QString sql = QString("delete from %1 where %2").arg("t_RecodManage").arg("f_CheckNum = '"+checkNum+"';");

    return m_sqlcore.execute(sql);

 }
