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.

163 lines
4.6 KiB

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