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.

177 lines
5.2 KiB

5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Constants\v3\ActivityType;
  4. use App\Constants\v3\ErrorCode;
  5. use App\Constants\v3\SsdbKeys;
  6. use App\Exception\ErrorCodeException;
  7. use App\Service\v3\Interfaces\GoodsActivityServiceInterface;
  8. use App\TaskWorker\SSDBTask;
  9. use App\Constants\v3\Store;
  10. use App\Constants\v3\Goods;
  11. use App\Model\v3\GoodsActivity;
  12. use App\Model\v3\GoodsActivityBanner;
  13. use Hyperf\Redis\Redis;
  14. use Hyperf\Utils\ApplicationContext;
  15. class GoodsActivityService implements GoodsActivityServiceInterface
  16. {
  17. public function do($goodsId)
  18. {
  19. }
  20. public function check(GoodsActivity $goods, $num, $userId)
  21. {
  22. $redis = ApplicationContext::getContainer()->get(Redis::class);
  23. $inventoryKey = 'goods_inventory_sold_2_'.$goods->id; // 拼接activity_type和goods_id
  24. if (empty($goods)) {
  25. return ErrorCode::GOODS_ACTIVITY_NOT_EXISTS;
  26. }
  27. // 活动是否已经结束
  28. if ($goods->expire_time < time()) {
  29. return ErrorCode::GOODS_ACTIVITY_EXPIRED;
  30. }
  31. // 商户歇业
  32. if($goods->store->is_rest == 1){
  33. return ErrorCode::STORE_REST;
  34. }
  35. // 商品下架或已删除
  36. if($goods->on_sale == 0 || !is_null($goods->deleted_at)){
  37. return ErrorCode::GOODS_ACTIVITY_ON_SALE_NO;
  38. }
  39. // 商品库存不足
  40. // 获取冻结的库存
  41. $inventoryFrozen = (int)$redis->get($inventoryKey);
  42. if($goods->is_infinite != 1 && $goods->inventory < ($num+$inventoryFrozen)){
  43. return ErrorCode::GOODS_ACTIVITY_INVENTORY_ERROR;
  44. }
  45. // 压redis库存
  46. // redis记录当前商品的购买数量,压库存,下单失败、下单成功扣库存成功、订单取消的时候释放
  47. if (!$redis->exists($inventoryKey)) {
  48. $redis->set($inventoryKey, $num);
  49. } else {
  50. $redis->incrBy($inventoryKey, $num);
  51. }
  52. // 是否超过限购数量
  53. if ($goods->restrict_num != 0 && $goods->restrict_num < $num) {
  54. return ErrorCode::GOODS_ACTIVITY_RESTRICT_LIMIT;
  55. }
  56. // 是否已经购买过(某个时间段内,时间段有商品的限制)
  57. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  58. $hasBuy = $ssdb->exec('get', SsdbKeys::ACTIVITY_GOODS_BUY_RECORD.$userId.'_'.$goods->type.'_'.$goods->id);
  59. if ($hasBuy && $hasBuy >= $goods->time_limit_num) {
  60. return ErrorCode::GOODS_ACTIVITY_BUY;
  61. }
  62. //服务站最晚营业时间
  63. if (env('APP_ENV') === 'prod') {
  64. $closedTime = strtotime(config('market_rest_time'));
  65. if(time() > $closedTime){
  66. throw new ErrorCodeException(ErrorCode::MARKET_REST);
  67. }
  68. }
  69. return true;
  70. }
  71. public function undo()
  72. {
  73. // TODO: Implement undo() method.
  74. }
  75. public function getBanner($goodsId)
  76. {
  77. $banner = GoodsActivityBanner::query()->where('goods_id',$goodsId)->orderByDesc('type')->get();
  78. return $banner;
  79. }
  80. public function detail($goodsId)
  81. {
  82. $res = GoodsActivity::query()->with('store')->where('id',$goodsId)->first();
  83. return $res;
  84. }
  85. public function cacheRecord($goodsId, $num, $userId)
  86. {
  87. $goods = GoodsActivity::query()
  88. ->where('id', $goodsId)
  89. ->first();
  90. $ssdbKey = SsdbKeys::ACTIVITY_GOODS_BUY_RECORD.$userId.'_'.$goods->type.'_'.$goodsId;
  91. $expireTime = 0;
  92. if ($goods->time_limit_days >= 1) {
  93. $expireTime += strtotime(date('Y-m-d 23:59:59')) - time();
  94. }
  95. $expireTime += ($goods->time_limit_days-1) * 86400;
  96. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  97. if (!$ssdb->exec('exists', $ssdbKey)) {
  98. $ssdb->exec('set', $ssdbKey, $num);
  99. $ssdb->exec('expire', $ssdbKey, $expireTime);
  100. } else {
  101. $ssdb->exec('incr', $ssdbKey, $num);
  102. }
  103. }
  104. public function clearCacheRecord($goodsId, $num, $userId)
  105. {
  106. $goods = GoodsActivity::query()
  107. ->where('id', $goodsId)
  108. ->first();
  109. if (empty($goods)) {
  110. return true;
  111. }
  112. $ssdbKey = SsdbKeys::ACTIVITY_GOODS_BUY_RECORD.$userId.'_'.$goods->type.'_'.$goodsId;
  113. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  114. if (!$ssdb->exec('exists', $ssdbKey)) {
  115. } else {
  116. $res = $ssdb->exec('incr', $ssdbKey, -1*$num);
  117. }
  118. }
  119. /**
  120. * 统计订单中活动商品的数量,并校验
  121. * @param $orderGoods
  122. * @param int $limitNum
  123. * @return bool
  124. */
  125. public function checkOrderActivityCount($orderGoods, $limitNum=1)
  126. {
  127. $sourceGoods = GoodsActivity::query()
  128. ->whereIn('id', array_values(array_column($orderGoods, 'goods_id')))
  129. ->get()->toArray();
  130. $limitNums = [
  131. ActivityType::FLASH_SALE => 1,
  132. ActivityType::GROUP_BUY => 1,
  133. ActivityType::NEW_PRODUCT => 1,
  134. ];
  135. $buyNum = 0;
  136. foreach ($sourceGoods as $key => &$goods) {
  137. if ($buyNum >= $limitNums[$goods['type']]) {
  138. return false;
  139. }
  140. $buyNum++;
  141. }
  142. return true;
  143. }
  144. }