UserCoupon.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\task\behavior;
  10. use think\Cache;
  11. use app\task\model\UserCoupon as UserCouponModel;
  12. /**
  13. * 优惠券行为管理
  14. * Class UserCoupon
  15. * @package app\task\behavior
  16. */
  17. class UserCoupon
  18. {
  19. /* @var \app\task\model\UserCoupon $model */
  20. private $model;
  21. /**
  22. * 执行函数
  23. * @param $model
  24. * @return bool
  25. */
  26. public function run($model)
  27. {
  28. if (!$model instanceof UserCouponModel) {
  29. return new UserCouponModel and false;
  30. }
  31. $this->model = $model;
  32. if (!Cache::has('__task_space__UserCoupon')) {
  33. // 设置优惠券过期状态
  34. $this->setExpired();
  35. Cache::set('__task_space__UserCoupon', time(), 3600);
  36. }
  37. return true;
  38. }
  39. /**
  40. * 设置优惠券过期状态
  41. * @return false|int
  42. */
  43. private function setExpired()
  44. {
  45. // 获取已过期的优惠券ID集
  46. $couponIds = $this->model->getExpiredCouponIds();
  47. // 记录日志
  48. $this->dologs('setExpired', [
  49. 'couponIds' => json_encode($couponIds),
  50. ]);
  51. // 更新已过期状态
  52. return $this->model->setIsExpire($couponIds);
  53. }
  54. /**
  55. * 记录日志
  56. * @param $method
  57. * @param array $params
  58. * @return bool|int
  59. */
  60. private function dologs($method, $params = [])
  61. {
  62. $value = 'UserCoupon --' . $method;
  63. foreach ($params as $key => $val)
  64. $value .= ' --' . $key . ' ' . $val;
  65. return log_write($value);
  66. }
  67. }