commit | author | age
|
2207d6
|
1 |
<?php |
W |
2 |
|
|
3 |
namespace app\models; |
|
4 |
|
|
5 |
use app\plugins\mch\models\Mch; |
|
6 |
use app\plugins\mch\models\MchGoods; |
|
7 |
use Yii; |
|
8 |
|
|
9 |
/** |
|
10 |
* This is the model class for table "{{%goods}}". |
|
11 |
* |
|
12 |
* @property int $id |
|
13 |
* @property int $mall_id |
|
14 |
* @property int $mch_id |
|
15 |
* @property int $goods_warehouse_id |
|
16 |
* @property int $status 上架状态:0=下架,1=上架 |
|
17 |
* @property string $price 售价 |
|
18 |
* @property int $use_attr 是否使用规格:0=不使用,1=使用 |
|
19 |
* @property string $attr_groups 商品规格组 |
|
20 |
* @property int $virtual_sales 已出售量 |
|
21 |
* @property string $created_at |
|
22 |
* @property string $updated_at |
|
23 |
* @property string $deleted_at |
|
24 |
* @property int $is_delete |
|
25 |
* @property int $sort 排序 |
|
26 |
* @property int $goods_stock 商品总库存 |
|
27 |
* @property int $confine_count 购物数量限制 |
|
28 |
* @property int $shipping_id 包邮模板ID |
|
29 |
* @property int $freight_id 运费模板ID |
|
30 |
* @property int $give_integral 赠送积分 |
|
31 |
* @property int $give_integral_type 赠送积分类型1.固定值 |2.百分比 |
|
32 |
* @property int $give_balance 赠送余额 |
|
33 |
* @property int $give_balance_type 赠送余额类型1.固定值 |2.百分比 |
|
34 |
* @property int $forehead_integral 可抵扣积分 |
|
35 |
* @property int $forehead_integral_type 可抵扣积分类型1.固定值 |2.百分比 |
|
36 |
* @property int $accumulative 允许多件累计折扣 |
|
37 |
* @property int $individual_share 是否单独分销设置:0=否,1=是 |
|
38 |
* @property int $attr_setting_type 分销设置类型 0.普通设置|1.详细设置 |
|
39 |
* @property int $is_level 是否享受会员价购买 |
|
40 |
* @property int $is_level_alone 是否单独设置会员价 |
|
41 |
* @property int $share_type 佣金配比 0--固定金额 1--百分比 |
|
42 |
* @property string $sign 商品标示用于区分商品属于什么模块 |
|
43 |
* @property string $app_share_pic 自定义分享图片 |
|
44 |
* @property string $app_share_title 自定义分享标题 |
|
45 |
* @property int $is_default_services 默认服务 0.否|1.是 |
|
46 |
* @property int $payment_people 支付人数 |
|
47 |
* @property int $payment_num 支付件数 |
|
48 |
* @property string $payment_amount 支付金额 |
|
49 |
* @property int $payment_order 支付订单数 |
|
50 |
* @property int $form_id 自定义表单id 0--表示默认表单 -1--表示不使用表单 |
|
51 |
* @property int $confine_order_count |
|
52 |
* @property int $is_area_limit 是否单独区域购买 |
|
53 |
* @property string $area_limit |
|
54 |
* @property int $sales 商品实际销量 |
|
55 |
* @property int $limit_buy_status 限购状态0--关闭 1--开启 |
|
56 |
* @property string $limit_buy_type 限购类型 day--每天 week--每周 month--每月 all--永久 |
|
57 |
* @property int $limit_buy_value 限购数量 |
|
58 |
* @property int $min_number 起售数量 |
|
59 |
* @property int $is_setting_show_and_buy_auth 是否单独设置浏览和购买权限 |
|
60 |
* @property string $show_goods_auth 会员等级浏览权限 |
|
61 |
* @property string $buy_goods_auth 会员等级购买权限 |
|
62 |
* @property int $is_setting_send_type 是否单独设置发货方式0--否 1--是 |
|
63 |
* @property string $send_type 发货方式 |
|
64 |
* @property int $is_time 是否单独设置销售时间 |
|
65 |
* @property string $sell_begin_time 开售时间 |
|
66 |
* @property string $sell_end_time 结束时间 |
|
67 |
* |
|
68 |
* @property GoodsShare[] $share |
|
69 |
* @property GoodsAttr[] $attr |
|
70 |
* @property GoodsCards[] $cards |
|
71 |
* @property Coupon[] $coupons |
|
72 |
* @property GoodsServices[] $services |
|
73 |
* @property GoodsWarehouse $goodsWarehouse |
|
74 |
* @property MallGoods $mallGoods |
|
75 |
* @property MchGoods $mchGoods |
|
76 |
* @property Mch $mch |
|
77 |
* |
|
78 |
* @property string $name 商品名称 |
|
79 |
* @property string $subtitle 副标题 |
|
80 |
* @property string $originalPrice 原价 |
|
81 |
* @property string $costPrice 成本价 |
|
82 |
* @property string $detail 商品详情,图文 |
|
83 |
* @property string $coverPic 商品缩略图 |
|
84 |
* @property string $picUrl 商品轮播图 |
|
85 |
* @property string $videoUrl 商品视频 |
|
86 |
* @property string $unit 单位 |
|
87 |
* @property string $pageUrl 商品跳转链接 |
|
88 |
* @property GoodsShare $shareLevel |
|
89 |
* @property GoodsCardRelation $goodsCardRelation |
|
90 |
* @property int $detail_count 商品详情浏览量 |
|
91 |
* @property GoodsCouponRelation $goodsCouponRelation |
|
92 |
* @property string $guarantee_title 商品服务标题 |
|
93 |
* @property string $guarantee_pic 商品服务标识 |
|
94 |
* @property string $param_name 参数名称 |
|
95 |
* @property string $param_content 参数内容 |
|
96 |
*/ |
|
97 |
class Goods extends ModelActiveRecord |
|
98 |
{ |
|
99 |
// 商品编辑事件 |
|
100 |
const EVENT_EDIT = 'goodsEdit'; |
|
101 |
// 商品删除事件 |
|
102 |
const EVENT_DESTROY = 'goodsDestroy'; |
|
103 |
// 商品上下架事件 |
|
104 |
const EVENT_STATUS = 'goodsStatus'; |
|
105 |
|
|
106 |
/** |
|
107 |
* {@inheritdoc} |
|
108 |
*/ |
|
109 |
public static function tableName() |
|
110 |
{ |
|
111 |
return '{{%goods}}'; |
|
112 |
} |
|
113 |
|
|
114 |
/** |
|
115 |
* {@inheritdoc} |
|
116 |
*/ |
|
117 |
public function rules() |
|
118 |
{ |
|
119 |
return [ |
|
120 |
[['mall_id', 'goods_warehouse_id', 'attr_groups', 'freight_id', 'created_at', 'updated_at', |
|
121 |
'deleted_at'], 'required'], |
|
122 |
[['mall_id', 'mch_id', 'goods_warehouse_id', 'status', 'use_attr', 'goods_stock', 'virtual_sales', |
|
123 |
'confine_count', 'shipping_id', 'freight_id', 'give_integral', 'give_integral_type', |
|
124 |
'forehead_integral_type', 'accumulative', 'individual_share', 'attr_setting_type', 'is_level', |
|
125 |
'is_level_alone', 'share_type', 'is_default_services', 'sort', 'is_delete', 'payment_people', |
|
126 |
'payment_num', 'payment_order', 'confine_order_count', 'is_area_limit', 'form_id', 'sales', |
|
127 |
'detail_count', 'give_balance_type', 'limit_buy_status', 'limit_buy_value', 'min_number', |
|
128 |
'is_setting_show_and_buy_auth', 'is_setting_send_type', 'is_time'], 'integer'], |
|
129 |
[['price', 'forehead', 'forehead_integral', 'payment_amount', 'give_balance'], 'number'], |
|
130 |
[['attr_groups', 'area_limit', 'param_content', 'show_goods_auth', 'buy_goods_auth', |
|
131 |
'send_type'], 'string'], |
|
132 |
[['created_at', 'updated_at', 'deleted_at', 'sell_begin_time', 'sell_end_time'], 'safe'], |
|
133 |
[['sign', 'app_share_pic', 'param_name'], 'string', 'max' => 255], |
|
134 |
[['app_share_title'], 'string', 'max' => 65], |
|
135 |
[['guarantee_title'], 'string', 'max' => 64], |
|
136 |
[['guarantee_pic'], 'string', 'max' => 2048], |
|
137 |
[['limit_buy_type'], 'string', 'max' => 50], |
|
138 |
]; |
|
139 |
} |
|
140 |
|
|
141 |
/** |
|
142 |
* {@inheritdoc} |
|
143 |
*/ |
|
144 |
public function attributeLabels() |
|
145 |
{ |
|
146 |
return [ |
|
147 |
'id' => 'ID', |
|
148 |
'mall_id' => 'Mall ID', |
|
149 |
'mch_id' => 'Mch ID', |
|
150 |
'goods_warehouse_id' => 'Goods Warehouse ID', |
|
151 |
'status' => '上架状态:0=下架,1=上架', |
|
152 |
'price' => '售价', |
|
153 |
'use_attr' => '是否使用规格:0=不使用,1=使用', |
|
154 |
'attr_groups' => '商品规格组', |
|
155 |
'virtual_sales' => '已出售量', |
|
156 |
'created_at' => 'Created At', |
|
157 |
'updated_at' => 'Updated At', |
|
158 |
'deleted_at' => 'Deleted At', |
|
159 |
'is_delete' => 'Is Delete', |
|
160 |
'sort' => '排序', |
|
161 |
'goods_stock' => '商品总库存', |
|
162 |
'confine_count' => '购物数量限制', |
|
163 |
'shipping_id' => '包邮模板ID', |
|
164 |
'freight_id' => '运费模板ID', |
|
165 |
'give_integral' => '赠送积分', |
|
166 |
'give_integral_type' => '赠送积分类型1.固定值 |2.百分比', |
|
167 |
'give_balance' => '赠送余额', |
|
168 |
'give_balance_type' => '赠送余额类型1.固定值 |2.百分比', |
|
169 |
'forehead_integral' => '可抵扣积分', |
|
170 |
'forehead_integral_type' => '可抵扣积分类型1.固定值 |2.百分比', |
|
171 |
'accumulative' => '允许多件累计折扣', |
|
172 |
'individual_share' => '是否单独分销设置:0=否,1=是', |
|
173 |
'attr_setting_type' => '分销设置类型 0.普通设置|1.详细设置', |
|
174 |
'is_level' => '是否享受会员价购买', |
|
175 |
'is_level_alone' => '是否单独设置会员价', |
|
176 |
'share_type' => '佣金配比 0--固定金额 1--百分比', |
|
177 |
'sign' => '商品标示用于区分商品属于什么模块', |
|
178 |
'app_share_pic' => '自定义分享图片', |
|
179 |
'app_share_title' => '自定义分享标题', |
|
180 |
'is_default_services' => '默认服务 0.否|1.是', |
|
181 |
'payment_people' => '支付人数', |
|
182 |
'payment_num' => '支付件数', |
|
183 |
'payment_amount' => '支付金额', |
|
184 |
'payment_order' => '支付订单数', |
|
185 |
'form_id' => '自定义表单id 0--表示默认表单 -1--表示不使用表单', |
|
186 |
'confine_order_count' => '限单数量', |
|
187 |
'sales' => '商品实际销量', |
|
188 |
'detail_count' => '商品详情浏览量', |
|
189 |
'guarantee_title' => '商品服务标题', |
|
190 |
'guarantee_pic' => '商品服务标识', |
|
191 |
'param_name' => '参数名称', |
|
192 |
'param_content' => '参数内容', |
|
193 |
'limit_buy_status' => '限购状态0--关闭 1--开启', |
|
194 |
'limit_buy_type' => '限购类型 day--每天 week--每周 month--每月 all--永久', |
|
195 |
'limit_buy_value' => '限购数量', |
|
196 |
'min_number' => '起售数量', |
|
197 |
'is_setting_show_and_buy_auth' => '是否单独设置浏览和购买权限', |
|
198 |
'show_goods_auth' => '会员等级浏览权限', |
|
199 |
'buy_goods_auth' => '会员等级购买权限', |
|
200 |
'is_setting_send_type' => '是否单独设置发货方式0--否 1--是', |
|
201 |
'send_type' => '发货方式', |
|
202 |
'is_time' => '是否单独设置销售时间', |
|
203 |
'sell_begin_time' => '开售时间', |
|
204 |
'sell_end_time' => '结束时间', |
|
205 |
]; |
|
206 |
} |
|
207 |
|
|
208 |
public function getServices() |
|
209 |
{ |
|
210 |
return $this->hasMany(GoodsServices::className(), ['id' => 'service_id']) |
|
211 |
->viaTable(GoodsServiceRelation::tableName(), ['goods_id' => 'id'], function ($query) { |
|
212 |
$query->andWhere(['is_delete' => 0]); |
|
213 |
})->andWhere(['is_delete' => 0])->orderBy(['sort' => SORT_ASC]); |
|
214 |
} |
|
215 |
|
|
216 |
public function getCards() |
|
217 |
{ |
|
218 |
return $this->hasMany(GoodsCards::className(), ['id' => 'card_id']) |
|
219 |
->viaTable(GoodsCardRelation::tableName(), ['goods_id' => 'id'], function ($query) { |
|
220 |
$query->andWhere(['is_delete' => 0]); |
|
221 |
})->andWhere(['is_delete' => 0]); |
|
222 |
} |
|
223 |
|
|
224 |
public function getGoodsCardRelation() |
|
225 |
{ |
|
226 |
return $this->hasMany(GoodsCardRelation::className(), ['goods_id' => 'id'])->andWhere(['is_delete' => 0]); |
|
227 |
} |
|
228 |
|
|
229 |
public function getCoupons() |
|
230 |
{ |
|
231 |
return $this->hasMany(Coupon::className(), ['id' => 'coupon_id']) |
|
232 |
->viaTable(GoodsCouponRelation::tableName(), ['goods_id' => 'id'], function ($query) { |
|
233 |
$query->andWhere(['is_delete' => 0]); |
|
234 |
})->andWhere(['is_delete' => 0]); |
|
235 |
} |
|
236 |
|
|
237 |
public function getGoodsCouponRelation() |
|
238 |
{ |
|
239 |
return $this->hasMany(GoodsCouponRelation::className(), ['goods_id' => 'id'])->andWhere(['is_delete' => 0]); |
|
240 |
} |
|
241 |
|
|
242 |
public function getAttr() |
|
243 |
{ |
|
244 |
return $this->hasMany(GoodsAttr::className(), ['goods_id' => 'id'])->andWhere([ |
|
245 |
'is_delete' => 0, |
|
246 |
]); |
|
247 |
} |
|
248 |
|
|
249 |
public function getCart() |
|
250 |
{ |
|
251 |
return $this->hasMany(Cart::className(), ['goods_id' => 'id']); |
|
252 |
} |
|
253 |
|
|
254 |
public function getMch() |
|
255 |
{ |
|
256 |
return $this->hasOne(Mch::className(), ['id' => 'mch_id']); |
|
257 |
} |
|
258 |
|
|
259 |
/** |
|
260 |
* 将规格组重新排列组合成一维数组 |
|
261 |
* 键名为 attr_id:attr_id 例如 1:2 |
|
262 |
* @param $arr null|string|array |
|
263 |
* @return array |
|
264 |
*/ |
|
265 |
public function resetAttr($arr = null) |
|
266 |
{ |
|
267 |
if (!$arr) { |
|
268 |
$arr = Yii::$app->serializer->decode($this->attr_groups); |
|
269 |
} |
|
270 |
if (is_string($arr)) { |
|
271 |
$arr = Yii::$app->serializer->decode($arr); |
|
272 |
} |
|
273 |
if (!(is_array($arr) || is_object($arr))) { |
|
274 |
$arr = Yii::$app->serializer->decode($arr); |
|
275 |
} |
|
276 |
$result = []; |
|
277 |
foreach ($arr as $item) { |
|
278 |
$newItem = []; |
|
279 |
$result2 = []; |
|
280 |
foreach ($item['attr_list'] as $value) { |
|
281 |
$result1 = []; |
|
282 |
$str = $value['attr_id']; |
|
283 |
$newItem[$str] = [ |
|
284 |
[ |
|
285 |
'attr_group_name' => $item['attr_group_name'], |
|
286 |
'attr_group_id' => $item['attr_group_id'], |
|
287 |
'attr_id' => $value['attr_id'], |
|
288 |
'attr_name' => $value['attr_name'], |
|
289 |
] |
|
290 |
]; |
|
291 |
if (count($result) > 0) { |
|
292 |
foreach ($result as $key2 => $item2) { |
|
293 |
$str2 = $key2 . ':' . $str; |
|
294 |
$result1[$str2] = array_merge($item2, $newItem[$str]); |
|
295 |
} |
|
296 |
$result2 = array_merge($result2, $result1); |
|
297 |
} else { |
|
298 |
$result2 = $newItem; |
|
299 |
} |
|
300 |
} |
|
301 |
$result = $result2; |
|
302 |
} |
|
303 |
return $result; |
|
304 |
} |
|
305 |
|
|
306 |
/** |
|
307 |
* 单个sing_id获取相应规格 |
|
308 |
* @param $signId |
|
309 |
* @param null|string|array $arr |
|
310 |
* @return mixed |
|
311 |
*/ |
|
312 |
public function signToAttr($signId, $arr = null) |
|
313 |
{ |
|
314 |
$attrGroupList = $this->resetAttr($arr); |
|
315 |
return $attrGroupList[$signId]; |
|
316 |
} |
|
317 |
|
|
318 |
public function getShare() |
|
319 |
{ |
|
320 |
return $this->hasMany(GoodsShare::className(), ['goods_id' => 'id'])->where(['is_delete' => 0, 'level' => 0]); |
|
321 |
} |
|
322 |
|
|
323 |
public function getMchGoods() |
|
324 |
{ |
|
325 |
return $this->hasOne(MchGoods::className(), ['goods_id' => 'id']); |
|
326 |
} |
|
327 |
|
|
328 |
public function getGoodsWarehouse() |
|
329 |
{ |
|
330 |
return $this->hasOne(GoodsWarehouse::className(), ['id' => 'goods_warehouse_id']); |
|
331 |
} |
|
332 |
|
|
333 |
public function getMallGoods() |
|
334 |
{ |
|
335 |
return $this->hasOne(MallGoods::className(), ['goods_id' => 'id']); |
|
336 |
} |
|
337 |
|
|
338 |
public function getName() |
|
339 |
{ |
|
340 |
return $this->goodsWarehouse->name; |
|
341 |
} |
|
342 |
|
|
343 |
public function getSubtitle() |
|
344 |
{ |
|
345 |
return $this->goodsWarehouse->subtitle; |
|
346 |
} |
|
347 |
|
|
348 |
public function getOriginalPrice() |
|
349 |
{ |
|
350 |
return $this->goodsWarehouse->original_price; |
|
351 |
} |
|
352 |
|
|
353 |
public function getCostPrice() |
|
354 |
{ |
|
355 |
return $this->goodsWarehouse->cost_price; |
|
356 |
} |
|
357 |
|
|
358 |
public function getDetail() |
|
359 |
{ |
|
360 |
return $this->goodsWarehouse->detail; |
|
361 |
} |
|
362 |
|
|
363 |
public function getCoverPic() |
|
364 |
{ |
|
365 |
return $this->goodsWarehouse->cover_pic; |
|
366 |
} |
|
367 |
|
|
368 |
public function getPicUrl() |
|
369 |
{ |
|
370 |
return $this->goodsWarehouse->pic_url; |
|
371 |
} |
|
372 |
|
|
373 |
public function getVideoUrl() |
|
374 |
{ |
|
375 |
return $this->goodsWarehouse->video_url; |
|
376 |
} |
|
377 |
|
|
378 |
public function getUnit() |
|
379 |
{ |
|
380 |
return $this->goodsWarehouse->unit; |
|
381 |
} |
|
382 |
|
|
383 |
public function getPrice() |
|
384 |
{ |
|
385 |
return $this->price; |
|
386 |
} |
|
387 |
|
|
388 |
public function getSales() |
|
389 |
{ |
|
390 |
return $this->sales; |
|
391 |
} |
|
392 |
|
|
393 |
/** |
|
394 |
* @return string |
|
395 |
*/ |
|
396 |
public function getPageUrl() |
|
397 |
{ |
|
398 |
try { |
|
399 |
if ($this->mch_id) { |
|
400 |
$plugins = \Yii::$app->plugin->getPlugin('mch'); |
|
401 |
} elseif ($this->sign) { |
|
402 |
$plugins = \Yii::$app->plugin->getPlugin($this->sign); |
|
403 |
} else { |
|
404 |
throw new \Exception('商城商品'); |
|
405 |
} |
|
406 |
if (!method_exists($plugins, 'getGoodsUrl')) { |
|
407 |
throw new \Exception('不存在getGoodsUrl方法'); |
|
408 |
} |
|
409 |
$pageUrl = $plugins->getGoodsUrl(['id' => $this->id, 'mch_id' => $this->mch_id]); |
|
410 |
} catch (\Exception $exception) { |
|
411 |
$pageUrl = '/pages/goods/goods?id=' . $this->id; |
|
412 |
} |
|
413 |
return $pageUrl; |
|
414 |
} |
|
415 |
|
|
416 |
public function getShareLevel() |
|
417 |
{ |
|
418 |
return $this->hasMany(GoodsShare::className(), ['goods_id' => 'id'])->where(['is_delete' => 0]); |
|
419 |
} |
|
420 |
} |