UpgradeProgressChapter.php 9.9 KB

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