Completed
Push — master ( d544c2...69b2ef )
by zacksleo
08:07
created

Order   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.1%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 10
c 4
b 0
f 1
lcom 1
cbo 3
dl 0
loc 119
ccs 27
cts 29
cp 0.931
rs 10

9 Methods

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