链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.5 KiB

  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Schema;
  6. class MigrateUserCollection extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'migrateData:userCollection';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Command 迁移用户收藏店铺数据';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return int
  33. */
  34. public function handle()
  35. {
  36. $oldTableName = 'ims_cjdc_collection';
  37. $oldStoreTableName = 'ims_cjdc_store';
  38. $newTableName = 'lanzu_user_collection';
  39. // 判断表是否存在
  40. if(!Schema::hasTable($oldTableName)){
  41. var_dump('旧表不存在');
  42. return 0;
  43. }
  44. if(!Schema::hasTable($newTableName)){
  45. var_dump('新表不存在');
  46. return 0;
  47. }
  48. $oldData = DB::table($oldTableName)->orderBy('id','asc')->get();
  49. $oldStoreData = DB::table($oldStoreTableName)->pluck('market_id','id');
  50. $bar = $this->output->createProgressBar(count($oldData));
  51. $bar->start();
  52. $startTime = time();
  53. $error = [];
  54. foreach ($oldData as $key => $value){
  55. $collectionId = $value->id;
  56. // 判断是否存在
  57. $exist = DB::table($newTableName)->where('id',$collectionId)->exists();
  58. if($exist){
  59. continue;
  60. }
  61. // 店铺存在
  62. if(isset($oldStoreData[$value->store_id])){
  63. $newData =[
  64. 'id'=>$collectionId,
  65. 'store_id'=>$value->store_id,
  66. 'user_id'=>$value->user_id,
  67. 'market_id'=> isset($oldStoreData[$value->store_id])?$oldStoreData[$value->store_id]:0,
  68. 'created_at' => $value->time,
  69. 'updated_at' => time(),
  70. ];
  71. $res = DB::table($newTableName)->insert($newData);
  72. if(!$res){
  73. $error[] = ['id'=>$collectionId];
  74. break;
  75. }
  76. }
  77. $bar->advance();
  78. }
  79. $bar->finish();
  80. var_dump([time()-$startTime]);
  81. var_dump($error);
  82. return 0;
  83. }
  84. }