OrderSettleCommand.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace app\common\command;
  3. use app\common\model\MobileOrder;
  4. use app\common\model\SysConfig;
  5. use think\console\Command;
  6. use think\console\Input;
  7. use think\console\Output;
  8. use think\Db;
  9. use think\Log;
  10. class OrderSettleCommand extends Command{
  11. protected function configure()
  12. {
  13. $this->setName('order:settle')->setDescription('订单结算');
  14. }
  15. protected function execute(Input $input, Output $output)
  16. {
  17. $time=SysConfig::look($name='command:order:settle');
  18. if(!$time||time()>$time+3600){
  19. SysConfig::set($name,time());
  20. }else{
  21. return;
  22. }
  23. $orders= MobileOrder::filterWillOver()->limit(100)->select();
  24. foreach ($orders as $order){
  25. try {
  26. Db::startTrans();
  27. $order->makeOver();
  28. user_log('MobileOrderSettle',"结算订单{$order['id']}成功");
  29. Db::commit();
  30. }catch (\Exception $e){
  31. Db::rollback();
  32. if($e->getMessage()=='该笔订单已结算成功'){
  33. user_log('MobileOrderSettle',["订单{$order['id']}结算失败",'当成功处理']);
  34. MobileOrder::find($order['id'])->makeOver(false);
  35. }
  36. user_log('MobileOrderSettle',["订单{$order['id']}结算失败",$e->getMessage(),$e->getFile(),$e->getLine()]);
  37. }
  38. }
  39. SysConfig::del($name);
  40. }
  41. }