PartRecord.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Models;
  3. use Dcat\Admin\Traits\HasDateTimeFormatter;
  4. use Dcat\Admin\Traits\ModelTree;
  5. use Exception;
  6. use Illuminate\Database\Eloquent\Factories\HasFactory;
  7. use Illuminate\Database\Eloquent\Model;
  8. use Illuminate\Database\Eloquent\Relations\HasOne;
  9. use Illuminate\Database\Eloquent\Relations\HasOneThrough;
  10. use Illuminate\Database\Eloquent\SoftDeletes;
  11. /**
  12. * @method static where(string $key, string $value, string $value = null)
  13. * @method static whereBetween(string $string, array $array)
  14. * @method static count()
  15. *
  16. * @property float price
  17. * @property string purchased
  18. * @property string expired
  19. * @property string specification
  20. * @property int category_id
  21. * @property int vendor_id
  22. * @property string name
  23. * @property string description
  24. * @property string sn
  25. * @property string asset_number
  26. * @property int id
  27. */
  28. class PartRecord extends Model
  29. {
  30. use HasFactory;
  31. use HasDateTimeFormatter;
  32. use SoftDeletes;
  33. use ModelTree;
  34. /**
  35. * 需要被包括进排序字段的字段,一般来说是虚拟出来的关联字段.
  36. *
  37. * @var string[]
  38. */
  39. public array $sortIncludeColumns = [
  40. 'category.name',
  41. 'vendor.name',
  42. 'depreciation.name',
  43. 'expiration_left_days',
  44. ];
  45. /**
  46. * 需要被排除出排序字段的字段,一般来说是关联字段的原始字段.
  47. *
  48. * @var string[]
  49. */
  50. public array $sortExceptColumns = [
  51. 'category_id',
  52. 'vendor_id',
  53. 'depreciation_rule_id',
  54. 'deleted_at',
  55. ];
  56. protected $table = 'part_records';
  57. /**
  58. * 模型事件
  59. */
  60. protected static function booting()
  61. {
  62. static::saving(function ($model) {
  63. if (!empty($model->deleted_at)) {
  64. abort(401, 'you can not do that.');
  65. }
  66. });
  67. }
  68. /**
  69. * 配件记录有一个分类.
  70. *
  71. * @return HasOne
  72. */
  73. public function category(): HasOne
  74. {
  75. return $this->hasOne(PartCategory::class, 'id', 'category_id');
  76. }
  77. /**
  78. * 配件记录有一个厂商.
  79. *
  80. * @return HasOne
  81. */
  82. public function vendor(): HasOne
  83. {
  84. return $this->hasOne(VendorRecord::class, 'id', 'vendor_id');
  85. }
  86. /**
  87. * 配件记录在远处有一个设备.
  88. *
  89. * @return HasOneThrough
  90. */
  91. public function device(): HasOneThrough
  92. {
  93. return $this->hasOneThrough(
  94. DeviceRecord::class, // 远程表
  95. PartTrack::class, // 中间表
  96. 'part_id', // 中间表对主表的关联字段
  97. 'id', // 远程表对中间表的关联字段
  98. 'id', // 主表对中间表的关联字段
  99. 'device_id'
  100. ); // 中间表对远程表的关联字段
  101. }
  102. /**
  103. * 配件分类有一个折旧规则.
  104. *
  105. * @return HasOne
  106. */
  107. public function depreciation(): HasOne
  108. {
  109. return $this->hasOne(DepreciationRule::class, 'id', 'depreciation_rule_id');
  110. }
  111. /**
  112. * 配件有一个归属
  113. * @return HasOne
  114. */
  115. public function track(): HasOne
  116. {
  117. return $this->hasOne(PartTrack::class, 'part_id', 'id');
  118. }
  119. /**
  120. * 删除方法.
  121. * 这是里为了兼容数据删除和字段删除.
  122. */
  123. public function delete()
  124. {
  125. $this->where($this->primaryKey, $this->getKey())->delete();
  126. try {
  127. return parent::delete();
  128. } catch (Exception $e) {
  129. }
  130. }
  131. /**
  132. * 强制删除方法.
  133. * 这里为了兼容数据强制删除和字段强制删除.
  134. *
  135. * @return bool|null
  136. */
  137. public function forceDelete()
  138. {
  139. $this->where($this->primaryKey, $this->getKey())->forceDelete();
  140. try {
  141. return parent::forceDelete();
  142. } catch (Exception $exception) {
  143. }
  144. }
  145. }