Order   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 81.08%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 135
ccs 30
cts 37
cp 0.8108
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A tableName() 0 4 1
A rules() 0 9 1
A attributeLabels() 0 20 1
A behaviors() 0 8 1
A beforeSave() 0 7 2
A createSN() 0 4 1
A getStatusList() 0 16 1
A getPaymentMethodList() 0 8 1
A getOrderPaymentRecord() 0 4 1
A afterDelete() 0 6 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 string $name
13
 * @property integer $user_id
14
 * @property integer $payment_method
15
 * @property string $total_amount
16
 * @property integer $status
17
 * @property string $sn
18
 * @property integer $created_at
19
 * @property integer $updated_at
20
 * @property string $address
21
 * @property string $remark
22
 * @property string $recipient
23
 * @property string $phone
24
 * @property string $express
25
 * @property string $tracking_no
26
 */
27
class Order extends \yii\db\ActiveRecord
28
{
29
    const PAYMENT_METHOD_ALIPAY = 1;
30
    const PAYMENT_METHOD_WECHATPAY = 2;
31
    const PAYMENT_METHOD_QRCODE = 3;
32
33
    const STATUS_RETURN_FAILED = -5; //退货失败
34
    const STATUS_RETURNED = -4;// 已退货
35
    const STATUS_RETURNING = -3; //退货中
36
    const STATUS_DELETED = -2; //已删除
37
    const STATUS_CANCELED = -1; //已取消
38
    const STATUS_UNPAID = 0; //未支付
39
    const STATUS_PAID = 1; //已支付
40
    const STATUS_UNSHIPPED = 2; //未发货
41
    const STATUS_SHIPPED = 3; //已发货
42
    const STATUS_DELIVERED = 4; //已签收
43
    const STATUS_CONSUMED = 5; //已消费
44
45
    /**
46
     * @inheritdoc
47
     */
48 1
    public static function tableName()
49
    {
50 1
        return '{{%order}}';
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56 1
    public function rules()
57
    {
58
        return [
59 1
            [['user_id', 'name'], 'required'],
60
            [['user_id', 'payment_method', 'status', 'created_at', 'updated_at'], 'integer'],
61
            [['total_amount'], 'number'],
62
            [['sn', 'address', 'phone', 'recipient', 'remark', 'express', 'tracking_no', 'name'], 'string', 'max' => 255],
63
        ];
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69 1
    public function attributeLabels()
70
    {
71
        return [
72 1
            'id' => 'ID',
73
            'name' => '名称',
74
            'user_id' => '用户',
75
            'payment_method' => '支付方式',
76
            'total_amount' => '总金额',
77
            'status' => '状态',
78
            'sn' => '订单编号',
79
            'created_at' => '创建时间',
80
            'updated_at' => '更新时间',
81
            'address' => '配送地址',
82
            'remark' => '备注',
83
            'recipient' => '收件人',
84
            'phone' => '电话',
85
            'express' => '送货公司',
86
            'tracking_no' => '物流编号',
87
        ];
88
    }
89
90
    /**
91
     * @inheritdoc
92
     */
93 2
    public function behaviors()
94
    {
95
        return [
96
            'timestamp' => [
97 2
                'class' => TimestampBehavior::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
98
            ],
99
        ];
100
    }
101
102 1
    public function beforeSave($insert)
103
    {
104 1
        if ($insert) {
105 1
            $this->sn = $this->createSN();
106
        }
107 1
        return parent::beforeSave($insert);
108
    }
109
110
    /**
111
     * 产生序列号
112
     * @param string $prefix 序列号前缀
113
     * @return string
114
     * @link http://goo.gl/TZYwZo 参考说明
115
     */
116 1
    public function createSN($prefix = '')
117
    {
118 1
        return $prefix . date('Y') . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
119
    }
120
121 1
    public static function getStatusList()
122
    {
123
        return [
124 1
            self::STATUS_RETURN_FAILED => '退货失败',
125 1
            self::STATUS_RETURNED => '已退货',
126 1
            self::STATUS_RETURNING => '退货中',
127 1
            self::STATUS_DELETED => '已删除',
128 1
            self::STATUS_CANCELED => '已取消',
129 1
            self::STATUS_UNPAID => '未支付',
130 1
            self::STATUS_PAID => '已支付',
131 1
            self::STATUS_UNSHIPPED => '未发货',
132 1
            self::STATUS_SHIPPED => '已发货',
133 1
            self::STATUS_DELIVERED => '已签收',
134 1
            self::STATUS_CONSUMED => '已消费',
135
        ];
136
    }
137
138 1
    public static function getPaymentMethodList()
139
    {
140
        return [
141 1
            self::PAYMENT_METHOD_ALIPAY => '支付宝',
142 1
            self::PAYMENT_METHOD_WECHATPAY => '微信',
143 1
            self::PAYMENT_METHOD_QRCODE => '扫码转账',
144
        ];
145
    }
146
147
    /**
148
     * @return \yii\db\ActiveQuery
149
     */
150
    public function getOrderPaymentRecord()
151
    {
152
        return $this->hasOne(OrderPaymentRecord::className(), ['order_sn' => 'sn']);
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
153
    }
154
155
    public function afterDelete()
156
    {
157
        parent::afterDelete();
158
        OrderField::deleteAll(['order_id' => $this->id]);
159
        OrderPaymentRecord::deleteAll(['order_sn' => $this->sn]);
160
    }
161
}
162