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

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