From f49d60057acac6d61832104a3e1cb8cafe4a590f Mon Sep 17 00:00:00 2001 From: liapples Date: Sun, 5 Sep 2021 12:05:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BB=84=E5=90=88=E4=BA=A7?= =?UTF-8?q?=E5=93=81=E5=BA=93=E5=AD=98=E5=92=8C=E7=8A=B6=E6=80=81=E5=88=A4?= =?UTF-8?q?=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/AgentProductController.php | 8 +++++--- app/Models/AgentProduct.php | 14 +++++++++++--- app/Models/AgentProductItem.php | 10 ++++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/Api/AgentProductController.php b/app/Http/Controllers/Api/AgentProductController.php index b274951..34f3cc6 100644 --- a/app/Http/Controllers/Api/AgentProductController.php +++ b/app/Http/Controllers/Api/AgentProductController.php @@ -40,14 +40,16 @@ class AgentProductController extends Controller $agent_product = AgentProduct::query() ->with('coupon:tag,agent_product_id') ->with('fav:agent_product_id') - ->whereHas('product', function ($query) { - return $query->where('status', ProductStatus::ON_SALE)->where('stock', '>', 0); + ->whereDoesntHave('agentProductItem', function ($query) { + return $query->whereHas('product', function ($query) { + return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE); + }); }) ->where('stock', '>', 0) ->firstWhere(['id' => $id, 'agent_id' => $this->agent_id, 'status' => ProductStatus::ON_SALE]); if (!$agent_product) { - return $this->error('产品不存在或已下架'); + return $this->error('产品已下架或库存不足'); } $prefix = Storage::disk('public')->url(''); diff --git a/app/Models/AgentProduct.php b/app/Models/AgentProduct.php index c2733ca..b35ab09 100644 --- a/app/Models/AgentProduct.php +++ b/app/Models/AgentProduct.php @@ -89,12 +89,20 @@ class AgentProduct extends BaseModel } //列表查询统一查询条件 - public function scopeList($query, $agent_id) + public static function list($agent_id) { - return $query->whereHas('product', function ($query) { - return $query->where('status', ProductStatus::ON_SALE)->where('stock', '>', 0); + return static::withoutGlobalScope('orderById') + ->whereDoesntHave('agentProductItem', function ($query) { + return $query->whereHas('product', function ($query) { + return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE); + }); }) ->where(['agent_id' => $agent_id, 'status' => ProductStatus::ON_SALE])->where('stock', '>', 0) ->select('id', 'sale', 'product_id', 'price', 'original_price', 'title', 'pictures'); } + + public function agentProductItem() + { + return $this->hasOne(AgentProductItem::class); + } } diff --git a/app/Models/AgentProductItem.php b/app/Models/AgentProductItem.php index 24015db..6a6a049 100644 --- a/app/Models/AgentProductItem.php +++ b/app/Models/AgentProductItem.php @@ -15,4 +15,14 @@ class AgentProductItem extends BaseModel parent::__construct($attributes); $this->timestamps = false; } + + public function product() + { + return $this->belongsTo(Product::class); + } + + public function agentProduct() + { + return $this->belongsTo(AgentProduct::class); + } }