152 lines
5.0 KiB
PHP
152 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Member;
|
|
|
|
use App\Models\Cart;
|
|
use App\Models\YiiModels\Drug;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Relations\Relation;
|
|
|
|
/**
|
|
* 购物车
|
|
*/
|
|
class CartController extends MemberBaseController
|
|
{
|
|
/**
|
|
* 添加购物车
|
|
*
|
|
* @return void
|
|
*/
|
|
public function cartAdd()
|
|
{
|
|
$input = $this->toValidator([
|
|
'drug_id' => 'required|integer',
|
|
'change_qty' => 'nullable|integer',
|
|
'true_qty' => 'nullable|integer',
|
|
], [], [
|
|
'drug_id' => '商品',
|
|
'change_qty' => '数量',
|
|
'true_qty' => '数量',
|
|
], true);
|
|
|
|
if (!$input['change_qty'] && !$input['true_qty']) {
|
|
json_error('商品数量不正确');
|
|
}
|
|
|
|
/* @var Drug $drug */
|
|
$drug = Drug::with([
|
|
'drugStoreRelation' => function (Relation $relation) {
|
|
$relation->select(['drug_id', 'total_sales_volume', 'status'])
|
|
->where('store_id', $this->getStoreId());
|
|
},
|
|
'drugstoreDrug' => function (Relation $relation) {
|
|
$relation->select(['drug_id', 'price', 'stock'])
|
|
->where('drugstore_id', $this->getDrugstoreId());
|
|
},
|
|
])
|
|
->select([
|
|
'id', 'status',
|
|
])
|
|
->whereHasIn('drugStoreRelation', function (Builder $builder) {
|
|
$builder->where('store_id', $this->getStoreId());
|
|
})
|
|
->where('id', $input['drug_id'])
|
|
->first();
|
|
if (!$drug && !$drug->drugStoreRelation && !$drug->drugstoreDrug) {
|
|
json_error('商品不存在');
|
|
}
|
|
if ($drug->status != Drug::STATUS_ON || $drug->drugStoreRelation->status != Drug::STATUS_ON) {
|
|
json_error('商品已下架');
|
|
}
|
|
$cart = $this->user()->carts()->firstOrCreate([
|
|
'drug_id' => $input['drug_id'],
|
|
'store_id' => $this->getStoreId(),
|
|
]);
|
|
if ($input['change_qty']) {
|
|
if ($input['change_qty'] > $drug->drugstoreDrug->stock) {
|
|
json_error("库存不足,最多可加入{$drug->drugstoreDrug->stock}个");
|
|
}
|
|
$cart->increment('qty', $input['change_qty'], ['join_at' => now()]);
|
|
if ($cart->qty <= 0) {
|
|
$cart->delete();
|
|
json_success('购物车商品已删除');
|
|
}
|
|
} else {
|
|
if (($input['true_qty'] - $cart['qty']) > $drug->drugstoreDrug->stock) {
|
|
json_error("库存不足,最多可加入{$drug->drugstoreDrug->stock}个");
|
|
}
|
|
$cart->update([
|
|
'qty' => $input['true_qty'],
|
|
'join_at' => now(),
|
|
]);
|
|
if ($cart->qty <= 0) {
|
|
$cart->delete();
|
|
json_success('购物车商品已删除');
|
|
}
|
|
}
|
|
json_success('已加入购物车');
|
|
}
|
|
|
|
/**
|
|
* 清空购物车
|
|
*
|
|
* @return void
|
|
*/
|
|
public function clearAll()
|
|
{
|
|
Cart::where('store_id', $this->getStoreId())
|
|
->where('user_id', $this->userId())
|
|
->delete();
|
|
json_success('已清空购物车');
|
|
}
|
|
|
|
/**
|
|
* 购物车列表
|
|
*
|
|
* @return void
|
|
*/
|
|
public function cartLists()
|
|
{
|
|
$carts = Cart::with([
|
|
'drug' => function (Relation $relation) {
|
|
$relation->with([
|
|
'drugStoreRelation' => function (Relation $relation) {
|
|
$relation->select(['drug_id', 'total_sales_volume', 'status'])
|
|
->where('store_id', $this->getStoreId());
|
|
},
|
|
'drugstoreDrug' => function (Relation $relation) {
|
|
$relation->select(['drug_id', 'price', 'stock'])
|
|
->where('drugstore_id', $this->getDrugstoreId());
|
|
},
|
|
])
|
|
->select(['id', 'drug_name', 'source', 'type', 'is_otc', 'small_info', 'usage', 'specification', 'image', 'status']);
|
|
}
|
|
])
|
|
->whereHasIn('drug')
|
|
->where('store_id', $this->getStoreId())
|
|
->where('user_id', $this->userId())
|
|
->orderByDesc('join_at')
|
|
->get();
|
|
|
|
$valid = $invalid = [];
|
|
/* @var Cart $cart */
|
|
foreach ($carts as $cart) {
|
|
if (!$cart->drug || !$cart->drug->drugStoreRelation || !$cart->drug->drugstoreDrug) {
|
|
$invalid[] = $cart;
|
|
continue;
|
|
}
|
|
if ($cart->drug->status != Drug::STATUS_ON || $cart->drug->drugStoreRelation->status != Drug::STATUS_ON) {
|
|
$invalid[] = $cart;
|
|
continue;
|
|
}
|
|
$valid[] = $cart;
|
|
}
|
|
|
|
json_success([
|
|
'store_name' => $this->getStore()->name,
|
|
'valid' => $valid,
|
|
'invalid' => $invalid,
|
|
]);
|
|
}
|
|
}
|