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.

170 lines
5.0 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
  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. $inventoryFrozen = 0;
  43. if($goods->is_infinite != 1 && $goods->inventory < ($num+$inventoryFrozen)){
  44. return ErrorCode::GOODS_ACTIVITY_INVENTORY_ERROR;
  45. }
  46. // 压redis库存
  47. // redis记录当前商品的购买数量,压库存,下单失败、下单成功扣库存成功、订单取消的时候释放
  48. if (!$redis->exists($inventoryKey)) {
  49. $redis->set($inventoryKey, $num);
  50. } else {
  51. $redis->incrBy($inventoryKey, intval($num));
  52. }
  53. // 是否超过限购数量
  54. if ($goods->restrict_num != 0 && $goods->restrict_num < $num) {
  55. return ErrorCode::GOODS_ACTIVITY_RESTRICT_LIMIT;
  56. }
  57. // 是否已经购买过(某个时间段内,时间段有商品的限制)
  58. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  59. $hasBuy = $ssdb->exec('get', SsdbKeys::ACTIVITY_GOODS_BUY_RECORD.$userId.'_'.$goods->type.'_'.$goods->id);
  60. if ($hasBuy && $hasBuy >= $goods->time_limit_num) {
  61. return ErrorCode::GOODS_ACTIVITY_BUY;
  62. }
  63. return true;
  64. }
  65. public function undo()
  66. {
  67. // TODO: Implement undo() method.
  68. }
  69. public function getBanner($goodsId)
  70. {
  71. $banner = GoodsActivityBanner::query()->where('goods_id',$goodsId)->orderByDesc('type')->get();
  72. return $banner;
  73. }
  74. public function detail($goodsId)
  75. {
  76. $res = GoodsActivity::query()->with('store')->where('id',$goodsId)->first();
  77. return $res;
  78. }
  79. public function cacheRecord($goodsId, $num, $userId)
  80. {
  81. $goods = GoodsActivity::query()
  82. ->where('id', $goodsId)
  83. ->first();
  84. $ssdbKey = SsdbKeys::ACTIVITY_GOODS_BUY_RECORD.$userId.'_'.$goods->type.'_'.$goodsId;
  85. $expireTime = 0;
  86. if ($goods->time_limit_days >= 1) {
  87. $expireTime += strtotime(date('Y-m-d 23:59:59')) - time();
  88. }
  89. $expireTime += ($goods->time_limit_days-1) * 86400;
  90. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  91. if (!$ssdb->exec('exists', $ssdbKey)) {
  92. $ssdb->exec('set', $ssdbKey, $num);
  93. $ssdb->exec('expire', $ssdbKey, $expireTime);
  94. } else {
  95. $ssdb->exec('incr', $ssdbKey, $num);
  96. }
  97. }
  98. public function clearCacheRecord($goodsId, $num, $userId)
  99. {
  100. $goods = GoodsActivity::query()
  101. ->where('id', $goodsId)
  102. ->first();
  103. if (empty($goods)) {
  104. return true;
  105. }
  106. $ssdbKey = SsdbKeys::ACTIVITY_GOODS_BUY_RECORD.$userId.'_'.$goods->type.'_'.$goodsId;
  107. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  108. if (!$ssdb->exec('exists', $ssdbKey)) {
  109. } else {
  110. $res = $ssdb->exec('incr', $ssdbKey, -1*$num);
  111. }
  112. }
  113. /**
  114. * 统计订单中活动商品的数量,并校验
  115. * @param $orderGoods
  116. * @param int $limitNum
  117. * @return bool
  118. */
  119. public function checkOrderActivityCount($orderGoods, $limitNum=1)
  120. {
  121. $sourceGoods = GoodsActivity::query()
  122. ->whereIn('id', array_values(array_column($orderGoods, 'goods_id')))
  123. ->get()->toArray();
  124. $limitNums = [
  125. ActivityType::FLASH_SALE => 1,
  126. ActivityType::GROUP_BUY => 1,
  127. ActivityType::NEW_PRODUCT => 1,
  128. ];
  129. $buyNum = 0;
  130. foreach ($sourceGoods as $key => &$goods) {
  131. if ($buyNum >= $limitNums[$goods['type']]) {
  132. return false;
  133. }
  134. $buyNum++;
  135. }
  136. return true;
  137. }
  138. }