优化批量导入代码

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

Loading…
Cancel
Save