AutoClearIntegralListen.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\listens;
  12. use app\common\model\user\User;
  13. use app\common\repositories\store\IntegralRepository;
  14. use app\common\repositories\user\UserBillRepository;
  15. use app\common\repositories\user\UserRepository;
  16. use crmeb\interfaces\ListenerInterface;
  17. use crmeb\jobs\ClearUserIntegralJob;
  18. use crmeb\jobs\SendSmsJob;
  19. use crmeb\services\TimerService;
  20. use Swoole\Timer;
  21. use think\facade\Cache;
  22. use think\facade\Db;
  23. use think\facade\Queue;
  24. class AutoClearIntegralListen extends TimerService implements ListenerInterface
  25. {
  26. public function handle($event): void
  27. {
  28. //TODO 自动解冻积分
  29. $this->tick(1000 * 60 * 20, function () {
  30. request()->clearCache();
  31. if (!systemConfig('integral_status')) return;
  32. $make = app()->make(IntegralRepository::class);
  33. $end = $make->getTimeoutDay();
  34. if ($end == strtotime(date('Y-m-d') . ' 00:00:00')) {
  35. $start = $make->getInvalidDay();
  36. $make->clearTimeoutDay();
  37. if ($start) {
  38. $startTime = date('Y-m-d H:i:s', $start);
  39. $endTime = date('Y-m-d H:i:s', $end);
  40. User::getDB()->where('integral', '>', 0)->field('uid')->chunk(1000, function ($users) use ($startTime, $endTime) {
  41. foreach ($users as $user) {
  42. $uid = $user['uid'];
  43. Queue::later(1800, ClearUserIntegralJob::class, compact('uid', 'startTime', 'endTime'));
  44. }
  45. usleep(100);
  46. });
  47. }
  48. } else if ($end < strtotime('+15 day')) {
  49. $make1 = app()->make(UserBillRepository::class);
  50. $invalidDay = $make->getInvalidDay();
  51. $cache = Cache::store('file');
  52. $checkKey = 'integral_check';
  53. if ($cache->has($checkKey)) {
  54. return;
  55. }
  56. $endTime = $end;
  57. $startTime = date('Y-m-d H:i:s', $invalidDay);
  58. User::getDB()->where('integral', '>', 0)->where('phone', '<>', '')->field('uid,phone,integral')->chunk(1000, function ($users) use ($endTime, $startTime, $invalidDay, $end, $make1, $cache) {
  59. foreach ($users as $user) {
  60. $cacheKey = 'integral_sms' . $user['uid'];
  61. if ($cache->has($cacheKey)) {
  62. continue;
  63. }
  64. $integral = $user['integral'];
  65. if ($integral > 0 && $invalidDay) {
  66. $validIntegral = $make1->validIntegral($user['uid'], $startTime, $endTime);
  67. if ($integral > $validIntegral) {
  68. $nextClearIntegral = (int)bcsub($integral, $validIntegral, 0);
  69. $cache->set($cacheKey, 1, 3600 * 24 * 20);
  70. Queue::push(SendSmsJob::class, [
  71. 'tempId' => 'INTEGRAL_INVALID',
  72. 'id' => [
  73. 'integral' => $nextClearIntegral,
  74. 'phone' => $user['phone'],
  75. 'date' => $endTime
  76. ]
  77. ]);
  78. continue;
  79. }
  80. }
  81. $cache->set($cacheKey, 1, 3600 * 24 * 2);
  82. }
  83. usleep(200);
  84. });
  85. $cache->set($checkKey, 1, 3600 * 24 * 1);
  86. }
  87. });
  88. }
  89. }