store-manage/app/Models/Store.php

104 lines
2.6 KiB
PHP

<?php
namespace App\Models;
use App\Enums\BusinessStatus;
use App\Traits\HasDateTimeFormatter;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* 门店
*/
class Store extends Model
{
use Filterable, HasDateTimeFormatter, HasFactory;
protected $attributes = [
'profit_ratio' => 0,
];
protected $fillable = ['title', 'master_id', 'category_id', 'business_id', 'level_id', 'region', 'location', 'lon', 'lat', 'address', 'profit_ratio', 'profit_money', 'business_status'];
protected $casts = [
// 地区 {province: 四川省, city: 成都市}
'region' => 'json',
// {address: 重庆市綦江区古南街道中山路1号, lng: 106.65781638883, lat: 29.033745284277, city: 重庆市, vendor: baidu}
'location' => 'json',
'business_status' => BusinessStatus::class,
];
protected $appends = ['business_status_text', 'business_status_color'];
public function modelFilter()
{
return \App\Admin\Filters\StoreFilter::class;
}
public function scopeOnlyOpen(Builder $query)
{
$query->where('business_status', BusinessStatus::Open);
}
// 店长
public function master()
{
return $this->belongsTo(Employee::class, 'master_id');
}
// 门店分类
public function category()
{
return $this->belongsTo(Keyword::class, 'category_id', 'key');
}
// 经验分类
public function business()
{
return $this->belongsTo(Keyword::class, 'business_id', 'key');
}
// 门店等级
public function level()
{
return $this->belongsTo(Keyword::class, 'level_id', 'key');
}
// 门店员工
public function employees()
{
// role(1: 店长, 2: 店员)
return $this->hasMany(Employee::class, 'store_id');
}
public function ledgers()
{
return $this->hasMany(Ledger::class);
}
/**
* 确认此门店是否是彩票店
*/
public function isLotteryStore(): bool
{
return preg_match('/^store_category_lottery_/', $this->category_id);
}
protected function businessStatusText(): Attribute
{
return new Attribute(
get: fn () => $this->business_status ? $this->business_status->text() : '',
);
}
protected function businessStatusColor(): Attribute
{
return new Attribute(
get: fn () => $this->business_status ? $this->business_status->color() : '',
);
}
}