1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace zacksleo\yii2\shop\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\behaviors\TimestampBehavior; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* This is the model class for table "{{%order}}". |
10
|
|
|
* |
11
|
|
|
* @property integer $id |
12
|
|
|
* @property integer $user_id |
13
|
|
|
* @property integer $payment_method |
14
|
|
|
* @property string $total_amount |
15
|
|
|
* @property integer $status |
16
|
|
|
* @property string $sn |
17
|
|
|
* @property integer $created_at |
18
|
|
|
* @property integer $updated_at |
19
|
|
|
* @property string $address |
20
|
|
|
* @property string $remark |
21
|
|
|
* @property string $recipient |
22
|
|
|
* @property string $phone |
23
|
|
|
*/ |
24
|
|
|
class Order extends \yii\db\ActiveRecord |
25
|
|
|
{ |
26
|
|
|
const PAYMENT_METHOD_ALIPAY = 1; |
27
|
|
|
const PAYMENT_METHOD_WECHATPAY = 2; |
28
|
|
|
const STATUS_DELETED = -2; //已删除 |
29
|
|
|
const STATUS_CANCELED = -1; //已取消 |
30
|
|
|
const STATUS_UNPAID = 0; //未支付 |
31
|
|
|
const STATUS_PAID = 1; //已支付 |
32
|
|
|
const STATUS_CONSUMED = 2; //已消费 |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
1 |
|
public static function tableName() |
38
|
|
|
{ |
39
|
1 |
|
return '{{%order}}'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
1 |
|
public function rules() |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
1 |
|
[['user_id', 'address', 'recipient', 'phone'], 'required'], |
49
|
|
|
[['user_id', 'payment_method', 'status', 'created_at', 'updated_at'], 'integer'], |
50
|
|
|
[['total_amount'], 'number'], |
51
|
|
|
[['sn', 'address', 'remark'], 'string', 'max' => 255], |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritdoc |
57
|
|
|
*/ |
58
|
1 |
|
public function attributeLabels() |
59
|
|
|
{ |
60
|
|
|
return [ |
61
|
1 |
|
'id' => 'ID', |
62
|
|
|
'user_id' => '用户', |
63
|
|
|
'payment_method' => '支付方式', |
64
|
|
|
'total_amount' => '总金额', |
65
|
|
|
'status' => '状态', |
66
|
|
|
'sn' => '订单编号', |
67
|
|
|
'created_at' => '创建时间', |
68
|
|
|
'updated_at' => '更新时间', |
69
|
|
|
'address' => '配送地址', |
70
|
|
|
'remark' => '备注', |
71
|
|
|
'recipient' => '收件人', |
72
|
|
|
'phone' => '电话', |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
2 |
|
public function behaviors() |
80
|
|
|
{ |
81
|
|
|
return [ |
82
|
|
|
'timestamp' => [ |
83
|
2 |
|
'class' => TimestampBehavior::className(), |
84
|
|
|
], |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public function beforeSave($insert) |
89
|
|
|
{ |
90
|
1 |
|
if ($insert) { |
91
|
1 |
|
$this->sn = $this->createSN(); |
92
|
|
|
} |
93
|
1 |
|
return parent::beforeSave($insert); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* 产生序列号 |
98
|
|
|
* @param string $prefix 序列号前缀 |
99
|
|
|
* @return string |
100
|
|
|
* @link http://goo.gl/TZYwZo 参考说明 |
101
|
|
|
*/ |
102
|
1 |
|
public function createSN($prefix = '') |
103
|
|
|
{ |
104
|
1 |
|
return $prefix . date('Y') . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99)); |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
public static function getStatusList() |
108
|
|
|
{ |
109
|
|
|
return [ |
110
|
1 |
|
self::STATUS_DELETED => '已删除', |
111
|
1 |
|
self::STATUS_CANCELED => '已取消', |
112
|
1 |
|
self::STATUS_UNPAID => '未支付', |
113
|
1 |
|
self::STATUS_PAID => '未解读', |
114
|
1 |
|
self::STATUS_CONSUMED => '已解读', |
115
|
|
|
]; |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
public static function getPaymentMethodList() |
119
|
|
|
{ |
120
|
|
|
return [ |
121
|
1 |
|
self::PAYMENT_METHOD_ALIPAY => '支付宝', |
122
|
1 |
|
self::PAYMENT_METHOD_WECHATPAY => '微信', |
123
|
|
|
]; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|