You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

335 lines
12 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Constants\v3\ErrorCode;
  4. use App\Exception\ErrorCodeException;
  5. use App\Model\v3\Goods;
  6. use App\Model\v3\GoodsActivity;
  7. use App\Model\v3\ShoppingCart;
  8. use App\Model\v3\Store;
  9. use App\Service\v3\Interfaces\GoodsActivityServiceInterface;
  10. use App\Service\v3\Interfaces\GoodsServiceInterface;
  11. use App\Service\v3\Interfaces\ShopCartServiceInterface;
  12. use App\Service\v3\Interfaces\StoreServiceInterface;
  13. use Hyperf\DbConnection\Db;
  14. use Hyperf\Di\Annotation\Inject;
  15. class ShopCartService implements ShopCartServiceInterface
  16. {
  17. /**
  18. * @Inject
  19. * @var StoreServiceInterface
  20. */
  21. protected $storeService;
  22. /**
  23. * @Inject
  24. * @var GoodsServiceInterface
  25. */
  26. protected $goodsService;
  27. /**
  28. * @Inject
  29. * @var GoodsActivityServiceInterface
  30. */
  31. protected $goodsActivityService;
  32. public function do($userId,$marketId)
  33. {
  34. $storeIds = Db::table('lanzu_shopping_cart')->where([
  35. ['user_id','=',$userId],
  36. ['market_id','=',$marketId],
  37. ])->pluck('store_id')->toArray();
  38. $stores = Store::query()->with(['ShoppingCart' => function($query) use ($userId,$marketId){
  39. $query->where([
  40. ['user_id','=',$userId],
  41. ['market_id','=',$marketId],
  42. ]);
  43. }])->whereIn('id',$storeIds)
  44. ->get()
  45. ->toArray();
  46. $storeArr = [];
  47. foreach ($stores as $key => &$store){
  48. $sotreType = $this->storeService->check($store['id']);
  49. if(!$sotreType){
  50. continue;
  51. }
  52. $subtotal = 0;
  53. foreach ($store['shopping_cart'] as $k => &$shopcart){
  54. if($shopcart['activity_type'] == 1){
  55. $goodsType = $this->goodsService->check($shopcart['goods_id']);
  56. }else{
  57. $goodsType = $this->goodsActivityService->check($shopcart['goods_id'],$shopcart['num'],$shopcart['user_id']);
  58. }
  59. if($goodsType !== true){
  60. unset($store['shopping_cart'][$k]);
  61. continue;
  62. }
  63. if($shopcart['activity_type'] == 1){
  64. $builder = Goods::query();
  65. }else{
  66. $builder = GoodsActivity::query();
  67. }
  68. $shopcart['goods'] = $builder->where('id',$shopcart['goods_id'])->first()->toArray();
  69. $subtotal+= $shopcart['num'] * $shopcart['goods']['price'];
  70. }
  71. $store['subtotal'] = $subtotal;
  72. $storeArr[] = $store;
  73. }
  74. return $storeArr;
  75. }
  76. public function check($userId,$goodsId,$activityType = 1)
  77. {
  78. $ShoppingCart = ShoppingCart::query()->where([
  79. ['user_id','=',$userId],
  80. ['goods_id','=',$goodsId],
  81. ['activity_type','=',$activityType],
  82. ])
  83. ->select('num')
  84. ->first();
  85. return $ShoppingCart->num ?? 0;
  86. }
  87. public function undo($userId,$marketId)
  88. {
  89. $storeIds = Db::table('lanzu_shopping_cart')->where([
  90. ['user_id','=',$userId],
  91. ['market_id','=',$marketId],
  92. ])->pluck('store_id')->toArray();
  93. $stores = Store::query()->with(['ShoppingCart' => function($query) use ($userId,$marketId){
  94. $query->where([
  95. ['user_id','=',$userId],
  96. ['market_id','=',$marketId],
  97. ]);
  98. }])->whereIn('id',$storeIds)
  99. ->get()
  100. ->toArray();
  101. $storeArr = [];
  102. foreach ($stores as $key => &$store){
  103. $addStore = false;
  104. $sotreType = $this->storeService->check($store['id']);
  105. if(!$sotreType){
  106. $addStore = true;
  107. }
  108. foreach ($store['shopping_cart'] as $k => &$shopcart){
  109. if($shopcart['activity_type'] == 1){
  110. $goodsType = $this->goodsService->check($shopcart['goods_id']);
  111. }else{
  112. $goodsType = $this->goodsActivityService->check($shopcart['goods_id'],$shopcart['num'],$shopcart['user_id']);
  113. }
  114. if($goodsType === true){
  115. unset($store['shopping_cart'][$k]);
  116. continue;
  117. }
  118. $addStore = true;
  119. if($shopcart['activity_type'] == 1){
  120. $builder = Goods::query();
  121. }else{
  122. $builder = GoodsActivity::query();
  123. }
  124. $shopcart['goods'] = $builder->where('id',$shopcart['goods_id'])->first()->toArray();
  125. $shopcart['goods']['invalid_cause'] = $goodsType;
  126. }
  127. if($addStore){
  128. $storeArr[] = $store;
  129. }
  130. }
  131. return $storeArr;
  132. }
  133. public function countGoods($userId,$marketId)
  134. {
  135. $shoppingCart = $this->allForUser($userId,$marketId);
  136. $count = 0;
  137. foreach ($shoppingCart['store_lists'] as $store){
  138. foreach ($store['shopping_cart'] as $cart) {
  139. $num = $cart['num'];
  140. $count += $num;
  141. }
  142. }
  143. return $count;
  144. }
  145. public function getTotal($userId,$marketId)
  146. {
  147. $shoppingCart = $this->allForUser($userId,$marketId);
  148. return $shoppingCart['total'];
  149. }
  150. public function getGoodsByShopcartId($shopcartIds)
  151. {
  152. bcscale(6);
  153. // 查询当前用户的市场下的购物车数据
  154. $shopcartIdsArr = explode(',',$shopcartIds);
  155. $carts = ShoppingCart::query()
  156. ->with(['store', 'goods'])
  157. ->whereIn('id',$shopcartIdsArr)
  158. ->get();
  159. $cartList = [];
  160. $totalAmount = 0;
  161. foreach ($carts as $key => &$cart) {
  162. if (empty($cart->store)) {
  163. $msg = ["car_module"=>"1","shopcartIdsArr"=>$shopcartIdsArr];
  164. throw new ErrorCodeException(ErrorCode::STORE_NOT_AVAILABLE,json_encode($msg));
  165. continue;
  166. }
  167. if (empty($cart->goods)) {
  168. throw new ErrorCodeException(ErrorCode::GOODS_NOT_EXISTS);
  169. continue;
  170. }
  171. $shoppingCart = $cart->toArray();
  172. $store = $shoppingCart['store'];
  173. unset($shoppingCart['store']);
  174. // 商户是否歇业
  175. $checkStore = $this->storeService->check($store['id']);
  176. if (!$checkStore) {
  177. throw new ErrorCodeException(ErrorCode::STORE_NOT_AVAILABLE);
  178. continue;
  179. }
  180. // 商品是否失效
  181. if ($cart->activity_type == 2) {
  182. $goods = GoodsActivity::query()->find($cart->goods_id);
  183. $res = $this->goodsActivityService->check($goods, $cart->num, $cart->user_id);
  184. } else {
  185. $goods = Goods::query()->find($cart->goods_id);
  186. $res = $this->goodsService->check($goods, $cart->num);
  187. }
  188. if ($res === true) {
  189. $cartList['store_lists'][$store['id']]['store'] = $store;
  190. $cartList['store_lists'][$store['id']]['shopping_cart'][] = $shoppingCart;
  191. if (isset($cartList['store_lists'][$store['id']]['subtotal'])) {
  192. $cartList['store_lists'][$store['id']]['subtotal'] = bcadd(
  193. $cartList['store_lists'][$store['id']]['subtotal'],
  194. bcmul($cart->goods->price, $cart->num)
  195. );
  196. } else {
  197. $cartList['store_lists'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
  198. }
  199. $totalAmount = bcadd($totalAmount, bcmul($cart->goods->price, $cart->num));
  200. }else{
  201. throw new ErrorCodeException($res);
  202. }
  203. }
  204. $cartList['store_lists'] = isset($cartList['store_lists']) ? array_values($cartList['store_lists']) : [];
  205. foreach ($cartList['store_lists'] as $key => &$value) {
  206. $value['subtotal'] = bcadd($value['subtotal'], '0' , 2);
  207. }
  208. $cartList['total'] = bcadd($totalAmount, '0', 2);
  209. return $cartList['store_lists'];
  210. }
  211. /**
  212. * 获取用户购物车数据
  213. * @param $userId
  214. * @param $marketId
  215. * @return array
  216. */
  217. public function allForUser($userId, $marketId)
  218. {
  219. bcscale(6);
  220. // 查询当前用户的市场下的购物车数据
  221. $carts = ShoppingCart::query()
  222. ->with(['store',
  223. 'goods' => function($query) {
  224. $query->withoutGlobalScope('normal');
  225. }
  226. ])
  227. ->where(['user_id' => $userId])
  228. ->where(['market_id' => $marketId])
  229. ->get();
  230. $cartList = [];
  231. $totalAmount = 0;
  232. foreach ($carts as $key => &$cart) {
  233. if (empty($cart->store)||empty($cart->goods)) {
  234. continue;
  235. }
  236. $shoppingCart = $cart->toArray();
  237. $store = $shoppingCart['store'];
  238. unset($shoppingCart['store']);
  239. // 商户是否歇业
  240. $checkStore = $this->storeService->check($store['id']);
  241. if (!$checkStore) {
  242. $cartList['store_lists_invalid'][$store['id']]['store'] = $store;
  243. $cartList['store_lists_invalid'][$store['id']]['shopping_cart'][] = $shoppingCart;
  244. if (isset($cartList['store_lists_invalid'][$store['id']]['subtotal'])) {
  245. $cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcadd(
  246. $cartList['store_lists_invalid'][$store['id']]['subtotal'],
  247. bcmul($cart->goods->price, $cart->num)
  248. );
  249. } else {
  250. $cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
  251. }
  252. continue;
  253. }
  254. // 商品是否失效
  255. if ($cart->activity_type == 2) {
  256. $res = $this->goodsActivityService->check($cart->goods, $cart->num, $cart->user_id);
  257. } else {
  258. $res = $this->goodsService->check($cart->goods, $cart->num);
  259. }
  260. if ($res === true) {
  261. $cartList['store_lists'][$store['id']]['store'] = $store;
  262. $cartList['store_lists'][$store['id']]['shopping_cart'][] = $shoppingCart;
  263. if (isset($cartList['store_lists'][$store['id']]['subtotal'])) {
  264. $cartList['store_lists'][$store['id']]['subtotal'] = bcadd(
  265. $cartList['store_lists'][$store['id']]['subtotal'],
  266. bcmul($cart->goods->price, $cart->num)
  267. );
  268. } else {
  269. $cartList['store_lists'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
  270. }
  271. $totalAmount = bcadd($totalAmount, bcmul($cart->goods->price, $cart->num));
  272. } else {
  273. $cartList['store_lists_invalid'][$store['id']]['store'] = $store;
  274. $cartList['store_lists_invalid'][$store['id']]['shopping_cart'][] = $shoppingCart;
  275. if (isset($cartList['store_lists_invalid'][$store['id']]['subtotal'])) {
  276. $cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcadd(
  277. $cartList['store_lists_invalid'][$store['id']]['subtotal'],
  278. bcmul($cart->goods->price, $cart->num)
  279. );
  280. } else {
  281. $cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
  282. }
  283. }
  284. }
  285. $cartList['store_lists'] = isset($cartList['store_lists']) ? array_values($cartList['store_lists']) : [];
  286. $cartList['store_lists_invalid'] = isset($cartList['store_lists_invalid']) ? array_values($cartList['store_lists_invalid']) : [];
  287. foreach ($cartList['store_lists'] as $key => &$value) {
  288. $value['subtotal'] = bcadd($value['subtotal'], '0' , 2);
  289. }
  290. foreach ($cartList['store_lists_invalid'] as $key => &$value) {
  291. $value['subtotal'] = bcadd($value['subtotal'], '0' , 2);
  292. }
  293. $cartList['total'] = bcadd($totalAmount, '0', 2);
  294. return $cartList;
  295. }
  296. }