AutoCancelGroupOrderListen.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\repositories\store\order\StoreGroupOrderRepository;
  13. use crmeb\interfaces\ListenerInterface;
  14. use crmeb\services\TimerService;
  15. use Swoole\Timer;
  16. use think\facade\Log;
  17. class AutoCancelGroupOrderListen extends TimerService implements ListenerInterface
  18. {
  19. public function handle($event): void
  20. {
  21. $this->tick(60000, function () {
  22. $storeGroupOrderRepository = app()->make(StoreGroupOrderRepository::class);
  23. request()->clearCache();
  24. $timer = ((int)systemConfig('auto_close_order_timer')) ?: 15;
  25. $time = date('Y-m-d H:i:s', strtotime("- $timer minutes"));
  26. $groupOrderIds = $storeGroupOrderRepository->getTimeOutIds($time);
  27. foreach ($groupOrderIds as $id) {
  28. try {
  29. $storeGroupOrderRepository->cancel($id);
  30. } catch (\Exception $e) {
  31. Log::info('自动关闭订单失败' . var_export($id, 1));
  32. }
  33. }
  34. });
  35. }
  36. }