0
0
Files
Babyclass/app/Models/SellerLesson.php
2020-08-04 10:09:42 +08:00

137 lines
2.8 KiB
PHP

<?php
namespace App\Models;
use App\Models\SellerLessonLog;
use Auth;
use RuLong\Order\Contracts\Orderable;
use RuLong\Panel\Models\Storage;
class SellerLesson extends Model implements Orderable
{
protected $dates = [
'start_at',
'end_at',
];
public function organ()
{
return $this->belongsTo(Seller::class, 'seller_id', 'id')->withDefault();
}
public function storage()
{
return $this->belongsTo(Storage::class)->withDefault();
}
public function category()
{
return $this->belongsTo(Category::class)->withDefault();
}
protected function getStatusTextAttribute()
{
if ($this->status == 1) {
if ($this->start_at->timestamp > time()) {
return "<span style='color:blue'>还没开始</span>";
} elseif ($this->end_at->timestamp < time()) {
return "<span style='color:yellow'>已经结束</span>";
}
return "<span style='color:green'>正常</span>";
} else {
return "<span style='color:red'>关闭</span>";
}
}
public function logs()
{
return $this->HasMany(SellerLessonLog::class, 'lesson_id', 'id');
}
public function getTitle()
{
return $this->title;
}
public function getPrice()
{
return $this->price;
}
public function getScore()
{
return 0;
}
public function getStock()
{
return $this->stock;
}
public function getSellerPrice()
{
return $this->price;
}
public function deductStock($stock)
{
$this->decrement('stock', $stock);
}
public function addStock($stock)
{
$this->increment('stock', $stock);
}
public function getStorage()
{
return $this->storage;
}
/**
* 可报名
* @return boolean
*/
public function canCart(): bool
{
return Cart::where('user_id', Auth::id())->where('lesson_id', $this->id)->count();
}
protected function getCanCartTextAttribute()
{
return $this->canCart();
}
//是否已购买
public function getBuyTextAttribute()
{
return $this->logs()->where('lesson_id', $this->id)->where('status', 1)->where('user_id', Auth::id())->count() ?? 0;
}
//是否已购买
public function getButtonTextAttribute()
{
if ($this->buy_text) {
return "学习中";
} elseif ($this->canCart()) {
return "已报名";
} else {
return "立即报名";
}
}
//是否已购买
public function getButtonValueAttribute()
{
if ($this->buy_text) {
return 1;
} elseif ($this->canCart()) {
return 2;
} else {
return 0;
}
}
}