OrderClear.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace app\data\command;
  3. use app\data\service\OrderService;
  4. use think\admin\Command;
  5. use think\admin\Exception;
  6. use think\console\Input;
  7. use think\console\Output;
  8. /**
  9. * 商城订单自动清理
  10. * Class OrderClear
  11. * @package app\data\command
  12. */
  13. class OrderClear extends Command
  14. {
  15. protected function configure()
  16. {
  17. $this->setName('xdata:OrderClear');
  18. $this->setDescription('批量清理商城订单数据');
  19. }
  20. /**
  21. * 业务指令执行
  22. * @param Input $input
  23. * @param Output $output
  24. * @return void
  25. * @throws Exception
  26. */
  27. protected function execute(Input $input, Output $output)
  28. {
  29. $this->_autoCancelOrder();
  30. $this->_autoRemoveOrder();
  31. }
  32. /**
  33. * 自动取消30分钟未支付的订单
  34. * @throws Exception
  35. */
  36. private function _autoCancelOrder()
  37. {
  38. try {
  39. $map = [];
  40. $map[] = ['status', '=', '1'];
  41. $map[] = ['payment_status', '=', '0'];
  42. $map[] = ['create_at', '<', date('Y-m-d H:i:s', strtotime('-30 minutes'))];
  43. [$total, $count] = [$this->app->db->name('ShopOrder')->where($map)->count(), 0];
  44. $this->app->db->name('ShopOrder')->where($map)->select()->map(function ($item) use ($total, &$count) {
  45. $this->queue->message($total, ++$count, "开始取消未支付的订单 {$item['order_no']}");
  46. $this->app->db->name('ShopOrder')->where(['order_no' => $item['order_no']])->update([
  47. 'status' => '0',
  48. 'cancel_status' => '1',
  49. 'cancel_datetime' => date('Y-m-d H:i:s'),
  50. 'cancel_remark' => '30分钟未完成支付已自动取消',
  51. ]);
  52. OrderService::instance()->syncStock($item['order_no']);
  53. $this->queue->message($total, $count, "完成取消未支付的订单 {$item['order_no']}", 1);
  54. });
  55. } catch (\Exception $exception) {
  56. $this->queue->error($exception->getMessage());
  57. }
  58. }
  59. /**
  60. * 自动清理已经取消的订单
  61. * @throws Exception
  62. */
  63. private function _autoRemoveOrder()
  64. {
  65. try {
  66. $map = [];
  67. $map[] = ['status', '=', 0];
  68. $map[] = ['payment_status', '=', 0];
  69. $map[] = ['create_at', '<', date('Y-m-d H:i:s', strtotime('-3 days'))];
  70. [$total, $count] = [$this->app->db->name('ShopOrder')->where($map)->count(), 0];
  71. foreach ($this->app->db->name('ShopOrder')->where($map)->cursor() as $item) {
  72. $this->queue->message($total, ++$count, "开始清理已取消的订单 {$item['order_no']}");
  73. $this->app->db->name('ShopOrder')->where(['order_no' => $item['order_no']])->delete();
  74. $this->app->db->name('ShopOrderItem')->where(['order_no' => $item['order_no']])->delete();
  75. $this->queue->message($total, $count, "完成清理已取消的订单 {$item['order_no']}", 1);
  76. }
  77. } catch (\Exception $exception) {
  78. $this->queue->error($exception->getMessage());
  79. }
  80. }
  81. }