|
|
|
@ -181,4 +181,107 @@ class ShopCartService implements ShopCartServiceInterface |
|
|
|
} |
|
|
|
return $storeArr; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取用户购物车数据 |
|
|
|
* @param $userId |
|
|
|
* @param $marketId |
|
|
|
* @return array |
|
|
|
*/ |
|
|
|
public function allForUser($userId, $marketId) |
|
|
|
{ |
|
|
|
|
|
|
|
bcscale(6); |
|
|
|
// 查询当前用户的市场下的购物车数据
|
|
|
|
$carts = ShoppingCart::query() |
|
|
|
->with(['store', 'goods']) |
|
|
|
->where(['user_id' => $userId]) |
|
|
|
->where(['market_id' => $marketId]) |
|
|
|
->get(); |
|
|
|
|
|
|
|
$cartList = []; |
|
|
|
$totalAmount = 0; |
|
|
|
foreach ($carts as $key => &$cart) { |
|
|
|
|
|
|
|
if (empty($cart->store)) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
$shoppingCart = $cart->toArray(); |
|
|
|
$store = $shoppingCart['store']; |
|
|
|
unset($shoppingCart['store']); |
|
|
|
|
|
|
|
// 商户是否歇业
|
|
|
|
$checkStore = $this->storeService->check($store['id']); |
|
|
|
if (!$checkStore) { |
|
|
|
|
|
|
|
$cartList['store_lists_invalid'][$store['id']]['store'] = $store; |
|
|
|
$cartList['store_lists_invalid'][$store['id']]['shopping_cart'][] = $shoppingCart; |
|
|
|
|
|
|
|
if (isset($cartList['store_lists_invalid'][$store['id']]['subtotal'])) { |
|
|
|
$cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcadd( |
|
|
|
$cartList['store_lists_invalid'][$store['id']]['subtotal'], |
|
|
|
bcmul($cart->goods->price, $cart->num) |
|
|
|
); |
|
|
|
} else { |
|
|
|
$cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num); |
|
|
|
} |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
// 商品是否失效
|
|
|
|
if ($cart->activity_type == 2) { |
|
|
|
$res = $this->goodsActivityService->check($cart->goods, $cart->num, $cart->user_id); |
|
|
|
} else { |
|
|
|
$res = $this->goodsService->check($cart->goods, $cart->num); |
|
|
|
} |
|
|
|
|
|
|
|
if ($res === true) { |
|
|
|
|
|
|
|
$cartList['store_lists'][$store['id']]['store'] = $store; |
|
|
|
$cartList['store_lists'][$store['id']]['shopping_cart'][] = $shoppingCart; |
|
|
|
|
|
|
|
if (isset($cartList['store_lists'][$store['id']]['subtotal'])) { |
|
|
|
$cartList['store_lists'][$store['id']]['subtotal'] = bcadd( |
|
|
|
$cartList['store_lists'][$store['id']]['subtotal'], |
|
|
|
bcmul($cart->goods->price, $cart->num) |
|
|
|
); |
|
|
|
} else { |
|
|
|
$cartList['store_lists'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num); |
|
|
|
} |
|
|
|
|
|
|
|
$totalAmount = bcadd($totalAmount, bcmul($cart->goods->price, $cart->num)); |
|
|
|
} else { |
|
|
|
|
|
|
|
$cartList['store_lists_invalid'][$store['id']]['store'] = $store; |
|
|
|
$cartList['store_lists_invalid'][$store['id']]['shopping_cart'][] = $shoppingCart; |
|
|
|
|
|
|
|
if (isset($cartList['store_lists_invalid'][$store['id']]['subtotal'])) { |
|
|
|
$cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcadd( |
|
|
|
$cartList['store_lists_invalid'][$store['id']]['subtotal'], |
|
|
|
bcmul($cart->goods->price, $cart->num) |
|
|
|
); |
|
|
|
} else { |
|
|
|
$cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$cartList['store_lists'] = isset($cartList['store_lists']) ? array_values($cartList['store_lists']) : []; |
|
|
|
$cartList['store_lists_invalid'] = isset($cartList['store_lists_invalid']) ? array_values($cartList['store_lists_invalid']) : []; |
|
|
|
|
|
|
|
foreach ($cartList['store_lists'] as $key => &$value) { |
|
|
|
$value['subtotal'] = bcadd($value['subtotal'], '0' , 2); |
|
|
|
} |
|
|
|
|
|
|
|
foreach ($cartList['store_lists_invalid'] as $key => &$value) { |
|
|
|
$value['subtotal'] = bcadd($value['subtotal'], '0' , 2); |
|
|
|
} |
|
|
|
|
|
|
|
$cartList['total'] = bcadd($totalAmount, '0', 2); |
|
|
|
|
|
|
|
return $cartList; |
|
|
|
} |
|
|
|
} |