#include "packinfo.h"
#include "globalinfo.h"
#include <QDateTime>

int PackInfo::lastMin = 0;
int PackInfo::lastNum = 1;

QString getDecimalbit(double v, int i)
{
    QString tmp;
    switch(i)
    {
    case 0 :
       tmp.sprintf("%.0lf",v);
        break;
    case 1:
        tmp.sprintf("%.1lf",v);
         break;
    case 2:
        tmp.sprintf("%.2lf",v);
         break;
    case 3:
        tmp.sprintf("%.3lf",v);
         break;
    case 4:
        tmp.sprintf("%.4lf",v);
         break;
    case 5:
        tmp.sprintf("%.5lf",v);
         break;
    case 6:
        tmp.sprintf("%.6lf",v);
         break;
    case 7:
        tmp.sprintf("%.7lf",v);
         break;
    case 8:
        tmp.sprintf("%.8lf",v);
         break;
    default:
        tmp.sprintf("%.9lf",v);
         break;
    }
    return tmp;
}

PackInfo::PackInfo(const QSharedPointer<ProjectInfo> & info) : _info(info)
{
    _boxIndex = 0;
    code_single = buildPackId();
    if(!info.isNull())
        goods_id = info->goods_id;
    _time = QDateTime::currentSecsSinceEpoch();
}


BoxInfo::BoxInfo(QSharedPointer<PackInfo> pack,QString code):case_number(code),parent(pack)
{}


QJsonObject BoxInfo::toObject()
{
    QJsonObject ret;
    ret.insert("case_number",case_number);
    ret.insert("box_weight",box_weight);
    ret.insert("tube_number",tube_number);
    ret.insert("bucket_weight",bucket_weight);
    ret.insert("gross_weight",gross_weight);
    ret.insert("net_weight",net_weight);
    ret.insert("Level",Level);
    ret.insert("twist_type",twist_type);
    ret.insert("bucket_color",bucket_color);
    ret.insert("sort",sort);
    ret.insert("remark",remark);
    return ret;
}

QSharedPointer<BoxInfo> BoxInfo::fromObject(QSharedPointer<PackInfo> & pack,QJsonObject & obj)
{
    QString case_number = obj.value("case_number").toString();
    if(case_number.isEmpty()) return QSharedPointer<BoxInfo>();

    QSharedPointer<BoxInfo> ret(new BoxInfo(pack,""));
    ret->case_number = case_number;
    ret->box_weight = obj.value("box_weight").toInt();
    ret->tube_number = obj.value("tube_number").toInt();
    ret->bucket_weight = obj.value("bucket_weight").toInt();
    ret->gross_weight = obj.value("gross_weight").toInt();
    ret->net_weight = obj.value("net_weight").toInt();
    ret->Level = obj.value("Level").toString();
    ret->twist_type = obj.value("twist_type").toString();
    ret->bucket_color = obj.value("bucket_color").toString();
    ret->sort = obj.value("sort").toString();
    ret->remark = obj.value("remark").toString();
    ret->parent = pack;
    return ret;
}

QJsonObject PackInfo::toObject()
{
    QJsonObject ret;
    ret.insert("time",QString::number(this->time()));
    ret.insert("tube_number",QString::number(this->tube_number()));
    ret.insert("gross_weight",QString::number(this->gross_weight()));
    ret.insert("net_weight",QString::number(this->net_weight()));
    ret.insert("goods_id",QString::number(this->goodsID()));
    ret.insert("code_single",this->codeSingle());

    ret.insert("quantity",quantity);
    ret.insert("level_id",level_id);
    ret.insert("level",level);
    ret.insert("machine_no",machine_no);
    ret.insert("packing_time",packing_time);
    ret.insert("packing_type",packing_type);
    ret.insert("operator",operator_id);
    ret.insert("carton_type",carton_type);
    ret.insert("remark",remark);
    ret.insert("telephone",telephone);
    ret.insert("customer",customer);
    ret.insert("batch_no",batch_no);
    ret.insert("autoRuku",autoRuku);
    if(warehouse_id>0){
        ret.insert("warehouse_id",warehouse_id);
    }

    ret.insert("packType",packType);

    QJsonArray det;
    for(int i = 0;i<_boxes.size();i++){
        det.append(_boxes.at(i)->toObject());
    }
    ret.insert("details",det);

    return ret;
}

QSharedPointer<PackInfo> PackInfo::fromObject(QJsonObject &obj)
{
    QSharedPointer<PackInfo> ret;
    QString code_single = obj.value("code_single").toString();
    QString batch_no = obj.value("batch_no").toString();
    auto project = GlobalInfo::this_()->project(batch_no);
    if(code_single.isEmpty()) return ret;
    ret.reset(new PackInfo(project));
    ret->code_single = code_single;
    ret->_time = obj.value("time").toString().toLongLong();

    ret->quantity = obj.value("quantity").toInt();
    ret->level_id = obj.value("level_id").toInt();
    ret->level = obj.value("level").toString();
    ret->machine_no = obj.value("machine_no").toString();
    ret->packing_time = obj.value("packing_time").toString();
    ret->packing_type = obj.value("packing_type").toString();
    ret->operator_id = obj.value("operator").toInt();
    ret->carton_type = obj.value("carton_type").toString();
    ret->remark = obj.value("remark").toString();
    ret->telephone = obj.value("telephone").toString();
    ret->customer = obj.value("customer").toString();
    ret->batch_no = obj.value("batch_no").toString();
    ret->autoRuku = obj.value("autoRuku").toBool();
    ret->warehouse_id = obj.value("warehouse_id").toInt();

    ret->packType = obj.value("packType").toInt();
    QJsonArray det = obj.value("details").toArray();

    for(int i = 0;i<det.size();i++){
        QJsonObject obj = det.at(i).toObject();
        ret->_boxes.append(BoxInfo::fromObject(ret,obj));
    }

    return ret;
}

QJsonObject RuKuInfo::toObject()
{
    QJsonObject obj;
    obj.insert("warehouse_id",warehouse_id);
    obj.insert("way",way);
    obj.insert("product_details",product_details);
    obj.insert("remark",remark);
    return obj;
}

QString PackInfo::buildPackId()
{
    QDateTime now = QDateTime::currentDateTime();
    int min = now.time().minute();
    if(min != lastMin) lastNum = 1;
    else lastNum ++;
    QString packId = GlobalInfo::this_()->packNum() + now.toString("yyMMddhhmmss") + QString::asprintf("%03d",lastNum);
    if(lastNum > 999) return QString();
    return packId;
}

QString PackInfo::buildBoxId()
{
    _boxIndex ++;
     int nowNum = _boxIndex;
     QDateTime now = QDateTime::currentDateTime();
     QString id = now.toString("yyMMddhhmmss") + /*GlobalInfo::this_()->packNum() +*/ QString::asprintf("%03d",nowNum);
     return id;
}

int PackInfo::tube_number()
{
    int ret = 0;
    for(int i = 0; i < _boxes.size();++i){
        ret += _boxes.at(i)->tube_number;
    }
    return ret;
}

int PackInfo::gross_weight()
{
    int ret = 0;
    for(int i = 0; i < _boxes.size();++i){
        ret += _boxes.at(i)->gross_weight;
    }
    return ret;
}

int PackInfo::net_weight()
{
    int ret = 0;
    for(int i = 0; i < _boxes.size();++i){
        ret += _boxes.at(i)->net_weight;
    }
    return ret;
}

QSharedPointer<BoxInfo> PackInfo::addBoxInfo()
{
    QSharedPointer<BoxInfo> ret(new BoxInfo(sharedFromThis(),buildBoxId()));
    return ret;
}

void PackInfo::addBoxInfoList(QSharedPointer<BoxInfo> boxes)
{
    _boxes.append(boxes);
}

void PackInfo::delBoxInfoAt(int num)
{
    _boxes.removeAt(num);
}

QSharedPointer<BoxInfo> PackInfo::boxInfoAt(int num)
{
    return _boxes.at(num);
}

int PackInfo::boxInfoSize()
{
    return _boxes.size();
}
