Completed
Push — master ( 103d46...fbae06 )
by zacksleo
03:18
created

Order::getOrderPaymentRecord()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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 PAYMENT_METHOD_QRCODE = 3;
29
30
    const STATUS_DELETED = -2; //已删除
31
    const STATUS_CANCELED = -1; //已取消
32
    const STATUS_UNPAID = 0; //未支付
33
    const STATUS_PAID = 1; //已支付
34
    const STATUS_CONSUMED = 2; //已消费
35
36
    /**
37
     * @inheritdoc
38
     */
39 1
    public static function tableName()
40
    {
41 1
        return '{{%order}}';
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47 1
    public function rules()
48
    {
49
        return [
50 1
            [['user_id', 'address', 'recipient', 'phone'], 'required'],
51
            [['user_id', 'payment_method', 'status', 'created_at', 'updated_at'], 'integer'],
52
            [['total_amount'], 'number'],
53
            [['sn', 'address', 'remark'], 'string', 'max' => 255],
54
        ];
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60 1
    public function attributeLabels()
61
    {
62
        return [
63 1
            'id' => 'ID',
64
            'user_id' => '用户',
65
            'payment_method' => '支付方式',
66
            'total_amount' => '总金额',
67
            'status' => '状态',
68
            'sn' => '订单编号',
69
            'created_at' => '创建时间',
70
            'updated_at' => '更新时间',
71
            'address' => '配送地址',
72
            'remark' => '备注',
73
            'recipient' => '收件人',
74
            'phone' => '电话',
75
        ];
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81 2
    public function behaviors()
82
    {
83
        return [
84
            'timestamp' => [
85 2
                'class' => TimestampBehavior::className(),
86
            ],
87
        ];
88
    }
89
90 1
    public function beforeSave($insert)
91
    {
92 1
        if ($insert) {
93 1
            $this->sn = $this->createSN();
94
        }
95 1
        return parent::beforeSave($insert);
96
    }
97
98
    /**
99
     * 产生序列号
100
     * @param string $prefix 序列号前缀
101
     * @return string
102
     * @link http://goo.gl/TZYwZo 参考说明
103
     */
104 1
    public function createSN($prefix = '')
105
    {
106 1
        return $prefix . date('Y') . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
107
    }
108
109 1
    public static function getStatusList()
110
    {
111
        return [
112 1
            self::STATUS_DELETED => '已删除',
113 1
            self::STATUS_CANCELED => '已取消',
114 1
            self::STATUS_UNPAID => '未支付',
115 1
            self::STATUS_PAID => '已支付',
116 1
            self::STATUS_CONSUMED => '已消费',
117
        ];
118
    }
119
120 1
    public static function getPaymentMethodList()
121
    {
122
        return [
123 1
            self::PAYMENT_METHOD_ALIPAY => '支付宝',
124 1
            self::PAYMENT_METHOD_WECHATPAY => '微信',
125 1
            self::PAYMENT_METHOD_QRCODE => '扫码转账',
126
        ];
127
    }
128
129
    /**
130
     * @return \yii\db\ActiveQuery
131
     */
132
    public function getOrderPaymentRecord()
133
    {
134
        return $this->hasOne(OrderPaymentRecord::className(), ['order_sn' => 'sn']);
135
    }
136
}
137