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.

124 lines
3.8 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
  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()
  82. {
  83. $storeIds = Db::table('lanzu_shopping_cart')->where('user_id',198)->pluck('store_id')->toArray();
  84. $res = Store::query()->with(['ShoppingCart' => function($query) {
  85. $query->with('goods');
  86. }])->whereIn('id',$storeIds)
  87. ->get();
  88. foreach ($res as &$k){
  89. $k->subtotal = '99.90';
  90. foreach ($k->ShoppingCart as &$v){
  91. switch (mt_rand(1,3)) {
  92. case 1:
  93. $str = '已下架';
  94. break;
  95. case 2:
  96. $str = '已抢光';
  97. break;
  98. case 3:
  99. $str = '已打烊';
  100. break;
  101. }
  102. $v['goods']['invalid_cause'] = $str;
  103. }
  104. }
  105. return $res;
  106. }
  107. public function countGoods()
  108. {
  109. return mt_rand(1,100);
  110. }
  111. public function getTotal($userId,$marketId)
  112. {
  113. $randomFloat = rand(100,999)/100;
  114. return $randomFloat;
  115. }
  116. }