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.

169 lines
4.9 KiB

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