AutoRun.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | framework
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2018 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://framework.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/ThinkAdmin
  12. // +----------------------------------------------------------------------
  13. namespace app\store\command;
  14. use think\console\Command;
  15. use think\Db;
  16. /**
  17. * 商城数据处理指令
  18. * Class AutoRun
  19. * @package app\store\command
  20. */
  21. class AutoRun extends Command
  22. {
  23. protected function configure()
  24. {
  25. $this->setName('xclean:store')->setDescription('clean up invalid store records');
  26. }
  27. /**
  28. * 执行指令
  29. * @param \think\console\Input $input
  30. * @param \think\console\Output $output
  31. * @throws \think\Exception
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. * @throws \think\exception\DbException
  35. * @throws \think\exception\PDOException
  36. */
  37. protected function execute(\think\console\Input $input, \think\console\Output $output)
  38. {
  39. # 自动取消30分钟未支付的订单
  40. $where = [['create_at', '<', date('Y-m-d H:i:s', strtotime('-30 minutes'))]];
  41. $count = Db::name('StoreOrder')->where(['pay_state' => '0'])->whereIn('status', ['1', '2'])->where($where)->update([
  42. 'status' => '0',
  43. 'cancel_state' => '1',
  44. 'cancel_at' => date('Y-m-d H:i:s'),
  45. 'cancel_desc' => '30分钟未完成支付自动取消订单',
  46. ]);
  47. if ($count > 0) {
  48. $this->output->info("共计自动取消了30分钟未支付的{$count}笔订单!");
  49. } else {
  50. $this->output->comment('没有需要自动取消30分钟未支付的订单记录!');
  51. }
  52. # 清理一天前未支付的订单
  53. $where = [['create_at', '<', date('Y-m-d H:i:s', strtotime('-1 day'))]];
  54. $list = Db::name('StoreOrder')->where(['pay_state' => '0'])->where($where)->limit(20)->select();
  55. if (count($order_nos = array_unique(array_column($list, 'order_no'))) > 0) {
  56. $this->output->info("自动删除前一天已经取消的订单:\n\t" . join(',' . PHP_EOL . "\t", $order_nos));
  57. Db::name('StoreOrder')->whereIn('order_no', $order_nos)->delete();
  58. Db::name('StoreOrderList')->whereIn('order_no', $order_nos)->delete();
  59. } else {
  60. $this->output->comment('没有需要自动删除前一天已经取消的订单!');
  61. }
  62. }
  63. }