commit | author | age
|
2207d6
|
1 |
<?php |
W |
2 |
|
|
3 |
namespace app\models; |
|
4 |
|
|
5 |
use Yii; |
|
6 |
use yii\db\Exception; |
|
7 |
|
|
8 |
/** |
|
9 |
* This is the model class for table "{{%coupon}}". |
|
10 |
* |
|
11 |
* @property int $id |
|
12 |
* @property int $mall_id |
|
13 |
* @property string $name 优惠券名称 |
|
14 |
* @property int $type 优惠券类型:1=折扣,2=满减 |
|
15 |
* @property string $discount 折扣率 |
|
16 |
* @property string $discount_limit 优惠上限 |
|
17 |
* @property int $pic_url 未用 |
|
18 |
* @property string $desc 未用 |
|
19 |
* @property string $min_price 最低消费金额 |
|
20 |
* @property string $sub_price 优惠金额 |
|
21 |
* @property int $total_count 发放总数量 |
|
22 |
* @property int $can_receive_count 可领取数量 |
|
23 |
* @property int $sort 排序按升序排列 |
|
24 |
* @property int $expire_type 到期类型:1=领取后N天过期,2=指定有效期 |
|
25 |
* @property int $expire_day 有效天数,expire_type=1时 |
|
26 |
* @property string $begin_time 有效期开始时间 |
|
27 |
* @property string $end_time 有效期结束时间 |
|
28 |
* @property int $appoint_type 类型 |
|
29 |
* @property string $rule 使用说明 |
|
30 |
* @property int $is_member 是否指定会员等级购买 |
|
31 |
* @property int $is_delete 删除 |
|
32 |
* @property string $deleted_at |
|
33 |
* @property string $created_at |
|
34 |
* @property string $updated_at |
|
35 |
* @property string $app_share_title |
|
36 |
* @property string $app_share_pic |
|
37 |
* @property GoodsCats[] $cat |
|
38 |
* @property GoodsWarehouse[] $goods |
|
39 |
* @property GoodsWarehouse[] $goodsWarehouse |
|
40 |
* @property $couponCat |
|
41 |
* @property $couponGoods |
|
42 |
* @property string $appointTypeText |
|
43 |
* @property int $use_obtain |
|
44 |
*/ |
|
45 |
class Coupon extends ModelActiveRecord |
|
46 |
{ |
|
47 |
/** |
|
48 |
* {@inheritdoc} |
|
49 |
*/ |
|
50 |
public static function tableName() |
|
51 |
{ |
|
52 |
return '{{%coupon}}'; |
|
53 |
} |
|
54 |
|
|
55 |
/** |
|
56 |
* {@inheritdoc} |
|
57 |
*/ |
|
58 |
public function rules() |
|
59 |
{ |
|
60 |
return [ |
|
61 |
[['mall_id', 'name', 'type', 'min_price', 'sub_price', 'expire_type', 'appoint_type', 'deleted_at', 'created_at', 'updated_at'], 'required'], |
|
62 |
[['mall_id', 'type', 'pic_url', 'total_count', 'sort', 'expire_type', 'expire_day', 'appoint_type', |
|
63 |
'is_member', 'is_delete', 'can_receive_count', 'use_obtain'], 'integer'], |
|
64 |
[['discount', 'discount_limit', 'min_price', 'sub_price'], 'number'], |
|
65 |
[['begin_time', 'end_time', 'deleted_at', 'created_at', 'updated_at'], 'safe'], |
|
66 |
[['name', 'app_share_title', 'app_share_pic'], 'string', 'max' => 255], |
|
67 |
[['desc', 'rule'], 'string', 'max' => 2000], |
|
68 |
]; |
|
69 |
} |
|
70 |
|
|
71 |
/** |
|
72 |
* {@inheritdoc} |
|
73 |
*/ |
|
74 |
public function attributeLabels() |
|
75 |
{ |
|
76 |
return [ |
|
77 |
'id' => 'ID', |
|
78 |
'mall_id' => 'mall ID', |
|
79 |
'name' => '优惠券名称', |
|
80 |
'type' => '优惠券类型:1=折扣,2=满减', |
|
81 |
'discount' => '折扣率', |
|
82 |
'discount_limit' => '优惠上限', |
|
83 |
'pic_url' => '未用', |
|
84 |
'desc' => '未用', |
|
85 |
'min_price' => '最低消费金额', |
|
86 |
'sub_price' => '优惠金额', |
|
87 |
'total_count' => '发放总数量', |
|
88 |
'can_receive_count' => '可领取数量', |
|
89 |
'sort' => '排序按升序排列', |
|
90 |
'expire_type' => '到期类型', |
|
91 |
'expire_day' => '有效天数', |
|
92 |
'appoint_type' => '指定方式', |
|
93 |
'begin_time' => '有效期开始时间', |
|
94 |
'end_time' => '有效期结束时间', |
|
95 |
'rule' => '使用说明', |
|
96 |
'is_member' => '是否指定会员等级领取', |
|
97 |
'is_delete' => '删除', |
|
98 |
'deleted_at' => 'Deleted At', |
|
99 |
'created_at' => 'Created At', |
|
100 |
'updated_at' => 'Updated At', |
|
101 |
'app_share_title' => 'App Share Title', |
|
102 |
'app_share_pic' => 'App Share Pic', |
|
103 |
'use_obtain' => '领取后赠送', |
|
104 |
]; |
|
105 |
} |
|
106 |
|
|
107 |
public function getCat() |
|
108 |
{ |
|
109 |
return $this->hasMany(GoodsCats::className(), ['id' => 'cat_id'])->where(['is_delete' => 0]) |
|
110 |
->via('couponCat'); |
|
111 |
} |
|
112 |
|
|
113 |
public function getGoodsWarehouse() |
|
114 |
{ |
|
115 |
return $this->hasMany(GoodsWarehouse::className(), ['id' => 'goods_warehouse_id']) |
|
116 |
->via('couponGoods'); |
|
117 |
} |
|
118 |
|
|
119 |
public function getGoods() |
|
120 |
{ |
|
121 |
return $this->hasMany(GoodsWarehouse::className(), ['id' => 'goods_warehouse_id']) |
|
122 |
->via('couponGoods'); |
|
123 |
} |
|
124 |
|
|
125 |
public function getCouponCat() |
|
126 |
{ |
|
127 |
return $this->hasMany(CouponCatRelation::className(), ['coupon_id' => 'id'])->where(['is_delete' => 0]); |
|
128 |
} |
|
129 |
|
|
130 |
public function getCouponGoods() |
|
131 |
{ |
|
132 |
return $this->hasMany(CouponGoodsRelation::className(), ['coupon_id' => 'id'])->where(['is_delete' => 0]); |
|
133 |
} |
|
134 |
|
|
135 |
public function getCouponMember() |
|
136 |
{ |
|
137 |
return $this->hasMany(CouponMemberRelation::className(), ['coupon_id' => 'id']); |
|
138 |
} |
|
139 |
|
|
140 |
public function getCouponCenter() |
|
141 |
{ |
|
142 |
return $this->hasOne(CouponCenter::className(), ['coupon_id' => 'id']); |
|
143 |
} |
|
144 |
|
|
145 |
public function getBeginTime() |
|
146 |
{ |
|
147 |
return date('Y-m-d', strtotime($this->begin_time)); |
|
148 |
} |
|
149 |
|
|
150 |
public function getEndTime() |
|
151 |
{ |
|
152 |
return date('Y-m-d', strtotime($this->end_time)); |
|
153 |
} |
|
154 |
|
|
155 |
/** |
|
156 |
* @param $num integer 修改的数量 |
|
157 |
* @param $type string 增加add|减少sub |
|
158 |
* @param null|integer $id 优惠券ID |
|
159 |
* @return Coupon|null |
|
160 |
* @throws Exception |
|
161 |
*/ |
|
162 |
public function updateCount($num, $type, $id = null) |
|
163 |
{ |
|
164 |
if ($id) { |
|
165 |
$coupon = self::findOne(['id' => $id, 'is_delete' => 0]); |
|
166 |
} else { |
|
167 |
$coupon = $this; |
|
168 |
} |
|
169 |
if (!$coupon || !$coupon->id) { |
|
170 |
throw new Exception('错误的优惠券信息'); |
|
171 |
} |
|
172 |
if ($coupon->total_count == -1) { |
|
173 |
return $coupon; |
|
174 |
} |
|
175 |
if ($type === 'add') { |
|
176 |
$coupon->total_count += $num; |
|
177 |
} elseif ($type === 'sub') { |
|
178 |
if ($coupon->total_count < $num) { |
|
179 |
throw new Exception('优惠券库存不足'); |
|
180 |
} |
|
181 |
$coupon->total_count -= $num; |
|
182 |
} else { |
|
183 |
throw new Exception('错误的$type'); |
|
184 |
} |
|
185 |
if ($coupon->save()) { |
|
186 |
return $coupon; |
|
187 |
} else { |
|
188 |
throw new Exception($coupon->errors[0]); |
|
189 |
} |
|
190 |
} |
|
191 |
|
|
192 |
public function getAppointTypeText() |
|
193 |
{ |
|
194 |
switch ($this->appoint_type) { |
|
195 |
case 1: |
|
196 |
$text = '指定商品'; |
|
197 |
break; |
|
198 |
case 2: |
|
199 |
$text = '指定商品类别'; |
|
200 |
break; |
|
201 |
case 3: |
|
202 |
$text = '全场通用'; |
|
203 |
break; |
|
204 |
case 4: |
|
205 |
$text = '仅限当面付'; |
|
206 |
break; |
|
207 |
default: |
|
208 |
$text = ''; |
|
209 |
} |
|
210 |
return $text; |
|
211 |
} |
|
212 |
} |