zoukankan      html  css  js  c++  java
  • think-queue 加 redis实现批量导入excel

    excel读取速度很快 插入很慢

    解决方案:每条数据都添加到队列中,然后让他慢慢执行

    1 composer require top-think/think-queue

    2  导入方法 放入queue中

      /**
         * @NodeAnotation(title="导入")
         */
        public function import()
        {
    
            set_time_limit(0);
            ini_set('memory_limit','1024M');
            if ($this->request->isAjax()) {
    
                //$file = "../public/upload/20200623/4b18db7eeb8f7184774353723af92787.xls";
                // $file1 = "http://hcc.cn/upload/20200623/b7ae2b2437c5afa95c6179e391b8922f.xlsx";
    
    
                $post = $this->request->post();
                $file = $post['import_file'];
    
                if ($file) {
                    $file = str_replace($_SERVER['HTTP_ORIGIN'], '../public', $file);
    
                    try {
                        $data = Excel::import($file);
                    } catch (Exception $e) {
                        echo '<pre>';
                        print_r($e->getMessage());
                        exit();
                    } catch (PhpOfficePhpSpreadsheetException $e) {
                        echo '<pre>';
                        print_r($e->getMessage());
                        exit();
                    }
                    // $data_arr = [];
    
                    /*echo '<pre>';
                    print_r(0);
                    exit();*/
    
                    foreach ($data as $k => $d) {
    
                        if ($k >= 2) {
                        $job = "index/Message";
                        Queue::push($job,$d,$queue="Import");
                         /*   echo '<pre>';
                            print_r($re);
                            exit();*/
    //                        if(!$d[0]){
    //                            break;
    //                        }
    //
    //                        /*循环插入数据*/
    //                        if ($d[1] == '正常' || $d[1] == '暂无') {
    //                            $status = 1;
    //                        } else {
    //                            $status = 0;
    //                        }
    //                        $cop = new appadminmodelCorporation();
    //
    //                        /*名字跟统一社会信用代码不能重复*/
    //                        $has = $cop->whereOr(['name' => $d[0], 'society_code' => $d[11]])->find();
    //
    //                        if ($has) {
    //                            continue;
    //                        }
    //
    //                        $cop = new appadminmodelCorporation();
    //
    //                        if ($d[4] == '') {
    //                            $d[4] = '1970-01-01';
    //                        }
    //
    //                        $data =
    //                            [
    //                                'name' => $d[0],
    //                                'status' => $status,
    //                                'law_person' => $d[2],
    //                                'register_money' => $d[3],
    //                                'create_date' => $d[4],
    //                                'create_time' => time(),
    //                                'province' => $d[5],
    //                                'city' => $d[6],
    //                                'district' => $d[7],
    //                                'phone' => $d[8],
    //                                'extra_phone' => $d[9],
    //                                'email' => $d[10],
    //                                'society_code' => $d[11],
    //                                'identity_code' => $d[12],
    //                                'register_code' => $d[13],
    //                                'organization_code' => $d[14],
    //                                'join_person_number' => $d[15],
    //                                'corporation_type' => $d[16],
    //                                'trade_type' => $d[17],
    //                                'web' => $d[18],
    //                                'address' => $d[19],
    //                                'manage_range' => $d[20],
    //
    //                            ];
    //
    //                        if ($d[21] ?? '') {
    //                            $data['path'] = $d[21];
    //                        } else {
    //                            $data['path'] = 'https://www.huixx.cn/index/index/content.html?type=2';
    //                        }
    //
    //                        $cop->save($data);
    
                        }
    
                    }
                }
                AdminController::addLog(6, '导入了公司数据', "导入文件路径:" . $file);
    
    
                $save = 1;
                $save ? $this->success('保存成功,后台自动插入数据中') : $this->error('保存失败');
            }
            $this->assign('row', '');
    
            return $this->fetch();
        }
    

      2.执行方法  appindexjobMessage.php

      

    <?php
    
    
    namespace appindexjob;
    
    
    use appadminmodelCorporation;
    use thinkqueueJob;
    
    class Message
    {
        /* 处理逻辑 */
        public function fire(Job $job, $data)
        {
            //任务执行超过1次,则删除任务
            $this->importCorp($data);
    
            $nums = $job->attempts();
            if ($nums > 0) {
                $job->delete();
            }
            $job->delete();
        }
    
        public function importCorp($d)
        {
            if (!$d[0]) {
                return;
            }
    
            /*循环插入数据*/
            if ($d[1] == '正常' || $d[1] == '暂无') {
                $status = 1;
            } else {
                $status = 0;
            }
            $cop = new Corporation();
    
            /*名字跟统一社会信用代码不能重复*/
            $has = $cop->whereOr(['name' => $d[0], 'society_code' => $d[11]])->find();
    
            if ($has) {
                return;
            }
    
            $cop = new Corporation();
    
            if ($d[4] == '') {
                $d[4] = '1970-01-01';
            }
    
            $data =
                [
                    'name' => $d[0],
                    'status' => $status,
                    'law_person' => $d[2],
                    'register_money' => $d[3],
                    'create_date' => $d[4],
                    'create_time' => time(),
                    'province' => $d[5],
                    'city' => $d[6],
                    'district' => $d[7],
                    'phone' => $d[8],
                    'extra_phone' => $d[9],
                    'email' => $d[10],
                    'society_code' => $d[11],
                    'identity_code' => $d[12],
                    'register_code' => $d[13],
                    'organization_code' => $d[14],
                    'join_person_number' => $d[15],
                    'corporation_type' => $d[16],
                    'trade_type' => $d[17],
                    'web' => $d[18],
                    'address' => $d[19],
                    'manage_range' => $d[20],
    
                ];
    
            if ($d[21] ?? '') {
                $data['path'] = $d[21];
            } else {
                $data['path'] = 'https://www.huixx.cn/index/index/content.html?type=2';
            }
    
            $cop->save($data);
        }
    }
    

      3. php think queue:work --queue Import

    你不能把坏习惯扔出窗外 但你可以一步步赶下电梯
  • 相关阅读:
    PHP常见安全问题及解决方法
    PHP中16个高危函数
    destoon7.0新增模块地图,自动输出当前模块的地图
    linux中lftp命令 – 优秀的命令行FTP客户端使用方法
    Linux中curl命令使用方法
    wordpress仿站常用标签大全
    织梦生成时提示DedeTag Engine Create File False的解决方法
    Spring 的 Controller 是单例还是多例?怎么保证并发的安全
    如何设计一个安全的对外接口
    MySQL 批量插入:如何不插入重复数据?
  • 原文地址:https://www.cnblogs.com/Ychao/p/13439013.html
Copyright © 2011-2022 走看看