where([ ['user_id','=',$userId], ['market_id','=',$marketId], ])->pluck('store_id')->toArray(); $stores = Store::query()->with(['ShoppingCart' => function($query) use ($userId,$marketId){ $query->where([ ['user_id','=',$userId], ['market_id','=',$marketId], ]); }])->whereIn('id',$storeIds) ->get() ->toArray(); $storeArr = []; foreach ($stores as $key => &$store){ $sotreType = $this->storeService->check($store['id']); if(!$sotreType){ continue; } $subtotal = 0; foreach ($store['shopping_cart'] as $k => &$shopcart){ if($shopcart['activity_type'] == 1){ $goodsType = $this->goodsService->check($shopcart['goods_id']); }else{ $goodsType = $this->goodsActivityService->check($shopcart['goods_id'],$shopcart['num'],$shopcart['user_id']); } if($goodsType !== true){ unset($store['shopping_cart'][$k]); continue; } if($shopcart['activity_type'] == 1){ $builder = Goods::query(); }else{ $builder = GoodsActivity::query(); } $shopcart['goods'] = $builder->where('id',$shopcart['goods_id'])->first()->toArray(); $subtotal+= $shopcart['num'] * $shopcart['goods']['price']; } $store['subtotal'] = $subtotal; $storeArr[] = $store; } return $storeArr; } public function check($userId,$goodsId,$activityType = 1) { $ShoppingCart = ShoppingCart::query()->where([ ['user_id','=',$userId], ['goods_id','=',$goodsId], ['activity_type','=',$activityType], ]) ->select('num') ->first(); return $ShoppingCart->num ?? 0; } public function undo($userId,$marketId) { $storeIds = Db::table('lanzu_shopping_cart')->where([ ['user_id','=',$userId], ['market_id','=',$marketId], ])->pluck('store_id')->toArray(); $stores = Store::query()->with(['ShoppingCart' => function($query) use ($userId,$marketId){ $query->where([ ['user_id','=',$userId], ['market_id','=',$marketId], ]); }])->whereIn('id',$storeIds) ->get() ->toArray(); $storeArr = []; foreach ($stores as $key => &$store){ $addStore = false; $sotreType = $this->storeService->check($store['id']); if(!$sotreType){ $addStore = true; } foreach ($store['shopping_cart'] as $k => &$shopcart){ if($shopcart['activity_type'] == 1){ $goodsType = $this->goodsService->check($shopcart['goods_id']); }else{ $goodsType = $this->goodsActivityService->check($shopcart['goods_id'],$shopcart['num'],$shopcart['user_id']); } if($goodsType === true){ unset($store['shopping_cart'][$k]); continue; } $addStore = true; if($shopcart['activity_type'] == 1){ $builder = Goods::query(); }else{ $builder = GoodsActivity::query(); } $shopcart['goods'] = $builder->where('id',$shopcart['goods_id'])->first()->toArray(); $shopcart['goods']['invalid_cause'] = $goodsType; } if($addStore){ $storeArr[] = $store; } } return $storeArr; } public function countGoods($userId,$marketId) { $shoppingCart = $this->allForUser($userId,$marketId); $count = 0; foreach ($shoppingCart['store_lists'] as $store){ foreach ($store['shopping_cart'] as $cart) { $num = $cart['num']; $count += $num; } } return $count; } public function getTotal($userId,$marketId) { $shoppingCart = $this->allForUser($userId,$marketId); return $shoppingCart['total']; } public function getGoodsByShopcartId($shopcartIds) { bcscale(6); // 查询当前用户的市场下的购物车数据 $shopcartIdsArr = explode(',',$shopcartIds); $carts = ShoppingCart::query() ->with(['store', 'goods']) ->whereIn('id',$shopcartIdsArr) ->get(); $cartList = []; $totalAmount = 0; foreach ($carts as $key => &$cart) { if (empty($cart->store)) { $msg = ["car_module"=>"1","shopcartIdsArr"=>$shopcartIdsArr]; throw new ErrorCodeException(ErrorCode::STORE_NOT_AVAILABLE,json_encode($msg)); continue; } if (empty($cart->goods)) { throw new ErrorCodeException(ErrorCode::GOODS_NOT_EXISTS); continue; } $shoppingCart = $cart->toArray(); $store = $shoppingCart['store']; unset($shoppingCart['store']); // 商户是否歇业 $checkStore = $this->storeService->check($store['id']); if (!$checkStore) { throw new ErrorCodeException(ErrorCode::STORE_NOT_AVAILABLE); continue; } // 商品是否失效 if ($cart->activity_type == 2) { $goods = GoodsActivity::query()->find($cart->goods_id); $res = $this->goodsActivityService->check($goods, $cart->num, $cart->user_id); } else { $goods = Goods::query()->find($cart->goods_id); $res = $this->goodsService->check($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{ throw new ErrorCodeException($res); } } $cartList['store_lists'] = isset($cartList['store_lists']) ? array_values($cartList['store_lists']) : []; foreach ($cartList['store_lists'] as $key => &$value) { $value['subtotal'] = bcadd($value['subtotal'], '0' , 2); } $cartList['total'] = bcadd($totalAmount, '0', 2); return $cartList['store_lists']; } /** * 获取用户购物车数据 * @param $userId * @param $marketId * @param array $shopcartIds * @return array */ public function allForUser($userId, $marketId, $shopcartIds = []) { bcscale(6); // 查询当前用户的市场下的购物车数据 $carts = ShoppingCart::query() ->with(['store', 'goods' => function($query) { $query->withoutGlobalScope('normal'); } ]) ->where(['user_id' => $userId]) ->where(['market_id' => $marketId]); if (!empty($shopcartIds)) { $carts = $carts->whereIn('id', $shopcartIds); } $carts = $carts->get(); $cartList = []; $totalAmount = 0; foreach ($carts as $key => &$cart) { if (empty($cart->store)||empty($cart->goods)) { 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; } }