#include "pro.h"
#include "config.h"
#include "httpclient.h"
#include <QAbstractSocket>
#include <QHostInfo>
#include <QNetworkInterface>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QTimeZone>
#include <QtCore>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QDir>

#include "Interactive/InteractiveData.h"
#include "Interactive/InteractiveConf.h"

extern config *pconfig;

pro::pro(QObject *parent) : QThread(parent)
{
    qApp->installEventFilter(this);

    m_pManager = new QNetworkAccessManager(this);
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &pro::updateTime);
}

void pro::slot_feature_login()
{
    token = InteractiveHttp::strToken;
    SINGLETON_CREATE(InteractiveData)()->m_token = InteractiveHttp::strToken;
    workstat = proWorkStat::userMaterial;
}

void pro::slot_set_thread_stop(void)
{
    this->threadstatus = false;
}

void pro::slot_saveConfig(void)
{
    pconfig->configWrite();
}

void pro::slot_addfinger(bool stat)
{
    addfingerStat = stat;
    emit signal_fingerWork(stat);
}

void pro::slot_fingerWork(bool stat)
{
    addfingerStat = false;
    emit signal_fingerWork(stat);
}

void pro::slot_fingerStat(bool stat, QByteArray img, int size)
{
    if(stat == true)
    {
        fingerimgbuff = img;
        if(addfingerStat == true)
        {
            workstat = proWorkStat::insertfacelogin;
            emit signal_fingerWork(false);
        }
        else{
            workstat = proWorkStat::fingerLogin;
        }
    }
}

void pro::slot_instructionsGet()
{
    workstat = proWorkStat::materialTypeGet;
}

void pro::slot_getMaterialCheckPlan()
{
    workstat = proWorkStat::getMaterialCheckPlan;
}

void pro::slot_getMaterialCheckForm(int planId)
{
    pconfig->currentPlanId = planId;
    workstat = proWorkStat::getMaterialCheckForm;
}

void pro::slot_postSaveForm(QString data)
{
    m_strPostSaveForm = data;
    workstat = proWorkStat::postSaveForm;
}

void pro::slot_postCheckSign(int planId, QString path)
{

    QJsonObject jsonRoot;

    QString url = pconfig->usernameLogin_url;
    jsonRoot.insert("cabinetCode", pconfig->devUuid);
    jsonRoot.insert("planId", planId);
    jsonRoot.insert("signatureImg", QString(path));

    QJsonDocument jsonDoc(jsonRoot);
    QByteArray jsonData = jsonDoc.toJson(QJsonDocument::Compact);

    m_strPostCheckSign = QString::fromUtf8(jsonData);

    workstat = proWorkStat::postCheckSign;
}

void pro::slot_postSubmit()
{
    workstat = proWorkStat::postSubmit;
}

void pro::slot_getMaterialInfoList()
{
    workstat = proWorkStat::getMaterialInfoList;
}

void pro::slot_pdf_click(QByteArray urlPath)
{
    QUrl url(urlPath);
    QString strPath = url.path();

    QString strFileName = strPath.mid(strPath.lastIndexOf('/') + 1);
    QString strFilePath = strPath.mid(0, strPath.lastIndexOf('/') + 1);
    strFilePath = qApp->applicationDirPath() + "/pdf/" + strFilePath;

    QString strFilePathName = strFilePath + strFileName;

    if (!QFile::exists(strFilePathName))
    {
        if(!QDir().mkpath(strFilePath))
        {
            return;
        }

        QString requestUrl = urlPath;
        QNetworkRequest request(requestUrl);

        request.setAttribute(QNetworkRequest::RedirectPolicyAttribute,
                             QNetworkRequest::NoLessSafeRedirectPolicy);


        QNetworkReply *pReply = m_pManager->get(request);

        // 添加超时处理，1ms 超时
        QEventLoop eventLoop;
        connect(pReply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
        QTimer::singleShot(1000, &eventLoop, &QEventLoop::quit);
        eventLoop.exec();

        QByteArray array;
        if(pReply->isFinished()){
            if (pReply->error() == QNetworkReply::NoError) {
                // 正常结束，读取响应数据
                QByteArray result = pReply->readAll();

                QFile file(strFilePathName);
                if (file.open(QIODevice::WriteOnly | QIODevice::Truncate))
                {
                    file.write(result);
                    file.close();
                }

                pReply->deleteLater();
            }
        }
        pReply->deleteLater();
    }

    emit signal_pdf_relay(strFilePathName);

}


void pro::http_response_usernamelogin(QByteArray data)
{
    QJsonParseError error;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
    if (error.error == QJsonParseError::NoError)
    {
        if(!(jsonDoc.isNull() || jsonDoc.isEmpty()))
        {
            QJsonObject rootObj = jsonDoc.object();
            if(rootObj.contains("code"))
            {
                if(rootObj.value("code").toInt() == 200)
                {
                    if(rootObj.contains("token"))
                    {
                        QJsonValue value = rootObj.value("token");
                        if(value.type() == QJsonValue::String)
                        {
                            SINGLETON_CREATE(InteractiveData)()->m_token = value.toString();
                            InteractiveHttp::strToken = value.toString();
                            token = value.toString();
                            QString username = "未知用户";
                            if(rootObj.contains("nickName"))
                            {
                                QJsonValue name = rootObj.value("nickName");
                                if(name.type() == QJsonValue::String)
                                {
                                    username = name.toString();
                                }
                            }
                            pconfig->username = username;
                            emit signal_login_return_stat(0, "登录成功");
                            emit signal_login_return_param(username, 0);
                            workstat = proWorkStat::userMaterial;
                        }
                        else{
                            emit signal_login_return_stat(-3, "登录失败");
                        }
                    }
                }
                else{
                    emit signal_login_return_stat(-1, "用户名或者密码错误");
                }
            }
        }
    }
    else{
        emit signal_login_return_stat(-3, "登录失败");
    }
}

void pro::http_response_cardlogin(QByteArray data)
{
    QJsonParseError error;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
    if (error.error == QJsonParseError::NoError)
    {
        if(!(jsonDoc.isNull() || jsonDoc.isEmpty()))
        {
            QJsonObject rootObj = jsonDoc.object();
            if(rootObj.contains("code"))
            {
                if(rootObj.value("code").toInt() == 200)
                {
                    if(rootObj.contains("token"))
                    {
                        QJsonValue value = rootObj.value("token");
                        if(value.type() == QJsonValue::String)
                        {
                            SINGLETON_CREATE(InteractiveData)()->m_token = value.toString();
                            InteractiveHttp::strToken = value.toString();
                            token = value.toString();
                            QString username = "未知用户";
                            if(rootObj.contains("nickName"))
                            {
                                QJsonValue name = rootObj.value("nickName");
                                if(name.type() == QJsonValue::String)
                                {
                                    username = name.toString();
                                }
                            }
                            if(rootObj.contains("userName"))
                            {
                                QJsonValue username = rootObj.value("userName");
                                if(username.type() == QJsonValue::String)
                                {
                                    pconfig->username = username.toString();
                                }
                            }
                            if(rootObj.contains("userId"))
                            {
                                QJsonValue userId = rootObj.value("userId");
                                if(userId.type() == QJsonValue::String)
                                {
                                    pconfig->userId = userId.toString();
                                }
                            }
                            pconfig->username = username;
                            emit signal_login_return_stat(0, "登录成功");
                            emit signal_login_return_param(username, 0);
                            workstat = proWorkStat::userMaterial;
                        }
                        else{
                            emit signal_login_return_stat(-3, "登录失败");
                        }
                    }
                }
                else{
                    emit signal_login_return_stat(-2, "用户不存在");
                }
            }
        }
    }
    else{
        emit signal_login_return_stat(-3, "登录失败");
    }
}

void pro::http_response_fingerLogin(QByteArray data)
{
    QJsonParseError error;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
    if (error.error == QJsonParseError::NoError)
    {
        if(!(jsonDoc.isNull() || jsonDoc.isEmpty()))
        {
            QJsonObject rootObj = jsonDoc.object();
            if(rootObj.contains("code"))
            {
                if(rootObj.value("code").toInt() == 200)
                {
                    if(rootObj.contains("token"))
                    {
                        QJsonValue value = rootObj.value("token");
                        if(value.type() == QJsonValue::String)
                        {
                            SINGLETON_CREATE(InteractiveData)()->m_token = value.toString();
                            InteractiveHttp::strToken = value.toString();
                            token = value.toString();
                            QString username = "未知用户";
                            if(rootObj.contains("nickName"))
                            {
                                QJsonValue name = rootObj.value("nickName");
                                if(name.type() == QJsonValue::String)
                                {
                                    username = name.toString();
                                }
                            }
                            pconfig->username = username;
                            emit signal_login_return_stat(0, "登录成功");
                            emit signal_login_return_param(username, 0);
                            workstat = proWorkStat::userMaterial;
                        }
                        else{
                            emit signal_login_return_stat(-3, "登录失败");
                        }
                    }
                }
                else{
                    emit signal_login_return_stat(-2, "用户不存在");
                }
            }
        }
    }
    else{
        emit signal_login_return_stat(-3, "登录失败");
    }
}

void pro::http_response_logout(QByteArray data)
{
    QJsonParseError error;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
    if (error.error == QJsonParseError::NoError)
    {
        if(!(jsonDoc.isNull() || jsonDoc.isEmpty()))
        {
            QJsonObject rootObj = jsonDoc.object();
            if(rootObj.contains("code"))
            {
                if(rootObj.value("code").toInt() == 200)
                {
                    qDebug() << "用户退出成功";
                }
            }
        }
    }
}

void pro::http_response_getMaterial(QByteArray data)
{
    QJsonParseError error;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
    if (error.error == QJsonParseError::NoError)
    {
        if(!(jsonDoc.isNull() || jsonDoc.isEmpty()))
        {
            QJsonObject rootObj = jsonDoc.object();
            if(rootObj.contains("code"))
            {
                if(rootObj.value("code").toInt() == 200)
                {
                    if(rootObj.contains("data"))
                    {
                        QJsonValue data = rootObj.value("data");
                        if(data.type() == QJsonValue::Array)
                        {
                            QJsonArray rfidArray = data.toArray();
                            for(int i = 0; i < rfidArray.size(); i++)
                            {
                                QString rfid = "";
                                QString name = "";
                                int materialsTypeId = 0;
                                QString materialsTypePicture;
                                bool take;

                                if(rfidArray.at(i).toObject().value("materialsRfid").type() == QJsonValue::String)
                                {
                                    rfid = rfidArray.at(i).toObject().value("materialsRfid").toString();
                                }
                                else{
                                    continue;
                                }

                                if(rfidArray.at(i).toObject().value("materialsName").type() == QJsonValue::String)
                                {
                                    name = rfidArray.at(i).toObject().value("materialsName").toString();
                                }

                                if(rfidArray.at(i).toObject().value("materialsTypeId").type() == QJsonValue::String)
                                {
                                    materialsTypeId = rfidArray.at(i).toObject().value("materialsTypeId").toString().toInt();
                                }
                                else if(rfidArray.at(i).toObject().value("materialsTypeId").type() == QJsonValue::Double)
                                {
                                    materialsTypeId = rfidArray.at(i).toObject().value("materialsTypeId").toInt();
                                }

                                if(rfidArray.at(i).toObject().value("materialsTypePicture").type() == QJsonValue::String)
                                {
                                    materialsTypePicture = rfidArray.at(i).toObject().value("materialsTypePicture").toString();
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

void pro::http_response_userMaterial(QByteArray data)
{
    QJsonParseError error;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);

    if (error.error == QJsonParseError::NoError)
    {
        if(!(jsonDoc.isNull() || jsonDoc.isEmpty()))
        {
            QJsonObject rootObj = jsonDoc.object();
            if(rootObj.contains("code"))
            {
                if(rootObj.value("code").toInt() == 200)
                {
                    if(rootObj.contains("data"))
                    {
                        QJsonValue data = rootObj.value("data");
                        if(data.type() == QJsonValue::Array)
                        {
                            pconfig->materialTypePngList.clear();
                            pconfig->materialTypeCountList.clear();

                            pconfig->materialNameList.clear();
                            pconfig->materialTypeList.clear();
                            pconfig->materialPictureList.clear();
                            pconfig->materialLoanUserList.clear();
                            pconfig->materialLoanStateList.clear();
                            QJsonArray dataArray = data.toArray();
                            for(int i = 0; i < dataArray.size(); i++)
                            {
                                if(dataArray.at(i).toObject().value("materials").type() == QJsonValue::Array)
                                {
                                    QJsonArray materialArray = dataArray.at(i).toObject().value("materials").toArray();
                                    for(int j = 0; j < materialArray.size(); j++)
                                    {
                                        QString rfid = "";

                                        if(materialArray.at(j).toObject().value("materialsRfid").type() == QJsonValue::String)
                                        {
                                            rfid = materialArray.at(j).toObject().value("materialsRfid").toString();
                                        }
                                        else{
                                            continue;
                                        }

                                        if (materialArray.at(j).toObject().value("materialsName").type() == QJsonValue::String)
                                        {
                                            QString name = materialArray.at(j).toObject().value("materialsName").toString();
                                            pconfig->materialNameList.insert(rfid, name);
                                        }
                                        else{
                                            pconfig->materialNameList.insert(rfid, "未知物资");
                                        }

                                        if (materialArray.at(j).toObject().value("status").type() == QJsonValue::String)
                                        {
                                            QString status = materialArray.at(j).toObject().value("status").toString();
                                            pconfig->materialStatusList.insert(rfid, status);
                                        }
                                        else
                                        {
                                            pconfig->materialStatusList.insert(rfid, "0");
                                        }


                                        if(materialArray.at(j).toObject().value("materialsTypePicture").type() == QJsonValue::String)
                                        {
                                            QString materialsTypePicture = materialArray.at(j).toObject().value("materialsTypePicture").toString();
                                            pconfig->materialPictureList.insert(rfid, materialsTypePicture);
                                        }
                                        else{
                                            pconfig->materialPictureList.insert(rfid, "");
                                        }

                                        if(materialArray.at(j).toObject().value("loanState").type() == QJsonValue::String)
                                        {
                                            int loanState = materialArray.at(j).toObject().value("loanState").toString().toInt();
                                            pconfig->materialLoanStateList.insert(rfid, loanState);
                                        }
                                        else if(materialArray.at(j).toObject().value("loanState").type() == QJsonValue::Double)
                                        {
                                            int loanState = materialArray.at(j).toObject().value("loanState").toInt();
                                            pconfig->materialLoanStateList.insert(rfid, loanState);
                                        }
                                        else{
                                            pconfig->materialLoanStateList.insert(rfid, -1);
                                        }


                                        if (materialArray.at(j).toObject().value("loanUserId").type() == QJsonValue::String)
                                        {
                                            QString loanUserId = materialArray.at(j).toObject().value("loanUserId").toString();;
                                            pconfig->materialLoanUserList.insert(rfid, loanUserId);
                                        }


                                        int materialtype = 0;
                                        QString materialtypename = "";
                                        QString pngfile = "";
                                        if(materialArray.at(j).toObject().value("materialsTypeId").type() == QJsonValue::String)
                                        {
                                            materialtype = materialArray.at(j).toObject().value("materialsTypeId").toString().toInt();
                                        }
                                        else if(materialArray.at(j).toObject().value("materialsTypeId").type() == QJsonValue::Double)
                                        {
                                            materialtype = materialArray.at(j).toObject().value("materialsTypeId").toInt();
                                        }

                                        if(materialArray.at(j).toObject().value("materialsTypeName").type() == QJsonValue::String)
                                        {
                                            materialtypename =  materialArray.at(j).toObject().value("materialsTypeName").toString();
                                        }
                                        else{
                                            materialtypename = "未知类型" +QString::number(materialtype);
                                        }

                                        pconfig->materialTypeList.insert(rfid, materialtypename);

                                        if(materialArray.at(j).toObject().value("materialsTypeIcon").type() == QJsonValue::String)
                                        {
                                            pngfile = materialArray.at(j).toObject().value("materialsTypeIcon").toString();
                                        }

                                        pconfig->materialTypePngList.insert(materialtypename, pngfile);

                                        // if(pconfig->materialTypeCountList.contains(materialtypename))
                                        // {
                                        //     pconfig->materialTypeCountList[materialtypename]++;
                                        // }
                                        // else{
                                        //     pconfig->materialTypeCountList.insert(materialtypename, 0);
                                        // }

                                    }

                                    QString materialsTypeName = dataArray.at(i).toObject().value("materialsTypeName").toString();
                                    int number = dataArray.at(i).toObject().value("number").toInt();
                                    int borrowableNumber = dataArray.at(i).toObject().value("borrowableNumber").toInt();

                                    if(materialsTypeName != "全部")
                                    {
                                        pconfig->materialTypeCountList[materialsTypeName] = number;
                                        pconfig->materialTypeBorrowableNumberList[materialsTypeName] = borrowableNumber;
                                    }
                                }
                            }
                            emit signal_updateMaterial();
                            emit signal_updateMaterialHand();
                            emit signal_updateMaterialAuto();
                        }
                    }
                }
            }
        }
    }
}


void pro::http_response_updateMaterial(QByteArray data)
{
    QJsonParseError error;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
    if (error.error == QJsonParseError::NoError)
    {
        if(!(jsonDoc.isNull() || jsonDoc.isEmpty()))
        {
            QJsonObject rootObj = jsonDoc.object();
            if(rootObj.contains("code"))
            {
                if(rootObj.value("code").toInt() == 200)
                {
                    workstat = proWorkStat::userMaterial;
                }
            }
        }
    }
}

void pro::http_response_postMaterialReplaceAuto(QByteArray data)
{
    QJsonParseError error;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
    if (error.error == QJsonParseError::NoError)
    {
        if(!(jsonDoc.isNull() || jsonDoc.isEmpty()))
        {
            QJsonObject rootObj = jsonDoc.object();
            if(rootObj.contains("code"))
            {
                if(rootObj.value("code").toInt() == 200)
                {
                    workstat = proWorkStat::userMaterial;
                }
            }
        }
    }
}

void pro::http_response_insertfingerLogin(QByteArray data)
{
    QJsonParseError error;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
    if (error.error == QJsonParseError::NoError)
    {
        if(!(jsonDoc.isNull() || jsonDoc.isEmpty()))
        {
            QJsonObject rootObj = jsonDoc.object();
            if(rootObj.contains("code"))
            {
                if(rootObj.value("code").toInt() == 200)
                {
                    qDebug() << "录入指纹成功";
                    emit signal_addFinger_act(true);
                }
                else{
                    qDebug() << "录入指纹失败";
                    emit signal_addFinger_act(false);
                }
            }
        }
    }
}

void pro::http_response_materialTypeGet(QByteArray data)
{
    emit signal_materialTypeGet(data);
}

void pro::http_response_instructionsGet(QByteArray data)
{
    emit signal_instructionsGet(data);
}

void pro::http_response_getMaterialCheckPlan(QByteArray data)
{
    emit signal_getMaterialCheckPlan(data);
}
void pro::http_response_getMaterialCheckForm(QByteArray data)
{

    emit signal_getMaterialCheckForm(data);
}
void pro::http_response_postSaveForm(QByteArray data)
{
    emit signal_postSaveForm(data);
}

void pro::http_response_postCheckSign(QByteArray data)
{
    emit signal_postCheckSign(data);
}

void pro::http_response_postSubmit(QByteArray data)
{
    emit signal_postSubmit(data);
}
void pro::http_response_getMaterialInfoList(QByteArray data)
{
    emit signal_getMaterialInfoList(data);
}

void pro::slot_response_data(quint64 idx, QString url, QByteArray data)
{
    qDebug() << "url:" << url << "\nresponse=" << QString::fromUtf8(data);

    if(url == pconfig->usernameLogin_url)
    {
        http_response_usernamelogin(data);
    }
    else if(url == pconfig->cardLogin_url)
    {
        http_response_cardlogin(data);
    }
    else if(url == pconfig->logout_url)
    {
        http_response_logout(data);
    }
    else if(url == pconfig->getMaterial_url)
    {
        http_response_getMaterial(data);
    }
    else if(url == pconfig->userMaterial_url)
    {
        http_response_userMaterial(data);
    }
    // 物资取还
    else if(url == pconfig->updateMaterial_url)
    {
        http_response_updateMaterial(data);
    }
    // 物资更换
    else if(url == pconfig->postMaterialReplaceAuto_url)
    {
        http_response_postMaterialReplaceAuto(data);
    }

    else if(url == pconfig->fingerLogin_url)
    {
        http_response_fingerLogin(data);
    }
    else if(url == pconfig->addfinger_url)
    {
        http_response_insertfingerLogin(data);
    }
    else if (url == pconfig->getMaterialTypeList_url)
    {
        http_response_materialTypeGet(data);
    }
    else if (url == pconfig->instructionsGet_url)
    {
        http_response_instructionsGet(data);
    }
    else if (url == pconfig->getMaterialCheckPlan_url)
    {
        http_response_getMaterialCheckPlan(data);
    }
    else if (url == pconfig->getMaterialCheckForm_url)
    {
        http_response_getMaterialCheckForm(data);
    }
    else if (url == pconfig->postSaveForm_url)
    {
        http_response_postSaveForm(data);
    }
    else if (url == pconfig->postCheckSign_url)
    {
        http_response_postCheckSign(data);
    }
    else if (url == pconfig->postSubmit_url)
    {
        http_response_postSubmit(data);
    }
    else if (url == pconfig->getMaterialInfoList_url)
    {
        http_response_getMaterialInfoList(data);
    }
}

void pro::slot_login(int type, QString username, QString password)
{
    if(type == 0)
    {
        login.username = username;
        login.password = password;
        login.type = LOGIN_USERNAME;
        workstat = proWorkStat::usernameLogin;
    }
    else if(type == 1)
    {
        login.cardNo = username;
        login.type = LOGIN_CARD;
        workstat = proWorkStat::cardLogin;
    }
    else if(type == 2)
    {
        emit signal_fingerWork(true);
    }
}

void pro::slot_logout(void)
{
    workstat = proWorkStat::userLogout;
}

void pro::http_request_usernamelogin(void)
{
    QJsonObject jsonRoot;
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();

    QString url = pconfig->usernameLogin_url;
    jsonRoot.insert("username", login.username);
    jsonRoot.insert("password", login.password);

    QJsonDocument jsonDoc(jsonRoot);
    QByteArray jsonData = jsonDoc.toJson(QJsonDocument::Compact);
    qDebug() << "json=" << QString::fromUtf8(jsonData);

    emit signal_post_request_data(timestampSeconds, url, jsonData, NULL, token);
    workstat = proWorkStat::sleep;
}

void pro::http_request_cardlogin(void)
{
    QJsonObject jsonRoot;
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();

    QString url = pconfig->cardLogin_url;
    jsonRoot.insert("cardNfc", login.cardNo);

    QJsonDocument jsonDoc(jsonRoot);
    QByteArray jsonData = jsonDoc.toJson(QJsonDocument::Compact);
    qDebug() << "json=" << QString::fromUtf8(jsonData);

    emit signal_post_request_data(timestampSeconds, url, jsonData, NULL, token);
    workstat = proWorkStat::sleep;
}

void pro::http_request_userlogout(void)
{
    QJsonObject jsonRoot;
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();

    QString url = pconfig->logout_url;

    QJsonDocument jsonDoc(jsonRoot);
    QByteArray jsonData = jsonDoc.toJson(QJsonDocument::Compact);
    qDebug() << "json=" << QString::fromUtf8(jsonData);

    emit signal_post_request_data(timestampSeconds, url, jsonData, NULL, token);
    workstat = proWorkStat::sleep;
}

void pro::http_request_fingerLogin(void)
{
    QJsonObject jsonRoot;
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();

    QString url = pconfig->fingerLogin_url;

    emit signal_post_request_data(timestampSeconds, url, NULL, fingerimgbuff, token);
    workstat = proWorkStat::sleep;
}

void pro::http_request_insertfingerLogin(void)
{
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();

    QString url = pconfig->addfinger_url;

    QByteArray data = pconfig->username.toUtf8();
    qDebug() << "data=" << QString::fromUtf8(data);

    emit signal_post_request_data(timestampSeconds, url, data, fingerimgbuff, token);
    workstat = proWorkStat::sleep;
}

void pro::http_request_getMaterial(void)
{
    QJsonObject jsonRoot;
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();

    QString url = pconfig->getMaterial_url;

    QJsonArray jsonarray;
    for (const QString& str : tidArray) {
        jsonarray.append(str);
    }

    jsonRoot.insert("list", jsonarray);

    QJsonDocument jsonDoc(jsonRoot);
    QByteArray jsonData = jsonDoc.toJson(QJsonDocument::Compact);
    qDebug() << "json=" << QString::fromUtf8(jsonData);

    emit signal_post_request_data(timestampSeconds, url, jsonData, NULL, token);
    workstat = proWorkStat::sleep;
}

void pro::http_request_userMaterial(void)
{
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();

    QString url = pconfig->userMaterial_url;
    QString getData = "cabinetCode=" + pconfig->devUuid;

    QByteArray jsonData = getData.toUtf8();

    emit signal_get_request_data(timestampSeconds, url, jsonData, token);
    workstat = proWorkStat::sleep;
}

void pro::http_request_userMaterial_copy(void)
{
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();

    QString url = pconfig->userMaterial_url;
    QString getData = "cabinetCode=" + pconfig->devUuid;

    QByteArray jsonData = getData.toUtf8();

    emit signal_get_request_data(timestampSeconds, url, jsonData, token);
    workstat = proWorkStat::upgradeMaterial;
}

void pro::http_request_openCabinet(void)
{
    QJsonObject jsonRoot;
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();

    QString url = pconfig->openCabinet_url;
    jsonRoot.insert("cabinetCode", pconfig->devUuid);
    jsonRoot.insert("openType", "0");

    QJsonDocument jsonDoc(jsonRoot);
    QByteArray jsonData = jsonDoc.toJson(QJsonDocument::Compact);
    qDebug() << "json=" << QString::fromUtf8(jsonData);

    emit signal_post_request_data(timestampSeconds, url, jsonData, NULL, token);
    workstat = proWorkStat::sleep;
}

void pro::http_request_closeCabinet(void)
{
    QJsonObject jsonRoot;
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();

    QString url = pconfig->closeCabinet_url;
    jsonRoot.insert("cabinetCode", pconfig->devUuid);

    QJsonDocument jsonDoc(jsonRoot);
    QByteArray jsonData = jsonDoc.toJson(QJsonDocument::Compact);
    qDebug() << "json=" << QString::fromUtf8(jsonData);

    emit signal_post_request_data(timestampSeconds, url, jsonData, NULL, token);
    workstat = proWorkStat::sleep;
}

void pro::http_request_updateMaterial(void)
{
    QJsonObject jsonRoot;
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();

    QString url = pconfig->updateMaterial_url;

    QJsonArray jsonarray;

    // 其他柜子
    for (const QString& str : pconfig->backElseMaterialList)
    {
        QJsonObject back;
        back.insert("loanState", "1");
        back.insert("materialsRfid", str);
        back.insert("restitutionToCabinetCode", pconfig->devUuid);
        jsonarray.append(back);
    }

    // 新绑定的
    for (const QString& str : pconfig->bindedMaterialList)
    {
        QJsonObject back;
        back.insert("loanState", "1");
        back.insert("materialsRfid", str);
        back.insert("restitutionToCabinetCode", pconfig->devUuid);
        jsonarray.append(back);
    }

    for (const QString& str : pconfig->backMaterialList)
    {
        QJsonObject back;
        back.insert("loanState", "1");
        back.insert("materialsRfid", str);
        back.insert("restitutionToCabinetCode", pconfig->devUuid);
        jsonarray.append(back);
    }


    // 其他柜子
    for (const QString& str : pconfig->takeElseMaterialList)
    {
        QJsonObject take;
        take.insert("loanState", "0");
        take.insert("materialsRfid", str);
        take.insert("restitutionToCabinetCode", pconfig->devUuid);
        jsonarray.append(take);
    }

    for (const QString& str : pconfig->takeMaterialList)
    {
        QJsonObject take;
        take.insert("loanState", "0");
        take.insert("materialsRfid", str);
        take.insert("restitutionToCabinetCode", pconfig->devUuid);
        jsonarray.append(take);
    }

    jsonRoot.insert("list", jsonarray);

    QJsonDocument jsonDoc(jsonRoot);
    QByteArray jsonData = jsonDoc.toJson(QJsonDocument::Compact);
    qDebug() << "json=" << QString::fromUtf8(jsonData);

    emit signal_post_request_data(timestampSeconds, url, jsonData, NULL, token);
    workstat = proWorkStat::sleep;
}

void pro::http_request_replaceMaterial(void)
{
    QJsonObject jsonRoot;
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();
    QString url = pconfig->postMaterialReplaceAuto_url;

    // 绑定
    QJsonArray jsonArrAppend;
    for (const QString& str : pconfig->bindedMaterialList)
    {
        jsonArrAppend.append(str);
    }

    // 解绑
    QJsonArray jsonArrRemove;
    for (const QString& str : pconfig->unbindMaterialList)
    {
        jsonArrRemove.append(str);
    }
    jsonRoot.insert("cabinetCode", pconfig->devUuid);

    jsonRoot.insert("bindingMaterialRfidList", jsonArrAppend);
    jsonRoot.insert("unbindMaterialRfidList", jsonArrRemove);


    QJsonDocument jsonDoc(jsonRoot);
    QByteArray jsonData = jsonDoc.toJson(QJsonDocument::Compact);
    qDebug() << "json=" << QString::fromUtf8(jsonData);

    emit signal_post_request_data(timestampSeconds, url, jsonData, NULL, token);

    workstat = proWorkStat::userMaterial_copy;
}

void pro::http_request_exceptionHandle()
{


}

void pro::http_request_materialTypeGet()
{
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();

    QString url = pconfig->getMaterialTypeList_url;
    QString getData = "cabinetCode=" + pconfig->devUuid;

    QByteArray jsonData = getData.toUtf8();
    emit signal_get_request_data(timestampSeconds, url, jsonData, token);

    workstat = proWorkStat::instructionsGet;
}

void pro::http_request_instructionsGet()
{
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();

    QString url = pconfig->instructionsGet_url;
    QString getData = "cabinetCode=" + pconfig->devUuid;

    QByteArray jsonData = getData.toUtf8();

    emit signal_get_request_data(timestampSeconds, url, jsonData, token);
    workstat = proWorkStat::sleep;
}

void pro::http_request_getMaterialCheckPlan()
{
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();

    QString url = pconfig->getMaterialCheckPlan_url;
    QString getData = "cabinetCode=" + pconfig->devUuid;

    QByteArray jsonData = getData.toUtf8();

    emit signal_get_request_data(timestampSeconds, url, jsonData, token);
    workstat = proWorkStat::sleep;
}

void pro::http_request_getMaterialCheckForm()
{
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();

    QString url = pconfig->getMaterialCheckForm_url;
    QString getData = "cabinetCode=" + pconfig->devUuid;
    getData += "&";
    getData += "planId=" + QString::number(pconfig->currentPlanId);

    QByteArray jsonData = getData.toUtf8();

    emit signal_get_request_data(timestampSeconds, url, jsonData, token);
    workstat = proWorkStat::sleep;
}

void pro::http_request_postSaveForm()
{
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();
    QString url = pconfig->postSaveForm_url;

    emit signal_post_request_data(timestampSeconds, url, m_strPostSaveForm.toUtf8(), NULL, token);
    workstat = proWorkStat::sleep;
}
void pro::http_request_postCheckSign()
{
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();
    QString url = pconfig->postCheckSign_url;

    emit signal_post_request_data(timestampSeconds, url, m_strPostCheckSign.toUtf8(), NULL, token);
    workstat = proWorkStat::sleep;
}

void pro::http_request_postSubmit()
{
    QJsonObject jsonRoot;
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();

    QString url = pconfig->postSubmit_url;
    jsonRoot.insert("cabinetCode", pconfig->devUuid);
    jsonRoot.insert("planId", pconfig->currentPlanId);

    QJsonDocument jsonDoc(jsonRoot);
    QByteArray jsonData = jsonDoc.toJson(QJsonDocument::Compact);
    qDebug() << "json=" << QString::fromUtf8(jsonData);

    emit signal_post_request_data(timestampSeconds, url, jsonData, NULL, token);
    workstat = proWorkStat::sleep;
}

void pro::http_request_getMaterialInfoList()
{
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();

    QString url = pconfig->getMaterialInfoList_url;
    QString getData = "cabinetCode=" + pconfig->devUuid;

    QByteArray jsonData = getData.toUtf8();

    emit signal_get_request_data(timestampSeconds, url, jsonData, token);
    workstat = proWorkStat::sleep;
}


void pro::getMaterialElseInfos()
{
    // 访问http服务
    QString strUrl = "http://" + pconfig->httpHost + pconfig->getMaterial_url;
    QByteArray inData;
    QByteArray outData;
    {

        QJsonObject jsonRoot;
        QDateTime currentDateTime = QDateTime::currentDateTime();
        qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();

        QJsonArray jsonarray;
        for (const QString& str : pconfig->elseMaterialList) {
            jsonarray.append(str);
        }

        jsonRoot.insert("list", jsonarray);

        QJsonDocument jsonDoc(jsonRoot);
        inData = jsonDoc.toJson(QJsonDocument::Compact);
    }


    httpClient::postRequest(strUrl, httpClient::sToken, inData, outData);

    pconfig->materialNameListElse.clear();
    pconfig->materialPictureListElse.clear();

    QJsonParseError error;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(outData, &error);
    if (error.error == QJsonParseError::NoError)
    {
        if(!(jsonDoc.isNull() || jsonDoc.isEmpty()))
        {
            QJsonObject rootObj = jsonDoc.object();
            if(rootObj.contains("code"))
            {
                if(rootObj.value("code").toInt() == 200)
                {
                    if(rootObj.contains("data"))
                    {
                        QJsonValue data = rootObj.value("data");
                        if(data.type() == QJsonValue::Array)
                        {
                            QJsonArray rfidArray = data.toArray();
                            for(int i = 0; i < rfidArray.size(); i++)
                            {
                                QString rfid = "";
                                QString name = "";
                                int materialsTypeId = 0;
                                QString materialsTypePicture;
                                bool take;

                                if(rfidArray.at(i).toObject().value("materialsRfid").type() == QJsonValue::String)
                                {
                                    rfid = rfidArray.at(i).toObject().value("materialsRfid").toString();
                                }
                                else{
                                    continue;
                                }

                                if(rfidArray.at(i).toObject().value("materialsName").type() == QJsonValue::String)
                                {
                                    name = rfidArray.at(i).toObject().value("materialsName").toString();
                                    pconfig->materialNameListElse.insert(rfid, name);
                                }
                                else{
                                    pconfig->materialNameListElse.insert(rfid, "未知物资");
                                }

                                if(rfidArray.at(i).toObject().value("materialsTypePicture").type() == QJsonValue::String)
                                {
                                    QString materialsTypePicture = rfidArray.at(i).toObject().value("materialsTypePicture").toString();
                                    pconfig->materialPictureListElse.insert(rfid, materialsTypePicture);
                                }
                                else{
                                    pconfig->materialPictureListElse.insert(rfid, "");
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

void pro::slot_tid_update(quint16 count, QList<QString> tidList)
{
    // 调用开始读rfid，进入此函数
    tidArray = tidList;

    if(pconfig->materworkstat == 0 || pconfig->materworkstat == 1 || pconfig->materworkstat == 3 || pconfig->materworkstat == 4)
    {
        // 业务逻辑
        if(pconfig->materialStock == false)
        {
            if(tidArray.length() > 0)
            {
                workstat = proWorkStat::getMaterial;
            }
        }
        else{
            pconfig->materialStock = true;

            pconfig->backElseMaterialList.clear();
            pconfig->takeElseMaterialList.clear();
            pconfig->takeMaterialList.clear();
            pconfig->backMaterialList.clear();
            pconfig->bindedMaterialList.clear();
            pconfig->unbindMaterialList.clear();

            // 如果属于本柜子
            for(auto it = pconfig->materialLoanStateList.cbegin(); it != pconfig->materialLoanStateList.cend(); ++it)
            {
                if(tidArray.contains(it.key()))
                {
                    if(it.value() == SERVER_MATERIAL_LOANSTATE_OUT)
                    {
                        pconfig->backMaterialList.append(it.key());
                    }
                }
                else{
                    if(it.value() == SERVER_MATERIAL_LOANSTATE_IN)
                    {
                        pconfig->takeMaterialList.append(it.key());
                    }
                }
            }

            // 如果不属于本柜子
            QStringList tids = tidArray;
            QStringList keys = pconfig->materialLoanStateList.keys();
            QSet<QString> tidSet(tids.begin(), tids.end());
            QSet<QString> keySet(keys.begin(), keys.end());
            QSet<QString> thisSet = tidSet - keySet;

            // 获取这次不属于本柜子物资列表
            QStringList thisMaterialList(thisSet.begin(), thisSet.end());
            // 获取上次不属于本柜子物资列表
            QStringList lastMaterialList = InteractiveConf::confReadElseMaterials();
            // 这次不属于本柜子物资列表写入
            InteractiveConf::confSaveElseMaterials(thisMaterialList);


            QSet<QString> thisMaterialSet(thisMaterialList.begin(), thisMaterialList.end());
            QSet<QString> laseMaterialSet(lastMaterialList.begin(), lastMaterialList.end());

            QSet<QString> backMaterialSet = thisMaterialSet - laseMaterialSet;  // 入柜
            QSet<QString> takeMaterialSet = laseMaterialSet - thisMaterialSet;  // 出柜

            pconfig->backElseMaterialList = QStringList(backMaterialSet.begin(), backMaterialSet.end());
            pconfig->takeElseMaterialList = QStringList(takeMaterialSet.begin(), takeMaterialSet.end());

            pconfig->elseMaterialList = pconfig->backElseMaterialList + pconfig->takeElseMaterialList;
            if (!pconfig->elseMaterialList.isEmpty())
            {
                getMaterialElseInfos();
            }

            if (!pconfig->backElseMaterialList.isEmpty())
            {
                // emit signal_backRefresh();
            }
            if (!pconfig->takeElseMaterialList.isEmpty())
            {
                // emit signal_takeRefresh();
            }

            // 调用对应的信号
            // 如果是取还操作
            if (pconfig->materworkstat == 0)
            {
                emit signal_updatetakebackMaterial();
            }
            // 如果是手动更换
            else if (pconfig->materworkstat == 1)
            {
                emit signal_updatetakebackMaterialHand();
            }
            // 如果是异常处理
            else if (pconfig->materworkstat == 3)
            {
                emit signal_updateExceptionHandle();
            }
            else if (pconfig->materworkstat == 4)
            {
                emit signal_updatematerialCheckPlan();
            }


            workstat = proWorkStat::upgradeMaterial;
        }
    }

    // 如果是自动更换
    else if (pconfig->materworkstat == 2)
    {

        if(pconfig->materialStock == false)
        {
            if(tidArray.length() > 0)
            {
                workstat = proWorkStat::getMaterial;
            }
        }
        else{
            pconfig->materialStock = true;
            pconfig->takeMaterialList.clear();
            pconfig->backMaterialList.clear();
            pconfig->backElseMaterialList.clear();
            pconfig->takeElseMaterialList.clear();
            pconfig->bindedMaterialList.clear();
            pconfig->unbindMaterialList.clear();

            // 如果属于本柜子
            for(auto it = pconfig->materialLoanStateList.cbegin(); it != pconfig->materialLoanStateList.cend(); ++it)
            {
                if(tidArray.contains(it.key()))
                {
                    // 放入当做归还
                    if(it.value() == SERVER_MATERIAL_LOANSTATE_OUT)
                    {
                        pconfig->backMaterialList.append(it.key());
                    }
                }
                else{
                    // 拿出当做解绑
                    if(it.value() == SERVER_MATERIAL_LOANSTATE_IN)
                    {
                        // pconfig->takeMaterialList.append(it.key());
                        pconfig->unbindMaterialList.append(it.key());
                    }
                }
            }

            // 如果不属于本柜子
            QStringList tids = tidArray;
            QStringList keys = pconfig->materialLoanStateList.keys();
            QSet<QString> tidSet(tids.begin(), tids.end());
            QSet<QString> keySet(keys.begin(), keys.end());
            QSet<QString> thisSet = tidSet - keySet;

            // 获取这次不属于本柜子物资列表
            QStringList thisMaterialList(thisSet.begin(), thisSet.end());
            // 获取上次不属于本柜子物资列表
            QStringList lastMaterialList = InteractiveConf::confReadElseMaterials();
            // 这次不属于本柜子物资列表写入
            // InteractiveConf::confSaveElseMaterials(thisMaterialList);


            QSet<QString> thisMaterialSet(thisMaterialList.begin(), thisMaterialList.end());
            QSet<QString> laseMaterialSet(lastMaterialList.begin(), lastMaterialList.end());

            QSet<QString> backMaterialSet = thisMaterialSet - laseMaterialSet;  // 入柜 绑定 且归还
            QSet<QString> takeMaterialSet = laseMaterialSet - thisMaterialSet;  // 出柜 解除异常

            // 本次要写入配置里的物资
            QSet<QString> writhSet = laseMaterialSet - takeMaterialSet;
            QList<QString> writhList(writhSet.begin(), writhSet.end());
            InteractiveConf::confSaveElseMaterials(writhList);


            pconfig->bindedMaterialList = QStringList(backMaterialSet.begin(), backMaterialSet.end());
            pconfig->takeElseMaterialList = QStringList(takeMaterialSet.begin(), takeMaterialSet.end());

            pconfig->elseMaterialList = pconfig->backElseMaterialList + pconfig->takeElseMaterialList
                                        + pconfig->bindedMaterialList + pconfig->unbindMaterialList;

            if (!pconfig->elseMaterialList.isEmpty())
            {
                getMaterialElseInfos();
            }

            emit signal_updatetakebackMaterialAuto();
            workstat = proWorkStat::replaceMaterial;
        }
    }


}

void pro::slot_lockStat(bool leftStat, bool rightStat)
{
    if(leftStat == false || rightStat == false)
    {
        pconfig->openDoor = true;

        // 开锁时刷新时间戳
        pconfig->lastLockTimeEpoch = QDateTime::currentSecsSinceEpoch();
    }
    else if(leftStat == true && rightStat == true)
    {
        pconfig->openDoor = false;

        // 关锁时时间戳置为0
        pconfig->lastLockTimeEpoch = 0;
    }

    if (pconfig->materworkstat == 0)
    {
        emit signal_lockStatTakeBack(leftStat, rightStat);
    }
    // 手动更换
    else if (pconfig->materworkstat == 1)
    {
        emit signal_lockStatReplaceHand(leftStat, rightStat);
    }
    // 自动更换
    else if (pconfig->materworkstat == 2)
    {
        emit signal_lockStatReplaceAuto(leftStat, rightStat);
    }
    else if (pconfig->materworkstat == 3)
    {
        emit signal_lockStatException(leftStat, rightStat);
    }
    else if (pconfig->materworkstat == 4)
    {
        emit signal_lockStatCheckPlan(leftStat, rightStat);
    }
}

void pro::slot_getMaterial(void)
{
    workstat = proWorkStat::userMaterial;
}

void pro::slot_takebackstat(bool stat)
{
    if(stat == true)
    {
        workstat = proWorkStat::openCabinet;
    }
    else{
        workstat = proWorkStat::closeCabinet;
    }
}

void pro::updateTime(void)
{
    timer->stop();
}

void pro::moveCursorTo(int x, int y)
{
    QCursor::setPos(x, y);
}

void pro::getSelModelNumber(QJSValue callback)
{
    int loanNumber = 0;
    int exceNumber = 0;
    for (auto it = pconfig->materialLoanStateList.begin(); it != pconfig->materialLoanStateList.end(); ++it)
    {
        if (it.value() == 0 && pconfig->materialLoanUserList.contains(it.key()))
        {
            if (pconfig->materialLoanUserList[it.key()] == pconfig->userId)
            {
                loanNumber++;
            }
        }
    }

    InteractiveData *pSingletonData = SINGLETON_CREATE(InteractiveData)();
    // 进行数据读操作
    QReadLocker locker(&pSingletonData->m_dataCacheLock);
    exceNumber = pSingletonData->m_dataCacheExMaterialInfoId.size();
    int chackPlanStatus = pSingletonData->m_checkPlanStatus;
    int chackNumber = (chackPlanStatus == 0 || chackPlanStatus == 1) ? 1 : 0;


    QString strJson = QString("{\"loanNumber\":%1,\"exceNumber\":%2, \"chackNumber\":%3}").arg(loanNumber).arg(exceNumber).arg(chackNumber);

    if (callback.isCallable())
    {
        QJSValueList args;
        args << strJson;
        callback.call(args);
    }
}

void pro::getError(QJSValue callback)
{
    QStringList rfids = InteractiveConf::confReadElseMaterials();

    if (callback.isCallable())
    {
        QJSValueList args;
        args << !rfids.isEmpty();
        // args << true;
        callback.call(args);
    }
}

QString pro::getUuid()
{
    QUuid uuid = QUuid::createUuid();
    return uuid.toString(QUuid::WithoutBraces);
}

bool pro::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonPress) {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        // qDebug() << "Global mouse click at:" << mouseEvent->globalPos();
        // 在这里处理全局鼠标点击
        emit signal_global_mouse_clicked();
    }

    return QObject::eventFilter(obj, event);;
}


void pro::run()
{
    while(this->threadstatus)
    {
        switch(workstat)
        {
        case proWorkStat::usernameLogin:http_request_usernamelogin();break;
        case proWorkStat::cardLogin:http_request_cardlogin();break;
        case proWorkStat::fingerLogin:http_request_fingerLogin();break;
        case proWorkStat::insertfacelogin:http_request_insertfingerLogin();break;
        case proWorkStat::userLogout:http_request_userlogout();break;
        case proWorkStat::getMaterial:http_request_getMaterial();break;
        case proWorkStat::userMaterial:http_request_userMaterial();break;
        case proWorkStat::userMaterial_copy: http_request_userMaterial_copy(); break;
        case proWorkStat::openCabinet:http_request_openCabinet();break;
        case proWorkStat::closeCabinet:http_request_closeCabinet();break;
        case proWorkStat::upgradeMaterial:http_request_updateMaterial();break;
        case proWorkStat::replaceMaterial:http_request_replaceMaterial();break;
            // case proWorkStat::exceptionHandle:http_request_exceptionHandle();break; // 异常处理

        case proWorkStat::materialTypeGet:http_request_materialTypeGet();break;
        case proWorkStat::instructionsGet:http_request_instructionsGet();break;

        case proWorkStat::getMaterialCheckPlan:http_request_getMaterialCheckPlan();break;
        case proWorkStat::getMaterialCheckForm:http_request_getMaterialCheckForm();break;

        case proWorkStat::postSaveForm:http_request_postSaveForm(); break;
        case proWorkStat::postCheckSign:http_request_postCheckSign(); break;
        case proWorkStat::postSubmit:http_request_postSubmit(); break;
        case proWorkStat::getMaterialInfoList:http_request_getMaterialInfoList();
        }

        msleep(20);
    }
    qDebug() << "pro thread exit!";
}
