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.

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