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

225 lines
5.1 KiB
PHP

<?php
namespace App\Models;
use App\Models\Agency;
use App\Models\Category;
use App\Models\SellerLesson;
use App\User;
use Illuminate\Database\Eloquent\SoftDeletes;
use RuLong\Order\Models\Order;
use RuLong\Panel\Models\Admin;
use RuLong\Panel\Models\Storage;
use \RuLong\Area\Models\OpenArea;
/**
* 商户模型
*/
class Seller extends Model
{
use SoftDeletes;
protected $casts = [
'storage_ids' => 'array',
'cert_ids' => 'array',
];
public static function boot()
{
parent::boot();
// self::created(function ($model) {
// $area_ids = Area::where('type', '省级')->pluck('id');
// foreach ($area_ids as $area_id) {
// $model->freights()->create([
// 'area_id' => $area_id,
// ]);
// }
// });
}
public function admins()
{
return $this->hasMany(Admin::class);
}
public function storage()
{
return $this->belongsTo(Storage::class)->withDefault();
}
public function agency()
{
return $this->belongsTo(Agency::class)->withDefault(['name' => '未设置']);
}
//返回多图
public function getStoragesAttribute()
{
$list = Storage::whereIn('id', $this->storage_ids)->get();
return $list;
}
//返回多图 资质
public function getCertsAttribute()
{
$list = Storage::whereIn('id', $this->cert_ids)->get();
return $list;
}
//封面
public function cover()
{
return $this->belongsTo(Storage::class, 'cover_id', 'id')->withDefault();
}
//微信二维码
public function wechat()
{
return $this->belongsTo(Storage::class, 'wechat_id', 'id')->withDefault();
}
public function freights()
{
return $this->hasMany(Freight::class);
}
public function getGoodsFirst()
{
$goods = Goods::where('seller_id', $this->id)
->whereHas('params', function ($query) {return $query->where('stock', '>', 0)->where('status', 1);})
->where('status', 1)
->orderBy('created_at', 'desc')
->first();
if ($goods) {
return $goods->storage->path ?? '';
} else {
return '';
}
}
public function goods()
{
return $this->hasMany(Goods::class);
}
public function orders()
{
return $this->hasMany(Order::class);
}
/**
* 是否可删除
* @Author:<ZhaoxiaWang>
* @Date:2018-11-30
* @return boolean
*/
public function canDel(): bool
{
return $this->status == 1 && count($this->orders) == 0 && count($this->goods) == 0 ? true : false;
}
/**
* 是否可关闭
* @Author:<ZhaoxiaWang>
* @Date:2018-11-30
* @return boolean
*/
public function canCancel(): bool
{
return $this->status == 1 && $this->goods()->where('status', 1)->count() == 0 ? true : false;
}
protected function getStatusTextAttribute()
{
return $this->status == 1 ? "<span style='color:green'>正常</span>" : "<span style='color:red'>锁定</span>";
}
protected function getTypeTextAttribute()
{
switch ($this->type) {
case 'organ':
return '机构';
break;
case 'seller':
return '商家';
break;
default:
return '未知';
break;
}
}
public function user()
{
return $this->belongsTo(User::class);
}
public function reports()
{
return $this->hasMany(WindupReport::class);
}
protected function getAllAddressAttribute()
{
return str_replace(",", "-", $this->Area->info) . '-' . $this->address;
}
public function Area()
{
return $this->belongsTo(OpenArea::class, 'area_sn', 'sn');
}
public function Province()
{
return $this->belongsTo(OpenArea::class, 'province_sn', 'sn');
}
public function category()
{
return $this->belongsTo(Category::class)->withDefault();
}
public function lesson()
{
return $this->hasMany(SellerLesson::class);
}
public function getTitle()
{
return $this->name;
}
public function salesman()
{
return $this->belongsTo(User::class, 'salesman_id', 'id')->withDefault();
}
//获取屏蔽分类id
public function getTopCateIdAttribute()
{
$cateids = $this->lesson()->pluck('category_id')->toArray();
$parenids = [];
if ($cateids) {
foreach ($cateids as $key => $id) {
$parenids[] = Category::findTop($id, 1);
}
} else {
return [];
}
$findcateids = Category::whereIn('parent_id', $parenids)->OrwhereIn('id', $parenids)->pluck('id')->toArray();
return $findcateids;
}
//机构报课数
public function getLessonLogsCountAttribute()
{
$lessons = $this->lesson()->with(['logs'])->get();
return $lessons->sum('logs_count');
}
}