WorkOrder.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace App;
  3. use App\Services\NotificationService;
  4. use App\Traits\ModelTimeFormat;
  5. use Illuminate\Database\Eloquent\Builder;
  6. use Illuminate\Database\Eloquent\Model;
  7. use App\Traits\ModelLogChanging;
  8. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  9. use Illuminate\Database\Eloquent\SoftDeletes;
  10. use Illuminate\Support\Facades\Auth;
  11. use Illuminate\Support\Facades\Gate;
  12. class WorkOrder extends Model
  13. {
  14. use ModelLogChanging;
  15. use ModelTimeFormat;
  16. use SoftDeletes;
  17. // 工单 信息
  18. protected $fillable = [
  19. 'status', // 状态
  20. 'creator_id', // 创建人
  21. 'reviewer_id', // 审核人
  22. 'owner_id', // 货主
  23. 'order_issue_type_id', // 问题件类型
  24. 'work_order_type_id', // 工单类型
  25. 'grad', // 紧急等级
  26. 'remark', // 工单信息描述
  27. 'outer_table_name', // 链接表名
  28. 'review_at', // 审核时间
  29. 'order_id', // 订单id
  30. 'uniquely_tag', // 唯一标识
  31. ];
  32. static public $enums = [
  33. 'status' => [
  34. '' => 0,
  35. '待审核' => 1,
  36. '已处理' => 2,
  37. ],
  38. 'grad' => [
  39. '' => 0,
  40. '一般' => 1,
  41. '重要' => 2,
  42. '紧急' => 3,
  43. '重要且紧急' => 4,
  44. ],
  45. ];
  46. function __construct(array $attributes = [])
  47. {
  48. foreach (self::$enums as &$enum) {
  49. $enum = $enum + array_flip($enum);
  50. }
  51. parent::__construct($attributes);
  52. }
  53. public function getStatusAttribute($value)
  54. {
  55. if (!$value) return '';
  56. return self::$enums['status'][$value];
  57. }
  58. public function setStatusAttribute($value)
  59. {
  60. if (!$value) return 0;
  61. if (is_numeric($value)) {
  62. $this->attributes['status'] = $value;
  63. } else {
  64. $this->attributes['status'] = self::$enums['status'][$value];
  65. }
  66. }
  67. public function getGradAttribute($value)
  68. {
  69. if (!$value) return '';
  70. return self::$enums['grad'][$value];
  71. }
  72. public function setGradAttribute($value)
  73. {
  74. if (!$value) return 0;
  75. if (is_numeric($value)) {
  76. $this->attributes['grad'] = $value;
  77. } else {
  78. $this->attributes['grad'] = self::$enums['grad'][$value];
  79. }
  80. }
  81. // 创建人
  82. public function creator(): BelongsTo
  83. {
  84. return $this->belongsTo(User::class, 'creator_id');
  85. }
  86. // 审核人
  87. public function reviewer(): BelongsTo
  88. {
  89. return $this->belongsTo(User::class, 'reviewer_id');
  90. }
  91. // 关联订单
  92. public function order(): BelongsTo
  93. {
  94. return $this->belongsTo(Order::class);
  95. }
  96. // 关联货主
  97. public function owner(): BelongsTo
  98. {
  99. return $this->belongsTo(Owner::class,'owner_id');
  100. }
  101. // 工单类型
  102. public function type(): BelongsTo
  103. {
  104. return $this->BelongsTo(WorkOrderType::class,'work_order_type_id');
  105. }
  106. // 生成问题件类型
  107. public function issueType():BelongsTo
  108. {
  109. return $this->belongsTo(OrderIssueType::class,'order_issue_type_id');
  110. }
  111. /** 对应问题件 */
  112. public function orderIssue(): BelongsTo
  113. {
  114. return $this->belongsTo(OrderIssue::class,'order_id','order_id');
  115. }
  116. public function scopeFilter($query, $filters)
  117. {
  118. return $filters->apply($query);
  119. }
  120. /** 默认 with 参数 */
  121. public function scopeDefaultWith($query)
  122. {
  123. $query->with(['type','owner','issueType','creator','reviewer','order'=>function($query){
  124. /** @var $query Builder */
  125. $query->with('packages','logistic','owner');
  126. },'orderIssue'=>function($query){
  127. /** @var $query Builder */
  128. $query->with(['issueType','logs'=>function($query){
  129. if (Gate::denies('订单管理-问题件-客户不可见')){
  130. $query->with('user')->orderByDesc('created_at');
  131. } else{
  132. $query->with('user')->where('tag','=',0)->orderByDesc('created_at');
  133. }
  134. }]);
  135. }]);
  136. }
  137. public function notification()
  138. {
  139. $user = Auth::user();
  140. $this->loadMissing('owner','order');
  141. $remark = $this->remark;
  142. $ownerName = $this->owner->name ?? '';
  143. $clientCode = $this->order->client_code ?? '';
  144. $msg = $user["name"]."建立了新工单<br/>".$ownerName.":".$clientCode."<br/>".$remark;
  145. NotificationService::SingleRegister($msg,$clientCode,"订单管理-问题件");
  146. }
  147. }