UpdateSentenceUnique.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\Sentence;
  5. use App\Models\SentHistory;
  6. use App\Models\Discussion;
  7. use Illuminate\Support\Facades\DB;
  8. class UpdateSentenceUnique extends Command
  9. {
  10. /**
  11. * 将channel+book+paragraph+start+end重复的数据筛查,合并
  12. * 与此句相关的资源也要合并,包括,pr,history,discussion
  13. * 多的句子软删除
  14. * php artisan update:sentence.unique
  15. * @var string
  16. */
  17. protected $signature = 'update:sentence.unique';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '将sentence中的重复数据合并';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return int
  37. */
  38. public function handle()
  39. {
  40. $queryCount = "SELECT count(*) from (SELECT * from (SELECT book_id ,paragraph ,word_start ,word_end ,channel_uid , count(*) as co from sentences s where ver = 2 group by book_id ,paragraph ,word_start ,word_end ,channel_uid) T where co>1) TT ";
  41. $total = DB::select($queryCount);
  42. $querySame = "SELECT * from (SELECT book_id ,paragraph ,word_start ,word_end ,channel_uid , count(*) as co from sentences s where ver = 2 group by book_id ,paragraph ,word_start ,word_end ,channel_uid) T where co>1";
  43. $query = DB::select($querySame);
  44. $count = 0;
  45. foreach ($query as $key => $value) {
  46. $count++;
  47. $same = Sentence::where('book_id',$value->book_id)
  48. ->where('paragraph',$value->paragraph)
  49. ->where('word_start',$value->word_start)
  50. ->where('word_end',$value->word_end)
  51. ->where('channel_uid',$value->channel_uid)
  52. ->orderBy('updated_at','desc')
  53. ->get();
  54. $per = (int)($count*100 / $total[0]->count);
  55. $this->info("[{$per}]-{$count} ".$same[0]->updated_at.' '.$same[1]->updated_at.' '.count($same));
  56. for ($i=1; $i < count($same); $i++) {
  57. //将旧数据的历史记录 重新定位到新数据
  58. $history = SentHistory::where('sent_uid',$same[$i]->uid)
  59. ->update(['sent_uid'=>$same[0]->uid]);
  60. //将旧数据的discussion 重新定位到新数据
  61. $discussion = Discussion::where('res_id',$same[$i]->uid)
  62. ->update(['res_id'=>$same[0]->uid]);
  63. $this->info("{$history}-$discussion");
  64. //将旧数据的 pr 重新定位到新数据
  65. //删除旧数据
  66. $same[$i]->delete();
  67. if($same[$i]->trashed()){
  68. $this->info('软删除成功!');
  69. }else{
  70. $this->error('软删除失败!');
  71. }
  72. }
  73. if($count >= 1){
  74. break;
  75. }
  76. }
  77. return 0;
  78. }
  79. }