songxingwei 2 years ago
parent
commit
63a93de30d

+ 133 - 70
application/admin/controller/Courier.php

@@ -144,15 +144,16 @@ class Courier extends Controller
      */
     public function import_table1()
     {
-        $query = "SHOW FULL COLUMNS FROM system_courier";
-        $table = Db::query($query); //获取表所有字段
-        $field = array_column($table,'Field'); //提取表字段,放到一维数组中
+//        $query = "SHOW FULL COLUMNS FROM system_courier";
+//        $table = Db::query($query); //获取表所有字段
+//        $field = array_column($table,'Field'); //提取表字段,放到一维数组中
+
+        $username = $this->app->session->get('user')['username'];
 
-        list($msec, $sec) = explode(' ', microtime());
-        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);  //毫秒值
         $get = $this->request->get();
         $import_path = $_SERVER['DOCUMENT_ROOT'].'/'.$get['path'];
         $url = $get['url'];
+        $ordfilename = $get['ordfilename'];
         if (getConfigValue('storage_type')=='oss'){
             $save_dir = "./upload/excle/"; // 服务资源目录
             $filename = date('Ymd').time().".xlsx"; // 自定义名称
@@ -160,90 +161,152 @@ class Courier extends Controller
             $import_path = $_SERVER['DOCUMENT_ROOT'].'/upload/excle/'.$res;
         }
 
-        $num = 0;
         try {
             $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
             $objExcel = $objReader->load($import_path);
             $list = $objExcel->getActiveSheet()->toArray();
-            $success = 0;
-            $number = 0;
             $array = array();
+
+            $dates = array_filter(array_unique(array_column($list,'0')));
+            if (count($dates)>2){
+                $this->error('表中存在多个日期,无法导入',[],3);
+            }
+
+            $datee = date('Y-m-d',strtotime($dates[1]));
+            $info = Db::name('system_values')->where('date',$datee)->count();
+            if ($info){
+                $this->error('表中日期数据已存在,无法导入',[],3);
+            }
+
+            $import_log_id = Db::name('system_import_log')->insertGetId(
+                [
+                    'name'=>$ordfilename,
+                    'url'=>$url,
+                    'import_name'=>$username
+                ]
+            );
+
             foreach ($list as $k=>$v){
-                echo $k."<br />";
-                if($k>$num-1){
-                    if(array_filter($v)){
-                        $data=[];
-                        foreach ($v as $kk=>$vv){
-                            $data[IntToChr($kk)]=trim($vv);
-                        }
+                if ($k==0){
+                    for ($i=9;$i<count($v);$i++){
+                        $name = mb_substr($v[$i],0,2,"utf-8");  //截取前两个汉字
+                        $array[$i] = $name;
+                    }
+                }else{
+                    if (!$v[0]){
+                        continue;
+                    }
+                    $date = date('Y-m-d',strtotime($v[0]));
+                    $insert_array = array();
+                    $site_id = Db::name('system_site')->where('code',$v[6])->value('id');
+                    if (!$site_id){
+                        $site_id = Db::name('system_site')->insertGetId(
+                            [
+                                'code'=>$v[6],
+                                'name'=>$v[7]
+                            ]
+                        );
                     }
-                    dump($v);
+                    for ($i=8;$i<count($v);$i++){
+                        $a['site_id'] = $site_id;
+                        $a['date'] = $date;
+                        $a['import_log_id'] = $import_log_id;
+                        $a['name'] = $i==8 ? "总签收" : $array[$i];
+                        $a['value'] = $v[$i];
+                        array_push($insert_array,$a);
+                    }
+                    Db::name('system_values')->insertAll($insert_array);
                 }
             }
-            die;
-            $error = 0;
-            $phone_error = array();
-            foreach ($array as &$v) {
-
-                if (!Validate::regex($v['phone'], "^1\d{10}$")) {
-                    $error = $error + 1;
-                    $phone_error[] = [
-                        'phone' => $v['phone'],
-                        'error' => '手机号格式不正确',
-                    ];
-                    continue;
-                }
+            $this->success('成功');
+        } catch (\think\exception\ValidateException $e) {
+            $this->error($e->getMessage());
+        }
+    }
 
-                $user_phone_count = Db::name('system_member')->where('phone', $v['phone'])->where('is_del', 1)->count();
-                if ($user_phone_count) {
-                    $error = $error + 1;
-                    $phone_error[] = [
-                        'phone' => $v['phone'],
-                        'error' => '手机已注册',
-                    ];
-                    continue;
-                }
+    /**
+     * 导入表二数据
+     * @auth true
+     * @menu true
+     * @throws \think\Exception
+     * @throws \think\exception\PDOException
+     */
+    public function import_table2()
+    {
 
+        $username = $this->app->session->get('user')['username'];
 
-                $user_name_count = Db::name('system_member')->where('user_name', $v['user_name'])->where('is_del', 1)->count();
-                if ($user_name_count) {
-                    $error = $error + 1;
-                    $phone_error[] = [
-                        'phone' => $v['phone'],
-                        'error' => '用户名已被使用',
-                    ];
-                    continue;
-                }
+        $get = $this->request->get();
+        $import_path = $_SERVER['DOCUMENT_ROOT'].'/'.$get['path'];
+        $url = $get['url'];
+        $ordfilename = $get['ordfilename'];
+        if (getConfigValue('storage_type')=='oss'){
+            $save_dir = "./upload/excle/"; // 服务资源目录
+            $filename = date('Ymd').time().".xlsx"; // 自定义名称
+            $res = getFile($url,$save_dir,$filename,1);
+            $import_path = $_SERVER['DOCUMENT_ROOT'].'/upload/excle/'.$res;
+        }
 
-                $id_card_count = Db::name('system_member')->where('id_card', $v['id_card'])->where('is_del', 1)->count();
-                if ($id_card_count) {
-                    $error = $error + 1;
-                    $phone_error[] = [
-                        'phone' => $v['phone'],
-                        'error' => '身份证号已被使用',
-                    ];
-                    continue;
-                }
+        try {
+            $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
+            $objExcel = $objReader->load($import_path);
+            dump($objExcel);die;
+            $list = $objExcel->getActiveSheet()->toArray();
+            $array = array();
 
-                $result = Db::name($this->table)->insert($v);
 
-                if ($result) {
-                    $success += 1;
-                } else {
-                    $error = $error + 1;
-                    $phone_error[] = [
-                        'phone' => $v['phone'],
-                        'error' => '新增失败',
-                    ];
-                }
+            $dates = array_filter(array_unique(array_column($list,'0')));
+            if (count($dates)>2){
+                $this->error('表中存在多个日期,无法导入',[],3);
             }
 
-            if ($error<1){
-                $this->success('成功');
-            }else{
-                $this->error('失败',['error'=>$error,'success'=>$success,'phone_error'=>$phone_error],2);
+            $datee = date('Y-m-d',strtotime($dates[1]));
+            $info = Db::name('system_values')->where('date',$datee)->count();
+            if ($info){
+                $this->error('表中日期数据已存在,无法导入',[],3);
             }
 
+            $import_log_id = Db::name('system_import_log')->insertGetId(
+                [
+                    'name'=>$ordfilename,
+                    'url'=>$url,
+                    'import_name'=>$username
+                ]
+            );
+
+            foreach ($list as $k=>$v){
+                if ($k==0){
+                    for ($i=9;$i<count($v);$i++){
+                        $name = mb_substr($v[$i],0,2,"utf-8");  //截取前两个汉字
+                        $array[$i] = $name;
+                    }
+                }else{
+                    if (!$v[0]){
+                        continue;
+                    }
+                    $date = date('Y-m-d',strtotime($v[0]));
+                    $insert_array = array();
+                    $site_id = Db::name('system_site')->where('code',$v[6])->value('id');
+                    if (!$site_id){
+                        $site_id = Db::name('system_site')->insertGetId(
+                            [
+                                'code'=>$v[6],
+                                'name'=>$v[7]
+                            ]
+                        );
+                    }
+                    for ($i=8;$i<count($v);$i++){
+                        $a['site_id'] = $site_id;
+                        $a['date'] = $date;
+                        $a['import_log_id'] = $import_log_id;
+                        $a['name'] = $i==8 ? "总签收" : $array[$i];
+                        $a['value'] = $v[$i];
+                        array_push($insert_array,$a);
+                    }
+                    Db::name('system_values')->insertAll($insert_array);
+                }
+            }
+            $this->success('成功');
         } catch (\think\exception\ValidateException $e) {
             $this->error($e->getMessage());
         }

+ 1 - 1
application/admin/controller/api/Plugs.php

@@ -78,7 +78,7 @@ class Plugs extends Controller
         $name = File::name($file->getPathname(), $this->extend, '', 'md5_file');
         $info = File::instance($this->uptype)->save($name, file_get_contents($file->getRealPath()), $this->safe);
         if (is_array($info) && isset($info['url'])) {
-            return json(['uploaded' => true, 'filename' => $name, 'key'=>$info['key'] ,'url' => $this->safe ? $name : $info['url']]);
+            return json(['uploaded' => true, 'filename' => $name,'oldfilename'=>$file->getInfo('name'), 'key'=>$info['key'] ,'url' => $this->safe ? $name : $info['url']]);
         } else {
             return json(['uploaded' => false, 'error' => ['message' => '文件处理失败,请稍候再试!']]);
         }

+ 67 - 17
application/admin/view/courier/index_search.html

@@ -11,11 +11,16 @@
             <button class="layui-btn layui-btn-primary"><i class="layui-icon">&#xe615;</i> 搜 索</button>
         </div>
     </form>
-    {if auth("import_user")}
+    {if auth("import_table1")}
     <a class="layui-btn layui-btn-primary layui-btn-sm import_table1" id="import_table1"  type="submit" style="height: 32px;width: 116.69px;margin-left:3px; margin-bottom: 15px">
         <i class="layui-icon">&#xe608;</i>导入表一
     </a>
     {/if}
+    {if auth("import_user")}
+    <a class="layui-btn layui-btn-primary layui-btn-sm import_table2" id="import_table2"  type="submit" style="height: 32px;width: 116.69px;margin-left:3px; margin-bottom: 15px">
+        <i class="layui-icon">&#xe608;</i>导入表二
+    </a>
+    {/if}
     <script>form.render()
     layui.use('upload', function(){
         var upload = layui.upload;
@@ -26,29 +31,81 @@
             ,url: 'admin/api.plugs/upload'
             ,accept: 'file', //普通文件,
             before: function(obj){ //obj参数包含的信息,跟 choose回调完全一致,可参见上文。
-                console.log(obj)
-                // layer.load(); //上传loading
+                 console.log(obj)
+                 //layer.load(); //上传loading
             }
             ,done: function(res){
+                layer.close('loading'); //上传loading
                 console.log(res)
                 if (res.uploaded == true){
-                    var value = {path:res.key,url:res.url}
+                    var value = {path:res.key,url:res.url,ordfilename:res.oldfilename}
+                    layer.load(2);
                     $.ajax({
                         url:"/admin/courier/import_table1",
                         type:'get',
                         data:value
                         ,success:function(result){
+                            layer.closeAll('loading');
+                            console.log(result);
+                            if (result.code==1){
+                                layer.msg('上传成功',{time:2000,end:function () {
+                                        location.reload();
+                                        // $.form.href('{:url("admin/main_project/index")}');
+                                    }});
+                            }else if (result.code==2){
+                                let a=[]
+                                result.data.phone_error.forEach(item=>{
+                                    a.push(`<div>手机号:${item.phone},原因:${item.error}</div>`)
+                                })
+                                layer.open({
+                                    type:1,
+                                    area:['500px', '500px'],
+                                    content:'成功:'+result.data.success+',失败:'+result.data.error+'<br />'+
+                                        '错误号码:<br />'+a.join(''),
+                                });
+                            }else if (result.code==3){
+                                layer.msg(result.info,{icon: 5});
+                            }
+                        }
+                    });
+                }else {
+                    layer.msg(res.error.message,{icon: 5});
+                }
+                //layer.closeAll('loading'); //关闭loading
+            }
+            ,error: function(){
+                layer.msg('上传失败');
+            }
+        });
+
+        //执行实例
+        var uploadInst = upload.render({
+            elem: '.import_table2' //绑定元素
+            ,url: 'admin/api.plugs/upload'
+            ,accept: 'file', //普通文件,
+            before: function(obj){ //obj参数包含的信息,跟 choose回调完全一致,可参见上文。
+                console.log(obj)
+                //layer.load(); //上传loading
+            }
+            ,done: function(res){
+                layer.close('loading'); //上传loading
+                console.log(res)
+                if (res.uploaded == true){
+                    var value = {path:res.key,url:res.url,ordfilename:res.oldfilename}
+                    layer.load(2);
+                    $.ajax({
+                        url:"/admin/courier/import_table2",
+                        type:'get',
+                        data:value
+                        ,success:function(result){
+                            layer.closeAll('loading');
                             console.log(result);
                             if (result.code==1){
-                                layer.msg('处理成功',{time:2000,end:function () {
+                                layer.msg('上传成功',{time:2000,end:function () {
                                         location.reload();
                                         // $.form.href('{:url("admin/main_project/index")}');
                                     }});
                             }else if (result.code==2){
-                                // layer.msg('成功:'+result.data.success+',失败:'+result.data.error+'<br />'+
-                                //         '错误号码'+result.data.phone_error
-                                //
-                                //     ,{icon: 5});
                                 let a=[]
                                 result.data.phone_error.forEach(item=>{
                                     a.push(`<div>手机号:${item.phone},原因:${item.error}</div>`)
@@ -58,13 +115,6 @@
                                     area:['500px', '500px'],
                                     content:'成功:'+result.data.success+',失败:'+result.data.error+'<br />'+
                                         '错误号码:<br />'+a.join(''),
-                                    // btn:['导出错误号码'],
-                                    // yes:function (index) {
-                                    //     var url = '/store/goods/get_excl';
-                                    //     var params = {'phone': result.data.phone_error};
-                                    //     jsSubmit(url, params);
-                                    //     // layer.msg('的确很重要', {icon: 1});
-                                    // }
                                 });
                             }else if (result.code==3){
                                 layer.msg(result.info,{icon: 5});
@@ -74,7 +124,7 @@
                 }else {
                     layer.msg(res.error.message,{icon: 5});
                 }
-                // layer.closeAll('loading'); //关闭loading
+                //layer.closeAll('loading'); //关闭loading
             }
             ,error: function(){
                 layer.msg('上传失败');