info("upgrade:progresschapter start."); $startTime = time(); $tagCount=0; #第一步 查询有多少书有译文 $books = Sentence::where('strlen','>',0) ->where('book_id','<',1000) ->where('channel_uid','<>','') ->groupby('book_id') ->select('book_id') ->get(); $bar = $this->output->createProgressBar(count($books)); foreach ($books as $book) { # code... $chapters = PaliText::where('book',$book->book_id) ->where('level','>',0) ->where('level','<',8) ->select('paragraph','chapter_strlen','chapter_len') ->get(); foreach ($chapters as $key => $chapter) { # code... $chapter_strlen = PaliSentence::where('book',$book->book_id) ->whereBetween('paragraph',[$chapter->paragraph,$chapter->paragraph+$chapter->chapter_len-1]) ->sum('length'); if($chapter_strlen == 0){ $this->error('chapter_strlen is 0 book:'.$book->book_id.' paragraph:'.$chapter->paragraph.'-'.($chapter->paragraph+$chapter->chapter_len-1)); continue; } $strlen = Progress::where('book',$book->book_id) ->whereBetween('para',[$chapter->paragraph,$chapter->paragraph+$chapter->chapter_len-1]) ->groupby('channel_id') ->selectRaw('channel_id, sum(all_strlen) as cp_len') ->get(); foreach ($strlen as $final) { # code... # 计算此段落完成时间 $finalAt = Progress::where('book',$book->book_id) ->whereBetween('para',[$chapter->paragraph,$chapter->paragraph+$chapter->chapter_len-1]) ->where('channel_id',$final->channel_id) ->max('created_at'); $updateAt = Progress::where('book',$book->book_id) ->whereBetween('para',[$chapter->paragraph,$chapter->paragraph+$chapter->chapter_len-1]) ->where('channel_id',$final->channel_id) ->max('updated_at'); $transTexts = Sentence::where('book_id',$book->book_id) ->whereBetween('paragraph',[$chapter->paragraph+1,$chapter->paragraph+$chapter->chapter_len-1]) ->where('channel_uid',$final->channel_id) ->select('content') ->orderBy('paragraph') ->orderBy('word_start') ->get(); $summaryText = ""; foreach ($transTexts as $text) { # code... $summaryText .= str_replace("\n","",$text->content); if(mb_strlen($summaryText,"UTF-8")>255){ break; } } #查询标题 $title = Sentence::where('book_id',$book->book_id) ->where('paragraph',$chapter->paragraph) ->where('channel_uid',$final->channel_id) ->value('content'); //查询语言 $channelLang = Channel::where('uid',$final->channel_id)->value('lang'); $lang = explode('-',$channelLang)[0]; $attributes = [ 'book'=>$book->book_id, 'para'=>$chapter->paragraph, 'channel_id'=>$final->channel_id]; $rules = array( 'book' => 'integer', 'para' => 'integer', 'channel_id' => 'uuid' ); $validator = Validator::make($attributes, $rules); if ($validator->fails()) { $this->error("Validator is fails"); return 0; } if(ProgressChapter::where($attributes)->exists()){ $chapterData = ProgressChapter::where($attributes)->first(); }else{ $chapterData = new ProgressChapter; $chapterData->book = $attributes["book"]; $chapterData->para = $attributes["para"]; $chapterData->channel_id = $attributes["channel_id"]; } $chapterData->lang = $lang; $chapterData->all_trans = $final->cp_len/$chapter_strlen; $chapterData->public = $final->cp_len/$chapter_strlen; $chapterData->progress = $final->cp_len/$chapter_strlen; $chapterData->title = mb_substr($title,0,255,"UTF-8"); $chapterData->summary = mb_substr($summaryText,0,255,"UTF-8"); $chapterData->created_at = $finalAt; $chapterData->updated_at = $updateAt; $chapterData->save(); $wasCreated = $chapterData->wasRecentlyCreated; $wasChanged = $chapterData->wasChanged(); #查询路径 $path = json_decode( PaliText::where('book',$book->book_id) ->where('paragraph',$chapter->paragraph) ->value('path')); if($path){ //查询标签 $tags = []; foreach ($path as $key => $value) { # code... if($value->level>0){ $paliTextUuid = PaliText::where('book',$value->book) ->where('paragraph',$value->paragraph) ->value('uid'); $tagUuids = TagMap::where('table_name','pali_texts') ->where('anchor_id',$paliTextUuid) ->select(['tag_id']) ->get(); foreach ($tagUuids as $key => $taguuid) { # code... $tags[$taguuid['tag_id']]=1; } } } //更新标签映射表 //删除旧的标签映射表 TagMap::where('table_name' , 'progress_chapters') ->where('anchor_id' , $chapterData->uid) ->delete(); foreach ($tags as $key => $tag) { # code... $tagmap = TagMap::create([ 'table_name' => 'progress_chapters', 'anchor_id' => $chapterData->uid, 'tag_id' => $key ]); if($tagmap){ $tagCount++; } } } } } $bar->advance(); } $bar->finish(); $time = time() - $startTime; $this->info("upgrade:progresschapter finished in {$time}s tag count:{$tagCount}"); return 0; } }