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.

134 lines
3.8 KiB

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