AutoClearIntegralListen.php 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace ln\listens;
  3. use app\common\model\user\User;
  4. use app\common\repositories\store\IntegralRepository;
  5. use app\common\repositories\user\UserBillRepository;
  6. use app\common\repositories\user\UserRepository;
  7. use ln\interfaces\ListenerInterface;
  8. use ln\jobs\ClearUserIntegralJob;
  9. use ln\jobs\SendSmsJob;
  10. use ln\services\TimerService;
  11. use Swoole\Timer;
  12. use think\facade\Cache;
  13. use think\facade\Db;
  14. use think\facade\Queue;
  15. class AutoClearIntegralListen extends TimerService implements ListenerInterface
  16. {
  17. public function handle($event): void
  18. {
  19. //TODO 自动解冻积分
  20. $this->tick(1000 * 60 * 20, function () {
  21. request()->clearCache();
  22. if (!systemConfig('integral_status')) return;
  23. $make = app()->make(IntegralRepository::class);
  24. $end = $make->getTimeoutDay();
  25. if ($end == strtotime(date('Y-m-d') . ' 00:00:00')) {
  26. $start = $make->getInvalidDay();
  27. $make->clearTimeoutDay();
  28. if ($start) {
  29. $startTime = date('Y-m-d H:i:s', $start);
  30. $endTime = date('Y-m-d H:i:s', $end);
  31. User::getDB()->where('integral', '>', 0)->field('uid')->chunk(1000, function ($users) use ($startTime, $endTime) {
  32. foreach ($users as $user) {
  33. $uid = $user['uid'];
  34. Queue::later(1800, ClearUserIntegralJob::class, compact('uid', 'startTime', 'endTime'));
  35. }
  36. usleep(100);
  37. });
  38. }
  39. } else if ($end < strtotime('+15 day')) {
  40. $make1 = app()->make(UserBillRepository::class);
  41. $invalidDay = $make->getInvalidDay();
  42. $cache = Cache::store('file');
  43. $checkKey = 'integral_check';
  44. if ($cache->has($checkKey)) {
  45. return;
  46. }
  47. $endTime = $end;
  48. $startTime = date('Y-m-d H:i:s', $invalidDay);
  49. User::getDB()->where('integral', '>', 0)->where('phone', '<>', '')->field('uid,phone,integral')->chunk(1000, function ($users) use ($endTime, $startTime, $invalidDay, $end, $make1, $cache) {
  50. foreach ($users as $user) {
  51. $cacheKey = 'integral_sms' . $user['uid'];
  52. if ($cache->has($cacheKey)) {
  53. continue;
  54. }
  55. $integral = $user['integral'];
  56. if ($integral > 0 && $invalidDay) {
  57. $validIntegral = $make1->validIntegral($user['uid'], $startTime, $endTime);
  58. if ($integral > $validIntegral) {
  59. $nextClearIntegral = (int)bcsub($integral, $validIntegral, 0);
  60. $cache->set($cacheKey, 1, 3600 * 24 * 20);
  61. Queue::push(SendSmsJob::class, [
  62. 'tempId' => 'INTEGRAL_INVALID',
  63. 'id' => [
  64. 'integral' => $nextClearIntegral,
  65. 'phone' => $user['phone'],
  66. 'date' => $endTime
  67. ]
  68. ]);
  69. continue;
  70. }
  71. }
  72. $cache->set($cacheKey, 1, 3600 * 24 * 2);
  73. }
  74. usleep(200);
  75. });
  76. $cache->set($checkKey, 1, 3600 * 24 * 1);
  77. }
  78. });
  79. }
  80. }