#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(paymentList);
    paymentList.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.paymentList) {
        PaymentObject *payment=new PaymentObject(*ptr,this);
        this->paymentList.append(payment);
    }
}

OrderObject& OrderObject::operator =(const OrderObject& order)
{
    qDeleteAll(proList);
    proList.clear();
    qDeleteAll(coupList);
    coupList.clear();
    qDeleteAll(paymentList);
    paymentList.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.paymentList) {
        PaymentObject *payment=new PaymentObject(*ptr,this);
        this->paymentList.append(payment);
    }
    return *this;
}

void OrderObject::FromJson(const QJsonObject &json)
{
    qDeleteAll(proList);
    proList.clear();
    qDeleteAll(coupList);
    coupList.clear();
    qDeleteAll(paymentList);
    paymentList.clear();
    FetchDataFromJson(json);
    QJsonArray products = json[JSON_PRODUCTS].toArray();
    foreach(QJsonValue product, products)
    {
        QJsonObject proJson = product.toObject();
        dishesObject *proObject = new dishesObject(this);
        //初始商品数据信息;
        proObject->promotionQty = 0;
        proObject->promotionPrice = 0;
        proObject->starPromotionQty = 0;
        proObject->starPromotionPrice = 0;
        proObject->couponSeq=QString('0');
        proObject->FetchDataFromJson(proJson);
        proList.append(proObject);
    }
    //解析预约实物-券信息 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->couponSeq=QString('0');
        coupObject->FetchDataFromJson(coupJson);
        coupList.append(coupObject);
    }

    //解析多条支付方式信息 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);
    }
    return;
}

void OrderObject::FetchDataFromJson(const QJsonObject &json)
{
    QStringList keys = json.keys();
    foreach(QString key, keys)
    {
        setProperty(key.toUtf8(), json[key].toVariant());
    }
    return;
}

QString OrderObject::getChannelName()
{
    QLOG_INFO() << QString("[<<<<---OrderObject:getChannelName [%1]--->>>>]").arg(channel);
    if ("MOD" == channel && 3 != orderType){
        if ( "4" ==  platformSource ) {
            return QString::fromLocal8Bit("微信-MOD");
        } else {
            return QString::fromLocal8Bit("星-MOD");
        }
    }else if("MOD" == channel && 3 == orderType){
        return QString::fromLocal8Bit("星-客诉");

    }else if("ELE" == channel){
        return QString::fromLocal8Bit("饿了么");

    }else if("BAIDU" == channel){
        return QString::fromLocal8Bit("百度外卖");

    }else if("MOP" == channel && 4 != orderType){
        if ( "5" ==  platformSource ) {
            return QString::fromLocal8Bit("口碑-MOP");
        } else {
            return QString::fromLocal8Bit("星-MOP");
        }

    }else if("MOP" == channel && 4 == orderType){
        return QString::fromLocal8Bit("星-自测");

    }else if("ORS" == channel){
        return QString::fromLocal8Bit("星-预约");

    }else if("STC" == channel){
        return QString::fromLocal8Bit("SmartCase");

    }else{
        if("HM" == channel) {
            return QString::fromLocal8Bit("盒马");
        }

        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("未知");
}
