#include "orderObject.h"
#include "preDefine.h"
#include "QsLog.h"
#include <QJsonArray>
OrderObject::OrderObject(const OrderObject &order, QObject *parent):QObject(parent)
{
    qDeleteAll(proList);
    proList.clear();

    qDeleteAll(coupList);
    coupList.clear();

    qDeleteAll(orderCoupList);
    orderCoupList.clear();

    qDeleteAll(paymentList);
    paymentList.clear();

    /*
    qDeleteAll(posExtendList);
    posExtendList.clear();
    */

    const QMetaObject *m = order.metaObject();
    for( int i=m->propertyOffset(); i<m->propertyCount(); i++)
    {
        QMetaProperty qmp = m->property(i);
        QString key = QString::fromLatin1(qmp.name());
        QVariant value = order.property(key.toUtf8());
        this->setProperty(key.toUtf8(), value);
    }

    foreach (auto ptr, order.proList) {
        dishesObject *product=new dishesObject(*ptr,this);
        this->proList.append(product);
    }

    foreach (auto ptr, order.coupList) {
        CouponsObject *coupon=new CouponsObject(*ptr,this);
        this->coupList.append(coupon);
    }

    foreach (auto ptr, order.orderCoupList) {
        CouponsObject *orderCoupon=new CouponsObject(*ptr,this);
        this->orderCoupList.append(orderCoupon);
    }

    foreach (auto ptr, order.paymentList) {
        PaymentObject *payment=new PaymentObject(*ptr,this);
        this->paymentList.append(payment);
    }

    /*
    foreach (auto ptr, order.posExtendList) {
        PosExtendObject *posExtendObj=new PosExtendObject(*ptr,this);
        this->posExtendList.append(posExtendObj);
    }
    */

}


OrderObject& OrderObject::operator =(const OrderObject& order)
{
    qDeleteAll(proList);
    proList.clear();

    qDeleteAll(coupList);
    coupList.clear();

    qDeleteAll(orderCoupList);
    orderCoupList.clear();

    qDeleteAll(paymentList);
    paymentList.clear();

    /*
    qDeleteAll(posExtendList);
    posExtendList.clear();
    */

    const QMetaObject *m = order.metaObject();
    for( int i=m->propertyOffset(); i<m->propertyCount(); i++)
    {
        QMetaProperty qmp = m->property(i);
        QString key = QString::fromLatin1(qmp.name());
        QVariant value = order.property(key.toUtf8());
        this->setProperty(key.toUtf8(), value);
    }

    foreach (auto ptr, order.proList) {
        dishesObject *product=new dishesObject(*ptr,this);
        this->proList.append(product);
    }

    foreach (auto ptr, order.coupList) {
        CouponsObject *coupon = new CouponsObject(*ptr,this);
        this->coupList.append(coupon);
    }

    foreach (auto ptr, order.orderCoupList) {
        CouponsObject *orderCoupon = new CouponsObject(*ptr,this);
        this->orderCoupList.append(orderCoupon);
    }

    foreach (auto ptr, order.paymentList) {
        PaymentObject *payment=new PaymentObject(*ptr,this);
        this->paymentList.append(payment);
    }

    /*
    foreach (auto ptr, order.posExtendList) {
        PosExtendObject *posExtendObj=new PosExtendObject(*ptr,this);
        this->posExtendList.append(posExtendObj);
    }
    */

    return *this;
}


void OrderObject::FromJson(const QJsonObject &json)
{
    qDeleteAll(proList);
    proList.clear();

    qDeleteAll(coupList);
    coupList.clear();

    qDeleteAll(orderCoupList);
    orderCoupList.clear();

    qDeleteAll(paymentList);
    paymentList.clear();

    /*
    qDeleteAll(posExtendList);
    posExtendList.clear();
    */

    FetchDataFromJson(json);
    QJsonArray products = json[JSON_PRODUCTS].toArray();

     // 组装改价标识Code;使用 QTextStream 流操作;
    // 目前Pos-Key最长9位,Simphony-sku程序容量长度28(右对齐,左补空格),第14位存储 改价标识(从右至左第15位);
    QString tempChgPrcFlgCode;
    QTextStream ss( &tempChgPrcFlgCode );
    ss.setFieldWidth(14);
    ss.setFieldAlignment(QTextStream::AlignRight);
    ss.setPadChar(' ');

    foreach(QJsonValue product, products)
    {
        QJsonObject proJson = product.toObject();
        dishesObject *proObject = new dishesObject(this);
        //初始商品数据信息;
        proObject->qty = 0;
        proObject->price = 0;
        proObject->qtyNoSplit = 0;
        proObject->promotionQty = 0;
        proObject->promotionPrice = 0;
        proObject->starPromotionQty = 0;
        proObject->starPromotionPrice = 0;
        proObject->couponSeq=QString('0');
        proObject->changePriceFlag = 0;
        proObject->chgPrcFlgCode = QString("");
        proObject->FetchDataFromJson(proJson);
        // 商品数据赋值后，组装改价标识Code;便于传递给POS;
        ss << proObject->code;
        proObject->chgPrcFlgCode.append( QString::number(proObject->changePriceFlag) );
        proObject->chgPrcFlgCode.append( tempChgPrcFlgCode );
        // 清空缓存数据,防止重叠脏数据;
        tempChgPrcFlgCode = QString("");

        proList.append(proObject);
    }

    //解析WSG-券信息 List;
    QJsonArray coupons = json[JSON_COUPONS].toArray();
    foreach(QJsonValue coupon, coupons)
    {
        QJsonObject coupJson = coupon.toObject();
        CouponsObject *coupObject = new CouponsObject(this);
        coupObject->consumeDiscountAmount = 0;
        coupObject->consumeFactAmount = 0;
        coupObject->storeChannel = false;
        coupObject->couponName="";
        coupObject->couponType = "";
        coupObject->discount="";
        coupObject->couponSeq=QString('0');
        coupObject->isShowGiver=0;
        coupObject->FetchDataFromJson(coupJson);
        coupList.append(coupObject);
    }

    //解析 组合支付券信息 List;
    QJsonArray orderCoupons = json[JSON_EXCHANGECOUPONS].toArray();
    foreach(QJsonValue orderCoupon, orderCoupons)
    {
        QJsonObject orderCoupJson = orderCoupon.toObject();
        CouponsObject *orderCoupObject = new CouponsObject(this);
        orderCoupObject->consumeDiscountAmount = 0;
        orderCoupObject->consumeFactAmount = 0;
        orderCoupObject->storeChannel = false;
        orderCoupObject->couponName="";
        orderCoupObject->couponType = "";
        orderCoupObject->discount="";
        orderCoupObject->couponSeq=QString('0');
        orderCoupObject->isShowGiver=0;
        orderCoupObject->FetchDataFromJson(orderCoupJson);
        orderCoupList.append(orderCoupObject);
    }

    //解析多条支付方式信息 List;
    QJsonArray payments = json[JSON_PAYMENTS].toArray();
    foreach(QJsonValue payment, payments)
    {
        QJsonObject paymJson = payment.toObject();
        PaymentObject *paymObject = new PaymentObject(this);
        paymObject->paymentFmId="";
        paymObject->payMethodId="";
        paymObject->paymentMoney = 0;
        paymObject->FetchDataFromJson(paymJson);
        paymentList.append(paymObject);
    }

    /*
    //解析多条POS扩展属性 List;
    QJsonArray posExtends = json[JSON_POS_EXTEND_LIST].toArray();
    foreach(QJsonValue posExtend, posExtends)
    {
        QJsonObject posExtendJson = posExtend.toObject();
        PosExtendObject *posExtendObject = new PosExtendObject(this);
        posExtendObject->qty = 0;
        posExtendObject->FetchDataFromJson(posExtendJson);
        posExtendList.append(posExtendObject);
    }
    */
    QJsonArray posExtends = json[JSON_POS_EXTEND_LIST].toArray();
    if ( posExtends.isEmpty() ) {
        posExtendListString = QString("");
    } else {
        QJsonDocument tempDoc( posExtends );
        // 获取JSON文本表示形式的字符串
        QByteArray tempByteArray = tempDoc.toJson(QJsonDocument::Compact);
        posExtendListString = QString::fromUtf8( tempByteArray );
    }

    // 更新 QJsonObject Value;
    if (json.contains(JSON_PUSH_PLUGIN_OPTIONS) ) {
        pluginOptions = json[JSON_PUSH_PLUGIN_OPTIONS].toObject();
        if ( pluginOptions.contains(JSON_PRINT_OPT) )  {
            printOpt = pluginOptions[JSON_PRINT_OPT].toObject();
        }
    }

    // 转换并存储 星巴克支付方式相关信息;
    //星巴克App端支付方式: 1:支付宝; 2:微信; 3:银联; 4:SVC卡; 5/7:MOP使用券或星星抵扣 0元支付; 8:IStore订单; 9:银联支付(=3);
    sbkAppPayType = 0 ; sbkAppPayValue = ""; sbkAppRefundPayValue = "";
    switch (payWay) {
    case 1: {
        sbkAppPayType = 78;
        sbkAppPayValue = thirdPartyBatch;
        }
        break;
    case 2: {
        sbkAppPayType = 77;
        sbkAppPayValue = thirdPartyBatch;
        }
        break;
    case 4: {
        sbkAppPayType = 14;
        sbkAppPayValue = payfmId;
        if (6 == orderStatus) { sbkAppRefundPayValue = refundFmId; }
        }
        break;
    case 5: {
        sbkAppPayType = 1;
        sbkAppPayValue = "-1";
        }
        break;
    case 7: {
        sbkAppPayType = 1;
        sbkAppPayValue = "-1";
        }
        break;
    case 3:
        break;
    case 9:
        break;
    default:
        break;
    }

    return;
}


void OrderObject::FetchDataFromJson(const QJsonObject &json)
{
    QStringList keys = json.keys();
    foreach(QString key, keys)
    {
        setProperty(key.toUtf8(), json[key].toVariant());
    }
    // 针对Json中没有找到 key: totalPriceForPos; 则用 totalPrice 进行覆盖;
    if ( !keys.contains("totalPriceForPos") && keys.contains("totalPrice") )
        setProperty("totalPriceForPos", json["totalPrice"].toVariant());

    // printTask  是否创建打印杯贴任务   0 创建 1 不创建
    if ( keys.contains("printTask") )
        setProperty("blockPrintCupTicketTask", json["printTask"].toVariant());

    // remindTicketFlag 是否打印送餐小票 0 打印 1 不打印
    if ( keys.contains("remindTicketFlag") )
        setProperty("blockPrintRemindTicketTask", json["remindTicketFlag"].toVariant());

    return;
}

QString OrderObject::getChannelName()
{
    if ( "MOD" == channel ) {
        if ( 3 == orderType ) {
            return QString::fromLocal8Bit("星-客诉");
        }else if ( 7 == orderType ) {
            return QString::fromLocal8Bit("预-MOD-当日");
        }else if ( 11 == orderType ) {
            return QString::fromLocal8Bit("预-MOD-隔日");
        }else if ( 8 == orderType ) {
            return QString::fromLocal8Bit("拼-MOD");
        }else if ( 89 == orderType ) {
            return QString::fromLocal8Bit("权益卡");
        } else {
            if ( "4" ==  platformSource ) {
                return QString::fromLocal8Bit("微信-MOD");
            } else {
                return QString::fromLocal8Bit("星-MOD");
            }
        }

    } else if ( "MT-MOD" == channel ) {
        if ( 7 == orderType ) {
            return QString::fromLocal8Bit("预-美团-当日");
        } else if ( 11 == orderType ) {
            return QString::fromLocal8Bit("预-美团-隔日");
        } else if ( 89 == orderType ) {
            return QString::fromLocal8Bit("权益卡");
        } else {
            return QString::fromLocal8Bit("美团-MOD");
        }

    } else if ("WSG-MOD" == channel) { // 兼容渠道名称不统一问题
        if ( 1 == isGiftitForward ) {
            return QString::fromLocal8Bit("美好咖啡");
        } else {
            return QString::fromLocal8Bit("用星说");
        }

    } else if ("WSG-MOP" == channel) {
        if ( 1 == isGiftitForward ) {
            return QString::fromLocal8Bit("美好咖啡");
        } else {
            return QString::fromLocal8Bit("WSG-MOP");
        }

    } else if ("ELE" == channel) {
         if ( 7 == orderType ) {
             return QString::fromLocal8Bit("预-饿了么-当日");
         } else if ( 11 == orderType ) {
             return QString::fromLocal8Bit("预-饿了么-隔日");
         } else if ( 89 == orderType ) {
             return QString::fromLocal8Bit("权益卡");
         } else {
             return QString::fromLocal8Bit("饿了么");
         }

    } else if ( "MOP" == channel ) {
        if ( 4 == orderType ) {
            return QString::fromLocal8Bit("星-自测");
        } else if ( 7 == orderType ) {
            if ( "8" ==  platformSource ) {
                return QString::fromLocal8Bit("预-街送");
            }
            return QString::fromLocal8Bit("预-MOP-当日");
        } else if ( 11 == orderType ) {
            return QString::fromLocal8Bit("预-MOP-隔日");
        } else if ( 8 == orderType ) {
            return QString::fromLocal8Bit("拼-MOP");
        } else if ( 89 == orderType ) {
            return QString::fromLocal8Bit("权益卡");
        } else if (10 == orderType) {
            return QString::fromLocal8Bit("星时刻");
        } else {
            if ( "5" ==  platformSource ) {
                return QString::fromLocal8Bit("口碑-MOP");
            } else if ( "4" ==  platformSource ) {
                return QString::fromLocal8Bit("微信-MOP");
            } else {
                return QString::fromLocal8Bit("星-MOP");
            }
        }

    } else if ( "MT-MOP" == channel ) {
        if ( 7 == orderType ) {
            return QString::fromLocal8Bit("预-美团-当日");
        } else if ( 11 == orderType ) {
            return QString::fromLocal8Bit("预-美团-隔日");
        } else if ( 89 == orderType ) {
            return QString::fromLocal8Bit("权益卡");
        } else {
            return QString::fromLocal8Bit("美团-MOP");
        }

    } else if ( "EC-MOP" == channel || "EC_WECHAT_MOP" == channel ) {
        return QString::fromLocal8Bit("电商MOP");
    } else {
        return QString::fromLocal8Bit("未知");
    }

}


QString OrderObject::getOrderStatusDec()
{
    switch(orderStatus) {
        case 1:return QString::fromLocal8Bit("下单待支付");
        case 2:return QString::fromLocal8Bit("已支付");
        case 3:return QString::fromLocal8Bit("接单");
        case 4:return QString::fromLocal8Bit("配送中");
        case 5:return QString::fromLocal8Bit("完成");
        case 6:return QString::fromLocal8Bit("取消");
        default: return QString::fromLocal8Bit("未知");
    }
    return QString::fromLocal8Bit("未知");
}
