Order.php
6.8 KB
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
<?php
namespace addons\shopro\model;
use think\Model;
use addons\shopro\exception\Exception;
use addons\shopro\library\traits\model\order\OrderScope;
use think\Db;
use think\Queue;
use traits\model\SoftDelete;
use addons\shopro\library\traits\model\order\OrderOper;
use addons\shopro\library\traits\model\order\OrderStatus;
/**
* 订单模型
*/
class Order extends Model
{
use SoftDelete, OrderOper, OrderScope, OrderStatus;
// 表名,不含前缀
protected $name = 'shopro_order';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = 'deletetime';
protected $hidden = ['updatetime', 'deletetime'];
// //列表动态隐藏字段
// protected static $list_hidden = ['content', 'params', 'images', 'service_ids'];
// // 追加属性
protected $append = [
'status_code',
'status_name',
'status_desc',
'btns',
'ext_arr'
];
// 订单状态
const STATUS_INVALID = -2;
const STATUS_CANCEL = -1;
const STATUS_NOPAY = 0;
const STATUS_PAYED = 1;
const STATUS_FINISH = 2;
/* -------------------------- 访问器 ------------------------ */
public function getExtArrAttr($value, $data)
{
return (isset($data['ext']) && $data['ext']) ? json_decode($data['ext'], true) : [];
}
protected function getStatus($data, $type)
{
$btns = [];
$status_name = '';
$status_desc = '';
switch ($this->status_code) {
case 'cancel':
$status_name = '已取消';
$status_desc = '订单已取消';
$btns[] = 'delete'; // 删除订单
break;
case 'invalid':
$status_name = '交易关闭';
$status_desc = '交易关闭';
$btns[] = 'delete'; // 删除订单
break;
case 'nopay':
$status_name = '待付款';
$status_desc = '等待买家付款';
$btns[] = 'cancel'; // 取消订单
$btns[] = 'pay'; // 支付
break;
// 已支付的
case 'commented':
$status_name = '已评价';
$status_desc = '订单已评价';
$dispatchType = $this->getItemDispatchType($this->item);
if (in_array('express', $dispatchType)) {
$btns[] = 'express'; // 查看物流
}
break;
case 'nocomment':
$status_name = '待评价';
$status_desc = '等待买家评价';
$dispatchType = $this->getItemDispatchType($this->item);
if (in_array('express', $dispatchType)) {
$btns[] = 'express'; // 查看物流
}
break;
case 'noget':
$dispatchType = $this->getItemDispatchType($this->item);
$status_name = '待收货';
$status_desc = '等待买家收货';
if (in_array('express', $dispatchType)) {
$btns[] = 'express'; // 查看物流
} else {
if (count($dispatchType) == 1) {
// item 只有一种发货方式
$dispatch_type = $dispatchType[0] ?? '';
switch($dispatch_type) {
case 'selfetch':
$status_name = '待提货/到店';
$status_desc = '等待买家提货/到店';
break;
case 'store':
$status_name = '待取货';
$status_desc = '卖家上门配送中';
break;
}
}
}
break;
case 'nosend':
$dispatchType = $this->getItemDispatchType($this->item);
$status_name = '待发货';
$status_desc = '等待卖家发货';
if (in_array('store', $dispatchType)) {
$status_name = '待配送';
$status_desc = '等待卖家上门配送';
} else if (in_array('selfetch', $dispatchType)) {
$status_name = '待备货';
$status_desc = '等待卖家备货';
}
break;
case 'refund_finish':
$status_name = '退款完成';
$status_desc = '订单退款完成';
break;
case 'refund_ing':
$status_name = '退款处理中';
$status_desc = '退款处理中';
break;
case 'groupon_ing':
$status_name = '等待成团';
$status_desc = '等待拼团成功';
break;
case 'groupon_invalid':
$status_name = '拼团失败';
$status_desc = '拼团失败';
break;
// 已支付的结束
case 'finish':
$status_name = '交易完成';
$status_desc = '交易完成';
$btns[] = 'delete'; // 删除订单
break;
}
$ext_arr = json_decode($data['ext'], true);
// 是拼团订单
if (
strpos($data['activity_type'], 'groupon') !== false &&
isset($ext_arr['groupon_id']) && $ext_arr['groupon_id']
) {
$btns[] = 'groupon'; // 拼团详情
}
return $type == 'status_name' ? $status_name : ($type == 'btns' ? $btns : $status_desc);
}
private function getItemDispatchType($item = []) {
$dispatchType = [];
foreach ($this->item as $key => $item) {
// 获取 item status
$dispatchType[] = $item['dispatch_type'];
}
$dispatchType = array_unique(array_filter($dispatchType)); // 过滤重复,过滤空值
return $dispatchType;
}
/* -------------------------- 访问器 ------------------------ */
/* -------------------------- 模型关联 ------------------------ */
public function item()
{
return $this->hasMany(\addons\shopro\model\OrderItem::class, 'order_id', 'id');
}
// 拼团只有一个商品,可以使用这个
public function firstItem()
{
return $this->hasOne(\addons\shopro\model\OrderItem::class, 'order_id', 'id');
}
/* -------------------------- 模型关联 ------------------------ */
}