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.

326 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
  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,$activityType = 1)
  75. {
  76. $ShoppingCart = ShoppingCart::query()->where([
  77. ['user_id','=',$userId],
  78. ['goods_id','=',$goodsId],
  79. ['activity_type','=',$activityType],
  80. ])
  81. ->select('num')
  82. ->first();
  83. return $ShoppingCart->num ?? 0;
  84. }
  85. public function undo($userId,$marketId)
  86. {
  87. $storeIds = Db::table('lanzu_shopping_cart')->where([
  88. ['user_id','=',$userId],
  89. ['market_id','=',$marketId],
  90. ])->pluck('store_id')->toArray();
  91. $stores = Store::query()->with(['ShoppingCart' => function($query) use ($userId,$marketId){
  92. $query->where([
  93. ['user_id','=',$userId],
  94. ['market_id','=',$marketId],
  95. ]);
  96. }])->whereIn('id',$storeIds)
  97. ->get()
  98. ->toArray();
  99. $storeArr = [];
  100. foreach ($stores as $key => &$store){
  101. $addStore = false;
  102. $sotreType = $this->storeService->check($store['id']);
  103. if(!$sotreType){
  104. $addStore = true;
  105. }
  106. foreach ($store['shopping_cart'] as $k => &$shopcart){
  107. if($shopcart['activity_type'] == 1){
  108. $goodsType = $this->goodsService->check($shopcart['goods_id']);
  109. }else{
  110. $goodsType = $this->goodsActivityService->check($shopcart['goods_id'],$shopcart['num'],$shopcart['user_id']);
  111. }
  112. if($goodsType === true){
  113. unset($store['shopping_cart'][$k]);
  114. continue;
  115. }
  116. $addStore = true;
  117. if($shopcart['activity_type'] == 1){
  118. $builder = Goods::query();
  119. }else{
  120. $builder = GoodsActivity::query();
  121. }
  122. $shopcart['goods'] = $builder->where('id',$shopcart['goods_id'])->first()->toArray();
  123. $shopcart['goods']['invalid_cause'] = $goodsType;
  124. }
  125. if($addStore){
  126. $storeArr[] = $store;
  127. }
  128. }
  129. return $storeArr;
  130. }
  131. public function countGoods($userId,$marketId)
  132. {
  133. $shoppingCart = $this->allForUser($userId,$marketId);
  134. $count = 0;
  135. foreach ($shoppingCart['store_lists'] as $store){
  136. foreach ($store['shopping_cart'] as $cart) {
  137. $num = $cart['num'];
  138. $count += $num;
  139. }
  140. }
  141. return $count;
  142. }
  143. public function getTotal($userId,$marketId)
  144. {
  145. $shoppingCart = $this->allForUser($userId,$marketId);
  146. return $shoppingCart['total'];
  147. }
  148. public function getGoodsByShopcartId($shopcartIds)
  149. {
  150. bcscale(6);
  151. // 查询当前用户的市场下的购物车数据
  152. $shopcartIdsArr = explode(',',$shopcartIds);
  153. $carts = ShoppingCart::query()
  154. ->with(['store', 'goods'])
  155. ->whereIn('id',$shopcartIdsArr)
  156. ->get();
  157. $cartList = [];
  158. $totalAmount = 0;
  159. foreach ($carts as $key => &$cart) {
  160. if (empty($cart->store)||empty($cart->goods)) {
  161. continue;
  162. }
  163. $shoppingCart = $cart->toArray();
  164. $store = $shoppingCart['store'];
  165. unset($shoppingCart['store']);
  166. // 商户是否歇业
  167. $checkStore = $this->storeService->check($store['id']);
  168. if (!$checkStore) {
  169. continue;
  170. }
  171. // 商品是否失效
  172. if ($cart->activity_type == 2) {
  173. $goods = GoodsActivity::query()->find($cart->goods_id);
  174. $res = $this->goodsActivityService->check($goods, $cart->num, $cart->user_id);
  175. } else {
  176. $goods = Goods::query()->find($cart->goods_id);
  177. $res = $this->goodsService->check($goods, $cart->num);
  178. }
  179. if ($res === true) {
  180. $cartList['store_lists'][$store['id']]['store'] = $store;
  181. $cartList['store_lists'][$store['id']]['shopping_cart'][] = $shoppingCart;
  182. if (isset($cartList['store_lists'][$store['id']]['subtotal'])) {
  183. $cartList['store_lists'][$store['id']]['subtotal'] = bcadd(
  184. $cartList['store_lists'][$store['id']]['subtotal'],
  185. bcmul($cart->goods->price, $cart->num)
  186. );
  187. } else {
  188. $cartList['store_lists'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
  189. }
  190. $totalAmount = bcadd($totalAmount, bcmul($cart->goods->price, $cart->num));
  191. }
  192. }
  193. $cartList['store_lists'] = isset($cartList['store_lists']) ? array_values($cartList['store_lists']) : [];
  194. foreach ($cartList['store_lists'] as $key => &$value) {
  195. $value['subtotal'] = bcadd($value['subtotal'], '0' , 2);
  196. }
  197. $cartList['total'] = bcadd($totalAmount, '0', 2);
  198. return $cartList['store_lists'];
  199. }
  200. /**
  201. * 获取用户购物车数据
  202. * @param $userId
  203. * @param $marketId
  204. * @return array
  205. */
  206. public function allForUser($userId, $marketId)
  207. {
  208. bcscale(6);
  209. // 查询当前用户的市场下的购物车数据
  210. $carts = ShoppingCart::query()
  211. ->with(['store',
  212. 'goods' => function($query) {
  213. $query->withoutGlobalScope('normal');
  214. }
  215. ])
  216. ->where(['user_id' => $userId])
  217. ->where(['market_id' => $marketId])
  218. ->get();
  219. $cartList = [];
  220. $totalAmount = 0;
  221. foreach ($carts as $key => &$cart) {
  222. if (empty($cart->store)||empty($cart->goods)) {
  223. continue;
  224. }
  225. $shoppingCart = $cart->toArray();
  226. $store = $shoppingCart['store'];
  227. unset($shoppingCart['store']);
  228. // 商户是否歇业
  229. $checkStore = $this->storeService->check($store['id']);
  230. if (!$checkStore) {
  231. $cartList['store_lists_invalid'][$store['id']]['store'] = $store;
  232. $cartList['store_lists_invalid'][$store['id']]['shopping_cart'][] = $shoppingCart;
  233. if (isset($cartList['store_lists_invalid'][$store['id']]['subtotal'])) {
  234. $cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcadd(
  235. $cartList['store_lists_invalid'][$store['id']]['subtotal'],
  236. bcmul($cart->goods->price, $cart->num)
  237. );
  238. } else {
  239. $cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
  240. }
  241. continue;
  242. }
  243. // 商品是否失效
  244. if ($cart->activity_type == 2) {
  245. $res = $this->goodsActivityService->check($cart->goods, $cart->num, $cart->user_id);
  246. } else {
  247. $res = $this->goodsService->check($cart->goods, $cart->num);
  248. }
  249. if ($res === true) {
  250. $cartList['store_lists'][$store['id']]['store'] = $store;
  251. $cartList['store_lists'][$store['id']]['shopping_cart'][] = $shoppingCart;
  252. if (isset($cartList['store_lists'][$store['id']]['subtotal'])) {
  253. $cartList['store_lists'][$store['id']]['subtotal'] = bcadd(
  254. $cartList['store_lists'][$store['id']]['subtotal'],
  255. bcmul($cart->goods->price, $cart->num)
  256. );
  257. } else {
  258. $cartList['store_lists'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
  259. }
  260. $totalAmount = bcadd($totalAmount, bcmul($cart->goods->price, $cart->num));
  261. } else {
  262. $cartList['store_lists_invalid'][$store['id']]['store'] = $store;
  263. $cartList['store_lists_invalid'][$store['id']]['shopping_cart'][] = $shoppingCart;
  264. if (isset($cartList['store_lists_invalid'][$store['id']]['subtotal'])) {
  265. $cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcadd(
  266. $cartList['store_lists_invalid'][$store['id']]['subtotal'],
  267. bcmul($cart->goods->price, $cart->num)
  268. );
  269. } else {
  270. $cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
  271. }
  272. }
  273. }
  274. $cartList['store_lists'] = isset($cartList['store_lists']) ? array_values($cartList['store_lists']) : [];
  275. $cartList['store_lists_invalid'] = isset($cartList['store_lists_invalid']) ? array_values($cartList['store_lists_invalid']) : [];
  276. foreach ($cartList['store_lists'] as $key => &$value) {
  277. $value['subtotal'] = bcadd($value['subtotal'], '0' , 2);
  278. }
  279. foreach ($cartList['store_lists_invalid'] as $key => &$value) {
  280. $value['subtotal'] = bcadd($value['subtotal'], '0' , 2);
  281. }
  282. $cartList['total'] = bcadd($totalAmount, '0', 2);
  283. return $cartList;
  284. }
  285. }