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.

349 lines
13 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)
  75. {
  76. $ShoppingCart = ShoppingCart::query()->where([
  77. ['user_id','=',$userId],
  78. ['goods_id','=',$goodsId],
  79. ])
  80. ->select('num')
  81. ->first();
  82. return $ShoppingCart->num;
  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. bcscale(6);
  176. // 查询当前用户的市场下的购物车数据
  177. $shopcartIdsArr = explode(',',$shopcartIds);
  178. $carts = ShoppingCart::query()
  179. ->with(['store', 'goods'])
  180. ->whereIn('id',$shopcartIdsArr)
  181. ->get();
  182. $cartList = [];
  183. $totalAmount = 0;
  184. foreach ($carts as $key => &$cart) {
  185. if (empty($cart->store)||empty($cart->goods)) {
  186. continue;
  187. }
  188. $shoppingCart = $cart->toArray();
  189. $store = $shoppingCart['store'];
  190. unset($shoppingCart['store']);
  191. // 商户是否歇业
  192. $checkStore = $this->storeService->check($store['id']);
  193. if (!$checkStore) {
  194. continue;
  195. }
  196. // 商品是否失效
  197. if ($cart->activity_type == 2) {
  198. $goods = GoodsActivity::query()->find($cart->goods_id);
  199. $res = $this->goodsActivityService->check($goods, $cart->num, $cart->user_id);
  200. } else {
  201. $goods = Goods::query()->find($cart->goods_id);
  202. $res = $this->goodsService->check($goods, $cart->num);
  203. }
  204. if ($res === true) {
  205. $cartList['store_lists'][$store['id']]['store'] = $store;
  206. $cartList['store_lists'][$store['id']]['shopping_cart'][] = $shoppingCart;
  207. if (isset($cartList['store_lists'][$store['id']]['subtotal'])) {
  208. $cartList['store_lists'][$store['id']]['subtotal'] = bcadd(
  209. $cartList['store_lists'][$store['id']]['subtotal'],
  210. bcmul($cart->goods->price, $cart->num)
  211. );
  212. } else {
  213. $cartList['store_lists'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
  214. }
  215. $totalAmount = bcadd($totalAmount, bcmul($cart->goods->price, $cart->num));
  216. }
  217. }
  218. $cartList['store_lists'] = isset($cartList['store_lists']) ? array_values($cartList['store_lists']) : [];
  219. foreach ($cartList['store_lists'] as $key => &$value) {
  220. $value['subtotal'] = bcadd($value['subtotal'], '0' , 2);
  221. }
  222. $cartList['total'] = bcadd($totalAmount, '0', 2);
  223. return $cartList['store_lists'];
  224. }
  225. /**
  226. * 获取用户购物车数据
  227. * @param $userId
  228. * @param $marketId
  229. * @return array
  230. */
  231. public function allForUser($userId, $marketId)
  232. {
  233. bcscale(6);
  234. // 查询当前用户的市场下的购物车数据
  235. $carts = ShoppingCart::query()
  236. ->with(['store', 'goods'])
  237. ->where(['user_id' => $userId])
  238. ->where(['market_id' => $marketId])
  239. ->get();
  240. $cartList = [];
  241. $totalAmount = 0;
  242. foreach ($carts as $key => &$cart) {
  243. if (empty($cart->store)||empty($cart->goods)) {
  244. continue;
  245. }
  246. $shoppingCart = $cart->toArray();
  247. $store = $shoppingCart['store'];
  248. unset($shoppingCart['store']);
  249. // 商户是否歇业
  250. $checkStore = $this->storeService->check($store['id']);
  251. if (!$checkStore) {
  252. $cartList['store_lists_invalid'][$store['id']]['store'] = $store;
  253. $cartList['store_lists_invalid'][$store['id']]['shopping_cart'][] = $shoppingCart;
  254. if (isset($cartList['store_lists_invalid'][$store['id']]['subtotal'])) {
  255. $cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcadd(
  256. $cartList['store_lists_invalid'][$store['id']]['subtotal'],
  257. bcmul($cart->goods->price, $cart->num)
  258. );
  259. } else {
  260. $cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
  261. }
  262. continue;
  263. }
  264. // 商品是否失效
  265. if ($cart->activity_type == 2) {
  266. $goods = GoodsActivity::query()->find($cart->goods_id);
  267. $res = $this->goodsActivityService->check($goods, $cart->num, $cart->user_id);
  268. } else {
  269. $goods = Goods::query()->find($cart->goods_id);
  270. $res = $this->goodsService->check($goods, $cart->num);
  271. }
  272. if ($res === true) {
  273. $cartList['store_lists'][$store['id']]['store'] = $store;
  274. $cartList['store_lists'][$store['id']]['shopping_cart'][] = $shoppingCart;
  275. if (isset($cartList['store_lists'][$store['id']]['subtotal'])) {
  276. $cartList['store_lists'][$store['id']]['subtotal'] = bcadd(
  277. $cartList['store_lists'][$store['id']]['subtotal'],
  278. bcmul($cart->goods->price, $cart->num)
  279. );
  280. } else {
  281. $cartList['store_lists'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
  282. }
  283. $totalAmount = bcadd($totalAmount, bcmul($cart->goods->price, $cart->num));
  284. } else {
  285. $cartList['store_lists_invalid'][$store['id']]['store'] = $store;
  286. $cartList['store_lists_invalid'][$store['id']]['shopping_cart'][] = $shoppingCart;
  287. if (isset($cartList['store_lists_invalid'][$store['id']]['subtotal'])) {
  288. $cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcadd(
  289. $cartList['store_lists_invalid'][$store['id']]['subtotal'],
  290. bcmul($cart->goods->price, $cart->num)
  291. );
  292. } else {
  293. $cartList['store_lists_invalid'][$store['id']]['subtotal'] = bcmul($cart->goods->price, $cart->num);
  294. }
  295. }
  296. }
  297. $cartList['store_lists'] = isset($cartList['store_lists']) ? array_values($cartList['store_lists']) : [];
  298. $cartList['store_lists_invalid'] = isset($cartList['store_lists_invalid']) ? array_values($cartList['store_lists_invalid']) : [];
  299. foreach ($cartList['store_lists'] as $key => &$value) {
  300. $value['subtotal'] = bcadd($value['subtotal'], '0' , 2);
  301. }
  302. foreach ($cartList['store_lists_invalid'] as $key => &$value) {
  303. $value['subtotal'] = bcadd($value['subtotal'], '0' , 2);
  304. }
  305. $cartList['total'] = bcadd($totalAmount, '0', 2);
  306. return $cartList;
  307. }
  308. }