#include "dataManger.h"
#include "preDefine.h"
#include "configManger.h"
#include <QApplication>
#include <QTime>


// 请求类型
enum
{
    LOGIN = 10, PULL_ORDER, CONFIRM_ORDER,
    REFUSE_ORDER,COMPLETE_ORDER = 16,
    REFUSE_REFUND = 18,SEND_ORDER = 19,
    REFUND_ORDER = 20, GET_DELIVER=21,
    UPDATE_STOCK=80
};

DataManger &DataManger::GetInstance()
{
    static DataManger dm;
    return dm;
}

void DataManger::SetToken(const QString &token)
{
    m_token = token;
}

QJsonObject DataManger::GetLoginData(const QString &storeId, const QString &password, const QString &stationId, const QString &cashierId)
{
    QJsonObject rObj, cObj;
    rObj.insert(JSON_REQTYPE, LOGIN);
    QString _storeId;
    if(storeId.startsWith("pos_my_"))
    {
        _storeId = storeId;
    }else
    {
        _storeId = ConfigManger::GetInstance().GetPrefix()+storeId;
    }
    /// 为了配合升级程序在此处获取到门店号则将其写入配置文件

        /// ../storeInfo.ini -- info/store_id -- info/pos_no

    QString iniPath = QString("%1/../storeInfo.ini").arg(QApplication::applicationDirPath());
    QSettings setting(iniPath ,QSettings::IniFormat);
    if(!setting.contains("info/store_id"))
    {
        qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
        setting.setValue("info/store_id", _storeId.simplified());
        setting.setValue("info/pos_no", qrand());
    }

    cObj.insert(JSON_USERNAME, _storeId);
    cObj.insert(JSON_PASSWORD, password);  
    rObj.insert(JSON_CURRENTUSER, cObj);
    rObj.insert(JSON_STATIONID, stationId);
    rObj.insert(JSON_OPERATORID, cashierId);
    return rObj;
}

QJsonObject DataManger::GetPullOrderData(const QString &timestamp)
{
    QJsonObject rObj;
    rObj.insert(JSON_REQTYPE, PULL_ORDER);
    rObj.insert(JSON_TIMESTAMPS, timestamp);
    rObj.insert(JSON_AUTOCONFIRM, "1");
    rObj.insert(JSON_TOKEN, m_token);
    rObj.insert(JSON_POSVERSION, APP_VERSION);
    return rObj;
}

QJsonObject DataManger::GetPullDeliverData(const QString &storeId, const QString &channelCode)
{
    QJsonObject rObj;
    rObj.insert(JSON_REQTYPE, GET_DELIVER);
    rObj.insert(JSON_STOREID, storeId);
    rObj.insert(JSON_CHANNEL, channelCode);
    rObj.insert(JSON_TOKEN, m_token);
    rObj.insert(JSON_POSVERSION, APP_VERSION);
    return rObj;
}

QJsonObject DataManger::GetConfirmOrderData(const QString &orderId, const QString &deliverId, const QString &deliverName, const QString &deliverPhone)
{
    QJsonObject rObj, cObj;
    rObj.insert(JSON_REQTYPE, CONFIRM_ORDER);
    cObj.insert(JSON_ORDERID, orderId);
    cObj.insert(JSON_DELIVERYID, deliverId);
    cObj.insert(JSON_DELIVERYNAME, deliverName);
    cObj.insert(JSON_DELIVERYPHONE, deliverPhone);
    rObj.insert(JSON_ORDER, cObj);
    rObj.insert(JSON_TOKEN, m_token);
    rObj.insert(JSON_POSVERSION, APP_VERSION);
    return rObj;
}

QJsonObject DataManger::GetRefuseOrderData(int refuseCode, const QString &orderId)
{
    QJsonObject rObj, cObj;
    rObj.insert(JSON_REQTYPE, REFUSE_ORDER);
    cObj.insert(JSON_ORDERID, orderId);
    cObj.insert(JSON_REASONCODE, refuseCode);
    rObj.insert(JSON_ORDER, cObj);
    rObj.insert(JSON_TOKEN, m_token);
    rObj.insert(JSON_POSVERSION, APP_VERSION);
    return rObj;
}

QJsonObject DataManger::GetSendOrderData(const QString &orderId)
{
    QJsonObject rObj, cObj;
    rObj.insert(JSON_REQTYPE, SEND_ORDER);
    cObj.insert(JSON_ORDERID, orderId);
    rObj.insert(JSON_ORDER, cObj);
    rObj.insert(JSON_TOKEN, m_token);
    rObj.insert(JSON_POSVERSION, APP_VERSION);
    return rObj;
}

QJsonObject DataManger::GetCompleteOrderData(const QString &orderId)
{
    QJsonObject rObj, cObj;
    rObj.insert(JSON_REQTYPE, COMPLETE_ORDER);
    cObj.insert(JSON_ORDERID, orderId);
    rObj.insert(JSON_ORDER, cObj);
    rObj.insert(JSON_TOKEN, m_token);
    rObj.insert(JSON_POSVERSION, APP_VERSION);
    return rObj;
}

QJsonObject DataManger::GetRefundOrderData(const QString &reason, const QString &orderId)
{
    QJsonObject rObj, cObj;
    rObj.insert(JSON_REQTYPE, REFUND_ORDER);
    cObj.insert(JSON_ORDERID, orderId);
    cObj.insert(JSON_REASON, reason);
    rObj.insert(JSON_ORDER, cObj);
    rObj.insert(JSON_TOKEN, m_token);
    rObj.insert(JSON_POSVERSION, APP_VERSION);
    return rObj;
}

QJsonObject DataManger::GetRefuseRefundData(const QString &reason, const QString &orderId)
{
    QJsonObject rObj, cObj;
    rObj.insert(JSON_REQTYPE, REFUSE_REFUND);
    cObj.insert(JSON_ORDERID, orderId);
    cObj.insert(JSON_REASON, reason);
    rObj.insert(JSON_ORDER, cObj);
    rObj.insert(JSON_TOKEN, m_token);
    rObj.insert(JSON_POSVERSION, APP_VERSION);
    return rObj;
}
