<pre class="blockcode"><code><?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/9/27
* Time: 13:57
*/
namespace site\admin\model;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use system\library\AliYunOSS;
use system\library\Unzip;
class ProductBatchModel extends \site\admin\component\Model
{
public function add($data){
if (!$this->_validate($data, 'add')) {
return false;
}
$languageModel = new LanguageModel();
$categoryModel = new CategoryModel();
$productModel = new ProductModel();
$urlAliasModel = new UrlAliasModel();
if(substr(strrchr($data['excel_file'], '.'), 1)<>'xlsx'){
$this->addError('excel_file', '请上传xlsx格式的Excel文件');
return false;
}
$arr_file = explode('com/', $data['excel_file']);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
$reader->setReadDataOnly(TRUE);
$spreadsheet = $reader->load(DIR_UPLOAD.$arr_file[1]); //载入excel表格
$worksheet = $spreadsheet->getActiveSheet();
$highestRow = $worksheet->getHighestRow(); // 总行数
$highestColumn = $worksheet->getHighestColumn(); // 总列数
$highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn); // e.g. 5
$lines = $highestRow - 1;
if ($lines <= 0) {
$this->addError('Excel', 'Excel表格中没有数据');
return false;
}
$enableRow = array();
$arr_url = array();
for ($row = 2; $row <= $highestRow; ++$row) {
/******************************验证产品型号***********************************/
$model_no = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
if(empty($model_no)){
$this->addError('model_no'.$row, '第'.$row.'行产品型号为空');
}else{
$if_exist = $productModel->getProductByModel($model_no);
if(empty($if_exist)){
$enableRow[] = $row; //新增该行
}
}
/******************************验证产品分类***********************************/
$category_name = $worksheet->getCellByColumnAndRow(2, $row)->getValue();
if(empty($category_name)){
$this->addError('category_name'.$row, '第'.$row.'行分类名为空');
}else{
$category = $categoryModel->getCategoryIdByName($category_name);
if(empty($category)){
$this->addError('category_name'.$row, '第'.$row.'行分类名不存在');
}
}
$sort_order = $worksheet->getCellByColumnAndRow(3, $row)->getValue();
if(empty($sort_order) || !is_numeric($sort_order)){
$this->addError('sort_order'.$row, '第'.$row.'行排序错误');
}
$status = $worksheet->getCellByColumnAndRow(4, $row)->getValue();
if(empty($status) || !is_numeric($status)){
$this->addError('status'.$row, '第'.$row.'行状态错误');
}
/******************************验证语言***********************************/
$language_name = $worksheet->getCellByColumnAndRow(5, $row)->getValue();
if(empty($language_name)){
$this->addError('language_name'.$row, '第'.$row.'行语言为空');
}else{
$languages = $languageModel->getSysLanguageByName($language_name);
if(empty($languages)){
$this->addError('languages'.$row, '第'.$row.'行语言不存在');
}
}
$name = $worksheet->getCellByColumnAndRow(6, $row)->getValue();
if(empty($name)){
$this->addError('name'.$row, '第'.$row.'行名称为空');
}
$keywords = $worksheet->getCellByColumnAndRow(7, $row)->getValue();
if(empty($keywords)){
$this->addError('keywords'.$row, '第'.$row.'行关键词为空');
}
$url = $worksheet->getCellByColumnAndRow(10, $row)->getValue();
if(!empty($url)){
if(in_array($url, $arr_url)){
$this->addError('url'.$row, '第'.$row.'行网址重复出现');
}else{
$arr_url[] = $url;
}
}
}
if($this->hasErrors()){
return false;
}
//组装数组
$enableData = array();
foreach ($enableRow as $key &# |
|