Demand.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App;
  3. use App\Traits\ModelLogChanging;
  4. use App\Traits\ModelTimeFormat;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Database\Eloquent\Relations\HasMany;
  8. use Illuminate\Database\Eloquent\Relations\HasOne;
  9. use Illuminate\Database\Eloquent\Builder;
  10. class Demand extends Model
  11. {
  12. use ModelLogChanging;
  13. use ModelTimeFormat;
  14. protected $fillable = ['authority_id', 'initiator', 'description', 'type', 'status', 'handler'];
  15. // 需求类型
  16. protected static $type = ['需求', '问题'];
  17. // 状态
  18. protected static $status = ['未处理', '处理中', '已处理'];
  19. public function authority(): BelongsTo
  20. {
  21. return $this->belongsTo(Authority::class);
  22. }
  23. public function initiator(): BelongsTo
  24. {
  25. return $this->belongsTo(User::class, 'initiator', 'id');
  26. }
  27. public function handle(): BelongsTo
  28. {
  29. return $this->belongsTo(User::class, 'handler', 'id');
  30. }
  31. public function uploadFile(): HasOne
  32. {
  33. return $this->hasOne(UploadFile::class, 'table_id', 'id')->where('table_name', 'Demands');
  34. }
  35. public function processes(): HasMany
  36. {
  37. return $this->hasMany(DemandProcess::class, 'demand_id', 'id');
  38. }
  39. public function scopeFilter($query, $filters)
  40. {
  41. return $filters->apply($query);
  42. }
  43. /**
  44. * 保存文件创建对应的文件对象
  45. *
  46. * @param $fileName
  47. * @param $fileSuffix
  48. * @return Builder|Model
  49. */
  50. public function saveFile($fileName,$fileSuffix)
  51. {
  52. return UploadFile::query()->create(['table_name' => $this->getTable(), 'table_id' => $this['id'], 'url' => '/files/issue/'.$fileName, 'type' => $fileSuffix]);
  53. }
  54. /**
  55. * 删除需求的同时删除处理过程
  56. *
  57. * @return bool|null
  58. * @throws \Exception
  59. */
  60. public function delete()
  61. {
  62. $this->processes()->delete();
  63. return parent::delete();
  64. }
  65. }