#include "couponsObject.h"

#include "preDefine.h"
#include <QJsonArray>

CouponsObject::CouponsObject(const CouponsObject &coupons, QObject *parent):QObject(parent)
{
    qDeleteAll(skus);
    skus.clear();

    qDeleteAll(itemsDiscount);
    itemsDiscount.clear();

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

    foreach (auto ptr, coupons.skus) {
        SingleProductObject *singleProduct=new SingleProductObject(*ptr,this);
        this->skus.append(singleProduct);
    }

    foreach (auto ptr, coupons.itemsDiscount) {
        PromotionItemObject *promotionItem=new PromotionItemObject(*ptr,this);
        this->itemsDiscount.append(promotionItem);
    }

}

CouponsObject& CouponsObject::operator =(const CouponsObject& coupons)
{
    qDeleteAll(skus);
    skus.clear();

    qDeleteAll(itemsDiscount);
    itemsDiscount.clear();

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

    foreach (auto ptr, coupons.skus) {
        SingleProductObject *singleProduct=new SingleProductObject(*ptr,this);
        this->skus.append(singleProduct);
    }

    foreach (auto ptr, coupons.itemsDiscount) {
        PromotionItemObject *promotionItem=new PromotionItemObject(*ptr,this);
        this->itemsDiscount.append(promotionItem);
    }

    return *this;
}

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

    qDeleteAll(skus);
    skus.clear();

    qDeleteAll(itemsDiscount);
    itemsDiscount.clear();

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

    QJsonArray singleProductsArray = json[JSON_COUPONPRODUCTS].toArray();
    foreach(QJsonValue singleProduct, singleProductsArray)
    {
        SingleProductObject *item = new SingleProductObject(this);
        //初始商品配料数据信息;
        item->isCoupon=false;
        item->code="";
        item->qty=0;
        item->price=0;
        item->changePriceFlag = 0;
        item->chgPrcFlgCode = QString("");
        item->FetchDataFromJson(singleProduct.toObject());
        // 券数据赋值后, 组装改价标识Code;便于传递给POS;
        ss << item->code;
        item->chgPrcFlgCode.append( QString::number(item->changePriceFlag) );
        item->chgPrcFlgCode.append( tempChgPrcFlgCode );
        // 清空缓存数据,防止重叠脏数据;
        tempChgPrcFlgCode = QString("");
        this->skus.append(item);
    }

    QJsonArray itemsDiscountArray = json[JSON_ITEMSDISCOUNT].toArray();
    foreach(QJsonValue itemDiscount, itemsDiscountArray)
    {
        PromotionItemObject *item = new PromotionItemObject(this);
        //初始商品配料数据信息;
        item->promotionSku="";
        item->promotionName="";
        item->promotionQty=0;
        item->discountFee=0;
        item->type=0;
        item->FetchDataFromJson(itemDiscount.toObject());
        itemsDiscount.append(item);
    }

    return;
}
