<?php




namespace ln\listens;


use app\common\repositories\store\order\StoreOrderRepository;
use app\common\repositories\store\order\StoreOrderStatusRepository;
use ln\interfaces\ListenerInterface;
use ln\jobs\OrderReplyJob;
use ln\services\TimerService;
use Swoole\Timer;
use think\facade\Log;
use think\facade\Queue;

class AuthTakeOrderListen extends TimerService implements ListenerInterface
{

    public function handle($event): void
    {
        $this->tick(1000 * 60 * 60, function () {
            $storeOrderStatusRepository = app()->make(StoreOrderStatusRepository::class);
            $storeOrderRepository = app()->make(StoreOrderRepository::class);
            request()->clearCache();
            $timer = ((int)systemConfig('auto_take_order_timer')) ?: 15;
            $time = date('Y-m-d H:i:s', strtotime("- $timer day"));
            $ids = $storeOrderStatusRepository->getTimeoutDeliveryOrder($time);
            foreach ($ids as $id) {
                try {
                    $storeOrderRepository->takeOrder($id);
                    Queue::push(OrderReplyJob::class, $id);
                } catch (\Exception $e) {
                    Log::error('自动收货失败:' . $e->getMessage());
                }
            }
        });
    }
}
