chenhao 4 years ago
parent
commit
755f54de7c

+ 236 - 0
application/admin/controller/Crontab.php

@@ -106,4 +106,240 @@ class Crontab extends Controller
 
         }
     }
+    /**
+     * 计划任务 每五分钟请求一次,检测有无导入的新文件
+     */
+    public function excelImport()
+    {
+        set_time_limit(6000);
+        $serialzierData = Db::name('serialize')->where('status',1)->find();
+        $excel_arrays =unserialize($serialzierData['text']);
+        $uni  = $serialzierData['uni'];
+        $uid  = $serialzierData['admin_id'];
+        $num['error_num'] = 0;
+        $excel_array = array_slice($excel_arrays,0,400);
+        $success = [];
+        $error = [];
+//        var_dump($excel_array);die;
+        foreach ($excel_array as $k => $v) {
+            $status = 1; // 为1标识此行正常
+            // 判断邮件
+            if (!empty($v[2])) {
+                $checkEmail = Db::name('customer')->where('email',$v[2])->where('spread_id',$uid)->find();
+                if ($checkEmail) {
+                    $error[] = [
+                        'rows'    => $k,
+                        'msg'     => '此客户邮箱已存在',
+                        'content' => $v[2],
+                        'uni'     => $uni,
+                    ];
+                    $status = 0; // 设置为0标识此行有错误
+                } else {
+                    if (!filter_var($v[2], FILTER_VALIDATE_EMAIL)) {
+                        $error[] = [
+                            'rows'    => $k,
+                            'msg'     => '邮箱格式不正确',
+                            'content' => $v[2],
+                            'uni'     => $uni,
+                        ];
+                        $status = 0; // 设置为0标识此行有错误
+                    }
+                }
+            }
+            $customer_chengk = new CustomerModel();
+            if ($customer_chengk->checkCustomer($v[2],$v[1],$v[3])) {
+                $error[] = [
+                    'rows'    => $k,
+                    'msg'     => '此客户已存在其他状态',
+                    'content' => $v[2],
+                    'uni'     => $uni,
+                ];
+                $status = 0; // 设置为0标识此行有错误
+            }
+            if (!empty($v[2])) {
+                $ckeckWebsiteCompany = Db::name('customer')->where('website','like','%'.$v[3].'%')->where('status',0)->where('email','')->find();
+                if ($ckeckWebsiteCompany) {
+                    Db::name('customer')->where('id',$ckeckWebsiteCompany['id'])->update(['spread_id'=>$uid,'emial' => $v[2]]);
+                    Db::name('customer')->where('emial',$v[2])->setInc('people',1);
+                    Db::name('ranking')->where('c_id',$ckeckWebsiteCompany['id'])->update([['admin_id' => $uid]]);
+                    $error[] = [
+                        'rows'    => $k,
+                        'msg'     => '已存在此公司,网站客户,直接更新,不计入添加次数',
+                        'content' => "已存在此公司,网站客户,直接更新,不计入添加次数",
+                        'uni'     => $uni,
+                    ];
+                    $status = 0; // 设置为0标识此行有错误
+                }
+            }
+            // 判断网站
+            if (empty($v[3])) {
+                $error[] = [
+                    'rows'    => $k,
+                    'msg'     => '网站不能为空',
+                    'content' => $v[3],
+                    'uni'     => $uni,
+                ];
+                $status = 0; // 设置为0标识此行有错误
+            } else {
+                $website =explode(chr(10), $v[3]);
+                $v[3] = implode("##",$website);
+            }
+            // 判断评分
+            if ($v[4] == '') {
+                $error[] = [
+                    'rows'    => $k,
+                    'msg'     => '评分不能为空',
+                    'content' => $v[4],
+                    'uni'     => $uni,
+                ];
+                $status = 0; // 设置为0标识此行有错误
+            } else {
+                // 判断评分
+                if (!is_numeric($v[4])) {
+                    $error[] = [
+                        'rows'    => $k,
+                        'msg'     => '评分必须为整数',
+                        'content' => $v[4],
+                        'uni'     => $uni,
+                    ];
+                    $status = 0; // 设置为0标识此行有错误
+                }
+            }
+
+            // 判断国家
+            $country = Db::name('countries')->where('country', $v[5])->find();
+            if (!$country) {
+                $error[] = [
+                    'rows'    => $k,
+                    'msg'     => '国家名称不正确',
+                    'content' => $v[5],
+                    'uni'     => $uni,
+                ];
+                $status = 0; // 设置为0标识此行有错误
+            }
+            $v[5] = $country['id'];
+
+            // 判断行业
+            if (empty($v[7])) {
+                $error[] = [
+                    'rows'    => $k,
+                    'msg'     => '行业不能为空',
+                    'content' => $v[7],
+                    'uni'     => $uni,
+                ];
+            } else {
+                $tapeNanme = explode('&',$v[7]);
+                foreach ($tapeNanme as $vo) {
+                    $type = Db::name('type d')
+                        ->join('admin a','a.id = d.admin_id')
+                        ->where('d.name', $vo)
+                        ->where(function ($query) {
+                            $query->whereOr('d.admin_id',session('uid'));
+                            $query->whereOr('a.groupid','3');
+                            $query->whereOr('a.groupid','5');
+                        })
+                        ->field('d.name,d.id')
+                        ->find();
+                    if (!$type) {
+                        $error[] = [
+                            'rows'    => $k,
+                            'msg'     => '未设置此行业名称-'.$vo,
+                            'content' => $v[7],
+                            'uni'     => $uni,
+                        ];
+                        $status = 0; // 设置为0标识此行有错误
+                    } else {
+                        $typdIds[] = $type['id'];
+                    }
+                }
+            }
+            $v[7] = ','.implode(',',$typdIds).','; // 改为id
+            unset($typdIds);
+            // 主营产品
+            if (empty($v[8])) {
+                $error[] = [
+                    'rows'    => $k,
+                    'msg'     => '主营产品不能为空',
+                    'content' => $v[8],
+                    'uni'     => $uni,
+                ];
+            } else {
+                $productName = explode('&',$v[8]);
+                foreach ($productName as $vo) {
+                    $produc = Db::name('product d')
+                        ->join('admin a','d.admin_id=a.id')
+                        ->where('d.product_name', $vo)
+                        ->where(function ($query) {
+                            $query->whereOr('d.admin_id',session('uid'));
+                            $query->whereOr('a.groupid','3');
+                            $query->whereOr('a.groupid','5');
+                        })
+                        ->field('d.product_name,d.id')
+                        ->find();
+                    if (!$produc) {
+                        $error[] = [
+                            'rows'    => $k,
+                            'msg'     => '未设置此主营产品名称-'.$vo,
+                            'content' => $v[8],
+                            'uni'     => $uni,
+                        ];
+                        $status = 0; // 设置为0标识此行有错误
+                    } else {
+                        $producIds[] = $produc['id'];
+                    }
+                }
+            }
+            $v[8] = ','.implode(',',$producIds).','; // 改为id
+            unset($producIds);
+            $where = [
+                'email'   => $v[2],
+                'company' => $v[1],
+                'website' => $v[3],
+                'spread_id'   => $uid,
+            ];
+            $isset = Db::name('customer')->where($where)->find();
+            if ($isset) {
+                $error[] = [
+                    'rows'    => $k,
+                    'msg'     => '此客户已经存在已经自动过滤',
+                    'content' => $v[0],
+                    'uni'     => $uni,
+                ];
+                $status = 0; // 设置为0标识此行有错误
+            }
+            if ($status == 1) {
+                $success[] = [
+                    'name'      => $v[0],
+                    'company'   => $v[1],
+                    'email'     => $v[2],
+                    'website'   => $v[3],
+                    'grade'     => $v[4],
+                    'country'   => $v[5],
+                    'phone'     => $v[6],
+                    'type'      => $v[7],
+                    'product'   => $v[8],
+                    'spread_id' => $uid,
+                    'follow_time' => time(),
+                    'uni'   => $uni,
+                ];
+                $import_success[] = [
+                    'name'  => $v[0],
+                    'email' => $v[2],
+                    'uni'   => $uni,
+                ];
+            } else {
+                $num['error_num'] = $num['error_num'] + 1;  // 记录失败条数
+            }
+        }
+        $num['success_num'] = count($success);
+        $num['zong'] = count($excel_array);
+        if ($num['success_num'] > 0) {
+            $customer = new CustomerModel();
+            $customer->add_all($success,$uid); // 保存处理所有数据;
+            Db::name("import_success")->insertAll($import_success);
+        }
+        if ($num['error_num'] > 0) {
+            Db::name('import_error')->insertAll($error);
+        }
+    }
 }

+ 13 - 6
application/admin/controller/Customer.php

@@ -1817,7 +1817,8 @@ class Customer extends Base
             $num['zong'] = count($excel_array);
             if ($num['success_num'] > 0) {
                 $customer = new CustomerModel();
-                $customer->add_all($success); // 保存处理所有数据;
+                $uid = session('uid');
+                $customer->add_all($success,$uid); // 保存处理所有数据;
                 Db::name("import_success")->insertAll($import_success);
             }
             if ($num['error_num'] > 0) {
@@ -1835,12 +1836,18 @@ class Customer extends Base
     public function see_import()
     {
         $uni = input('uni');
-        $error_num = input('error_num');
-        $success_num = input('success_num');
+//        $serialize = Db::name('serialize')->where('uni',$uni)->find();
+//        if ($serialize['status'] == 1) {
+//            return $this->success('excel文件还在导入中,请稍后查看');
+//            return json(['code' => 100,'msg' => '']);
+//        }
         $error = Db::name('import_error')->where('uni', $uni)->select();
-        $num['success'] = $success_num;
-        $num['error'] = $error_num;
-        $num['zong'] = $num['success'] + $num['error'];
+        $success = Db::name('import_success')->where('uni', $uni)->select();
+        $errorNum = count($error);
+        $successNum = count($success);
+        $num['zong'] = $errorNum + $successNum;
+        $num['success'] =  $successNum;
+        $num['error'] = $errorNum;
         $this->assign('error', $error);
         $this->assign('num', $num);
         $this->assign('uni', $uni);

+ 45 - 17
application/admin/controller/Upload.php

@@ -413,6 +413,7 @@ class Upload extends Base
         $objPHPExcel = new \PHPExcel();
         //获取表单上传文件
         $file = request()->file('file');
+        $fileInfo = $file->getInfo();
         $info = $file->validate(['ext' => 'xls'])->move(ROOT_PATH . 'public');  //上传验证后缀名,以及上传之后移动的地址  E:\wamp\www\bick\public
         if ($info) {
             $exclePath = $info->getSaveName();                  //获取文件名
@@ -424,12 +425,23 @@ class Upload extends Base
             array_shift($excel_array);                                        //删除第一个数组(标题);
             $success = [];
             $error = [];
-
             $num['error_num'] = 0;
             $uni = time() . rand(0, 1000);
-//            $serialize = serialize($excel_array);
-//
-//            Db::name('serialize')->insert(['text'=>$serialize,'uni' => $uni,]);die;
+            $fileName = $fileInfo['name'];
+            $serialize = serialize($excel_array);
+            $serializeTime = time();
+            if (count($excel_array) > 1000) {
+                Db::name('serialize')->insert([
+                    'text' => $serialize,
+                    'uni' => $uni,
+                    'status' => 1,
+                    'admin_id' => session('uid'),
+                    'excel_name' => $fileName,
+                    'time' => $serializeTime,
+                ]);
+                $tableSerialize = Db::name('serialize')->where('admin_id',session('uid'))->select();
+                return json(['code' => 110,'msg' => '文件导入时间过长,已转入本地导入,五分钟开始', 'data' => $tableSerialize]);
+            }
             foreach ($excel_array as $k => $v) {
                 $status = 1; // 为1标识此行正常
                 // 判断邮件
@@ -640,28 +652,44 @@ class Upload extends Base
                     $num['error_num'] = $num['error_num'] + 1;  // 记录失败条数
                 }
             }
-            var_dump($success);die;
             $num['success_num'] = count($success);
             $num['zong'] = count($excel_array);
             if ($num['success_num'] > 0) {
-                $serializeSuccess = [
-                  'uni' => $uni,
-                  'type' => 2,
-                  'text' => serialize($success),
-                ];
-                Db::name('serialize')->insert($serializeSuccess);
-                die;
+                Db::name('serialize')->insert(['text' => $serialize, 'uni' => $uni, 'status' => 2, 'admin_id' => session('uid'), 'excel_name' => $fileName, 'time' => $serializeTime]);
                 $customer = new CustomerModel();
-//                $customer->add_all($success); // 保存处理所有数据;
-//                die;
-//                Db::name("import_success")->insertAll($import_success);
+                $customer->add_all($success,session('uid')); // 保存处理所有数据;
+                Db::name("import_success")->insertAll($import_success);
             }
             if ($num['error_num'] > 0) {
-//                Db::name('import_error')->insertAll($error);
+                Db::name('serialize')->insert(['text' => $serialize, 'uni' => $uni, 'status' => 3, 'admin_id' => session('uid'), 'excel_name' => $fileName, 'time' => $serializeTime]);
+                Db::name('import_error')->insertAll($error);
             }
-            return json(['code' => 200, 'uni' => $uni, 'msg' => '完成导入,正在查询状态', 'success_num' => $num['success_num'], 'error_num' => $num['error_num']]);
+            $tableSerialize = Db::name('serialize')->where('admin_id',session('uid'))->select();
+            return json(['code' => 200, 'uni' => $uni, 'msg' => '完成导入', 'success_num' => $num['success_num'], 'error_num' => $num['error_num'],'data' => $tableSerialize]);
         } else {
             echo $file->getError();
         }
     }
+    /**
+     * 获取序列化数据
+     */
+    public function serializeData()
+    {
+        $data = Db::name('serialize')->where('admin_id',session('uid'))->order('time desc')->select();
+        $arr = ['1' => '导入中','2' => '成功', '3' => '失败'];
+        foreach ($data as &$v) {
+            $v['status'] = $arr[$v['status']];
+            $v['time'] = date('Y-m-d H:i:s',$v['time']);
+        }
+        return json(['code' => 0, 'msg' => '', 'count' => count($data), 'data' => $data]);
+    }
+    /**
+     * 删除序列化
+     */
+    public function serilizeDel()
+    {
+        $param = $this->request->param();
+//        Db::name('serialize')->where('uni',$param['uni'])->delete();
+        return json(['code' => 200, 'msg' => '删除成功']);
+    }
 }

+ 2 - 2
application/admin/model/CustomerModel.php

@@ -168,7 +168,7 @@ class CustomerModel extends Model
     /**
      * 批量导入储存所有数据并且处理建档人数
      */
-    public function add_all($data)
+    public function add_all($data , $uid)
     {
         foreach ($data as &$v)
         {
@@ -178,7 +178,7 @@ class CustomerModel extends Model
             $id = $this->getLastInsID();
             $ranhdata[] = [
                 'c_id' => $id,
-                'admin_id' => session('uid'),
+                'admin_id' => $uid,
                 'create_time' => time(),
                 'type' => 0,
             ];

+ 65 - 3
application/admin/view/customer/daoru.html

@@ -24,6 +24,7 @@
 <!--                        <button class="layui-btn site-demo-active" data-type="loading">模拟loading</button>-->
 <!--                    </div>-->
                 </div>
+
                 <div class="layui-form-item layui-col-md-offset1 layui-col-md8" style="margin-top: 50px">
                     <div class="layui-input-block" style="margin-top: 5%">
 <!--                        <button class="layui-btn layui-btn-normal site-demo-active" data-type="loading"  style="width: 200px;" lay-submit="" lay-filter="component-form-element" id="test9">上传-->
@@ -32,18 +33,53 @@
 <!--                        <button class="layui-btn layui-btn-primary" id = 'data-stop'>关闭</button>-->
                     </div>
                 </div>
+
             </div>
+
+        </div>
+    </div>
+    <div class="layui-card" style="height: 400px">
+        <div class="layui-card-body layui-form">
+            <div>导入历史</div>
+            <table class="layui-hide" id="test"></table>
         </div>
     </div>
 </div>
+
 {include file="public/footer" /}
+<script type="text/html" id="barDemo">
+
+    {{# if(d.status == '导入中'){ }}
+       <a class="layui-btn layui-btn-xs layui-bg-gray" title="查看" onclick="layer.msg('文件正在导入中.....')">查看</i></a>
+    {{# }else{ }}
+
+       <a class="layui-btn layui-btn-xs layui-bg-gray" title="查看" onclick="wk.layer_show('','{:url('admin/customer/see_import')}?uni={{d.uni}}')">查看</i></a>
+    {{# } }}
+
+</script>
 <script>
 
-    layui.use(['form','upload','layedit','element'], function() {
+    layui.use(['form','upload','layedit','element','table'], function() {
         var form = layui.form
             , layedit = layui.layedit
             , upload = layui.upload
             , element = layui.element
+        var table = layui.table;
+
+        table.render({
+            elem: '#test'
+            ,url:'/admin/upload/serializeData'
+            ,page: false
+            ,title: '用户数据表'
+            ,id: 'testReload'
+            ,cols: [[
+                {type:'numbers', title:'排序', width:80, unresize: true}
+                ,{field:'excel_name', title:'文件名', width:'', }
+                ,{field:'time', title:'导入时间', width:'',}
+                ,{field:'status', title:'导入状态', width:''}
+                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
+            ]]
+        });
         //选完文件后不自动上传
         upload.render({
             elem: '#test8'
@@ -67,8 +103,15 @@
             ,done: function(res){
                 if (res.code == 200) {
                     layer.close(layuiIndex);
+                    layer.msg(res.msg)
+                    table.reload('testReload');
                     // element.progress('demo', '0%')
-                    window.location.href="{:url('see_import')}?uni="+res.uni+'&success_num='+res.success_num+'&error_num='+res.error_num;
+                    // window.location.href="{:url('see_import')}?uni="+res.uni+'&success_num='+res.success_num+'&error_num='+res.error_num;
+                }
+                if (res.code == 110) {
+                    layer.close(layuiIndex);
+                    layer.msg(res.msg)
+                    table.reload('testReload');
                 }
             }
         });
@@ -101,7 +144,6 @@
                 othis.addClass(DISABLED);
             }
         };
-
         $('.site-demo-active').on('click', function(){
             var values = document.getElementById('values').value;
             if (values=='') {
@@ -114,6 +156,26 @@
             // active[type] ? active[type].call(this, othis) : '';
         });
     });
+    // function edit_status(uni,status){
+    //     layer.confirm('确认删除吗', {icon: 7, title:'警告'}, function(index){
+    //         $.ajax({
+    //             url: "{:url('admin/upload/serilizeDel')}",
+    //             type: "post",
+    //             dataType: "json",
+    //             data: {'uni':uni,'status':status},
+    //             success: function (res) {
+    //                 console.log(res);
+    //                 if (res.code == 200) {
+    //                     layer.msg(res.msg)
+    //                 } else if (res.code == 100) {
+    //                     wk.error(res.msg,"layui.table.reload('LAY-table');");
+    //                 }
+    //             }
+    //         });
+    //         layer.close(index);
+    //     })
+    //
+    // }
 </script>
 </body>
 </html>