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.

155 lines
4.4 KiB

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\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. return true;
  51. }
  52. public function undo()
  53. {
  54. // TODO: Implement undo() method.
  55. }
  56. public function getBanner($goodsId)
  57. {
  58. $banner = GoodsActivityBanner::query()->where('goods_id',$goodsId)->orderByDesc('type')->get();
  59. return $banner;
  60. }
  61. public function detail($goodsId)
  62. {
  63. $res = GoodsActivity::query()->with('store')->where('id',$goodsId)->first();
  64. return $res;
  65. }
  66. public function cacheRecord($goodsId, $num, $userId)
  67. {
  68. $goods = GoodsActivity::query()
  69. ->where('id', $goodsId)
  70. ->first();
  71. $ssdbKey = SsdbKeys::ACTIVITY_GOODS_BUY_RECORD.$userId.'_'.$goods->type.'_'.$goodsId;
  72. $expireTime = 0;
  73. if ($goods->time_limit_days >= 1) {
  74. $expireTime += strtotime(date('Y-m-d 23:59:59')) - time();
  75. }
  76. $expireTime += ($goods->time_limit_days-1) * 86400;
  77. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  78. if (!$ssdb->exec('exists', $ssdbKey)) {
  79. $ssdb->exec('set', $ssdbKey, $num);
  80. $ssdb->exec('expire', $ssdbKey, $expireTime);
  81. } else {
  82. $ssdb->exec('incr', $ssdbKey, $num);
  83. }
  84. }
  85. public function clearCacheRecord($goodsId, $num, $userId)
  86. {
  87. $goods = GoodsActivity::query()
  88. ->where('id', $goodsId)
  89. ->first();
  90. if (empty($goods)) {
  91. return true;
  92. }
  93. $ssdbKey = SsdbKeys::ACTIVITY_GOODS_BUY_RECORD.$userId.'_'.$goods->type.'_'.$goodsId;
  94. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  95. if (!$ssdb->exec('exists', $ssdbKey)) {
  96. } else {
  97. $res = $ssdb->exec('incr', $ssdbKey, -1*$num);
  98. }
  99. }
  100. /**
  101. * 统计订单中活动商品的数量,并校验
  102. * @param $orderGoods
  103. * @param int $limitNum
  104. * @return bool
  105. */
  106. public function checkOrderActivityCount($orderGoods, $limitNum=1)
  107. {
  108. $sourceGoods = GoodsActivity::query()
  109. ->whereIn('id', array_values(array_column($orderGoods, 'goods_id')))
  110. ->get()->toArray();
  111. $limitNums = [
  112. ActivityType::FLASH_SALE => 1,
  113. ActivityType::GROUP_BUY => 1,
  114. ActivityType::NEW_PRODUCT => 1,
  115. ];
  116. $buyNum = 0;
  117. foreach ($sourceGoods as $key => &$goods) {
  118. if ($buyNum >= $limitNums[$goods['type']]) {
  119. return false;
  120. }
  121. $buyNum++;
  122. }
  123. return true;
  124. }
  125. }