WorkOrder.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace App;
  3. use App\Traits\ModelTimeFormat;
  4. use Illuminate\Database\Eloquent\Model;
  5. use App\Traits\ModelLogChanging;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Database\Eloquent\SoftDeletes;
  8. class WorkOrder extends Model
  9. {
  10. use ModelLogChanging;
  11. use ModelTimeFormat;
  12. use SoftDeletes;
  13. // 工单 信息
  14. 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'];
  15. static public $enums = [
  16. 'status' => [
  17. '' => 0,
  18. '待审核' => 1,
  19. '已处理' => 2,
  20. ],
  21. 'grad' => [
  22. '' => 0,
  23. '一般' => 1,
  24. '重要' => 2,
  25. '紧急' => 3,
  26. '重要且紧急' => 4,
  27. ],
  28. ];
  29. function __construct(array $attributes = [])
  30. {
  31. foreach (self::$enums as &$enum) {
  32. $enum = $enum + array_flip($enum);
  33. }
  34. parent::__construct($attributes);
  35. }
  36. public function getStatusAttribute($value)
  37. {
  38. if (!$value) return '';
  39. return self::$enums['status'][$value];
  40. }
  41. public function setStatusAttribute($value)
  42. {
  43. if (!$value) return 0;
  44. if (is_numeric($value)) {
  45. $this->attributes['status'] = $value;
  46. } else {
  47. $this->attributes['status'] = self::$enums['status'][$value];
  48. }
  49. }
  50. public function getGradAttribute($value)
  51. {
  52. if (!$value) return '';
  53. return self::$enums['grad'][$value];
  54. }
  55. public function setGradAttribute($value)
  56. {
  57. if (!$value) return 0;
  58. if (is_numeric($value)) {
  59. $this->attributes['grad'] = $value;
  60. } else {
  61. $this->attributes['grad'] = self::$enums['grad'][$value];
  62. }
  63. }
  64. // 创建人
  65. public function creator(): BelongsTo
  66. {
  67. return $this->belongsTo(User::class, 'creator_id');
  68. }
  69. // 审核人
  70. public function reviewer(): BelongsTo
  71. {
  72. return $this->belongsTo(User::class, 'reviewer_id');
  73. }
  74. // 关联订单
  75. public function order(): BelongsTo
  76. {
  77. return $this->belongsTo(Order::class, 'outer_table_id');
  78. }
  79. // 类型
  80. public function type(): BelongsTo
  81. {
  82. return $this->belongsTo(WorkOrderType::class, 'work_order_type_id', 'id');
  83. }
  84. // 子类型
  85. public function childType(): BelongsTo
  86. {
  87. return $this->belongsTo(WorkOrderChildType::class, 'work_order_child_type_id', 'id');
  88. }
  89. public function scopeFilter($query, $filters)
  90. {
  91. return $filters->apply($query);
  92. }
  93. }