$where, 'select' => $select]; // 查询已过期的订单列表 $list = PlatformOrder::lists($params); $tables = $poids = []; $total_pay_money = 0;// 取消的总订单金额 $total_order_num = count($list);// 取消的总订单总数 // 获取不同表需要更新的订单id foreach ($list as $item) { $tables[$item['source_table']][] = $item['order_id']; $poids[] = $item['id']; $total_pay_money += $item['pay_money']; } if (!empty($tables)) { $trans = \Yii::$app->db->beginTransaction(); try { // 修改平台订单的订单状态 \Yii::$app->db->createCommand()->update(PlatformOrder::tableName(), ['order_status' => (OrderStatusEnum::REPEAL)], 'id in('. implode(',', $poids) .')')->execute(); // 循环订单数据 foreach ($tables as $table => $oids) { // 批量修改过期订单 \Yii::$app->db->createCommand()->update($table, [ 'order_status' => OrderStatusEnum::REPEAL, 'close_time' => $time, ], 'id in('. implode(',', $oids) .')')->execute(); } // 修改订单统计 $res = PlatformOrderStatisticsDay::setData([ 'cancel_order_num' => $total_order_num, 'cancel_order_money' => $total_pay_money, 'cancel_goods_num' => $total_order_num, ]); if ($res == false) throw new \Exception(PlatformOrderStatisticsDay::getStaticError()); $trans->commit(); } catch (\Exception $e) { $trans->rollBack(); throw new \Exception($e->getMessage()); } } echo '操作成功'; return ExitCode::OK; } catch (\Exception $e) { echo $e->getMessage(); $this->stderr('平台订单Error: ' . ExitCode::getReason(ExitCode::UNSPECIFIED_ERROR)); return ExitCode::UNSPECIFIED_ERROR; } } }