-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModelProduct.cpp
194 lines (174 loc) · 5.78 KB
/
ModelProduct.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
#include "ModelProduct.h"
#include <QDebug>
ModelProduct::ModelProduct(QObject *parent)
: QAbstractTableModel(parent)
{
}
QVariant ModelProduct::headerData(int section, Qt::Orientation orientation, int role) const
{
if(role == Qt::TextAlignmentRole){
return Qt::AlignCenter;
}
if (role != Qt::DisplayRole)
return QVariant();
if(orientation == Qt::Horizontal){
QString title;
switch(section){
case ColumnHeaderName:
title = u8"商品名";
break;
case ColumnHeaderCategory:
title = u8"类别";
break;
case ColumnHeaderBusiness:
title = u8"商家";
break;
case ColumnHeaderCost:
title = u8"进货价(元)";
break;
case ColumnHeaderSale:
title = u8"销售价(元)";
break;
case ColumnHeaderStockCount:
title = u8"库存数量";
break;
case ColumnHeaderRemark:
title = u8"备注";
break;
}
return title;
}
return QString();
}
int ModelProduct::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return (int)products_.size();
}
int ModelProduct::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return ColumnHeaderCount;
}
QVariant ModelProduct::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if(role == Qt::TextAlignmentRole){
return Qt::AlignCenter;
}
if(role != Qt::DisplayRole){
return QVariant();
}
QVariant ret;
auto& product = products_[index.row()];
switch (index.column()) {
case ColumnHeaderName:
ret = product->name;
break;
case ColumnHeaderCategory:
if(product->category){
ret = product->category->name;
}
break;
case ColumnHeaderBusiness:
if(product->business){
ret = product->business->name;
}
break;
case ColumnHeaderCost:
ret = product->cost_price;
break;
case ColumnHeaderStockCount:
ret = product->stock_count;
break;
case ColumnHeaderSale:
ret = product->sale_price;
break;
case ColumnHeaderRemark:
ret = product->remark;
break;
default:
break;
}
return ret;
}
void ModelProduct::sort(int column, Qt::SortOrder order)
{
beginResetModel();
switch (column) {
case ColumnHeaderName:
std::sort(products_.begin(),products_.end(),[order](const ProductPointer& item1,const ProductPointer& item2)->bool{
return order == Qt::AscendingOrder ? (item1->name < item2->name) : (item1->name > item2->name);
});
break;
case ColumnHeaderCategory:
std::sort(products_.begin(),products_.end(),[order](const ProductPointer& item1,const ProductPointer& item2)->bool{
return order == Qt::AscendingOrder ? (item1->category->name < item2->category->name) : (item1->category->name > item2->category->name);
});
break;
case ColumnHeaderBusiness:
std::sort(products_.begin(),products_.end(),[order](const ProductPointer& item1,const ProductPointer& item2)->bool{
return order == Qt::AscendingOrder ? (item1->business->name < item2->business->name) : (item1->business->name > item2->business->name);
});
break;
case ColumnHeaderCost:
std::sort(products_.begin(),products_.end(),[order](const ProductPointer& item1,const ProductPointer& item2)->bool{
return order == Qt::AscendingOrder ? (item1->cost_price < item2->cost_price) : (item1->cost_price > item2->cost_price);
});
break;
case ColumnHeaderSale:
std::sort(products_.begin(),products_.end(),[order](const ProductPointer& item1,const ProductPointer& item2)->bool{
return order == Qt::AscendingOrder ? (item1->sale_price < item2->sale_price) : (item1->sale_price > item2->sale_price);
});
break;
case ColumnHeaderStockCount:
std::sort(products_.begin(),products_.end(),[order](const ProductPointer& item1,const ProductPointer& item2)->bool{
return order == Qt::AscendingOrder ? (item1->stock_count < item2->stock_count) : (item1->stock_count > item2->stock_count);
});
break;
case ColumnHeaderRemark:
std::sort(products_.begin(),products_.end(),[order](const ProductPointer& item1,const ProductPointer& item2)->bool{
return order == Qt::AscendingOrder ? (item1->remark < item2->remark) : (item1->remark > item2->remark);
});
break;
default:
break;
}
endResetModel();
}
ProductPointer ModelProduct::GetProduct(const QModelIndex &index)
{
return products_[index.row()];
}
void ModelProduct::SetProducts(std::vector<ProductPointer>& products)
{
beginResetModel();
products_ = products;
endResetModel();
}
void ModelProduct::RefreshItem(const QModelIndex &index,ProductPointer& item)
{
products_[index.row()] = item;
}
void ModelProduct::DeleteItem(const QModelIndex &index)
{
beginRemoveRows(index.parent(),index.row(),index.row());
products_.erase(products_.begin()+index.row());
endRemoveRows();
}
std::pair<double,double> ModelProduct::GetTotalCost()
{
std::pair<double,double> result;
//总成本
result.first = std::accumulate(products_.begin(),products_.end(),0,[](double sum,ProductPointer& product)->double{
return sum + product->cost_price * product->stock_count;
});
//总费用
result.second = std::accumulate(products_.begin(),products_.end(),0,[](double sum,ProductPointer& product)->double{
return sum + product->sale_price * product->stock_count;
});
return result;
}