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

     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_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::updateMedicRecord(
           QString CheckNum ,
           QString Type ,
           QString  _Name ,
           QString Sex,
           QString Birthday,
           QString Year,
           QString Laterality,
           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_Other", Other);
    SqlGenerate sqlGener;
    QString sql = sqlGener.insertData("t_MedicRecords",map);
    qDebug()<<sql<<endl;
    return m_sqlcore.execute(sql);
}
int  DataManager::getMedicRecordCount( QString sName)
{
    int ret  = -1;
     QString querySql = QString("select count(*) from t_MedicRecords 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).toUInt();
          }
          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_Other from t_MedicRecords 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_Other = query->value(7).toString();

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

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

      return ret;
}
