think-queue 加 redis實現批量匯入excel
阿新 • • 發佈:2020-08-05
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