Completed
Push — master ( 561d90...23a77b )
by zacksleo
03:06
created

Order::beforeSave()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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