链街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.

96 lines
2.4 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 MigrateUser extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'migrateData:user';
  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_user';
  37. $newTableName = 'lanzu_user';
  38. // 判断表是否存在
  39. if(!Schema::hasTable($oldTableName)){
  40. var_dump('旧表不存在');
  41. return 0;
  42. }
  43. if(!Schema::hasTable($newTableName)){
  44. var_dump('新表不存在');
  45. return 0;
  46. }
  47. $oldData = DB::table($oldTableName)->orderBy('id','asc')->get();
  48. $bar = $this->output->createProgressBar(count($oldData));
  49. $bar->start();
  50. $startTime = time();
  51. $error = [];
  52. foreach ($oldData as $key => $value){
  53. $userId = $value->id;
  54. // 判断是否存在
  55. $exist = DB::table($newTableName)->where('id',$userId)->exists();
  56. if($exist){
  57. continue;
  58. }
  59. $newData =[
  60. 'id'=>$userId,
  61. 'nick_name'=>$value->name,
  62. 'avatar'=>$value->img,
  63. 'openid'=>$value->openid,
  64. 'total_score'=>$value->total_score,
  65. 'wallet'=>$value->wallet,
  66. 'real_name'=>$value->user_name,
  67. 'tel'=>$value->user_tel,
  68. 'unionid'=>$value->unionid,
  69. 'created_at' => strtotime($value->join_time),
  70. 'updated_at' => $value->updated_at,
  71. ];
  72. $res = DB::table($newTableName)->insert($newData);
  73. if(!$res){
  74. $error[] = ['id'=>$userId];
  75. break;
  76. }
  77. $bar->advance();
  78. }
  79. $bar->finish();
  80. var_dump([time()-$startTime]);
  81. var_dump($error);
  82. return 0;
  83. }
  84. }