超灬超  

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 (\PhpOffice\PhpSpreadsheet\Exception $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 \app\admin\model\Corporation();
//
//                        /*名字跟统一社会信用代码不能重复*/
//                        $has = $cop->whereOr(['name' => $d[0], 'society_code' => $d[11]])->find();
//
//                        if ($has) {
//                            continue;
//                        }
//
//                        $cop = new \app\admin\model\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);

                    }

                }
            }
            AdminController::addLog(6, '导入了公司数据', "导入文件路径:" . $file);


            $save = 1;
            $save ? $this->success('保存成功,后台自动插入数据中') : $this->error('保存失败');
        }
        $this->assign('row', '');

        return $this->fetch();
    }

  2.执行方法  \app\index\job\Message.php

  

<?php


namespace app\index\job;


use app\admin\model\Corporation;
use think\queue\Job;

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

posted on 2020-08-05 12:22  超灬超  阅读(413)  评论(0编辑  收藏  举报