commit | author | age
|
2207d6
|
1 |
<?php |
W |
2 |
|
|
3 |
namespace app\models; |
|
4 |
|
|
5 |
use Yii; |
|
6 |
|
|
7 |
/** |
|
8 |
* This is the model class for table "{{%user_card}}". |
|
9 |
* |
|
10 |
* @property int $id |
|
11 |
* @property int $mall_id |
|
12 |
* @property int $user_id |
|
13 |
* @property int $card_id |
|
14 |
* @property string $name 名称 |
|
15 |
* @property string $pic_url 图片 |
|
16 |
* @property string $content 详情 |
|
17 |
* @property int $is_delete 删除 |
|
18 |
* @property string $created_at |
|
19 |
* @property string $updated_at |
|
20 |
* @property string $deleted_at |
|
21 |
* @property int $is_use 是否使用 0--未使用 1--已使用 |
|
22 |
* @property int $clerk_id 核销人id |
|
23 |
* @property int $store_id 门店ID |
|
24 |
* @property string $clerked_at 核销时间 |
|
25 |
* @property int $order_id 发放卡券的订单id |
|
26 |
* @property int $order_detail_id 订单详情ID |
|
27 |
* @property string $data 额外信息字段 |
|
28 |
* @property string $start_time |
|
29 |
* @property string $end_time |
|
30 |
* @property int $number |
|
31 |
* @property int $use_number |
|
32 |
* @property int $receive_id 转赠领取的用户id |
|
33 |
* @property int $parent_card_id 转赠的用户卡券id |
|
34 |
* @property GoodsCards $card |
|
35 |
* @property Order $order |
|
36 |
* @property OrderDetail $detail |
|
37 |
* @property Store $store |
|
38 |
* @property User $user |
|
39 |
* @property GoodsCardClerkLog $clerkLog |
|
40 |
* @property GoodsCardClerkLog $lastClerkLog |
|
41 |
* @property string $remark |
|
42 |
* @property User $receive 领取的用户 |
|
43 |
* @property User $from 卡券来源的用户 |
|
44 |
*/ |
|
45 |
class UserCard extends ModelActiveRecord |
|
46 |
{ |
|
47 |
/** |
|
48 |
* {@inheritdoc} |
|
49 |
*/ |
|
50 |
public static function tableName() |
|
51 |
{ |
|
52 |
return '{{%user_card}}'; |
|
53 |
} |
|
54 |
|
|
55 |
/** |
|
56 |
* {@inheritdoc} |
|
57 |
*/ |
|
58 |
public function rules() |
|
59 |
{ |
|
60 |
return [ |
|
61 |
[['mall_id', 'user_id', 'card_id', 'name', 'pic_url', 'content', 'created_at', 'updated_at', 'deleted_at', 'clerked_at'], 'required'], |
|
62 |
[['mall_id', 'user_id', 'card_id', 'is_delete', 'is_use', 'clerk_id', 'store_id', 'order_id', 'order_detail_id', 'use_number', 'number', 'receive_id', 'parent_card_id'], 'integer'], |
|
63 |
[['content', 'data'], 'string'], |
|
64 |
[['created_at', 'updated_at', 'deleted_at', 'clerked_at', 'start_time', 'end_time'], 'safe'], |
|
65 |
[['name', 'pic_url', 'remark'], 'string', 'max' => 255], |
|
66 |
]; |
|
67 |
} |
|
68 |
|
|
69 |
/** |
|
70 |
* {@inheritdoc} |
|
71 |
*/ |
|
72 |
public function attributeLabels() |
|
73 |
{ |
|
74 |
return [ |
|
75 |
'id' => 'ID', |
|
76 |
'mall_id' => 'Mall ID', |
|
77 |
'user_id' => 'User ID', |
|
78 |
'card_id' => 'Card ID', |
|
79 |
'name' => '名称', |
|
80 |
'pic_url' => '图片', |
|
81 |
'content' => '详情', |
|
82 |
'is_delete' => '删除', |
|
83 |
'created_at' => 'Created At', |
|
84 |
'updated_at' => 'Updated At', |
|
85 |
'deleted_at' => 'Deleted At', |
|
86 |
'is_use' => '是否使用 0--未使用 1--已使用', |
|
87 |
'clerk_id' => '核销人id', |
|
88 |
'store_id' => '门店ID', |
|
89 |
'clerked_at' => ' 核销时间', |
|
90 |
'order_id' => '发放卡券的订单id', |
|
91 |
'order_detail_id' => '订单详情ID', |
|
92 |
'data' => '额外信息字段', |
|
93 |
'start_time' => 'Start Time', |
|
94 |
'end_time' => 'End Time', |
|
95 |
'number' => '核销总次数', |
|
96 |
'use_number' => '已核销次数', |
|
97 |
'remark' => '备注', |
|
98 |
'receive_id' => '转赠领取的用户id', |
|
99 |
'parent_card_id' => '转赠的用户卡券id', |
|
100 |
]; |
|
101 |
} |
|
102 |
|
|
103 |
public function getClerk() |
|
104 |
{ |
|
105 |
return $this->hasOne(User::className(), ['id' => 'clerk_id']); |
|
106 |
} |
|
107 |
|
|
108 |
public function getStore() |
|
109 |
{ |
|
110 |
return $this->hasOne(Store::className(), ['id' => 'store_id']); |
|
111 |
} |
|
112 |
|
|
113 |
public function getCard() |
|
114 |
{ |
|
115 |
return $this->hasOne(GoodsCards::className(), ['id' => 'card_id']); |
|
116 |
} |
|
117 |
|
|
118 |
public function getUser() |
|
119 |
{ |
|
120 |
return $this->hasOne(User::className(), ['id' => 'user_id']); |
|
121 |
} |
|
122 |
|
|
123 |
public function getOrder() |
|
124 |
{ |
|
125 |
return $this->hasOne(Order::className(), ['id' => 'order_id']); |
|
126 |
} |
|
127 |
|
|
128 |
public function getDetail() |
|
129 |
{ |
|
130 |
return $this->hasOne(OrderDetail::className(), ['id' => 'order_detail_id']); |
|
131 |
} |
|
132 |
|
|
133 |
public function getClerkLog() |
|
134 |
{ |
|
135 |
return $this->hasMany(GoodsCardClerkLog::className(), ['user_card_id' => 'id']); |
|
136 |
} |
|
137 |
|
|
138 |
public function getLastClerkLog() |
|
139 |
{ |
|
140 |
return $this->hasOne(GoodsCardClerkLog::className(), ['user_card_id' => 'id'])->orderBy(['clerked_at' => SORT_DESC]); |
|
141 |
} |
|
142 |
|
|
143 |
/** |
|
144 |
* 兼容旧卡券 |
|
145 |
* @param UserCard $userCard |
|
146 |
* @return array; |
|
147 |
*/ |
|
148 |
public function getNewItem($userCard) |
|
149 |
{ |
|
150 |
$data = []; |
|
151 |
if ($userCard->is_use == 1 && $userCard->number - $userCard->use_number > 0) { |
|
152 |
$data['use_number'] = $userCard->number; |
|
153 |
} |
|
154 |
|
|
155 |
$data['is_show_history'] = 0; |
|
156 |
if ($userCard->is_use || $userCard->lastClerkLog) { |
|
157 |
$data['is_show_history'] = 1; |
|
158 |
} |
|
159 |
|
|
160 |
if ($userCard->lastClerkLog) { |
|
161 |
$data['store_name'] = $userCard->lastClerkLog->store->name; |
|
162 |
$data['clerked_at'] = new_date($userCard->lastClerkLog->clerked_at); |
|
163 |
} |
|
164 |
|
|
165 |
return $data; |
|
166 |
} |
|
167 |
|
|
168 |
public function getReceive() |
|
169 |
{ |
|
170 |
return $this->hasOne(User::className(), ['id' => 'receive_id']); |
|
171 |
} |
|
172 |
|
|
173 |
public function getFrom() |
|
174 |
{ |
|
175 |
return $this->hasOne(User::className(), ['id' => 'user_id']) |
|
176 |
->viaTable(UserCard::tableName(), ['id' => 'parent_card_id']); |
|
177 |
} |
|
178 |
} |