Browse Source

增加组合产品库存和状态判断

develop
李可松 4 years ago
parent
commit
f49d60057a
  1. 8
      app/Http/Controllers/Api/AgentProductController.php
  2. 12
      app/Models/AgentProduct.php
  3. 10
      app/Models/AgentProductItem.php

8
app/Http/Controllers/Api/AgentProductController.php

@ -40,14 +40,16 @@ class AgentProductController extends Controller
$agent_product = AgentProduct::query() $agent_product = AgentProduct::query()
->with('coupon:tag,agent_product_id') ->with('coupon:tag,agent_product_id')
->with('fav: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) ->where('stock', '>', 0)
->firstWhere(['id' => $id, 'agent_id' => $this->agent_id, 'status' => ProductStatus::ON_SALE]); ->firstWhere(['id' => $id, 'agent_id' => $this->agent_id, 'status' => ProductStatus::ON_SALE]);
if (!$agent_product) { if (!$agent_product) {
return $this->error('产品不存在或已下架');
return $this->error('产品已下架或库存不足');
} }
$prefix = Storage::disk('public')->url(''); $prefix = Storage::disk('public')->url('');

12
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 static::withoutGlobalScope('orderById')
->whereDoesntHave('agentProductItem', function ($query) {
return $query->whereHas('product', function ($query) { return $query->whereHas('product', function ($query) {
return $query->where('status', ProductStatus::ON_SALE)->where('stock', '>', 0);
return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE);
});
}) })
->where(['agent_id' => $agent_id, 'status' => ProductStatus::ON_SALE])->where('stock', '>', 0) ->where(['agent_id' => $agent_id, 'status' => ProductStatus::ON_SALE])->where('stock', '>', 0)
->select('id', 'sale', 'product_id', 'price', 'original_price', 'title', 'pictures'); ->select('id', 'sale', 'product_id', 'price', 'original_price', 'title', 'pictures');
} }
public function agentProductItem()
{
return $this->hasOne(AgentProductItem::class);
}
} }

10
app/Models/AgentProductItem.php

@ -15,4 +15,14 @@ class AgentProductItem extends BaseModel
parent::__construct($attributes); parent::__construct($attributes);
$this->timestamps = false; $this->timestamps = false;
} }
public function product()
{
return $this->belongsTo(Product::class);
}
public function agentProduct()
{
return $this->belongsTo(AgentProduct::class);
}
} }
Loading…
Cancel
Save