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.

144 lines
4.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 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. unset($stores[$key]);
  49. continue;
  50. }
  51. $subtotal = 0;
  52. foreach ($store['shopping_cart'] as &$shopcart){
  53. if($shopcart['activity_type'] == 1){
  54. $builder = Goods::query();
  55. }else{
  56. $builder = GoodsActivity::query();
  57. }
  58. $shopcart['goods'] = $builder->where('id',$shopcart['goods_id'])->first()->toArray();
  59. foreach ($shopcart['goods'] as $goods){
  60. if($shopcart['activity_type'] == 1){
  61. $goodsType = $this->goodsService->check($goods['id']);
  62. }else{
  63. $goodsType = $this->goodsActivityService->check($goods['id'],$shopcart['num'],$shopcart['user_id']);
  64. }
  65. if($goodsType !== true){
  66. unset($shopcart['goods'][$key]);
  67. continue;
  68. }
  69. $subtotal+= $shopcart['num'] * $shopcart['goods']['price'];
  70. }
  71. }
  72. $store['subtotal'] = $subtotal;
  73. $storeArr[] = $store;
  74. }
  75. return $storeArr;
  76. }
  77. public function check($goodsId)
  78. {
  79. return mt_rand(0,6);
  80. }
  81. public function undo($userId,$marketId)
  82. {
  83. $storeIds = Db::table('lanzu_shopping_cart')->where([
  84. ['user_id','=',$userId],
  85. ['market_id','=',$marketId],
  86. ])->pluck('store_id')->toArray();
  87. $stores = Store::query()->with(['ShoppingCart' => function($query) use ($userId,$marketId){
  88. $query->where([
  89. ['user_id','=',$userId],
  90. ['market_id','=',$marketId],
  91. ]);
  92. }])->whereIn('id',$storeIds)
  93. ->get()
  94. ->toArray();
  95. $storeArr = [];
  96. foreach ($stores as $key => &$store){
  97. $sotreType = $this->storeService->check($store['id']);
  98. if(!$sotreType){
  99. unset($stores[$key]);
  100. continue;
  101. }
  102. $subtotal = 0;
  103. foreach ($store['shopping_cart'] as &$shopcart){
  104. if($shopcart['activity_type'] == 1){
  105. $builder = Goods::query();
  106. }else{
  107. $builder = GoodsActivity::query();
  108. }
  109. $shopcart['goods'] = $builder->where('id',$shopcart['goods_id'])->first()->toArray();
  110. foreach ($shopcart['goods'] as $goods){
  111. if($shopcart['activity_type'] == 1){
  112. $goodsType = $this->goodsService->check($goods['id']);
  113. }else{
  114. $goodsType = $this->goodsActivityService->check($goods['id'],$shopcart['num'],$shopcart['user_id']);
  115. }
  116. if($goodsType !== true){
  117. unset($shopcart['goods'][$key]);
  118. continue;
  119. }
  120. $subtotal+= $shopcart['num'] * $shopcart['goods']['price'];
  121. }
  122. }
  123. $store['subtotal'] = $subtotal;
  124. $storeArr[] = $store;
  125. }
  126. return $storeArr;
  127. }
  128. public function countGoods()
  129. {
  130. return mt_rand(1,100);
  131. }
  132. public function getTotal($userId,$marketId)
  133. {
  134. $randomFloat = rand(100,999)/100;
  135. return $randomFloat;
  136. }
  137. }