Asset.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Models;
  3. use App\Models\Enums\AssetACL;
  4. use App\Models\Scopes\CompanyScope;
  5. use EloquentFilter\Filterable;
  6. use Illuminate\Database\Eloquent\Builder;
  7. use Illuminate\Database\Eloquent\Factories\HasFactory;
  8. use Illuminate\Database\Eloquent\Model;
  9. use Illuminate\Support\Facades\Auth;
  10. /**
  11. * @method static \Illuminate\Database\Eloquent\Builder allowed()
  12. */
  13. class Asset extends Model
  14. {
  15. use HasFactory, Filterable;
  16. protected $fillable = [
  17. "name", "code", "description", "company_id", "status", "created_by",
  18. "owner", "address", "group_id", "geo_address_code", "acl",
  19. "whitelist", "latitude", "longitude","parent_id", "path",
  20. "equity_interest","developer","date_completed","total_floor_area","contact_person","contact_phone","contact_email","property","building_type_description"
  21. ];
  22. protected static function booted(): void
  23. {
  24. static::addGlobalScope(new CompanyScope);
  25. }
  26. public function scopeAllowed(Builder $query): void
  27. {
  28. $query->where(function (Builder $query) {
  29. return $query->where('acl', AssetACL::PRIVATE->value)->where('owner', Auth::id());
  30. })->orWhere(function (Builder $query) {
  31. return $query->where('acl', AssetACL::CUSTOM->value)->where('whitelist', 'like', '%,' . Auth::id() . ',%');
  32. });
  33. }
  34. public function children(){
  35. return $this->hasMany(Asset::class ,'parent_id');
  36. }
  37. public function parent(){
  38. return $this->belongsTo(Asset::class ,'parent_id');
  39. }
  40. public function requirementGroups(){
  41. return $this->hasMany(RequirementGroup::class ,'asset_id');
  42. }
  43. public function requirements(){
  44. return $this->hasMany(Requirement::class ,'asset_id');
  45. }
  46. public function plans(){
  47. return $this->hasMany(Plan::class ,'asset_id');
  48. }
  49. public function projects(){
  50. return $this->belongsToMany(Project::class,'project_asset');
  51. }
  52. //获取子级需求数量
  53. public function getTotalRequirementsCountAttribute()
  54. {
  55. $childIds = [];
  56. Asset::where('path','like','%,'.$this->id.',%')->get('id')->each(function ($id)use (&$childIds){
  57. $childIds[]=$id['id'];
  58. });
  59. return Requirement::whereIn('asset_id',$childIds)->count();
  60. }
  61. //获取子级计划数量
  62. public function getTotalPlansCountAttribute()
  63. {
  64. $childIds = [];
  65. Asset::where('path','like','%,'.$this->id.',%')->get('id')->each(function ($id)use (&$childIds){
  66. $childIds[]=$id['id'];
  67. });
  68. return Plan::whereIn('asset_id',$childIds)->count();
  69. }
  70. public function library(){
  71. return $this->hasMany(Library::class ,'asset_id')->where('type', 'asset');
  72. }
  73. public function createdBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  74. {
  75. return $this->belongsTo(User::class, 'created_by');
  76. }
  77. public function byOwner(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  78. {
  79. return $this->belongsTo(User::class, 'owner');
  80. }
  81. public function assetGroup(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  82. {
  83. return $this->belongsTo(AssetGroup::class, 'group_id');
  84. }
  85. }