更新代码
This commit is contained in:
164
app/Models/Goods.php
Normal file
164
app/Models/Goods.php
Normal file
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Admin;
|
||||
use App\Contracts\Advertable;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use RuLong\Order\Contracts\Orderable;
|
||||
use RuLong\Order\Models\OrderDetail;
|
||||
use RuLong\Panel\Models\Storage;
|
||||
|
||||
class Goods extends Model implements Orderable, Advertable
|
||||
{
|
||||
|
||||
use SoftDeletes;
|
||||
|
||||
public function link()
|
||||
{
|
||||
return route('goods.show', $this);
|
||||
}
|
||||
|
||||
public function scopeMine($query)
|
||||
{
|
||||
return $query->where('seller_id', Admin::user()->seller_id);
|
||||
}
|
||||
|
||||
public function seller()
|
||||
{
|
||||
return $this->belongsTo(Seller::class)->withDefault();
|
||||
}
|
||||
|
||||
public function storage()
|
||||
{
|
||||
return $this->belongsTo(Storage::class)->withDefault();
|
||||
}
|
||||
|
||||
public function banner()
|
||||
{
|
||||
return $this->belongsTo(Storage::class, 'banner_id', 'id')->withDefault();
|
||||
}
|
||||
|
||||
public function category()
|
||||
{
|
||||
return $this->belongsTo(Category::class)->withDefault();
|
||||
}
|
||||
|
||||
public function params()
|
||||
{
|
||||
return $this->hasMany(GoodsParams::class, 'goods_id', 'id');
|
||||
}
|
||||
|
||||
public function getTitle()
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
public function getPrice()
|
||||
{
|
||||
return $this->price;
|
||||
}
|
||||
|
||||
public function getScore()
|
||||
{
|
||||
return $this->score;
|
||||
}
|
||||
|
||||
public function getStock()
|
||||
{
|
||||
return $this->stock;
|
||||
}
|
||||
|
||||
public function getSellerPrice()
|
||||
{
|
||||
return $this->cost;
|
||||
}
|
||||
|
||||
public function deductStock($stock)
|
||||
{
|
||||
$this->decrement('stock', $stock);
|
||||
}
|
||||
|
||||
public function addStock($stock)
|
||||
{
|
||||
$this->increment('stock', $stock);
|
||||
}
|
||||
|
||||
protected function getMinPriceAttribute()
|
||||
{
|
||||
return number_format($this->params()->where('status', 1)->min('price'), 2);
|
||||
}
|
||||
|
||||
protected function getMinOriginalAttribute()
|
||||
{
|
||||
return number_format($this->params()->where('status', 1)->min('original'), 2);
|
||||
}
|
||||
|
||||
protected function getMinBaoAttribute()
|
||||
{
|
||||
return number_format($this->params()->where('status', 1)->min('taobao'), 2);
|
||||
}
|
||||
|
||||
protected function getMaxScoreAttribute()
|
||||
{
|
||||
return number_format($this->params()->where('status', 1)->max('score'), 2);
|
||||
}
|
||||
|
||||
public function cashScore()
|
||||
{
|
||||
return $this->params()->where('status', 1)->where('stock', '>', 0)->where('status', 1)->where('score', '>', 0)->whereRaw('price > score')->orderBy('score', 'desc')->first();
|
||||
}
|
||||
|
||||
protected function getScoreRangeAttribute()
|
||||
{
|
||||
$params = $this->params()->where('status', 1)->orderBy('score', 'desc')->distinct()->pluck('score');
|
||||
$params_size = count($params);
|
||||
if ($params_size == 1) {
|
||||
return number_format($params[0], 2);
|
||||
} elseif ($params_size > 1) {
|
||||
if ($params[$params_size - 1] > 0) {
|
||||
return number_format($params[0], 2) . '~' . number_format($params[$params_size - 1], 2);
|
||||
} else {
|
||||
return number_format($params[0], 2);
|
||||
}
|
||||
} else {
|
||||
return 0.00;
|
||||
}
|
||||
}
|
||||
|
||||
protected function getStatusTextAttribute()
|
||||
{
|
||||
return $this->status == 1 ? "<span style='color:green'>正常</span>" : "<span style='color:red'>已下架</span>";
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否可删除
|
||||
* @Author:<ZhaoxiaWang>
|
||||
* @Date:2018-12-10
|
||||
* @return boolean
|
||||
*/
|
||||
|
||||
public function canDel(): bool
|
||||
{
|
||||
$orderDetails = OrderDetail::whereIn('item_id', $this->params()->pluck('id'))->where('item_type', 'App\Models\GoodsParams')->first();
|
||||
return $this->status == 1 && empty($orderDetails);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否可下架
|
||||
* @Author:<ZhaoxiaWang>
|
||||
* @Date:2018-12-10
|
||||
* @return boolean
|
||||
*/
|
||||
|
||||
public function canCancel(): bool
|
||||
{
|
||||
return $this->status == 1 ? true : false;
|
||||
}
|
||||
|
||||
public function isScore(): bool
|
||||
{
|
||||
$params = $this->params()->where('status', 1)->where('score', '>', '0')->get();
|
||||
return $params->count() > 0 ? true : false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user