UpgradeProgressChapter.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Support\Facades\Validator;
  4. use Illuminate\Console\Command;
  5. use App\Models\Sentence;
  6. use App\Models\PaliSentence;
  7. use App\Models\Progress;
  8. use App\Models\ProgressChapter;
  9. use App\Models\PaliText;
  10. use App\Models\Tag;
  11. use App\Models\TagMap;
  12. use App\Models\Channel;
  13. class UpgradeProgressChapter extends Command
  14. {
  15. /**
  16. * The name and signature of the console command.
  17. *
  18. * @var string
  19. */
  20. protected $signature = 'upgrade:progresschapter';
  21. /**
  22. * The console command description.
  23. *
  24. * @var string
  25. */
  26. protected $description = '更新章节完成度,以channel为单位';
  27. /**
  28. * Create a new command instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct()
  33. {
  34. parent::__construct();
  35. }
  36. /**
  37. * Execute the console command.
  38. *
  39. * @return int
  40. */
  41. public function handle()
  42. {
  43. $this->info("upgrade:progresschapter start.");
  44. $startTime = time();
  45. $tagCount=0;
  46. #第一步 查询有多少书有译文
  47. $books = Sentence::where('strlen','>',0)
  48. ->where('book_id','<',1000)
  49. ->where('channel_uid','<>','')
  50. ->groupby('book_id')
  51. ->select('book_id')
  52. ->get();
  53. $bar = $this->output->createProgressBar(count($books));
  54. foreach ($books as $book) {
  55. # code...
  56. $chapters = PaliText::where('book',$book->book_id)
  57. ->where('level','>',0)
  58. ->where('level','<',8)
  59. ->select('paragraph','chapter_strlen','chapter_len')
  60. ->get();
  61. foreach ($chapters as $key => $chapter) {
  62. # code...
  63. $chapter_strlen = PaliSentence::where('book',$book->book_id)
  64. ->whereBetween('paragraph',[$chapter->paragraph,$chapter->paragraph+$chapter->chapter_len-1])
  65. ->sum('length');
  66. if($chapter_strlen == 0){
  67. $this->error('chapter_strlen is 0 book:'.$book->book_id.' paragraph:'.$chapter->paragraph.'-'.($chapter->paragraph+$chapter->chapter_len-1));
  68. continue;
  69. }
  70. $strlen = Progress::where('book',$book->book_id)
  71. ->whereBetween('para',[$chapter->paragraph,$chapter->paragraph+$chapter->chapter_len-1])
  72. ->groupby('channel_id')
  73. ->selectRaw('channel_id, sum(all_strlen) as cp_len')
  74. ->get();
  75. foreach ($strlen as $final) {
  76. # code...
  77. # 计算此段落完成时间
  78. $finalAt = Progress::where('book',$book->book_id)
  79. ->whereBetween('para',[$chapter->paragraph,$chapter->paragraph+$chapter->chapter_len-1])
  80. ->where('channel_id',$final->channel_id)
  81. ->max('created_at');
  82. $updateAt = Progress::where('book',$book->book_id)
  83. ->whereBetween('para',[$chapter->paragraph,$chapter->paragraph+$chapter->chapter_len-1])
  84. ->where('channel_id',$final->channel_id)
  85. ->max('updated_at');
  86. $transTexts = Sentence::where('book_id',$book->book_id)
  87. ->whereBetween('paragraph',[$chapter->paragraph+1,$chapter->paragraph+$chapter->chapter_len-1])
  88. ->where('channel_uid',$final->channel_id)
  89. ->select('content')
  90. ->orderBy('paragraph')
  91. ->orderBy('word_start')
  92. ->get();
  93. $summaryText = "";
  94. foreach ($transTexts as $text) {
  95. # code...
  96. $summaryText .= str_replace("\n","",$text->content);
  97. if(mb_strlen($summaryText,"UTF-8")>255){
  98. break;
  99. }
  100. }
  101. #查询标题
  102. $title = Sentence::where('book_id',$book->book_id)
  103. ->where('paragraph',$chapter->paragraph)
  104. ->where('channel_uid',$final->channel_id)
  105. ->value('content');
  106. //查询语言
  107. $channelLang = Channel::where('uid',$final->channel_id)->value('lang');
  108. $lang = explode('-',$channelLang)[0];
  109. $attributes = [
  110. 'book'=>$book->book_id,
  111. 'para'=>$chapter->paragraph,
  112. 'channel_id'=>$final->channel_id];
  113. $rules = array(
  114. 'book' => 'integer',
  115. 'para' => 'integer',
  116. 'channel_id' => 'uuid'
  117. );
  118. $validator = Validator::make($attributes, $rules);
  119. if ($validator->fails()) {
  120. $this->error("Validator is fails");
  121. return 0;
  122. }
  123. if(ProgressChapter::where($attributes)->exists()){
  124. $chapterData = ProgressChapter::where($attributes)->first();
  125. }else{
  126. $chapterData = new ProgressChapter;
  127. $chapterData->book = $attributes["book"];
  128. $chapterData->para = $attributes["para"];
  129. $chapterData->channel_id = $attributes["channel_id"];
  130. }
  131. $chapterData->lang = $lang;
  132. $chapterData->all_trans = $final->cp_len/$chapter_strlen;
  133. $chapterData->public = $final->cp_len/$chapter_strlen;
  134. $chapterData->progress = $final->cp_len/$chapter_strlen;
  135. $chapterData->title = mb_substr($title,0,255,"UTF-8");
  136. $chapterData->summary = mb_substr($summaryText,0,255,"UTF-8");
  137. $chapterData->created_at = $finalAt;
  138. $chapterData->updated_at = $updateAt;
  139. $chapterData->save();
  140. $wasCreated = $chapterData->wasRecentlyCreated;
  141. $wasChanged = $chapterData->wasChanged();
  142. #查询路径
  143. $path = json_decode(
  144. PaliText::where('book',$book->book_id)
  145. ->where('paragraph',$chapter->paragraph)
  146. ->value('path'));
  147. if($path){
  148. //查询标签
  149. $tags = [];
  150. foreach ($path as $key => $value) {
  151. # code...
  152. if($value->level>0){
  153. $paliTextUuid = PaliText::where('book',$value->book)
  154. ->where('paragraph',$value->paragraph)
  155. ->value('uid');
  156. $tagUuids = TagMap::where('table_name','pali_texts')
  157. ->where('anchor_id',$paliTextUuid)
  158. ->select(['tag_id'])
  159. ->get();
  160. foreach ($tagUuids as $key => $taguuid) {
  161. # code...
  162. $tags[$taguuid['tag_id']]=1;
  163. }
  164. }
  165. }
  166. //更新标签映射表
  167. //删除旧的标签映射表
  168. TagMap::where('table_name' , 'progress_chapters')
  169. ->where('anchor_id' , $chapterData->uid)
  170. ->delete();
  171. foreach ($tags as $key => $tag) {
  172. # code...
  173. $tagmap = TagMap::create([
  174. 'table_name' => 'progress_chapters',
  175. 'anchor_id' => $chapterData->uid,
  176. 'tag_id' => $key
  177. ]);
  178. if($tagmap){
  179. $tagCount++;
  180. }
  181. }
  182. }
  183. }
  184. }
  185. $bar->advance();
  186. }
  187. $bar->finish();
  188. $time = time() - $startTime;
  189. $this->info("upgrade:progresschapter finished in {$time}s tag count:{$tagCount}");
  190. return 0;
  191. }
  192. }