OrderExpiredCommand.php 800 B

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. namespace app\common\command;
  3. use app\common\model\UserOrder;
  4. use think\console\Command;
  5. use think\console\Input;
  6. use think\console\Output;
  7. use think\Db;
  8. use think\Log;
  9. class OrderExpiredCommand extends Command{
  10. protected function configure()
  11. {
  12. $this->setName('order:expired')->setDescription('订单超时关闭');
  13. }
  14. protected function execute(Input $input, Output $output)
  15. {
  16. $orders= UserOrder::expired()->select();
  17. foreach ($orders as $order){
  18. try {
  19. Db::startTrans();
  20. $order->cancel();
  21. Db::commit();
  22. }catch (\Exception $e){
  23. Log::error("关闭订单[$order->id]失败:{$e->getMessage()}");
  24. }
  25. }
  26. $output->info('success');
  27. }
  28. }