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.

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