海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Models;
  3. use App\Common\ProductStatus;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\SoftDeletes;
  6. class AgentProduct extends BaseModel
  7. {
  8. use HasFactory, SoftDeletes;
  9. protected $guarded = ['id'];
  10. protected $casts = ['extends' => 'json'];
  11. public function product()
  12. {
  13. return $this->belongsTo(Product::class);
  14. }
  15. public function coupon()
  16. {
  17. return $this->hasMany(Coupon::class);
  18. }
  19. public function agent()
  20. {
  21. return $this->belongsTo(Agent::class)->withTrashed();
  22. }
  23. public function category()
  24. {
  25. return $this->belongsTo(Category::class);
  26. }
  27. public function user()
  28. {
  29. return $this->hasOne(User::class, 'id', 'verifier');
  30. }
  31. public function guide()
  32. {
  33. return $this->belongsTo(Guide::class)->withTrashed();
  34. }
  35. public function item()
  36. {
  37. return $this->hasMany(AgentProductItem::class);
  38. }
  39. public function agentCloudProduct()
  40. {
  41. return $this->belongsTo(self::class, 'agent_cloud_pid', 'id');
  42. }
  43. public function setPicturesAttribute($value)
  44. {
  45. $this->attributes['pictures'] = is_array($value) ? (json_encode(array_filter($value)) ?? '[]') : '[]';
  46. }
  47. public function setProductIdsAttribute($value)
  48. {
  49. $this->attributes['product_ids'] = is_array($value) ? join(',', array_filter($value)) : $value;
  50. }
  51. // 获取所有产品图片
  52. public function getPicturesAttribute($value): array
  53. {
  54. if (is_string($value)) {
  55. $value = $value ? json_decode($value, true) : [];
  56. }
  57. $this->append('picture');
  58. return $value ?? [];
  59. }
  60. // 获取第一张产品图片
  61. public function getPictureAttribute($value): string
  62. {
  63. return $this->pictures[0] ?? '';
  64. }
  65. //列表查询统一查询条件
  66. public static function list($agent_id)
  67. {
  68. return static::withoutGlobalScope('orderById')
  69. ->where(function ($query) {
  70. $query->whereHas('product', function ($query) {
  71. return $query->where([['stock', '>', 0], ['status', '=', ProductStatus::ON_SALE]]);
  72. })->orWhere('product_id', 0);
  73. })
  74. ->where('stock', '>', 0)->where(['agent_id' => $agent_id, 'status' => ProductStatus::ON_SALE])
  75. ->select('id', 'sale', 'product_id', 'price', 'original_price', 'title', 'pictures');
  76. }
  77. public function agentProductItem()
  78. {
  79. return $this->hasOne(AgentProductItem::class);
  80. }
  81. public function spec()
  82. {
  83. return $this->hasMany(AgentProductSpec::class);
  84. }
  85. //自营产品信息收集表
  86. public function diyForm()
  87. {
  88. return $this->belongsTo(DiyForm::class);
  89. }
  90. }