UpgradeProgressChapter.php 9.5 KB

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