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.

97 lines
2.6 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
  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 Hyperf\Di\Annotation\Inject;
  13. class ShopCartUpdateService implements ShopCartUpdateServiceInterface
  14. {
  15. /**
  16. * @Inject
  17. * @var GoodsServiceInterface
  18. */
  19. protected $goodsService;
  20. /**
  21. * @Inject
  22. * @var GoodsActivityServiceInterface
  23. */
  24. protected $goodsActivityService;
  25. public function do($userId,$goodsId,$num,$activityType)
  26. {
  27. $goodsType = '';
  28. //判断是普通商品还是特价商品
  29. if($activityType == GoodsConstants::IS_ACTIVITY){
  30. $builder = GoodsActivity::query();
  31. $goodsType = GoodsActivity::class;
  32. $goods = $builder->find($goodsId);
  33. if(empty($goods)){
  34. throw new ErrorCodeException(ErrorCode::GOODS_NOT_EXISTS);
  35. }
  36. $goodsCheck = $this->goodsActivityService->check($goods,$num,$userId);
  37. }else{
  38. $builder = Goods::query();
  39. $goodsType = Goods::class;
  40. $goods = $builder->find($goodsId);
  41. if(empty($goods)){
  42. throw new ErrorCodeException(ErrorCode::GOODS_NOT_EXISTS);
  43. }
  44. $goodsCheck = $this->goodsService->check($goods,$num);
  45. }
  46. if(empty($goods)){
  47. throw new ErrorCodeException(ErrorCode::GOODS_NOT_EXISTS);
  48. }
  49. if($goodsCheck !== true)
  50. {
  51. throw new ErrorCodeException($goodsCheck);
  52. }
  53. return ShoppingCart::query()->lockForUpdate()->updateOrCreate(
  54. [
  55. 'user_id' => $userId,
  56. 'goods_id' => $goodsId,
  57. 'activity_type' => $activityType
  58. ],
  59. [
  60. 'market_id' => $goods->market_id,
  61. 'store_id' => $goods->store_id,
  62. 'num' => $num,
  63. 'goods_type' => $goodsType
  64. ]
  65. );
  66. }
  67. public function check()
  68. {
  69. // TODO: Implement check() method.
  70. }
  71. public function undo($shopcartIds)
  72. {
  73. $shopcartIdsArr = explode(',',$shopcartIds);
  74. return ShoppingCart::destroy($shopcartIdsArr);
  75. }
  76. /**
  77. * 清空购物车
  78. * @param $userId
  79. * @param $marketId
  80. */
  81. public function doClear($userId, $marketId)
  82. {
  83. return ShoppingCart::query()->where([
  84. ['user_id','=',$userId],
  85. ['market_id','=',$marketId],
  86. ])
  87. ->delete();
  88. }
  89. }