| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- namespace App;
- use App\Traits\ModelTimeFormat;
- use Illuminate\Database\Eloquent\Model;
- use App\Traits\ModelLogChanging;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\SoftDeletes;
- class WorkOrder extends Model
- {
- use ModelLogChanging;
- use ModelTimeFormat;
- use SoftDeletes;
- // 工单 信息
- protected $fillable = ['status', 'creator_id', 'reviewer_id', 'work_order_type_id', 'work_order_child_type_id', 'grad', 'remark', 'outer_table_name', 'outer_table_id', 'review_at'];
- static public $enums = [
- 'status' => [
- '' => 0,
- '待审核' => 1,
- '已处理' => 2,
- ],
- 'grad' => [
- '' => 0,
- '一般' => 1,
- '重要' => 2,
- '紧急' => 3,
- '重要且紧急' => 4,
- ],
- ];
- function __construct(array $attributes = [])
- {
- foreach (self::$enums as &$enum) {
- $enum = $enum + array_flip($enum);
- }
- parent::__construct($attributes);
- }
- public function getStatusAttribute($value)
- {
- if (!$value) return '';
- return self::$enums['status'][$value];
- }
- public function setStatusAttribute($value)
- {
- if (!$value) return 0;
- if (is_numeric($value)) {
- $this->attributes['status'] = $value;
- } else {
- $this->attributes['status'] = self::$enums['status'][$value];
- }
- }
- public function getGradAttribute($value)
- {
- if (!$value) return '';
- return self::$enums['grad'][$value];
- }
- public function setGradAttribute($value)
- {
- if (!$value) return 0;
- if (is_numeric($value)) {
- $this->attributes['grad'] = $value;
- } else {
- $this->attributes['grad'] = self::$enums['grad'][$value];
- }
- }
- // 创建人
- public function creator(): BelongsTo
- {
- return $this->belongsTo(User::class, 'creator_id');
- }
- // 审核人
- public function reviewer(): BelongsTo
- {
- return $this->belongsTo(User::class, 'reviewer_id');
- }
- // 关联订单
- public function order(): BelongsTo
- {
- return $this->belongsTo(Order::class, 'outer_table_id');
- }
- // 类型
- public function type(): BelongsTo
- {
- return $this->belongsTo(WorkOrderType::class, 'work_order_type_id', 'id');
- }
- // 子类型
- public function childType(): BelongsTo
- {
- return $this->belongsTo(WorkOrderChildType::class, 'work_order_child_type_id', 'id');
- }
- public function scopeFilter($query, $filters)
- {
- return $filters->apply($query);
- }
- }
|