<?php




namespace ln\listens;

use app\common\repositories\store\order\PresellOrderRepository;
use app\common\repositories\store\product\ProductPresellRepository;
use ln\jobs\CheckProductPresellJob;
use ln\jobs\SendSmsJob;
use ln\services\TimerService;
use Swoole\Timer;
use think\facade\Log;
use ln\interfaces\ListenerInterface;
use think\facade\Queue;

class ProductPresellStatusListen extends TimerService implements ListenerInterface
{
    public function handle($event): void
    {
        $this->tick(1000 * 300, function () {
            try {
                Queue::push(CheckProductPresellJob::class, []);
            } catch (\Exception $e) {
                Log::info('自动更新失败');
            }
            try {
                $ids = app()->make(PresellOrderRepository::class)->sendSmsIds(date('Y-m-d H'));
                foreach ($ids as $id) {
                    Queue::push(SendSmsJob::class, ['tempId' => 'PAY_PRESELL_CODE', 'id' => (int)$id]);
                }
            } catch (\Exception $e) {
                Log::info('预售短信通知失败');
            }
        });
    }
}
