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.

183 lines
6.4 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
  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. }