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.

130 lines
3.8 KiB

  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 Hyperf\DbConnection\Db;
  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. $res = GoodsActivity::query()->with('store')->where('id',$goodsId)->first();
  19. return $res;
  20. }
  21. public function check($goodsId, $num, $userId)
  22. {
  23. $goods = GoodsActivity::query()->with('store')->where('id', $goodsId)->first();
  24. // 活动是否已经结束
  25. if ($goods->expire_time < time()) {
  26. return ErrorCode::GOODS_ACTIVITY_EXPIRED;
  27. }
  28. // 商户歇业
  29. if($goods->store->is_rest != Store::IS_OPEN_YES){
  30. return ErrorCode::GOODS_ACTIVITY_ON_SALE_NO;
  31. }
  32. // 商品下架或已删除
  33. if($goods->on_sale == Goods::ON_SALE_NO || !is_null($goods->deleted_at)){
  34. return ErrorCode::GOODS_ACTIVITY_ON_SALE_NO;
  35. }
  36. // 商品库存不足
  37. if($goods->is_infinite != Goods::IS_INVENTORY && $goods->inventory < $num){
  38. return ErrorCode::GOODS_ACTIVITY_INVENTORY_ERROR;
  39. }
  40. // 是否超过限购数量
  41. if ($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.'_'.$goodsId);
  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()
  57. {
  58. $banner = GoodsActivityBanner::query()->where('goods_id',1572)->get();
  59. return $banner;
  60. }
  61. public function cacheRecord($goodsId, $num, $userId)
  62. {
  63. $goods = GoodsActivity::query()->select('time_limit_days', 'time_limit_num')
  64. ->where('id', $goodsId)
  65. ->first();
  66. $ssdbKey = SsdbKeys::ACTIVITY_GOODS_BUY_RECORD.$userId.'_'.$goodsId;
  67. $expireTime = 0;
  68. if ($goods->time_limit_days <= 1) {
  69. $expireTime += strtotime(date('Y-m-d 23:59:59')) - time();
  70. }
  71. $expireTime += ($goods->time_limit_days-1) *86400;
  72. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  73. if (!$ssdb->exec('exists', $ssdbKey)) {
  74. $ssdb->exec('set', $ssdbKey, $num);
  75. $ssdb->exec('expire', $ssdbKey, $expireTime);
  76. } else {
  77. $ssdb->exec('incr', $ssdbKey, $num);
  78. }
  79. }
  80. /**
  81. * 统计订单中活动商品的数量,并校验
  82. * @param $orderGoods
  83. * @param int $limitNum
  84. * @return bool
  85. */
  86. public function checkOrderActivityCount($orderGoods, $limitNum=1)
  87. {
  88. $sourceGoods = GoodsActivity::query()
  89. ->whereIn('id', array_values(array_column($orderGoods, 'goods_id')))
  90. ->get()->toArray();
  91. $limitNums = [
  92. ActivityType::FLASH_SALE => 1,
  93. ActivityType::GROUP_BUY => 1,
  94. ActivityType::NEW_PRODUCT => 1,
  95. ];
  96. $buyNum = 0;
  97. foreach ($sourceGoods as $key => &$goods) {
  98. if ($buyNum >= $limitNums[$goods['type']]) {
  99. return false;
  100. }
  101. $buyNum++;
  102. }
  103. return true;
  104. }
  105. }