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

108 lines
2.4 KiB

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. public function product()
  11. {
  12. return $this->belongsTo(Product::class);
  13. }
  14. public function coupon()
  15. {
  16. return $this->hasMany(Coupon::class);
  17. }
  18. public function fav()
  19. {
  20. return $this->hasOne(UserFav::class);
  21. }
  22. public function agent()
  23. {
  24. return $this->belongsTo(Agent::class);
  25. }
  26. public function category()
  27. {
  28. return $this->belongsTo(Category::class);
  29. }
  30. public function user()
  31. {
  32. return $this->hasOne(User::class, 'id', 'verifier');
  33. }
  34. public function guide()
  35. {
  36. return $this->belongsTo(Guide::class);
  37. }
  38. public function item()
  39. {
  40. return $this->hasMany(AgentProductItem::class);
  41. }
  42. public function agentCloudProduct()
  43. {
  44. return $this->belongsTo(self::class, 'agent_cloud_pid', 'id');
  45. }
  46. public function setChannelIdAttribute($value)
  47. {
  48. $this->attributes['channel_id'] = is_array($value) ? join(',', array_filter($value)) : $value;
  49. }
  50. public function setPicturesAttribute($value)
  51. {
  52. $this->attributes['pictures'] = is_array($value) ? (json_encode(array_filter($value)) ?? '[]') : '[]';
  53. }
  54. public function setProductIdsAttribute($value)
  55. {
  56. $this->attributes['product_ids'] = is_array($value) ? join(',', array_filter($value)) : $value;
  57. }
  58. // 获取所有产品图片
  59. public function getPicturesAttribute($value): array
  60. {
  61. if (is_string($value)) {
  62. $value = $value ? json_decode($value, true) : [];
  63. }
  64. $this->append('picture');
  65. return $value ?? [];
  66. }
  67. // 获取第一张产品图片
  68. public function getPictureAttribute($value): string
  69. {
  70. return $this->pictures[0] ?? '';
  71. }
  72. //列表查询统一查询条件
  73. public static function list($agent_id)
  74. {
  75. return static::withoutGlobalScope('orderById')
  76. ->whereDoesntHave('agentProductItem', function ($query) {
  77. return $query->whereHas('product', function ($query) {
  78. return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE);
  79. });
  80. })
  81. ->where('stock', '>', 0)->where(['agent_id' => $agent_id, 'status' => ProductStatus::ON_SALE])
  82. ->select('id', 'sale', 'product_id', 'price', 'original_price', 'title', 'pictures');
  83. }
  84. public function agentProductItem()
  85. {
  86. return $this->hasOne(AgentProductItem::class);
  87. }
  88. }