|
@@ -3,6 +3,8 @@ namespace app\common\service;
|
|
|
|
|
|
use app\admin\library\Auth;
|
|
|
use app\admin\model\Admin;
|
|
|
+use app\admin\model\MobileUpload;
|
|
|
+use app\common\model\Area;
|
|
|
use app\common\model\Mobile;
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Csv;
|
|
@@ -14,9 +16,11 @@ use think\exception\PDOException;
|
|
|
|
|
|
class MobileImport{
|
|
|
public static function import($file,$admin_id,$type=1,$s_id=null){
|
|
|
+ $time=time();
|
|
|
if (!$file) {
|
|
|
throw_user(__('Parameter %s can not be empty', 'file'));
|
|
|
}
|
|
|
+ $mobileUpload=new MobileUpload(['admin_id'=>$admin_id]);
|
|
|
$file=parse_url($file,PHP_URL_PATH);
|
|
|
$filePath = ROOT_PATH . DS . 'public' . DS . $file;
|
|
|
if (!is_file($filePath)) {
|
|
@@ -27,6 +31,14 @@ class MobileImport{
|
|
|
if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
|
|
|
throw_user(__('Unknown data format'));
|
|
|
}
|
|
|
+ $filename=$filePath;
|
|
|
+ $filenameArr=explode('.',$filename);
|
|
|
+ $filePathError=$filenameArr[0].'_err.'.$filenameArr[1];
|
|
|
+ copy($filePath,$filePathError);
|
|
|
+ $mobileUpload['filename']=basename($filePath);
|
|
|
+ $mobileUpload['filename_error']=sprintf("%s/%s",request()->domain(),$filePathError);
|
|
|
+ $errLog=[];
|
|
|
+ $succNum=0;
|
|
|
if ($ext === 'csv') {
|
|
|
$file = fopen($filePath, 'r');
|
|
|
$filePath = tempnam(sys_get_temp_dir(), 'import_csv');
|
|
@@ -116,7 +128,7 @@ class MobileImport{
|
|
|
$fields[] = preg_replace("/\s+/",'',$val);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+ $dataRowNum=0;
|
|
|
for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
|
|
|
$values = [];
|
|
|
for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
|
|
@@ -127,6 +139,9 @@ class MobileImport{
|
|
|
$row_info = [];
|
|
|
$proxy_info = [];
|
|
|
$temp = array_combine($fields, $values);
|
|
|
+ if(!array_filter(array_values($temp))){
|
|
|
+ break;
|
|
|
+ }
|
|
|
foreach ($temp as $k => $v) {
|
|
|
if (isset($fieldArr[$k]) && $k !== '') {
|
|
|
$row[$fieldArr[$k]] = $v;
|
|
@@ -139,8 +154,20 @@ class MobileImport{
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if(!$row['no']/* || Mobile::where('no',$row['no'])->find()*/){
|
|
|
- continue;
|
|
|
+ if(!$row['no'] || strlen($row['no'])!=11){
|
|
|
+ $errLog[]=[
|
|
|
+ 'line' =>$currentRow,
|
|
|
+ 'error'=>'手机号有误',
|
|
|
+ ];
|
|
|
+ goto NEXT;
|
|
|
+ }
|
|
|
+ $city=Area::shi($row['city'])->find();
|
|
|
+ if(!$city){
|
|
|
+ $errLog[]=[
|
|
|
+ 'line' =>$currentRow,
|
|
|
+ 'error'=>'城市有误',
|
|
|
+ ];
|
|
|
+ goto NEXT;
|
|
|
}
|
|
|
Db::startTrans();
|
|
|
if($admin_id) {
|
|
@@ -172,15 +199,33 @@ class MobileImport{
|
|
|
$mobile->allowField(true)->save($row);
|
|
|
if(!$mobile){
|
|
|
Db::rollback();
|
|
|
- continue;
|
|
|
+ $errLog[]=[
|
|
|
+ 'line' =>$currentRow,
|
|
|
+ 'error'=>'保存手机号失败',
|
|
|
+ ];
|
|
|
+ goto NEXT;
|
|
|
}
|
|
|
$info=$mobile->info()->save($row_info);
|
|
|
if(!$info){
|
|
|
Db::rollback();
|
|
|
- continue;
|
|
|
+ $errLog[]=[
|
|
|
+ 'line' =>$currentRow,
|
|
|
+ 'error'=>'保存手机号套餐失败',
|
|
|
+ ];
|
|
|
+ goto NEXT;
|
|
|
}
|
|
|
- Db::commit();
|
|
|
+ $succNum++;
|
|
|
+ //Db::commit();
|
|
|
+
|
|
|
+ NEXT:
|
|
|
+ $dataRowNum++;
|
|
|
}
|
|
|
+ $mobileUpload['rows_total']=$dataRowNum;
|
|
|
+ $mobileUpload['rows_succ']=$succNum;
|
|
|
+ $mobileUpload['rows_fail']=count($errLog);
|
|
|
+ $mobileUpload['run_time']=time()-$time;
|
|
|
+ $mobileUpload->save();
|
|
|
+ $mobileUpload->log()->saveAll($errLog);
|
|
|
} catch (Exception $exception) {
|
|
|
throw $exception;
|
|
|
throw_user($exception->getMessage());
|