| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- namespace App\Console\Commands;
- use App\Model\TodoList;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- class ToDoReminder extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'command:todo_reminder';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Command description';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $this->handleReminders();
- }
- public function handleReminders()
- {
- $now = time();
- TodoList::where('status', '<', TodoList::status_two)
- ->where('del_time', 0)
- ->where('remind_start', '<=', $now)
- ->select(TodoList::$field)
- ->orderBy('id')
- ->chunkById(10, function ($data) use ($now) {
- DB::transaction(function () use ($data, $now) {
- foreach ($data as $todo) {
- // 单次提醒
- if ($todo->remind_interval == 0) {
- if (empty($todo->last_remind_time)) {
- $this->sendReminder($todo);
- $todo->last_remind_time = $now;
- $todo->save();
- }
- continue;
- }
- // 第一次提醒 或 距离上次提醒超过间隔时间
- if (empty($todo->last_remind_time) || ($now - $todo->last_remind_time) >= $todo->remind_interval) {
- $this->sendReminder($todo);
- $todo->last_remind_time = $now;
- $todo->save();
- }
- }
- });
- });
- }
- }
|