2
0

UpdateSentenceUnique.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Discussion;
  4. use App\Models\Sentence;
  5. use App\Models\SentHistory;
  6. use Illuminate\Console\Command;
  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. *
  16. * @var string
  17. */
  18. protected $signature = 'update:sentence.unique';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = '将sentence中的重复数据合并';
  25. /**
  26. * Create a new command instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. }
  34. /**
  35. * Execute the console command.
  36. *
  37. * @return int
  38. */
  39. public function handle()
  40. {
  41. $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 ';
  42. $total = DB::select($queryCount);
  43. $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';
  44. $query = DB::select($querySame);
  45. $count = 0;
  46. foreach ($query as $key => $value) {
  47. $count++;
  48. $same = Sentence::where('book_id', $value->book_id)
  49. ->where('paragraph', $value->paragraph)
  50. ->where('word_start', $value->word_start)
  51. ->where('word_end', $value->word_end)
  52. ->where('channel_uid', $value->channel_uid)
  53. ->orderBy('updated_at', 'desc')
  54. ->get();
  55. $per = (int) ($count * 100 / $total[0]->count);
  56. $this->info("[{$per}]-{$count} ".$same[0]->updated_at.' '.$same[1]->updated_at.' '.count($same));
  57. for ($i = 1; $i < count($same); $i++) {
  58. // 将旧数据的历史记录 重新定位到新数据
  59. $history = SentHistory::where('sent_uid', $same[$i]->uid)
  60. ->update(['sent_uid' => $same[0]->uid]);
  61. // 将旧数据的discussion 重新定位到新数据
  62. $discussion = Discussion::where('res_id', $same[$i]->uid)
  63. ->update(['res_id' => $same[0]->uid]);
  64. $this->info("{$history}-$discussion");
  65. // 将旧数据的 pr 重新定位到新数据
  66. // 删除旧数据
  67. $same[$i]->delete();
  68. if ($same[$i]->trashed()) {
  69. $this->info('软删除成功!');
  70. } else {
  71. $this->error('软删除失败!');
  72. }
  73. }
  74. if ($count >= 1) {
  75. break;
  76. }
  77. }
  78. return 0;
  79. }
  80. }