500) { return '文件行数最多500'; } $orderList = []; if (false !== ($handle = fopen($file, 'r'))) { while (false !== ($data = fgetcsv($handle, (filesize($file) + 1)))) { // 订单号和运单号都不能为空 $orderSn = $data[0] ?? ''; $orderTraceNo = $data[1] ?? ''; if (!$orderSn) { fclose($handle); return '订单号不能为空'; } if (!$orderTraceNo) { fclose($handle); return '运单号不能为空'; } if ((strlen($orderTraceNo) < 5) || (strlen($orderTraceNo) > 20)) { fclose($handle); return '运单号长度错误'; } $orderList[$orderSn] = $orderTraceNo; } } else { return '文件打开错误'; } fclose($handle); if (!$orderList) { return '订单内容为空'; } $dayLimit = 100; $transaction = \Yii::$app->db->beginTransaction(); try { foreach ($orderList as $orderSn => $trackNo) { // 查询订单 $order = Order::findOne(['SN' => $orderSn]); if (!$order->SN) { return "订单【{$orderSn}】无效"; } if ($order->STATUS !== \Yii::$app->params['orderStatus']['paid']['value']) { return "订单【{$orderSn}】支付状态错误"; } if ($order->CREATED_AT < strtotime( "-{$dayLimit} days")) { return "订单【{$orderSn}】创建已超过{$dayLimit}天"; } try { // 更新运单号 $order->ORDER_TRACK_NO = $trackNo; $order->save(false); } catch (\Exception $e) { $transaction->rollBack(); return "订单【{$orderSn}】更新运单号错误. " . $e->getMessage(); } } $transaction->commit(); } catch (\Exception $e) { $transaction->rollBack(); return "订单更新运单号错误. Exception " . $e->getMessage(); } return true; } }