-
Notifications
You must be signed in to change notification settings - Fork 2
/
appordersframe.cpp
339 lines (266 loc) · 9.13 KB
/
appordersframe.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "appordersframe.h"
#include "orderpainter.h"
#include <QDebug>
#include <QLabel>
#include <QMessageBox>
#include "TableviewEx/buttondelegate.h"
#include "datathread.h"
#include "mainwindow.h"
extern MainWindow* gWnd;
/////////////////////////////////////////////////////////
class OrderContact {
public:
OrderContact() {
_status = -1;
_oid = 0;
}
public:
QString _sid;
QString _contact;
QString _time;
QString _addr;
int _status;
int _oid;
};
class AppOrdersModel : public QAbstractTableModel {
QList<OrderContact> m_data;
public:
AppOrdersModel(QObject * parent = {}) : QAbstractTableModel{parent} {}
int rowCount(const QModelIndex &) const override { return m_data.count(); }
int columnCount(const QModelIndex &) const override { return 5; }
QVariant data(const QModelIndex &index, int role) const override {
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: {
QString type = tr("线上");
if(order._sid.startsWith("TC-")) {
type = tr("堂吃");
}
return type + ":" + order._sid.mid(3) + "\n" + order._addr;
}
case 2: return QObject::tr("预览");
case 3: return QObject::tr("接收");
case 4: return QObject::tr("拒绝");
default: return {};
}
}
QVariant headerData(int section, Qt::Orientation orientation, int role) const override {
if (orientation != Qt::Horizontal || role != Qt::DisplayRole) return {};
switch (section) {
case 0: return "#";
case 1: return QObject::tr("联系方式");
case 2: return "";
case 3: return "";
case 4: return "";
default: return {};
}
}
int append(const OrderContact & order) {
//判断是否重复
bool exist = false;
foreach(OrderContact oc, m_data ) {
if(oc._oid == order._oid) {
exist = true;
}
}
if(false == exist) {
beginInsertRows({}, m_data.count(), m_data.count());
m_data.append(order);
endInsertRows();
return m_data.length();
}
return 0;
}
void clean() {
beginResetModel();
m_data.erase(m_data.begin(), m_data.end());
endResetModel();
}
OrderContact order(const QModelIndex& index) {
if( m_data.length() > index.row() ) {
return m_data[index.row()];
}
return OrderContact();
}
void remove(const QModelIndex& index) {
if( m_data.length() > index.row() ) {
m_data.removeAt(index.row());
}
}
};
//////////////////////////////////////////////////////////////////
/// \brief AppOrdersDataThread::run
///
AppOrdersDataThread::AppOrdersDataThread(AppOrdersFrame* appFrm)
:_appFrm(appFrm),
_isRunning(false)
{
}
AppOrdersDataThread::~AppOrdersDataThread(){
_appFrm = nullptr;
}
void AppOrdersDataThread::run() {
_isRunning = true;
while(_isRunning) {
if( DataThread::inst().isLoggedIn() &&
_appFrm->getModel()->rowCount(QModelIndex()) < 5 &&
false == _appFrm->isHidden()) {
//需要加载数据
qDebug() << " load orders from cloud web server";
_appFrm->updateOrders();
}
msleep(5000);
}
qDebug() << " app orders data thread is end now";
// msleep(100);
}
//////////////////////////////////////////////////////////////////
/// \brief AppOrdersFrame::AppOrdersFrame
/// \param parent
///
AppOrdersFrame::AppOrdersFrame(QWidget *parent) :
QFrame(parent),
_dataThread(nullptr)
{
this->setStyleSheet("background-color: #ffffff");
_odTab = new QTableView(this);
_model = new AppOrdersModel(this);
_odTab->setSelectionMode(QAbstractItemView::SingleSelection);
_odTab->setEditTriggers(QAbstractItemView::NoEditTriggers);
// _odTab->setSelectionBehavior(QAbstractItemView::SelectRows);
_odTab->setModel(_model);
init();
}
AppOrdersFrame::~AppOrdersFrame()
{
_dataThread->stop();
if(nullptr != _model) {
delete _model;
_model = nullptr;
}
}
void AppOrdersFrame::init() {
qDebug() << "AppOrdersFrame::init";
if(nullptr == _dataThread) {
_dataThread = new AppOrdersDataThread(this);
_dataThread->start();
}
qDebug() << "AppOrdersFrame::init end";
retranslateUI();
}
void AppOrdersFrame::retranslateUI() {
ButtonDelegate* _view = new ButtonDelegate(tr("预览"), dynamic_cast<QFrame*>(this));
ButtonDelegate* _acc = new ButtonDelegate(tr("接收"), dynamic_cast<QFrame*>(this));
ButtonDelegate* _rej = new ButtonDelegate(tr("拒绝"), dynamic_cast<QFrame*>(this));
this->connect(_view, SIGNAL(onClicked(const QModelIndex&)), this, SLOT(onClicked(const QModelIndex&)));
this->connect(_acc, SIGNAL(onClicked(const QModelIndex&)), this, SLOT(onClicked(const QModelIndex&)));
this->connect(_rej, SIGNAL(onClicked(const QModelIndex&)), this, SLOT(onClicked(const QModelIndex&)));
_odTab->setItemDelegateForColumn(2, _view);
_odTab->setItemDelegateForColumn(3, _acc);
_odTab->setItemDelegateForColumn(4, _rej);
// this->connect(_odTab, SIGNAL(clicked(const QModelIndex &)), this, SLOT(onClicked(const QModelIndex &)));
}
void AppOrdersFrame::resizeEvent(QResizeEvent* e) {
QFrame::resizeEvent(e);
QRect rect = this->geometry();
//不会多次调用,内存消耗不大
POSLabelEx* label = new POSLabelEx( QObject::tr("自助点餐申请"), this);
label->setGeometry(0, 0, rect.width(), 30);
_odTab->setGeometry(0, 30, rect.width(), rect.height() - 30);
this->_odTab->setColumnWidth(0, 20);
this->_odTab->setColumnWidth(2, 80);
this->_odTab->setColumnWidth(3, 80);
this->_odTab->setColumnWidth(4, 80);
int w = 20 + 80 * 3;
this->_odTab->setColumnWidth(1, _odTab->geometry().width() - w - 50);
}
void AppOrdersFrame::showEvent(QShowEvent* e) {
QFrame::showEvent(e);
}
void AppOrdersFrame::onClicked(const QModelIndex &index) {
qDebug() << " clicked : column: " << index.column() << ", row: " << index.row();
OrderContact oc = _model->order(index);
if(-1 == oc._status) {
return;
}
//追加到本地数据库
QVariantMap order = DataThread::inst().getOrderbyOidOnline(oc._oid); //loadOrderByOId(oc._oid, DataThread::inst().cloudServer());
if(order.empty()) {
QMessageBox::warning(this, "出错", "加载云端订单失败");
return;
}
//存入本地数据库
OrderBean ob(order);
ob.status = POSDATA::OD_RECVREQ;
int code = -1;
if(index.column() == _model->columnCount(index) - 2) //access
{
code = DataThread::inst().auditOrder(oc._oid, POSDATA::OD_ACCESS, DataThread::inst().cloudServer());
} else if(index.column() == _model->columnCount(index) - 1 ){ // reject
code = DataThread::inst().auditOrder(oc._oid, POSDATA::OD_REJECTOD, DataThread::inst().cloudServer());
} else { //view
//预览
gWnd->openMenuFrame(ob);
return;
}
if(code != 0) {
QMessageBox::warning(this, "出错", "提交操作失败, 请稍后重试");
return;
}
if(index.column() == _model->columnCount(index) - 2) //access
{
ob.status = POSDATA::OD_SUBMIT;
ob.total = 0.0;
for(int i = 0; i < ob.foods.length(); i++) {
ob.foods[i].reid = -1;
ob.total += ob.foods[i].unit_price * ob.foods[i].count;
}
ob.amount = ob.total * (1 + ob.taxRate);
order = DataThread::inst().newOrder(ob.toMap());
if(order.empty()) {
QMessageBox::warning(this, "出错", "订单存入本地失败,联系客服");
return;
}
try {
//打印订单
OrderPainter::Print(ob, 1, DataThread::inst().getCashierPrinter());
} catch (...) {
QMessageBox::warning(this, "出错", "打印订单失败,请检查打印机状态或打印机配置");
}
}
if( index.column() >= _model->columnCount(index) - 2) {
//清空本view
_model->remove(index);
_model->layoutChanged();
}
}
void AppOrdersFrame::updateOrders() {
QList<QVariant> orders = DataThread::inst().loadOnlineOrders();
if(orders.empty()) {
return;
}
foreach (QVariant order, orders) {
QVariantMap item = order.toMap();
OrderContact oc;
oc._oid = item["id"].toInt();
oc._sid = item["sid"].toString();
oc._time = item["time"].toString();
oc._status = item["status"].toInt();
oc._addr = item["addr"].toString();
_model->append(oc);
}
int rows = _model->rowCount(QModelIndex());
for (int i = 0; i < rows; i++) {
_odTab->setRowHeight(i, 50);
}
_odTab->updateGeometry();
_odTab->showMaximized();
_odTab->update();
}
void AppOrdersFrame::rowSelected(const QModelIndex &index)
{
// index.row()
// QMessageBox::warning(nullptr, "tr", QString::number(index.column()));
}