#include "core/database/DogDatabase.h"

// 单例实例指针定义
DogDatabase *DogDatabase::m_instance = nullptr;

DogDatabase *DogDatabase::getInstance()
{
    if (m_instance == nullptr) {
        m_instance = new DogDatabase();
    }
    return m_instance;
}

DogDatabase::~DogDatabase()
{
    closeDatabase();
}

DogDatabase::DogDatabase()
{
    m_sqlDatabase = QSqlDatabase::addDatabase("QMYSQL");
    m_fieldCount = 0;
}

//添加记录
bool DogDatabase::addDevice(const Dog &data)
{
    if(openDatabase("fly_land_database","root","hzk200407140238"))
    {
        beginAddField("dogdatabase");
        addField("id");
        addField("state");
        addField("ip");
        addField("port");
        addField("lon");
        addField("lat");
        endAddField();

        beginAddRow();
        addValue(data.id);
        addValue(data.state);
        addValue(data.ip);
        addValue(data.port);
        addValue(data.lon);
        addValue(data.lat);
        endAddRow();

        m_valueSQL = m_valueSQL.left(m_valueSQL.length()-1);
        QString sql;
        sql = m_headerSQL + m_valueSQL;
        return executeSQL(sql);
    }
    closeDatabase();
    return false;
}

//查询位置信息的记录
Point DogDatabase::getDevicePosition(const QString &id)
{
    Point position;
    position.lon = 0;
    position.lat = 0;
    
    if(openDatabase("fly_land_database","root","hzk200407140238"))
    {
        QSqlQuery query(m_sqlDatabase);

        QString strQuery;
        strQuery = "SELECT lon, lat FROM ";
        strQuery += "dogdatabase";
        strQuery += " WHERE id = ";
        strQuery += id;

        qDebug()<<strQuery;

        query.prepare(strQuery);
        bool isSuccess = query.exec();
        if(isSuccess)
        {
            while(query.next())
            {
                double lon = query.value(0).toDouble();
                double lat = query.value(1).toDouble();

                position.lon = lon;
                position.lat = lat;
            }
        }

        return position;
    }
    closeDatabase();
    return position;
}

//返回状态信息
int DogDatabase::getDeviceState(const QString &id)
{
    int state = -1;
    if(openDatabase("fly_land_database","root","hzk200407140238"))
    {
        QSqlQuery query(m_sqlDatabase);

        QString strQuery;
        strQuery = "SELECT state FROM ";
        strQuery += "dogdatabase";
        strQuery += " WHERE id = ";
        strQuery += id;

        qDebug()<<strQuery;

        query.prepare(strQuery);
        bool isSuccess = query.exec();
        if(isSuccess)
        {
            while(query.next())
            {
                state = query.value(0).toInt();
            }
        }
        return state;
    }
    closeDatabase();
    return state;
}

//——————————————————私有方法————————————————————
//建立连接
bool DogDatabase::openDatabase(const QString &dbName, const QString &userName, const QString &passwd)
{
    m_sqlDatabase.setHostName("localhost"); // 本地数据库 远程DB是：ipaddress
    m_sqlDatabase.setPort(3306); // 设置端口号
    m_sqlDatabase.setDatabaseName(dbName);
    m_sqlDatabase.setUserName(userName);
    m_sqlDatabase.setPassword(passwd);

    if(!m_sqlDatabase.open())
    {
        qDebug()<<"连接失败！";
        return false;
    }
    else
    {
        qDebug()<<"连接成功！";
        return true;
    }
}


void DogDatabase::closeDatabase()
{
    m_sqlDatabase.close();
}



void DogDatabase::beginAddField(const QString &tableName)
{
    m_tableName = tableName;
    m_fieldNames.clear();
    m_headerSQL.clear();
    m_valueSQL.clear();
    m_fieldCount = 0;
}

void DogDatabase::addField(const QString &fieldName)
{
    m_fieldNames.append(fieldName);
}

void DogDatabase::endAddField()
{
    m_headerSQL = QString("INSERT INTO %0 (%1) values").arg(m_tableName).arg(m_fieldNames.join(","));
}

void DogDatabase::beginAddRow()
{
    m_valueSQL += "(";
}

void DogDatabase::addValue(const QVariant &value)
{
    m_valueSQL += QString("\'%0\',").arg(value.toString());
    m_fieldCount++;
}

void DogDatabase::endAddRow()
{
    m_valueSQL = m_valueSQL.left(m_valueSQL.length() - 1);
    m_valueSQL += "),";
}


bool DogDatabase::executeSQL(const QString &sql)
{
    QSqlQuery query(m_sqlDatabase);
    bool success = query.exec(sql);
    return success;
}

