Browse Source

whereDoesntHave 改为 whereHas + orWhere('product_id', 0)

master
李可松 4 years ago
parent
commit
302e1bbdef
  1. 9
      app/AdminAgent/Renderable/SelectAgentCloudProduct.php
  2. 9
      app/AdminAgent/Renderable/SelectAgentProduct.php
  3. 8
      app/Http/Controllers/Api/AgentProductController.php
  4. 10
      app/Http/Controllers/Api/OrderController.php
  5. 8
      app/Models/AgentProduct.php

9
app/AdminAgent/Renderable/SelectAgentCloudProduct.php

@ -30,12 +30,11 @@ class SelectAgentCloudProduct extends LazyRenderable
['is_cloud', '=', 1],
['type', '=', 1],
['agent_id', '<>', Admin::user()->id],
])->whereDoesntHave('agentProductItem', function ($query) {
return $query->whereHas('product', function ($query) {
return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE);
])->where(function ($query) {
$query->whereHas('product', function ($query) {
return $query->where([['stock', '>', 0], ['status', '=', ProductStatus::ON_SALE]]);
})->orWhere('product_id', 0);
});
});
$grid->quickSearch(['title'])->placeholder('搜索产品名称');

9
app/AdminAgent/Renderable/SelectAgentProduct.php

@ -26,11 +26,12 @@ class SelectAgentProduct extends LazyRenderable
$grid->model()->where('stock', '>', 0)
->where(['agent_id' => Admin::user()->id, 'status' => ProductStatus::ON_SALE])
->whereDoesntHave('agentProductItem', function ($query) {
return $query->whereHas('product', function ($query) {
return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE);
});
->where(function ($query) {
$query->whereHas('product', function ($query) {
return $query->where([['stock', '>', 0], ['status', '=', ProductStatus::ON_SALE]]);
})->orWhere('product_id', 0);
});
$grid->quickSearch(['title'])->placeholder('搜索产品名称');
$grid->column('id');

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

@ -166,10 +166,10 @@ SQL
});
}
])
->whereDoesntHave('agentProductItem', function ($query) {
return $query->whereHas('product', function ($query) {
return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE);
});
->where(function ($query) {
$query->whereHas('product', function ($query) {
return $query->where([['stock', '>', 0], ['status', '=', ProductStatus::ON_SALE]]);
})->orWhere('product_id', 0);
})
->where('stock', '>', 0)
->firstWhere($where);

10
app/Http/Controllers/Api/OrderController.php

@ -143,11 +143,11 @@ class OrderController extends Controller
$ap = AgentProduct::with(['coupon', 'product.diyForm.fields', 'diyForm.fields', 'agentCloudProduct:id,price'])
->where('stock', '>=', $formData['num'])
->where(['id' => $formData['id'], 'status' => ProductStatus::ON_SALE, 'agent_id' => $this->agent_id]) //判断agent_id,防止新入驻小程序的演示产品被下单
/*->whereDoesntHave('agentProductItem', function ($query) { 与代理商自营产品冲突,所以注释掉
return $query->whereHas('product', function ($query) {
return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE);
});
})*/
->where(function ($query) {
$query->whereHas('product', function ($query) {
return $query->where([['stock', '>', 0], ['status', '=', ProductStatus::ON_SALE]]);
})->orWhere('product_id', 0);
})
->first();
if (!$ap) {
return $this->error('产品已下架或库存不足');

8
app/Models/AgentProduct.php

@ -88,10 +88,10 @@ class AgentProduct extends BaseModel
public static function list($agent_id)
{
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(function ($query) {
$query->whereHas('product', function ($query) {
return $query->where([['stock', '>', 0], ['status', '=', ProductStatus::ON_SALE]]);
})->orWhere('product_id', 0);
})
->where('stock', '>', 0)->where(['agent_id' => $agent_id, 'status' => ProductStatus::ON_SALE])
->select('id', 'sale', 'product_id', 'price', 'original_price', 'title', 'pictures');

Loading…
Cancel
Save