-
Notifications
You must be signed in to change notification settings - Fork 2
/
OrderData.cpp
287 lines (240 loc) · 8.76 KB
/
OrderData.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "OrderData.h"
#include <QJsonDocument>
#include "datathread.h"
QString OrderType::TC = "TC";
QString OrderType::A = "A*";
QString OrderType::Q = "Q*";
QString OrderType::C = "C*";
QString OrderType::D = "*D";
QString OrderType::P = "*P";
QString OrderType::ALL = "AL";
/////////////////////////////////
/// \brief FoodBean::FoodBean
///
FoodBean::FoodBean() {
this->id = 0;
this->reid = -1;
this->oid = 0;
this->count = 0;
this->unit_price = 0.0;
this->discount = 0.0;
this->cid = 0;
this->rid = 0;
this->icon = "";
}
FoodBean::~FoodBean() {
}
QVariantMap FoodBean::toMap() {
QVariantMap map;
map.insert("oid", this->oid);
map.insert("fid", this->id);
map.insert("count", this->count);
map.insert("unit_price", this->unit_price);
map.insert("discount", this->discount);
map.insert("cid", this->cid);
map.insert("rid", this->rid);
map.insert("food_name", this->name);
map.insert("flavor", this->flavor);
return map;
}
OrderBean::OrderBean() {
this->oid = 0;
this->sid = "";
this->time = "";
this->amount = 0;
this->orderid = "";
this->status = 0;
this->gratuity = 0.0;
this->discount = 0.0;
this->payType = 0;
this->isSubOrder = false;
this->parentOid = 0;
this->creditType = "";
this->addr = "";
this->mark = "";
this->name = DataThread::inst().getRestaurantInfo().value("name").toString();
this->taxRate = DataThread::inst().getRestaurantInfo().value("taxRate").toDouble();
}
OrderBean::OrderBean(int oid,
const QString& sid,
const QString& time,
double amount,
const QString& orderid,
int status ) {
this->oid = oid;
this->sid = sid;
this->time = time;
this->amount = amount;
this->orderid = orderid;
this->status = status;
this->gratuity = 0.0;
this->discount = 0.0;
this->payType = 0;
this->isSubOrder = false;
this->parentOid = 0;
this->creditType = "";
this->addr = "";
this->mark = "";
this->name = DataThread::inst().getRestaurantInfo().value("name").toString();
this->taxRate = DataThread::inst().getRestaurantInfo().value("taxRate").toDouble();
}
OrderBean::OrderBean(const QVariantMap& order) {
if(0 == order.count("id") ) throw QString("not found id");
if(0 == order.count("sid") ) throw QString("not found sid");
if(0 == order.count("time") ) throw QString("not found time");
if(0 == order.count("orderid") ) throw QString("not found orderId");
this->oid = order["id"].toInt();
this->sid = order["sid"].toString();
this->time = order["time"].toString();
this->orderid = order["orderId"].toString();
this->status = order["status"].toInt();
this->gratuity = order["gratuity"].toDouble(); //0 == order.count("gratuity") ? 0 : order["gratuity"].toDouble();
this->name = DataThread::inst().getRestaurantInfo().value("name").toString();
this->taxRate = DataThread::inst().getRestaurantInfo().value("taxRate").toDouble();
this->payType = order["payType"].toInt();
this->isSubOrder = false;
this->parentOid = 0;
this->creditType = order["creditType"].toString();
this->mark = order["mark"].toString();
this->addr = "";
if(order.count("addr") > 0) {
this->addr = order["addr"].toString();
}
this->total = 0;
this->amount = 0.0;
this->discount = order["discount"].toDouble();
// this->total = order["totalPrice"].toDouble();
// this->amount = order["amount"].toDouble();
this->cookingTime = order["cookingTime"].toString();
this->finishTime = order["finishTime"].toString();
foreach (QVariant food, order["foods"].toList()) {
FoodBean fb;
QVariantMap fooditem = food.toMap();
fb.reid = fooditem["reid"].toInt();
fb.count = fooditem["count"].toInt();
fb.name = fooditem["name"].toString();
// fb.enName = fooditem["enName"].toString();
fb.unit_price = fooditem["unit_price"].toDouble();
// gb.flavor = fooditem["flavor"].toString().toStdString();
fb.flavor = QByteArray::fromPercentEncoding(fooditem["flavor"].toString().toUtf8());
fb.cid = fooditem["cid"].toInt();
fb.rid = fooditem["rid"].toInt();
fb.discount = fooditem["discount"].toDouble();
fb.id = fooditem["fid"].toInt();
fb.oid = fooditem["oid"].toInt();
fb.icon = fooditem["icon"].toString();
this->total += fb.count * fb.unit_price * (1 - fb.discount);
qDebug() << "flavor: " << fb.flavor;
this->foods.append(fb);
}
foreach( QVariant suboid, order["subOids"].toList()) {
QVariantMap sub = suboid.toMap();
this->subOids.append( sub["id"].toInt());
}
// this->amount = this->total * (1 - this->discount) + this->total * this->taxRate + this->gratuity;
}
OrderBean::~OrderBean() {
}
QJsonDocument OrderBean::toJsonDoc() {
return QJsonDocument::fromVariant(toMap());
}
QVariantMap OrderBean::toMap() {
QVariantMap map;
map.insert("oid", this->oid);
map.insert("sid", this->sid);
map.insert("time", this->time);
map.insert("orderid", this->orderid);
map.insert("status", this->status);
map.insert("gratuity", this->gratuity);
map.insert("totalPrice", this->total);
map.insert("discount", this->discount);
map.insert("taxRate", this->taxRate);
map.insert("amount", this->amount);
map.insert("payType", this->payType);
map.insert("creditType", this->creditType);
map.insert("isSubOrder", this->isSubOrder);
map.insert("parentOid", this->parentOid);
map.insert("addr", this->addr);
map.insert("cookingTime", this->cookingTime);
map.insert("finishTime", this->finishTime);
map.insert("mark", this->mark);
map.insert("foods", QVariantList());
QVariantList _l;
foreach (FoodBean food, this->foods) {
QVariantMap _m;
_m.insert("name", food.name);
_m.insert("count", food.count);
_m.insert("price", food.unit_price);
_m.insert("flavor", food.flavor.toUtf8().toPercentEncoding());
_m.insert("id", food.id);
_m.insert("reid", food.reid);
_m.insert("rid" , food.rid);
_m.insert("cid", food.cid);
_m.insert("discount", food.discount);
_l.push_back(_m);
}
map.insert("foods", _l);
return map;
}
////////////////////////////////////////////////////////
/// \brief OrdersModel::orderStatus
/// \param status
/// \return
///
QString OrdersModel::orderStatus(int status) const {
switch( status ) {
case POSDATA::OD_APPSUMIT: return QObject::tr("排队");
case POSDATA::OD_RECVREQ: return QObject::tr("网络下单中");
case POSDATA::OD_ACCESS: return QObject::tr("本地下单中");
case POSDATA::OD_SUBMIT: return QObject::tr("已下单");
case POSDATA::OD_COOKING: return QObject::tr("已下厨");
case POSDATA::OD_REJECTOD: return QObject::tr("已取消");
case POSDATA::OD_BOOKING_SHARE: return QObject::tr("已预约,未占位");
case POSDATA::OD_BOOKING_TAKEUP: return QObject::tr("已预约,已占位");
case POSDATA::OD_ODPAIED: return QObject::tr("已结算");
case POSDATA::OD_FINISHORDER: return QObject::tr("完成");
default: {
qDebug() << "订单未知状态";
return QObject::tr("未知状态");
}
}
}
OrdersModel::OrdersModel(QObject * parent) : QAbstractTableModel{parent} {}
int OrdersModel::rowCount(const QModelIndex &) const { return m_data.count(); }
int OrdersModel::columnCount(const QModelIndex &) const { return 5; }
QVariant OrdersModel::data(const QModelIndex &index, int role) const {
if (role != Qt::DisplayRole && role != Qt::EditRole) return {};
const auto & order = m_data[index.row()];
switch (index.column()) {
case 0: return index.row();
case 1: return order.sid;
case 2: return order.time;
case 3: return "$" + QString::number(order.amount);
case 4: return orderStatus(order.status);
default: return {};
}
}
QVariant OrdersModel::headerData(int section, Qt::Orientation orientation, int role) const {
if (orientation != Qt::Horizontal || role != Qt::DisplayRole) return {};
switch (section) {
case 0: return "#";
case 1: return QObject::tr("订单号");
case 2: return QObject::tr("下单时间");
case 3: return QObject::tr("总消费");
case 4: return QObject::tr("状态");
default: return {};
}
}
void OrdersModel::append(const OrderBean & order) {
beginInsertRows({}, m_data.count(), m_data.count());
m_data.append(order);
endInsertRows();
}
void OrdersModel::clean() {
beginResetModel();
m_data.erase(m_data.begin(), m_data.end());
endResetModel();
}
OrderBean OrdersModel::order(const QModelIndex& index) {
return m_data[index.row()];
}