ToDoReminder.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Model\TodoList;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\DB;
  6. class ToDoReminder extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'command:todo_reminder';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Command description';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle()
  35. {
  36. $this->handleReminders();
  37. }
  38. public function handleReminders()
  39. {
  40. $now = time();
  41. TodoList::where('status', '<', TodoList::status_two)
  42. ->where('del_time', 0)
  43. ->where('remind_start', '<=', $now)
  44. ->select(TodoList::$field)
  45. ->orderBy('id')
  46. ->chunkById(10, function ($data) use ($now) {
  47. DB::transaction(function () use ($data, $now) {
  48. foreach ($data as $todo) {
  49. // 单次提醒
  50. if ($todo->remind_interval == 0) {
  51. if (empty($todo->last_remind_time)) {
  52. $this->sendReminder($todo);
  53. $todo->last_remind_time = $now;
  54. $todo->save();
  55. }
  56. continue;
  57. }
  58. // 第一次提醒 或 距离上次提醒超过间隔时间
  59. if (empty($todo->last_remind_time) || ($now - $todo->last_remind_time) >= $todo->remind_interval) {
  60. $this->sendReminder($todo);
  61. $todo->last_remind_time = $now;
  62. $todo->save();
  63. }
  64. }
  65. });
  66. });
  67. }
  68. }