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.

134 lines
4.2 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\Constants\v3\ErrorCode;
  4. use App\Exception\ErrorCodeException;
  5. use App\Service\v3\Interfaces\GoodsActivityServiceInterface;
  6. use App\Service\v3\Interfaces\GoodsServiceInterface;
  7. use App\Service\v3\Interfaces\ShopCartUpdateServiceInterface;
  8. use App\Model\v3\ShoppingCart;
  9. use App\Model\v3\Goods;
  10. use App\Model\v3\GoodsActivity;
  11. use App\Constants\v3\Goods as GoodsConstants;
  12. use App\Service\v3\Interfaces\StoreServiceInterface;
  13. use Hyperf\Di\Annotation\Inject;
  14. class ShopCartUpdateService implements ShopCartUpdateServiceInterface
  15. {
  16. /**
  17. * @Inject
  18. * @var GoodsServiceInterface
  19. */
  20. protected $goodsService;
  21. /**
  22. * @Inject
  23. * @var GoodsActivityServiceInterface
  24. */
  25. protected $goodsActivityService;
  26. /**
  27. * @Inject
  28. * @var StoreServiceInterface
  29. */
  30. protected $storeService;
  31. public function do($userId,$goodsId,$num,$activityType)
  32. {
  33. $goodsType = '';
  34. //判断是普通商品还是特价商品
  35. if($activityType == GoodsConstants::IS_ACTIVITY){
  36. $builder = GoodsActivity::query();
  37. $goodsType = GoodsActivity::class;
  38. $goods = $builder->find($goodsId);
  39. if(empty($goods)){
  40. throw new ErrorCodeException(ErrorCode::GOODS_NOT_EXISTS);
  41. }
  42. //特价商品限购
  43. $goodsArr = ShoppingCart::query()
  44. ->where(['user_id' => $userId,'market_id' => $goods->market_id,'activity_type' => GoodsConstants::IS_ACTIVITY])
  45. ->pluck('goods_id')->toArray();
  46. array_push($goodsArr,$goodsId);
  47. $checkGoodsActivity = $this->goodsActivityService->checkOrderActivityCount($goodsArr);
  48. if(!$checkGoodsActivity){
  49. throw new ErrorCodeException(ErrorCode::GOODS_ACTIVITY_RESTRICT_LIMIT);
  50. }
  51. $goodsCheck = $this->goodsActivityService->check($goods,$num,$userId);
  52. }else{
  53. $builder = Goods::query();
  54. $goodsType = Goods::class;
  55. $goods = $builder->find($goodsId);
  56. if(empty($goods)){
  57. throw new ErrorCodeException(ErrorCode::GOODS_NOT_EXISTS);
  58. }
  59. $goodsCheck = $this->goodsService->check($goods,$num);
  60. }
  61. if(empty($goods)){
  62. throw new ErrorCodeException(ErrorCode::GOODS_NOT_EXISTS);
  63. }
  64. if($goodsCheck !== true)
  65. {
  66. if($goodsCheck === ErrorCode::GOODS_ACTIVITY_INVENTORY_ERROR){
  67. $shopcartNum = ShoppingCart::query()
  68. ->where([
  69. 'user_id' => $userId,
  70. 'goods_id' => $goodsId,
  71. 'activity_type' => $activityType
  72. ])
  73. ->value('num');
  74. if($num > $shopcartNum){
  75. throw new ErrorCodeException($goodsCheck);
  76. }
  77. }else{
  78. throw new ErrorCodeException($goodsCheck);
  79. }
  80. }
  81. $shoppingCart = ShoppingCart::query()->lockForUpdate()->updateOrCreate(
  82. [
  83. 'user_id' => $userId,
  84. 'goods_id' => $goodsId,
  85. 'activity_type' => $activityType
  86. ],
  87. [
  88. 'market_id' => $goods->market_id,
  89. 'store_id' => $goods->store_id,
  90. 'num' => $num,
  91. 'goods_type' => $goodsType
  92. ]
  93. );
  94. $storeCheck = $this->storeService->check($goods->store_id);
  95. $shoppingCart->check = $storeCheck;
  96. return $shoppingCart;
  97. }
  98. public function check()
  99. {
  100. // TODO: Implement check() method.
  101. }
  102. public function undo($shopcartIds)
  103. {
  104. $shopcartIdsArr = explode(',',$shopcartIds);
  105. return ShoppingCart::destroy($shopcartIdsArr);
  106. }
  107. /**
  108. * 清空购物车
  109. * @param $userId
  110. * @param $marketId
  111. * @param array $shopcartIds
  112. * @return int|mixed
  113. */
  114. public function doClear($userId, $marketId, $shopcartIds = [])
  115. {
  116. $builder = ShoppingCart::query()->where([
  117. ['user_id','=',$userId],
  118. ['market_id','=',$marketId],
  119. ]);
  120. if (!empty($shopcartIds)) {
  121. $builder = $builder->whereIn('id', $shopcartIds);
  122. }
  123. return $builder->delete();
  124. }
  125. }