12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- namespace app\store\command;
- use think\console\Command;
- use think\Db;
- class AutoRun extends Command
- {
- protected function configure()
- {
- $this->setName('xclean:store')->setDescription('clean up invalid store records');
- }
-
- protected function execute(\think\console\Input $input, \think\console\Output $output)
- {
-
- $where = [['create_at', '<', date('Y-m-d H:i:s', strtotime('-30 minutes'))]];
- $count = Db::name('StoreOrder')->where(['pay_state' => '0'])->whereIn('status', ['1', '2'])->where($where)->update([
- 'status' => '0',
- 'cancel_state' => '1',
- 'cancel_at' => date('Y-m-d H:i:s'),
- 'cancel_desc' => '30分钟未完成支付自动取消订单',
- ]);
- if ($count > 0) {
- $this->output->info("共计自动取消了30分钟未支付的{$count}笔订单!");
- } else {
- $this->output->comment('没有需要自动取消30分钟未支付的订单记录!');
- }
-
- $where = [['create_at', '<', date('Y-m-d H:i:s', strtotime('-1 day'))]];
- $list = Db::name('StoreOrder')->where(['pay_state' => '0'])->where($where)->limit(20)->select();
- if (count($order_nos = array_unique(array_column($list, 'order_no'))) > 0) {
- $this->output->info("自动删除前一天已经取消的订单:\n\t" . join(',' . PHP_EOL . "\t", $order_nos));
- Db::name('StoreOrder')->whereIn('order_no', $order_nos)->delete();
- Db::name('StoreOrderList')->whereIn('order_no', $order_nos)->delete();
- } else {
- $this->output->comment('没有需要自动删除前一天已经取消的订单!');
- }
- }
- }
|