UpgradeProgressChapter.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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:progress.chapter {--book=} {--para=} {--channel=}';
  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. $book = $this->option('book');
  46. $para = $this->option('para');
  47. $channelId = $this->option('channel');
  48. $tagCount=0;
  49. #第一步 查询有多少书有译文
  50. if($book){
  51. $books = Sentence::where('book_id',$book)
  52. ->groupby('book_id')
  53. ->select('book_id')
  54. ->get();
  55. }else{
  56. $books = Sentence::where('strlen','>',0)
  57. ->where('book_id','<',1000)
  58. ->where('channel_uid','<>','')
  59. ->groupby('book_id')
  60. ->select('book_id')
  61. ->get();
  62. }
  63. $bar = $this->output->createProgressBar(count($books));
  64. foreach ($books as $book) {
  65. if($para){
  66. $table = PaliText::where('book',$book->book_id)
  67. ->where('paragraph','<=',$para);
  68. }else{
  69. $table = PaliText::where('book',$book->book_id);
  70. }
  71. $chapters = $table->where('level','>',0)
  72. ->where('level','<',8)
  73. ->select('paragraph','chapter_strlen','chapter_len')
  74. ->get();
  75. foreach ($chapters as $key => $chapter) {
  76. # code...
  77. $chapter_strlen = PaliSentence::where('book',$book->book_id)
  78. ->whereBetween('paragraph',[$chapter->paragraph,$chapter->paragraph+$chapter->chapter_len-1])
  79. ->sum('length');
  80. if($chapter_strlen == 0){
  81. $this->error('chapter_strlen is 0 book:'.$book->book_id.' paragraph:'.$chapter->paragraph.'-'.($chapter->paragraph+$chapter->chapter_len-1));
  82. continue;
  83. }
  84. $table = Progress::where('book',$book->book_id)
  85. ->whereBetween('para',[$chapter->paragraph,$chapter->paragraph+$chapter->chapter_len-1]);
  86. if($channelId){
  87. $table->where('channel_id',$channelId);
  88. }
  89. $strlen = $table->groupby('channel_id')
  90. ->selectRaw('channel_id, sum(all_strlen) as cp_len')
  91. ->get();
  92. foreach ($strlen as $final) {
  93. # code...
  94. # 计算此段落完成时间
  95. $finalAt = Progress::where('book',$book->book_id)
  96. ->whereBetween('para',[$chapter->paragraph,$chapter->paragraph+$chapter->chapter_len-1])
  97. ->where('channel_id',$final->channel_id)
  98. ->max('created_at');
  99. $updateAt = Progress::where('book',$book->book_id)
  100. ->whereBetween('para',[$chapter->paragraph,$chapter->paragraph+$chapter->chapter_len-1])
  101. ->where('channel_id',$final->channel_id)
  102. ->max('updated_at');
  103. $transTexts = Sentence::where('book_id',$book->book_id)
  104. ->whereBetween('paragraph',[$chapter->paragraph+1,$chapter->paragraph+$chapter->chapter_len-1])
  105. ->where('channel_uid',$final->channel_id)
  106. ->select('content')
  107. ->orderBy('paragraph')
  108. ->orderBy('word_start')
  109. ->get();
  110. $summaryText = "";
  111. foreach ($transTexts as $text) {
  112. # code...
  113. $summaryText .= str_replace("\n","",$text->content);
  114. if(mb_strlen($summaryText,"UTF-8")>255){
  115. break;
  116. }
  117. }
  118. #查询标题
  119. $title = Sentence::where('book_id',$book->book_id)
  120. ->where('paragraph',$chapter->paragraph)
  121. ->where('channel_uid',$final->channel_id)
  122. ->value('content');
  123. //查询语言
  124. $channelLang = Channel::where('uid',$final->channel_id)->value('lang');
  125. $lang = explode('-',$channelLang)[0];
  126. $attributes = [
  127. 'book'=>$book->book_id,
  128. 'para'=>$chapter->paragraph,
  129. 'channel_id'=>$final->channel_id];
  130. $rules = array(
  131. 'book' => 'integer',
  132. 'para' => 'integer',
  133. 'channel_id' => 'uuid'
  134. );
  135. $validator = Validator::make($attributes, $rules);
  136. if ($validator->fails()) {
  137. $this->error("Validator is fails");
  138. return 0;
  139. }
  140. if(ProgressChapter::where($attributes)->exists()){
  141. $chapterData = ProgressChapter::where($attributes)->first();
  142. }else{
  143. $chapterData = new ProgressChapter;
  144. $chapterData->book = $attributes["book"];
  145. $chapterData->para = $attributes["para"];
  146. $chapterData->channel_id = $attributes["channel_id"];
  147. }
  148. $chapterData->lang = $lang;
  149. $chapterData->all_trans = $final->cp_len/$chapter_strlen;
  150. $chapterData->public = $final->cp_len/$chapter_strlen;
  151. $chapterData->progress = $final->cp_len/$chapter_strlen;
  152. $chapterData->title = mb_substr($title,0,255,"UTF-8");
  153. $chapterData->summary = mb_substr($summaryText,0,255,"UTF-8");
  154. $chapterData->created_at = $finalAt;
  155. $chapterData->updated_at = $updateAt;
  156. $chapterData->save();
  157. $wasCreated = $chapterData->wasRecentlyCreated;
  158. $wasChanged = $chapterData->wasChanged();
  159. #查询路径
  160. $path = json_decode(
  161. PaliText::where('book',$book->book_id)
  162. ->where('paragraph',$chapter->paragraph)
  163. ->value('path'));
  164. if($path){
  165. //查询标签
  166. $tags = [];
  167. foreach ($path as $key => $value) {
  168. # code...
  169. if($value->level>0){
  170. $paliTextUuid = PaliText::where('book',$value->book)
  171. ->where('paragraph',$value->paragraph)
  172. ->value('uid');
  173. $tagUuids = TagMap::where('table_name','pali_texts')
  174. ->where('anchor_id',$paliTextUuid)
  175. ->select(['tag_id'])
  176. ->get();
  177. foreach ($tagUuids as $key => $taguuid) {
  178. # code...
  179. $tags[$taguuid['tag_id']]=1;
  180. }
  181. }
  182. }
  183. //更新标签映射表
  184. //删除旧的标签映射表
  185. TagMap::where('table_name' , 'progress_chapters')
  186. ->where('anchor_id' , $chapterData->uid)
  187. ->delete();
  188. foreach ($tags as $key => $tag) {
  189. # code...
  190. $tagmap = TagMap::create([
  191. 'table_name' => 'progress_chapters',
  192. 'anchor_id' => $chapterData->uid,
  193. 'tag_id' => $key
  194. ]);
  195. if($tagmap){
  196. $tagCount++;
  197. }
  198. }
  199. }
  200. }
  201. }
  202. $bar->advance();
  203. }
  204. $bar->finish();
  205. $time = time() - $startTime;
  206. $this->info("upgrade:progresschapter finished in {$time}s tag count:{$tagCount}");
  207. return 0;
  208. }
  209. }