#include "orderprintwork.h"

#include "preDefine.h"

#include "event/fmapplication.h"
#include "event/posevent.h"

#include "model/orderObject.h"
#include "model/posorderpool.h"

#include "../fmPrinter/fmPrinter.h"

#include "QsLog.h"

orderprintwork::orderprintwork(QObject *parent) : QObject(parent)
{
    //订阅订单本地化事件
    FMApplication::subscibeEvent(this, PosEvent::s_order_location);
}

bool orderprintwork::event(QEvent *e)
{
    if(e->type() == PosEvent::s_order_location)
    {
        QVariantMap map;

        GETEVENTINFO(map, e, QVariantMap);

        if(map.contains(EVENT_KEY_ORDERID) && map.contains(EVENT_KEY_MSG) &&
                map[EVENT_KEY_MSG].toInt() == DEFAULT_LOCATION_PRINT)
        {
            OrderObject order;
            int last_status;

            PosOrderPool::GetOrderObject(map[EVENT_KEY_ORDERID].toString(), order, last_status);

            QLOG_INFO() << "begin print ...";

            if(FmPrinter::GetInstance().DoPrint(QString("takeout"), &order))
            {
                QLOG_INFO() << "print succcess";

                QVariantMap map;

                map.insert(EVENT_KEY_ORDERID, order.order_id);
                map.insert(EVENT_KEY_MSG, DEFAULT_LOCATION_PRINT);
                map.insert(EVENT_KEY_STATUS, 1);

                POSTEVENTTYPE(PosEvent::s_location_orderpool,map,QVariantMap);
            }
            else
            {
                QLOG_INFO() << "print failed";
            }

        }
        return true;
    }

    return QObject::event(e);
}

