优化批量导入代码

dev
曾超新 5 years ago
parent 7e19e00fff
commit 1445ffa914
  1. 291
      frontend/controllers/FinanceController.php

@ -6335,170 +6335,177 @@ class FinanceController extends \frontend\controllers\UserBaseController
$PHPReader = new \PHPExcel_Reader_Excel5();
if (!$PHPReader->canRead($filename)) {
$errorMessage = "Can not read file.";
echo $errorMessage;
return;
$result['msg'] = $errorMessage;
return $result;
}
}
$PHPExcel = $PHPReader->load($filename);
$currentSheet = $PHPExcel->getSheet(0);
$tran = CarT::getDb()->beginTransaction();
try {
$PHPExcel = $PHPReader->load($filename);
$currentSheet = $PHPExcel->getSheet(0);
$rowCount = $currentSheet->getHighestRow();
//创建新的xlsx表
$insert_total = 0;
$rowCount = $currentSheet->getHighestRow();
//创建新的xlsx表
$insert_total = 0;
for ($i = 2; $i <= $rowCount; $i++) {
for ($i = 2; $i <= $rowCount; $i++) {
// echo $i.'/'.$rowCount."...";
$cell = $currentSheet->getCellByColumnAndRow(0, $i);
$car_no = $cell->getFormattedValue();
$cell = $currentSheet->getCellByColumnAndRow(0, $i);
$car_no = $cell->getFormattedValue();
// $cell = $currentSheet->getCellByColumnAndRow(1, $i);
// $car_man = $cell->getValue();
$cell = $currentSheet->getCellByColumnAndRow(1, $i);
$factory_model = $cell->getFormattedValue();//$data[$i][3];
$cell = $currentSheet->getCellByColumnAndRow(2, $i);
$engine_no = $cell->getFormattedValue();//$data[$i][2];
$cell = $currentSheet->getCellByColumnAndRow(1, $i);
$factory_model = $cell->getFormattedValue();//$data[$i][3];
$cell = $currentSheet->getCellByColumnAndRow(2, $i);
$engine_no = $cell->getFormattedValue();//$data[$i][2];
// var_dump($cars);
$cell = $currentSheet->getCellByColumnAndRow(3, $i);
$car_frame_no = $cell->getFormattedValue();//$data[$i][1];
$cell = $currentSheet->getCellByColumnAndRow(3, $i);
$car_frame_no = $cell->getFormattedValue();//$data[$i][1];
// var_dump($car_frame_no);
$cell = $currentSheet->getCellByColumnAndRow(4, $i);
$register_date = $cell->getFormattedValue();
$cell = $currentSheet->getCellByColumnAndRow(5, $i);
$company = $cell->getValue();//$data[$i][8];
$cell = $currentSheet->getCellByColumnAndRow(6, $i);
$insurer1_date = $cell->getFormattedValue();
$cell = $currentSheet->getCellByColumnAndRow(7, $i);
$insurer2_date = $cell->getFormattedValue();
$cell = $currentSheet->getCellByColumnAndRow(8, $i);
$seats = $cell->getFormattedValue();
$cell = $currentSheet->getCellByColumnAndRow(9, $i);
$car_man = $cell->getFormattedValue();
$cell = $currentSheet->getCellByColumnAndRow(10, $i);
$car_man_number = $cell->getFormattedValue();
$cell = $currentSheet->getCellByColumnAndRow(11, $i);
$car_man_phone = $cell->getFormattedValue();
$cell = $currentSheet->getCellByColumnAndRow(12, $i);
$id_man = $cell->getFormattedValue();
$cell = $currentSheet->getCellByColumnAndRow(13, $i);
$id_number = $cell->getFormattedValue();
$cell = $currentSheet->getCellByColumnAndRow(14, $i);
$id_phone = $cell->getFormattedValue();
if($car_no == '' && $engine_no == '' && $car_frame_no == '') continue;
$car_info = CarT::findOne(['car_frame_no' => $car_frame_no]);
if ($car_info) {
if($car_no != ''){
$car_info->car_no = $car_no;
}
$cell = $currentSheet->getCellByColumnAndRow(4, $i);
$register_date = $cell->getFormattedValue();
$cell = $currentSheet->getCellByColumnAndRow(5, $i);
$company = $cell->getValue();//$data[$i][8];
$cell = $currentSheet->getCellByColumnAndRow(6, $i);
$insurer1_date = $cell->getFormattedValue();
$cell = $currentSheet->getCellByColumnAndRow(7, $i);
$insurer2_date = $cell->getFormattedValue();
$cell = $currentSheet->getCellByColumnAndRow(8, $i);
$seats = $cell->getFormattedValue();
$cell = $currentSheet->getCellByColumnAndRow(9, $i);
$car_man = $cell->getFormattedValue();
$cell = $currentSheet->getCellByColumnAndRow(10, $i);
$car_man_number = $cell->getFormattedValue();
$cell = $currentSheet->getCellByColumnAndRow(11, $i);
$car_man_phone = $cell->getFormattedValue();
$cell = $currentSheet->getCellByColumnAndRow(12, $i);
$id_man = $cell->getFormattedValue();
$cell = $currentSheet->getCellByColumnAndRow(13, $i);
$id_number = $cell->getFormattedValue();
$cell = $currentSheet->getCellByColumnAndRow(14, $i);
$id_phone = $cell->getFormattedValue();
if($car_no == '' && $engine_no == '' && $car_frame_no == '') continue;
$car_info = CarT::findOne(['car_frame_no' => $car_frame_no]);
if ($car_info) {
if($car_no != ''){
$car_info->car_no = $car_no;
}
if($car_man != ''){
$car_info->car_man = $car_man;
}
if($factory_model != ''){
$car_info->factory_model = $factory_model;
}
if($engine_no != ''){
$car_info->engine_no = $engine_no;
}
if($car_man != ''){
$car_info->car_man = $car_man;
}
if($factory_model != ''){
$car_info->factory_model = $factory_model;
}
if($engine_no != ''){
$car_info->engine_no = $engine_no;
}
// if($car_frame_no != ''){
// $car_info->car_frame_no = $car_frame_no;
// }
if($register_date != ''){
$car_info->register_date = $register_date;
}
if($company != ''){
$car_info->company = $company;
}
if($insurer1_date != ''){
$car_info->insurer1_date = $insurer1_date;
}
if($insurer2_date != ''){
$car_info->insurer2_date = $insurer2_date;
}
if($id_man != ''){
$car_info->id_man = $id_man;
}
if($id_number != ''){
$car_info->id_number = $id_number;
}
if($id_phone != ''){
$car_info->id_phone = $id_phone;
}
if($seats != ''){
$car_info->seats = $seats;
}
if($car_man_number != ''){
$car_info->car_man_number = $car_man_number;
}
if($car_man_phone != ''){
$car_info->phone = $car_man_phone;
}
if(!$car_info->save(false)) {
var_dump($car_info->errors);
exit;
if($register_date != ''){
$car_info->register_date = $register_date;
}
if($company != ''){
$car_info->company = $company;
}
if($insurer1_date != ''){
$car_info->insurer1_date = $insurer1_date;
}
if($insurer2_date != ''){
$car_info->insurer2_date = $insurer2_date;
}
if($id_man != ''){
$car_info->id_man = $id_man;
}
if($id_number != ''){
$car_info->id_number = $id_number;
}
if($id_phone != ''){
$car_info->id_phone = $id_phone;
}
if($seats != ''){
$car_info->seats = $seats;
}
if($car_man_number != ''){
$car_info->car_man_number = $car_man_number;
}
if($car_man_phone != ''){
$car_info->phone = $car_man_phone;
}
if(!$car_info->save(false)) {
throw new \Exception(print_r($car_info->getErrors(), true));
}
$insert_total++;
}
$insert_total++;
}
if (!$car_info) {
if (!$car_info) {
$car_info = new CarT();
$car_info = new CarT();
if($car_no != ''){
$car_info->car_no = $car_no;
}
if($car_no != ''){
$car_info->car_no = $car_no;
if($car_man != ''){
$car_info->car_man = $car_man;
}
if($factory_model != ''){
$car_info->factory_model = $factory_model;
}
if($engine_no != ''){
$car_info->engine_no = $engine_no;
}
if($car_frame_no != ''){
$car_info->car_frame_no = $car_frame_no;
}
if($register_date != ''){
$car_info->register_date = $register_date;
}
if($company != ''){
$car_info->company = $company;
}
if($insurer1_date != ''){
$car_info->insurer1_date = $insurer1_date;
}
if($insurer2_date != ''){
$car_info->insurer2_date = $insurer2_date;
}
if($id_man != ''){
$car_info->id_man = $id_man;
}
if($id_number != ''){
$car_info->id_number = $id_number;
}
if($id_phone != ''){
$car_info->id_phone = $id_phone;
}
if($seats != ''){
$car_info->seats = $seats;
}
if($car_man_number != ''){
$car_info->car_man_number = $car_man_number;
}
if($car_man_phone != ''){
$car_info->phone = $car_man_phone;
}
if(!$car_info->save(false)) {
throw new \Exception(print_r($car_info->getErrors(), true));
}
$insert_total++;
}
if($car_man != ''){
$car_info->car_man = $car_man;
}
if($factory_model != ''){
$car_info->factory_model = $factory_model;
}
if($engine_no != ''){
$car_info->engine_no = $engine_no;
}
if($car_frame_no != ''){
$car_info->car_frame_no = $car_frame_no;
}
if($register_date != ''){
$car_info->register_date = $register_date;
}
if($company != ''){
$car_info->company = $company;
}
if($insurer1_date != ''){
$car_info->insurer1_date = $insurer1_date;
}
if($insurer2_date != ''){
$car_info->insurer2_date = $insurer2_date;
}
if($id_man != ''){
$car_info->id_man = $id_man;
}
if($id_number != ''){
$car_info->id_number = $id_number;
}
if($id_phone != ''){
$car_info->id_phone = $id_phone;
}
if($seats != ''){
$car_info->seats = $seats;
}
if($car_man_number != ''){
$car_info->car_man_number = $car_man_number;
}
if($car_man_phone != ''){
$car_info->phone = $car_man_phone;
}
if(!$car_info->save(false)) {
var_dump($car_info->errors);
exit;
}
$insert_total++;
}
$tran->commit();
} catch (\Exception $e) {
$tran->rollBack();
$result['msg'] = $e->getMessage();
return $result;
}
$result['success'] = true;

Loading…
Cancel
Save