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.

323 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
  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 = $this->allForUser($userId,$marketId);
  133. $count = 0;
  134. foreach ($shoppingCart['store_lists'] as $store){
  135. foreach ($store['shopping_cart'] as $cart) {
  136. $num = $cart['num'];
  137. $count += $num;
  138. }
  139. }
  140. return $count;
  141. }
  142. public function getTotal($userId,$marketId)
  143. {
  144. $shoppingCart = $this->allForUser($userId,$marketId);
  145. return $shoppingCart['total'];
  146. }
  147. public function getGoodsByShopcartId($shopcartIds)
  148. {
  149. bcscale(6);
  150. // 查询当前用户的市场下的购物车数据
  151. $shopcartIdsArr = explode(',',$shopcartIds);
  152. $carts = ShoppingCart::query()
  153. ->with(['store', 'goods'])
  154. ->whereIn('id',$shopcartIdsArr)
  155. ->get();
  156. $cartList = [];
  157. $totalAmount = 0;
  158. foreach ($carts as $key => &$cart) {
  159. if (empty($cart->store)||empty($cart->goods)) {
  160. continue;
  161. }
  162. $shoppingCart = $cart->toArray();
  163. $store = $shoppingCart['store'];
  164. unset($shoppingCart['store']);
  165. // 商户是否歇业
  166. $checkStore = $this->storeService->check($store['id']);
  167. if (!$checkStore) {
  168. continue;
  169. }
  170. // 商品是否失效
  171. if ($cart->activity_type == 2) {
  172. $goods = GoodsActivity::query()->find($cart->goods_id);
  173. $res = $this->goodsActivityService->check($goods, $cart->num, $cart->user_id);
  174. } else {
  175. $goods = Goods::query()->find($cart->goods_id);
  176. $res = $this->goodsService->check($goods, $cart->num);
  177. }
  178. if ($res === true) {
  179. $cartList['store_lists'][$store['id']]['store'] = $store;
  180. $cartList['store_lists'][$store['id']]['shopping_cart'][] = $shoppingCart;
  181. if (isset($cartList['store_lists'][$store['id']]['subtotal'])) {
  182. $cartList['store_lists'][$store['id']]['subtotal'] = bcadd(
  183. $cartList['store_lists'][$store['id']]['subtotal'],
  184. bcmul($cart->goods->price, $cart->num)
  185. );
  186. } else {
  187. $cartList['store_lists'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
  188. }
  189. $totalAmount = bcadd($totalAmount, bcmul($cart->goods->price, $cart->num));
  190. }
  191. }
  192. $cartList['store_lists'] = isset($cartList['store_lists']) ? array_values($cartList['store_lists']) : [];
  193. foreach ($cartList['store_lists'] as $key => &$value) {
  194. $value['subtotal'] = bcadd($value['subtotal'], '0' , 2);
  195. }
  196. $cartList['total'] = bcadd($totalAmount, '0', 2);
  197. return $cartList['store_lists'];
  198. }
  199. /**
  200. * 获取用户购物车数据
  201. * @param $userId
  202. * @param $marketId
  203. * @return array
  204. */
  205. public function allForUser($userId, $marketId)
  206. {
  207. bcscale(6);
  208. // 查询当前用户的市场下的购物车数据
  209. $carts = ShoppingCart::query()
  210. ->with(['store', 'goods'])
  211. ->where(['user_id' => $userId])
  212. ->where(['market_id' => $marketId])
  213. ->get();
  214. $cartList = [];
  215. $totalAmount = 0;
  216. foreach ($carts as $key => &$cart) {
  217. if (empty($cart->store)||empty($cart->goods)) {
  218. continue;
  219. }
  220. $shoppingCart = $cart->toArray();
  221. $store = $shoppingCart['store'];
  222. unset($shoppingCart['store']);
  223. // 商户是否歇业
  224. $checkStore = $this->storeService->check($store['id']);
  225. if (!$checkStore) {
  226. $cartList['store_lists_invalid'][$store['id']]['store'] = $store;
  227. $cartList['store_lists_invalid'][$store['id']]['shopping_cart'][] = $shoppingCart;
  228. if (isset($cartList['store_lists_invalid'][$store['id']]['subtotal'])) {
  229. $cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcadd(
  230. $cartList['store_lists_invalid'][$store['id']]['subtotal'],
  231. bcmul($cart->goods->price, $cart->num)
  232. );
  233. } else {
  234. $cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
  235. }
  236. continue;
  237. }
  238. // 商品是否失效
  239. if ($cart->activity_type == 2) {
  240. $goods = GoodsActivity::query()->find($cart->goods_id);
  241. $res = $this->goodsActivityService->check($goods, $cart->num, $cart->user_id);
  242. } else {
  243. $goods = Goods::query()->find($cart->goods_id);
  244. $res = $this->goodsService->check($goods, $cart->num);
  245. }
  246. if ($res === true) {
  247. $cartList['store_lists'][$store['id']]['store'] = $store;
  248. $cartList['store_lists'][$store['id']]['shopping_cart'][] = $shoppingCart;
  249. if (isset($cartList['store_lists'][$store['id']]['subtotal'])) {
  250. $cartList['store_lists'][$store['id']]['subtotal'] = bcadd(
  251. $cartList['store_lists'][$store['id']]['subtotal'],
  252. bcmul($cart->goods->price, $cart->num)
  253. );
  254. } else {
  255. $cartList['store_lists'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
  256. }
  257. $totalAmount = bcadd($totalAmount, bcmul($cart->goods->price, $cart->num));
  258. } else {
  259. $cartList['store_lists_invalid'][$store['id']]['store'] = $store;
  260. $cartList['store_lists_invalid'][$store['id']]['shopping_cart'][] = $shoppingCart;
  261. if (isset($cartList['store_lists_invalid'][$store['id']]['subtotal'])) {
  262. $cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcadd(
  263. $cartList['store_lists_invalid'][$store['id']]['subtotal'],
  264. bcmul($cart->goods->price, $cart->num)
  265. );
  266. } else {
  267. $cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
  268. }
  269. }
  270. }
  271. $cartList['store_lists'] = isset($cartList['store_lists']) ? array_values($cartList['store_lists']) : [];
  272. $cartList['store_lists_invalid'] = isset($cartList['store_lists_invalid']) ? array_values($cartList['store_lists_invalid']) : [];
  273. foreach ($cartList['store_lists'] as $key => &$value) {
  274. $value['subtotal'] = bcadd($value['subtotal'], '0' , 2);
  275. }
  276. foreach ($cartList['store_lists_invalid'] as $key => &$value) {
  277. $value['subtotal'] = bcadd($value['subtotal'], '0' , 2);
  278. }
  279. $cartList['total'] = bcadd($totalAmount, '0', 2);
  280. return $cartList;
  281. }
  282. }