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.

57 lines
1.2 KiB

  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Service\v3\Interfaces\CouponServiceInterface;
  4. use Hyperf\Redis\Redis;
  5. use Hyperf\Utils\ApplicationContext;
  6. class CouponService implements CouponServiceInterface
  7. {
  8. /**
  9. * @inheritDoc
  10. */
  11. public function do()
  12. {
  13. // TODO: Implement do() method.
  14. }
  15. /**
  16. * @inheritDoc
  17. */
  18. public function check()
  19. {
  20. // TODO: Implement check() method.
  21. }
  22. /**
  23. * @inheritDoc
  24. */
  25. public function undo()
  26. {
  27. // TODO: Implement undo() method.
  28. }
  29. /**
  30. * @inheritDoc
  31. */
  32. public function countAvailableByUser($userId)
  33. {
  34. return mt_rand(0,10);
  35. }
  36. /**
  37. * 缓存优惠券今日使用情况
  38. * @param $userId
  39. * @param $couponId
  40. * @param $couponRecId
  41. * @return bool
  42. */
  43. function cacheTodayCouponUsed($userId, $couponId, $couponRecId)
  44. {
  45. $redis = ApplicationContext::getContainer()->get(Redis::class);
  46. $setRes = $redis->sAdd('coupon_' . date('Ymd') . '_used_' . $userId, $couponId);
  47. $expireRes = $redis->expire('coupon_' . date('Ymd') . '_used_' . $userId, strtotime(date('Y-m-d') . ' 23:59:59') - time());
  48. return $setRes && $expireRes;
  49. }
  50. }