generated from liutk/owl-admin-base
76 lines
1.9 KiB
PHP
76 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\TaskPerformanceStatus;
|
|
use App\Traits\HasDateTimeFormatter;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne;
|
|
|
|
class TaskPerformance extends Model
|
|
{
|
|
use HasFactory, HasDateTimeFormatter;
|
|
|
|
protected $appends = [
|
|
'task_status',
|
|
];
|
|
|
|
protected $fillable = [
|
|
'month',
|
|
'store_id',
|
|
'store_master_id',
|
|
'expected_performance',
|
|
'actual_performance',
|
|
];
|
|
|
|
public function task(): MorphOne
|
|
{
|
|
return $this->morphOne(Task::class, 'taskable');
|
|
}
|
|
|
|
public function store(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Store::class);
|
|
}
|
|
|
|
public function storeMaster(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Employee::class, 'store_master_id');
|
|
}
|
|
|
|
/**
|
|
* 此业绩指标是否已完成
|
|
*/
|
|
public function isSuccess(): bool
|
|
{
|
|
return bccomp($this->actual_performance, $this->expected_performance, 2) >= 0;
|
|
}
|
|
|
|
/**
|
|
* 任务状态
|
|
*/
|
|
protected function taskStatus(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: function (mixed $value, array $attributes) {
|
|
if ($this->isSuccess()) {
|
|
return TaskPerformanceStatus::Success;
|
|
}
|
|
|
|
// 当前时间
|
|
$datetime = now();
|
|
|
|
if ($datetime->lt($this->task->start_at)) {
|
|
return TaskPerformanceStatus::None;
|
|
} elseif ($datetime->gte($this->task->end_at)) {
|
|
return TaskPerformanceStatus::Failed;
|
|
}
|
|
return TaskPerformanceStatus::Pending;
|
|
},
|
|
);
|
|
}
|
|
}
|