StoreExpress.php
4.7 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
<?php
namespace addons\groupon\model\store;
use think\Model;
use traits\model\SoftDelete;
use addons\groupon\model\Store;
use addons\groupon\model\User;
use addons\groupon\model\OrderItem;
use addons\groupon\exception\Exception;
class StoreExpress extends Model
{
// 表名
protected $name = 'groupon_order_store_express';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
// 发货状态
const DISPATCH_STATUS_NOSEND = 0; // 未发货
const DISPATCH_STATUS_SENDED = 1; // 已发货
const DISPATCH_STATUS_GETED = 2; // 已收货
// 追加属性
protected $append = [
'dispatch_status_name'
];
public static function getList($params) {
extract($params);
$user = User::info();
$store = Store::info();
$express = self::with('itemFirst')->where('store_id', $store['id'])->order('id', 'desc')->paginate($per_page ?? 10); // 未填写物流信息的包裹也会出现在包裹列表
return $express;
}
public static function getDetail($params) {
extract($params);
$user = User::info();
$store = Store::info();
$express = self::with(['log', 'items'])->where('store_id', $store['id'])->where('id', $id)->find(); // 未填写物流信息的包裹也可以查看
if (!$express) {
new Exception('未找到包裹');
}
$express = $express->toArray();
$items = $express['items'];
$newItems = [];
foreach ($items as $item) {
$goodsKey = $item['goods_id'] . '-' . $item['goods_sku_price_id'];
if (isset($newItems[$goodsKey])) {
$newItems[$goodsKey]['goods_num'] = $newItems[$goodsKey]['goods_num'] + $item['goods_num'];
} else {
$newItems[$goodsKey] = $item;
}
}
$express['items'] = array_values($newItems);
return $express;
}
/**
* 订单到货
*/
public static function operArrive($params)
{
$user = User::info();
$store = Store::info();
// 包裹里面的商品全部到货
\think\Db::transaction(function () use ($user, $store, $params) {
extract($params);
$express = self::where('store_id', $store['id'])->where('express_no', 'not null')->where('id', $id)->lock(true)->find();
if (!$express) {
new Exception('未找到包裹');
}
// 包裹中订单
$orders = Order::payed()->where('store_id', $store['id'])->with(['item' => function ($query) use ($express) {
return $query->where('store_express_id', $express['id'])
->where('dispatch_status', \addons\groupon\model\OrderItem::DISPATCH_STATUS_READY)
->where('refund_status', 'not in', [OrderItem::REFUND_STATUS_OK, OrderItem::REFUND_STATUS_FINISH]);
}])->whereExists(function ($query) use ($express) {
$itemTableName = (new OrderItem())->getQuery()->getTable();
$tableName = (new Order())->getQuery()->getTable();
return $query->table($itemTableName)
->where($itemTableName . '.order_id=' . $tableName . '.id')
->where('store_express_id', $express['id'])
->where('dispatch_status', \addons\groupon\model\OrderItem::DISPATCH_STATUS_READY)
->where('refund_status', 'not in', [OrderItem::REFUND_STATUS_OK, OrderItem::REFUND_STATUS_FINISH]);
})->select();
// 将包裹确认收货
$express->dispatch_status = self::DISPATCH_STATUS_GETED;
$express->save();
// 将订单标记为已到货
(new Order)->storeArrive($express, $orders);
});
}
public function getDispatchStatusList()
{
return ['0' => '未发货', '1' => '已发货', '2' => '已收货'];
}
public function getDispatchStatusNameAttr($value, $data)
{
$value = $value ? $value : (isset($data['dispatch_status']) ? $data['dispatch_status'] : '');
$list = $this->getDispatchStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
public function itemFirst() {
return $this->hasOne(OrderItem::class, 'store_express_id');
}
public function items() {
return $this->hasMany(OrderItem::class, 'store_express_id');
}
public function log()
{
return $this->hasMany(\addons\groupon\model\OrderExpressLog::class, 'order_express_id')->where('store_id', '<>', 0)->order('id', 'desc');
}
}