bztang-admin/plugins/red-packet/src/models/RedPacketGoodsModel.php

68 lines
1.7 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: yunzhong
* Date: 2019/10/9
* Time: 15:52
*/
namespace Yunshop\RedPacket\models;
use app\common\models\BaseModel;
use Illuminate\Database\Eloquent\SoftDeletes;
use app\frontend\models\Goods;
class RedPacketGoodsModel extends BaseModel
{
use SoftDeletes;
public $table = 'yz_red_packet_goods';
public $timestamps = true;
protected $guarded = [''];
protected $search_fields = ['title'];
public static function relationSave($goodsId, $data, $operate)
{
\Log::info('红包挂件');
if (!$goodsId) {
return false;
}
if (!$data) {
return false;
}
$RedPacketGoodsModel = self::getModel($goodsId, $operate);
//判断deleted
if ($operate == 'deleted') {
return $RedPacketGoodsModel->delete();
}
$data['goods_id'] = $goodsId;
$data['uniacid'] = \YunShop::app()->uniacid;
$data['is_open'] = $data['is_open'] ?$data['is_open'] : 0;
$data['scale'] = $data['scale'] ?$data['scale'] : 0;
$RedPacketGoodsModel->setRawAttributes($data);
return $RedPacketGoodsModel->save();
}
public static function getModel($goodsId, $operate)
{
$model = false;
if ($operate != 'created') {
$model = static::where(['goods_id' => $goodsId])->first();
}
!$model && $model = new static;
return $model;
}
public function getGoodsSet($goodsId)
{
return self::where('goods_id', $goodsId);
}
//关联商品信息
public function belongsToGoods()
{
return $this->belongsTo(Goods::class, 'goods_id', 'id');
}
}