generated from liutk/owl-admin-base
72 lines
2.0 KiB
PHP
72 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Casts\StorageJson;
|
|
use App\Enums\EmployeeStatus;
|
|
use App\Traits\HasDateTimeFormatter;
|
|
use EloquentFilter\Filterable;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Slowlyo\OwlAdmin\Models\AdminUser;
|
|
|
|
/**
|
|
* 员工
|
|
*/
|
|
class Employee extends Model
|
|
{
|
|
use Filterable, HasDateTimeFormatter;
|
|
|
|
const JOB_KEY = 'job';
|
|
|
|
protected $fillable = ['name', 'phone', 'prize_images', 'skill_images', 'employee_status', 'admin_user_id', 'leave_at', 'join_at', 'remarks'];
|
|
|
|
protected $casts = [
|
|
'employee_status' => EmployeeStatus::class,
|
|
'prize_images' => 'json',
|
|
'skill_images' => 'json',
|
|
'leave_at' => 'datetime',
|
|
'join_at' => 'date:Y-m-d',
|
|
];
|
|
|
|
protected $attributes = [
|
|
'employee_status' => EmployeeStatus::Online,
|
|
];
|
|
|
|
protected $appends = ['employee_status_text', 'employee_status_color'];
|
|
|
|
// 拥有的职位
|
|
public function jobs()
|
|
{
|
|
// $related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null, $parentKey = null, $relatedKey = null, $relation = null
|
|
return $this->belongsToMany(Keyword::class, 'employee_jobs', 'employee_id', 'job', null, 'key');
|
|
}
|
|
|
|
// 关联登录账户
|
|
public function adminUser()
|
|
{
|
|
return $this->belongsTo(AdminUser::class, 'admin_user_id');
|
|
}
|
|
|
|
// 关联的门店
|
|
public function stores()
|
|
{
|
|
// role(1: 店长, 2: 店员)
|
|
return $this->belongsToMany(Store::class, 'store_employees', 'employee_id', 'store_id')->withPivot(['role']);
|
|
}
|
|
|
|
protected function employeeStatusText(): Attribute
|
|
{
|
|
return new Attribute(
|
|
get: fn () => $this->employee_status ? $this->employee_status->text() : '',
|
|
);
|
|
}
|
|
|
|
protected function employeeStatusColor(): Attribute
|
|
{
|
|
return new Attribute(
|
|
get: fn () => $this->employee_status ? $this->employee_status->color() : '',
|
|
);
|
|
}
|
|
}
|