海南旅游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.

115 lines
2.6 KiB

4 years ago
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 setChannelIdAttribute($value)
  44. {
  45. $this->attributes['channel_id'] = is_array($value) ? join(',', array_filter($value)) : $value;
  46. }
  47. public function setPicturesAttribute($value)
  48. {
  49. $this->attributes['pictures'] = is_array($value) ? (json_encode(array_filter($value)) ?? '[]') : '[]';
  50. }
  51. public function setProductIdsAttribute($value)
  52. {
  53. $this->attributes['product_ids'] = is_array($value) ? join(',', array_filter($value)) : $value;
  54. }
  55. // 获取所有产品图片
  56. public function getPicturesAttribute($value): array
  57. {
  58. if (is_string($value)) {
  59. $value = $value ? json_decode($value, true) : [];
  60. }
  61. $this->append('picture');
  62. return $value ?? [];
  63. }
  64. // 获取第一张产品图片
  65. public function getPictureAttribute($value): string
  66. {
  67. return $this->pictures[0] ?? '';
  68. }
  69. //列表查询统一查询条件
  70. public static function list($agent_id)
  71. {
  72. return static::withoutGlobalScope('orderById')
  73. ->where(function ($query) {
  74. $query->whereHas('product', function ($query) {
  75. return $query->where([['stock', '>', 0], ['status', '=', ProductStatus::ON_SALE]]);
  76. })->orWhere('product_id', 0);
  77. })
  78. ->where('stock', '>', 0)->where(['agent_id' => $agent_id, 'status' => ProductStatus::ON_SALE])
  79. ->select('id', 'sale', 'product_id', 'price', 'original_price', 'title', 'pictures');
  80. }
  81. public function agentProductItem()
  82. {
  83. return $this->hasOne(AgentProductItem::class);
  84. }
  85. public function spec()
  86. {
  87. return $this->hasMany(AgentProductSpec::class);
  88. }
  89. //自营产品信息收集表
  90. public function diyForm()
  91. {
  92. return $this->belongsTo(DiyForm::class);
  93. }
  94. }