Ticket.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Models;
  3. use Auth;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. use Illuminate\Database\Eloquent\Relations\HasMany;
  7. /**
  8. * 工单.
  9. *
  10. * @property int $id
  11. * @property int $user_id 用户ID
  12. * @property int|null $admin_id 管理员ID
  13. * @property string $title 标题
  14. * @property string $content 内容
  15. * @property int $status 状态:0-待处理、1-已处理未关闭、2-已关闭
  16. * @property \Illuminate\Support\Carbon $created_at 创建时间
  17. * @property \Illuminate\Support\Carbon $updated_at 最后更新时间
  18. * @property-read \App\Models\User|null $admin
  19. * @property-read string $status_label
  20. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\TicketReply[] $reply
  21. * @property-read int|null $reply_count
  22. * @property-read \App\Models\User $user
  23. * @method static \Illuminate\Database\Eloquent\Builder|Ticket newModelQuery()
  24. * @method static \Illuminate\Database\Eloquent\Builder|Ticket newQuery()
  25. * @method static \Illuminate\Database\Eloquent\Builder|Ticket query()
  26. * @method static \Illuminate\Database\Eloquent\Builder|Ticket uid()
  27. * @method static \Illuminate\Database\Eloquent\Builder|Ticket whereAdminId($value)
  28. * @method static \Illuminate\Database\Eloquent\Builder|Ticket whereContent($value)
  29. * @method static \Illuminate\Database\Eloquent\Builder|Ticket whereCreatedAt($value)
  30. * @method static \Illuminate\Database\Eloquent\Builder|Ticket whereId($value)
  31. * @method static \Illuminate\Database\Eloquent\Builder|Ticket whereStatus($value)
  32. * @method static \Illuminate\Database\Eloquent\Builder|Ticket whereTitle($value)
  33. * @method static \Illuminate\Database\Eloquent\Builder|Ticket whereUpdatedAt($value)
  34. * @method static \Illuminate\Database\Eloquent\Builder|Ticket whereUserId($value)
  35. * @mixin \Eloquent
  36. */
  37. class Ticket extends Model
  38. {
  39. protected $table = 'ticket';
  40. protected $guarded = [];
  41. public function scopeUid($query)
  42. {
  43. return $query->whereUserId(Auth::id());
  44. }
  45. public function user(): BelongsTo
  46. {
  47. return $this->belongsTo(User::class);
  48. }
  49. public function admin(): BelongsTo
  50. {
  51. return $this->belongsTo(User::class);
  52. }
  53. public function reply(): HasMany
  54. {
  55. return $this->hasMany(TicketReply::class);
  56. }
  57. public function close(): bool
  58. {
  59. $this->status = 2;
  60. return $this->save();
  61. }
  62. public function getStatusLabelAttribute(): string
  63. {
  64. switch ($this->attributes['status']) {
  65. case 0:
  66. $status_label = '<span class="badge badge-lg badge-success">'.trans('user.status.pending').'</span>';
  67. break;
  68. case 1:
  69. $status_label = '<span class="badge badge-lg badge-danger">'.trans('user.status.reply').'</span>';
  70. break;
  71. case 2:
  72. $status_label = '<span class="badge badge-lg badge-default">'.trans('user.status.closed').'</span>';
  73. break;
  74. default:
  75. $status_label = '<span class="badge badge-lg badge-default">'.trans('user.unknown').'</span>';
  76. }
  77. return $status_label;
  78. }
  79. }