Order.php
3.4 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
<?php
namespace app\common\model;
use think\Model;
use think\Db;
class Order extends Model
{
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
/**
* 订单商品列表
*/
public function goods()
{
return $this->hasMany('OrderGoods');
}
/**
* 关联订单收货地址表
*/
public function address()
{
return $this->hasOne('OrderAddress');
}
/**
* 新增订单
*/
public function add($user_id, $order)
{
if (empty($order['address'])) {
$this->error = '请先选择收货地址';
return false;
}
Db::startTrans();
// 记录订单信息
$this->save([
'user_id' => $user_id,
'order_no' => date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8),
'total_price' => $order['total_price'],
'order_price' => $order['order_price'],
'express_price' => $order['express_price'],
'reduce_price' => $order['reduce_price'],
'user_coupon_id' => $order['coupon']['user_coupon_id'],
'score' => $order['score']['score_price'] > 0 ? $order['score']['score'] : 0,
]);
// 订单商品列表
$goodsList = [];
foreach ($order['goods_list'] as $goods) {
$goodsList[] = [
'goods_id' => $goods['goods_id'],
'goods_name' => $goods['goods']['goods_name'],
'goods_image' => $goods['goods']['goods_image'],
'goods_price' => $goods['goods_price'],
'goods_num' => $goods['goods_num'],
'user_size_id' => !empty($goods['user_size']) ? $goods['user_size']['id'] : 0,
'goods_style' => $goods['goods_style']
];
}
// 保存订单商品信息
$this->goods()->saveAll($goodsList);
// 记录收货地址
$this->address()->save([
'name' => $order['address']['name'],
'phone' => $order['address']['phone'],
'province_id' => $order['address']['province_id'],
'city_id' => $order['address']['city_id'],
'district_id' => $order['address']['district_id'],
'detail' => $order['address']['detail'],
]);
Db::commit();
return true;
}
/**
* 用户中心订单列表
*/
public function getList($user_id, $type = 'all')
{
// 筛选条件
$filter = [];
// 订单数据类型
switch ($type) {
case 'all':
break;
case 'payment';
$filter['pay_status'] = 10;
break;
case 'delivery';
$filter['pay_status'] = 20;
$filter['delivery_status'] = 10;
break;
case 'received';
$filter['pay_status'] = 20;
$filter['delivery_status'] = 20;
$filter['receipt_status'] = 10;
break;
}
return $this->with(['goods.image'])
->where('user_id', '=', $user_id)
->where('order_status', '<>', 20)
->where($filter)
->order(['create_time' => 'desc'])
->select();
}
}