Asset.php 3.7 KB

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