Litestoreorder.php
15.3 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<?php
namespace addons\litestore\model;
use app\api\model\LitestoreGoods;
use think\Model;
use think\Db;
class Litestoreorder extends Model
{
// 表名
protected $name = 'litestore_order';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
// 追加属性
protected $append = [
'pay_status_text',
'pay_time_text',
'freight_status_text',
'freight_time_text',
'receipt_status_text',
'receipt_time_text',
'order_status_text',
'creattime_text',
];
public function cancel($user_id,$order_id){
if ($this['pay_status'] === '20') {
$this->error = '已付款订单不可取消';
return false;
}
$goods = $this['goods'];
$this->backGoodsStock($goods);
$order = self::get([
'id' => $order_id,
'user_id' => $user_id,
'order_status' => ['<>', '20']
]);
return $order->save([
'order_status' => '20'
]);
}
private function backGoodsStock(&$goodsList)
{
$goodsSpecSave = [];
foreach ($goodsList as $goods) {
// 下单减库存
if ($goods['deduct_stock_type'] === '10') {
$goodsSpecSave[] = [
'goods_spec_id' => $goods['goods_spec_id'],
'stock_num' => ['inc', $goods['total_num']]
];
}
}
if(!empty($goodsSpecSave)){
// 更新商品规格库存
return (new Litestoregoodsspec)->isUpdate()->saveAll($goodsSpecSave);
}
return true;
}
public function finish($user_id,$order_id){
if (!$order = self::get([
'id' => $order_id,
'user_id' => $user_id,
'order_status' => ['<>', '20']
])) {
throw new BaseException(['msg' => '订单不存在']);
}
if ($order['freight_status'] === '10' || $order['receipt_status'] === '20') {
$order->error = '该订单不合法';
return false;
}
return $order->save([
'receipt_status' => '20',
'receipt_time' => time(),
'order_status' => '30'
]);
}
public function checkGoodsStatusFromOrder($goodsList){
foreach ($goodsList as $goods) {
//商品是否下架
if ($goods['goods']['goods_status'] !== '10') {
$this->setError('很抱歉,商品 [' . $goods['goods_name'] . '] 已下架');
return false;
}
//付款减库存
if ($goods['deduct_stock_type'] === '20' && $goods['spec']['stock_num'] < 1) {
$this->setError('很抱歉,商品 [' . $goods['goods_name'] . '] 库存不足');
return false;
}
}
return true;
}
public function getList($user_id,$status = 0){
$where = [
'user_id' => $user_id,
];
if($status == 0) {
$where['order_status'] = ['<>','20'];
} elseif($status == 1) {
$where['pay_status'] = '10';
$where['order_status'] = '10';
} elseif($status == 2) {
$where['pay_status'] = '20';
$where['order_status'] = '10';
} elseif($status == 3) {
$where['pay_status'] = '20';
$where['order_status'] = '30';
}
$list = $this->with(['goods'])
->where($where)
->order(['createtime' => 'desc'])->limit(50)
->select();
foreach($list as &$v){
$goodsList = [];
foreach ($v['goods'] as $index => $item) {
$item['image'] = cdnurl(explode(",",$item['images'])[0], true);
$item['sku_image'] = $item['spec']['spec_image']=='' ? '' : cdnurl($item['spec']['spec_image'], true);
$goodsList[] = $item;
}
// 判断状态
if($v['pay_status'] == '10' && $v['order_status'] == '10') {
$v['status'] = 1;
} elseif($v['pay_status'] == '20' && $v['order_status'] == '10') {
$v['status'] = 2;
} elseif($v['pay_status'] == '20' && $v['order_status'] == '30') {
$v['status'] = 3;
}
$v['goods'] = $goodsList;
}
return $list;
}
public function getOrderDetail($order_id, $user_id){
if (!$order = self::get([
'id' => $order_id,
'user_id' => $user_id,
'order_status' => ['<>', '20']
], ['goods'=>['spec','goods']])) {
throw new BaseException(['msg' => '订单不存在']);
}
// 判断状态
if($order['pay_status'] == '10' && $order['order_status'] == '10') {
$order['status'] = 1;
} elseif($order['pay_status'] == '20' && $order['order_status'] == '10') {
$order['status'] = 2;
} elseif($order['pay_status'] == '20' && $order['order_status'] == '30') {
$order['status'] = 3;
}
$goodsList = [];
foreach ($order['goods'] as $index => $item) {
$item['image'] = cdnurl(explode(",",$item['images'])[0], true);
$item['sku_image'] = $item['spec']['spec_image']=='' ? '' : cdnurl($item['spec']['spec_image'], true);
$goodsList[] = $item;
}
$order['goods'] = $goodsList;
return $order;
}
public function payDetail($order_no){
return self::get(['order_no' => $order_no, 'pay_status' => 10], ['goods']);
}
public function getCart($user_id){
$model = new CacheCart($user_id);
return $model->getList($user_id);
}
public function CarclearAll($user_id){
$Card = new CacheCart($user_id);
$Card->clearAll();
}
public function getBuyNow($user_id, $goods_id, $goods_num, $goods_sku_id)
{
// 商品信息
$goods = Wxlitestoregoods::detail($goods_id);
$goods['show_error'] = 0;
// 判断商品是否下架
if ($goods['goods_status'] !== '10') {
$goods['show_error'] = 1;
$goods['show_error_text'] = '已下架';
$this->setError('很抱歉,商品信息不存在或已下架');
}
// 商品sku信息
$goods['goods_sku'] = $goods->getGoodsSku($goods_sku_id);
// 判断商品库存
if ($goods_num > $goods['goods_sku']['stock_num']) {
$goods['show_error'] = 2;
$goods['show_error_text'] = '库存不足';
$this->setError('很抱歉,商品库存不足');
}
// 商品单价
$goods['goods_price'] = $goods['goods_sku']['goods_price'];
// 商品总价
$goods['total_num'] = $goods_num;
$goods['total_price'] = $totalPrice = bcmul($goods['goods_price'], $goods_num, 2);
// 商品总重量
$goods_total_weight = bcmul($goods['goods_sku']['goods_weight'], $goods_num, 2);
// // 当前用户收货城市id
// $defaultcity = Litestoreadress::getdefault($user_id);
// $cityId = $defaultcity ? $defaultcity['city_id'] : null;
// // 是否存在收货地址
// $exist_address = $defaultcity;
// // 验证用户收货地址是否存在运费规则中
// if (!$intraRegion = $goods['freight']->checkAddress($cityId)) {
// $exist_address && $this->setError('很抱歉,您的收货地址不在配送范围内');
// }
// // 计算配送费用
// $expressPrice = $intraRegion ?
// $goods['freight']->calcTotalFee($goods_num, $goods_total_weight, $cityId) : 0;
return [
'store_id' => $goods['store_id'],
'goods_list' => [$goods], // 商品详情
'order_total_num' => $goods_num, // 商品总数量
'order_total_price' => $totalPrice, // 商品总金额 (不含运费)
'order_pay_price' => $totalPrice, // 实际支付金额
// 'order_pay_price' => bcadd($totalPrice, $expressPrice, 2), // 实际支付金额
// 'address' => $defaultcity, // 默认地址
// 'exist_address' => $exist_address, // 是否存在收货地址
// 'express_price' => $expressPrice, // 配送费用
// 'intra_region' => $intraRegion, // 当前用户收货城市是否存在配送规则中
'has_error' => $this->hasError(),
'error_msg' => $this->getError(),
];
}
public function order_add($user_id, $order, $remark=''){
// if (empty($order['address'])) {
// $this->error = '请先选择收货地址';
// return false;
// }
Db::startTrans();
// 记录订单信息
$this->save([
'store_id' => $order['store_id'],
'user_id' => $user_id,
'order_no' => $this->orderNo(),
'total_price' => $order['order_total_price'],
'pay_price' => $order['order_pay_price'],
'remark' => $remark,
// 'express_price' => $order['express_price'],
]);
// 订单商品列表
$goodsList = [];
// 更新商品库存 (下单减库存)
$deductStockData = [];
foreach ($order['goods_list'] as $goods) {
/* @var Goods $goods */
$goodsList[] = [
'user_id' => $user_id,
'goods_id' => $goods['goods_id'],
'goods_name' => $goods['goods_name'],
'images' => $goods['images'],
'deduct_stock_type' => $goods['deduct_stock_type'],
'spec_type' => $goods['spec_type'],
'spec_sku_id' => $goods['goods_sku']['spec_sku_id'],
'goods_spec_id' => $goods['goods_sku']['goods_spec_id'],
'goods_attr' => $goods['goods_sku']['goods_attr'],
'content' => $goods['content'],
'goods_no' => $goods['goods_sku']['goods_no'],
'goods_price' => $goods['goods_sku']['goods_price'],
'line_price' => $goods['goods_sku']['line_price'],
'goods_weight' => $goods['goods_sku']['goods_weight'],
'total_num' => $goods['total_num'],
'total_price' => $goods['total_price'],
];
// 下单减库存
$goods['deduct_stock_type'] === '10' && $deductStockData[] = [
'goods_spec_id' => $goods['goods_sku']['goods_spec_id'],
'stock_num' => ['dec', $goods['total_num']]
];
}
// 保存订单商品信息
$this->goods()->saveAll($goodsList);
// 更新商品库存
!empty($deductStockData) && (new Litestoregoodsspec)->isUpdate()->saveAll($deductStockData);
// // 记录收货地址
// $this->address()->save([
// 'user_id' => $user_id,
// 'name' => $order['address']['name'],
// 'phone' => $order['address']['phone'],
// 'province_id' => $order['address']['province_id'],
// 'city_id' => $order['address']['city_id'],
// 'region_id' => $order['address']['region_id'],
// 'detail' => $order['address']['detail'],
// ]);
Db::commit();
return true;
}
public function getPayStatusList()
{
return ['10' => __('Pay_status 10'),'20' => __('Pay_status 20')];
}
public function getFreightStatusList()
{
return ['10' => __('Freight_status 10'),'20' => __('Freight_status 20')];
}
public function getReceiptStatusList()
{
return ['10' => __('Receipt_status 10'),'20' => __('Receipt_status 20')];
}
public function getOrderStatusList()
{
return ['10' => __('Order_status 10'),'20' => __('Order_status 20'),'30' => __('Order_status 30')];
}
public function getPayStatusTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['pay_status']) ? $data['pay_status'] : '');
$list = $this->getPayStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getPayTimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['pay_time']) ? $data['pay_time'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
public function getFreightStatusTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['freight_status']) ? $data['freight_status'] : '');
$list = $this->getFreightStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getFreightTimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['freight_time']) ? $data['freight_time'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
public function getReceiptStatusTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['receipt_status']) ? $data['receipt_status'] : '');
$list = $this->getReceiptStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getReceiptTimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['receipt_time']) ? $data['receipt_time'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
public function getOrderStatusTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['order_status']) ? $data['order_status'] : '');
$list = $this->getOrderStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getCreattimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;;
}
protected function setPayTimeAttr($value)
{
return $value && !is_numeric($value) ? strtotime($value) : $value;
}
protected function setFreightTimeAttr($value)
{
return $value && !is_numeric($value) ? strtotime($value) : $value;
}
protected function setReceiptTimeAttr($value)
{
return $value && !is_numeric($value) ? strtotime($value) : $value;
}
public function goods()
{
return $this->hasMany('Litestoreordergoods','order_id', 'id');
}
public function address()
{
return $this->hasOne('Litestoreorderaddress', 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
private function setError($error)
{
empty($this->error) && $this->error = $error;
}
protected function orderNo()
{
return date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
}
public function hasError()
{
return !empty($this->error);
}
public function updatePayStatus($transaction_id)
{
Db::startTrans();
// 更新商品库存、销量
$GoodsModel = new Wxlitestoregoods;
$GoodsModel->updateStockSales($this['goods']);
// 更新订单状态
$this->save([
'pay_status' => '20',
'pay_time' => time(),
'transaction_id' => $transaction_id,
]);
Db::commit();
return true;
}
}