212 lines
4.5 KiB
PHP
212 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\Mall\Models\Traits;
|
|
|
|
use Modules\Mall\Models\Goods;
|
|
use Modules\User\Models\User;
|
|
|
|
trait GoodsAttribute
|
|
{
|
|
|
|
/**
|
|
* Notes: 获取商品库存
|
|
*
|
|
* @Author: 玄尘
|
|
* @Date : 2021/5/14 11:17
|
|
* @param int $user_id
|
|
* @return int
|
|
*/
|
|
public function getGoodsStock(int $user_id = 0): int
|
|
{
|
|
if ($this->goods->type == Goods::TYPE_SINGLE) {
|
|
|
|
}
|
|
return $this->stock ?? 0;
|
|
}
|
|
|
|
/**
|
|
* Notes: 扣除库存方法
|
|
*
|
|
* @Author: 玄尘
|
|
* @Date : 2021/5/14 11:12
|
|
* @param $stock
|
|
* @param $user_id
|
|
* @return bool
|
|
*/
|
|
public function deductStock($stock, $user_id): bool
|
|
{
|
|
if ($this->goods->channel == Goods::CHANNEL_FREE) {
|
|
$user = User::find($user_id);
|
|
$user->addHold($stock);
|
|
} else {
|
|
$this->decrement('stock', $stock);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Notes: 增加库存方法
|
|
*
|
|
* @Author: 玄尘
|
|
* @Date : 2021/5/14 11:12
|
|
* @param $stock
|
|
*/
|
|
public function addStock($stock)
|
|
{
|
|
$this->increment('stock', $stock);
|
|
}
|
|
|
|
/**
|
|
* Notes: 增加销量
|
|
*
|
|
* @Author: 玄尘
|
|
* @Date : 2021/5/14 11:12
|
|
* @param $sold
|
|
*/
|
|
public function addSold($sold)
|
|
{
|
|
$this->goods()->increment('sales', $sold);
|
|
}
|
|
|
|
/**
|
|
* Notes: 减少销量
|
|
*
|
|
* @Author: 玄尘
|
|
* @Date : 2021/5/14 11:12
|
|
* @param $sold
|
|
*/
|
|
public function deductSold($sold)
|
|
{
|
|
$this->goods()->decrement('sales', $sold);
|
|
}
|
|
|
|
/**
|
|
* Notes: 获取卖家ID
|
|
*
|
|
* @Author: 玄尘
|
|
* @Date : 2021/5/14 11:14
|
|
* @return int
|
|
*/
|
|
public function getShopIdAttribute(): int
|
|
{
|
|
return $this->goods->shop_id ?? 0;
|
|
}
|
|
|
|
/**
|
|
* Notes: 获取卖家type
|
|
*
|
|
* @Author: 玄尘
|
|
* @Date : 2021/5/14 11:14
|
|
* @return string|null
|
|
*/
|
|
public function getShopTypeAttribute(): ?string
|
|
{
|
|
if ($this->goods && $this->goods->shop) {
|
|
return get_class($this->goods->shop);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Notes: 获取卖家名称
|
|
*
|
|
* @Author: 玄尘
|
|
* @Date : 2021/5/14 11:14
|
|
* @return false|string|null
|
|
*/
|
|
public function getShopNameAttribute()
|
|
{
|
|
if ($this->goods && $this->goods->shop) {
|
|
return $this->goods->shop->name;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Notes: 重量
|
|
*
|
|
* @Author: 玄尘
|
|
* @Date : 2021/5/14 11:15
|
|
* @return int
|
|
*/
|
|
public function getGoodsWeight(): int
|
|
{
|
|
return $this->weight ?? 0;
|
|
}
|
|
|
|
/**
|
|
* Notes: 返回source
|
|
*
|
|
* @Author: 玄尘
|
|
* @Date : 2021/5/14 11:15
|
|
* @return array
|
|
*/
|
|
public function getSource(): array
|
|
{
|
|
return [
|
|
'goods_sku_id' => $this->id,
|
|
'goods_id' => $this->goods_id,
|
|
'deduct_stock_type' => $this->goods->deduct_stock_type,
|
|
'goods_name' => $this->getGoodsName(),
|
|
// 'unit' => $this->getItemValue(),
|
|
'cover' => $this->goods->cover_url,
|
|
'price' => $this->getItemPrice(),
|
|
'stock' => $this->getGoodsStock(),
|
|
'weight' => $this->weight,
|
|
'is_post_sale' => $this->goods->is_post_sale ?? 0,
|
|
'score' => $this->score, // 积分/原石
|
|
'assets' => $this->assets, // 资产
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Notes: 获取商品名称
|
|
*
|
|
* @Author: 玄尘
|
|
* @Date : 2021/5/14 11:19
|
|
* @return string
|
|
*/
|
|
public function getGoodsName(): string
|
|
{
|
|
return $this->goods->name ?? '--';
|
|
}
|
|
|
|
/**
|
|
* Notes: 获取商品规格名称
|
|
*
|
|
* @Author: 玄尘
|
|
* @Date : 2021/5/14 11:16
|
|
* @return string
|
|
*/
|
|
public function getItemValue(): string
|
|
{
|
|
switch ($this->goods->type) {
|
|
case Goods::TYPE_SINGLE:
|
|
return '';
|
|
case Goods::TYPE_MULTIPLE:
|
|
return $this->unit ?? '';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Notes: 获取商品单价
|
|
*
|
|
* @Author: 玄尘
|
|
* @Date : 2021/5/14 11:16
|
|
* @return mixed
|
|
*/
|
|
public function getItemPrice()
|
|
{
|
|
$tags = $this->goods->tags()->pluck('tag_id')->toArray();
|
|
//1 为试用产品
|
|
if (in_array(1, $tags)) {
|
|
return 0;
|
|
}
|
|
|
|
return $this->price;
|
|
}
|
|
|
|
}
|