xxxrrrdddd 2 years ago
parent
commit
934b02ccd4

+ 15 - 0
application/admin/model/MobileUpload.php

@@ -0,0 +1,15 @@
+<?php
+
+namespace app\admin\model;
+
+use think\Model;
+
+
+class MobileUpload extends Model
+{
+    protected $autoWriteTimestamp=true;
+    protected $updateTime=null;
+    public function log(){
+        return $this->hasMany(MobileUploadLog::class);
+    }
+}

+ 10 - 0
application/admin/model/MobileUploadLog.php

@@ -0,0 +1,10 @@
+<?php
+
+namespace app\admin\model;
+
+use think\Model;
+
+
+class MobileUploadLog extends Model
+{
+}

+ 5 - 2
application/common/model/Area.php

@@ -9,7 +9,7 @@ use think\Model;
 /**
  * 地区数据模型
  * @property bool has_air_coordinate
- * @method static static shi()
+ * @method static static shi($name=null)
  * @method static static area()
  */
 class Area extends Model
@@ -95,8 +95,11 @@ class Area extends Model
     public function scopePro(Query $query){
         $query->level(1);
     }
-    public function scopeShi(Query $query){
+    public function scopeShi(Query $query,$name=null){
         $query->level(2);
+        if($name!==null){
+            $query->where('name|shortname','like',"%{$name}%");
+        }
     }
     public function scopeArea(Query $query){
         $query->level(3);

+ 51 - 6
application/common/service/MobileImport.php

@@ -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());