/** * Handle the result properly, and then return it to controller * * @param $import * @return mixed */ public function handle($import) { $this->import = $import; try { // $this->import->skip(1)->calculate(false)->chunk(_Import::CHUNK_SIZE, $this->getChunkCallback()); // 無法處理多個 sheets 的 bug,因此改為下面的方式處理: //======================================================// // 取得上傳暫存檔路徑 $filePath = $this->import->skip(0)->file; // 透過直接指定選擇第一個sheet的方式,繞過 chunk 的 bug $reader = Excel::filter('chunk')->selectSheetsByIndex(0)->load($filePath)->skip(1); $totalRows = $reader->getTotalRowsOfFile(); //======================================================// $this->task = $this->createNewTask($totalRows); $kind = $this->task->kind()->first(); $factoryClass = $kind->factory; $adapterClass = $kind->adapter; $this->modelFactory = new $factoryClass(); $this->adapter = new $adapterClass([_Import::OPTIONS_TASK => $this->task, _Import::OPTIONS_DISTINCTION => PosMemberImportTask::getBDSerNo(Input::get(_Import::OPTIONS_DISTINCTION)), _Import::OPTIONS_CATEGORY => PosMemberImportTask::getCategorySerNo(Input::get(_Import::OPTIONS_CATEGORY)), _Import::OPTIONS_INSERTFLAG => Input::get(_Import::OPTIONS_INSERTFLAG), _Import::OPTIONS_UPDATEFLAG => Input::get(_Import::OPTIONS_UPDATEFLAG)]); $reader->chunk(_Import::CHUNK_SIZE, $this->getChunkCallback()); $this->_removeDuplicate()->_saveTaskStatic(); } catch (\Exception $e) { $this->task->delete(); } return $this->task; }
public function updatedTask(PosMemberImportTask $task) { $this->task = $task; $this->distinction = PosMemberImportTask::getBDSerNo($this->task->distinction); $this->category = PosMemberImportTask::getCategorySerNo($this->task->category); $this->task->content()->isNotExecuted()->chunk(Import::CHUNK_SIZE, function ($contents) { $contents->each(function ($content) { $content->flags = $content->getActFlags(); $content->category = $this->category; $content->distinction = $this->distinction; $content->save(); }); }); }
public function proc() { //======================================================// // 取得上傳暫存檔路徑 $filePath = storage_path("exports/posmember/{$this->getTask()->id}.xls"); if (!file_exists($filePath)) { Log::error("{$filePath} Not exists!"); } // 透過直接指定選擇第一個sheet的方式,繞過 chunk 的 bug $reader = Excel::filter('chunk')->selectSheetsByIndex(0)->load($filePath)->skip(1); $totalRows = $reader->getTotalRowsOfFile(); $this->getTask()->status_code = PosMemberImportTask::STATUS_IMPORTING; $this->getTask()->total_count = $totalRows - 1; $this->getTask()->save(); //======================================================// $kind = $this->getTask()->kind()->first(); $factoryClass = $kind->factory; $adapterClass = $kind->adapter; $this->modelFactory = new $factoryClass(); $this->adapter = new $adapterClass([_Import::OPTIONS_TASK => $this->getTask(), _Import::OPTIONS_DISTINCTION => PosMemberImportTask::getBDSerNo($this->getTask()->distinction), _Import::OPTIONS_CATEGORY => PosMemberImportTask::getCategorySerNo($this->getTask()->category), _Import::OPTIONS_INSERTFLAG => Flater::getFlagString($this->getTask()->insert_flags), _Import::OPTIONS_UPDATEFLAG => Flater::getFlagString($this->getTask()->update_flags)]); $reader->chunk(_Import::CHUNK_SIZE, $this->getChunkCallback()); $this->_removeDuplicate(); $this->_saveTaskStatic(); }