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

91 lines
2.3 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 MigrateUserAddr extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'migrateData:userAddr';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'migrate 迁移用户地址数据';
  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_useradd';
  37. $newTableName = 'lanzu_user_address';
  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)->get();
  48. $bar = $this->output->createProgressBar(count($oldData));
  49. $bar->start();
  50. $newData = [];
  51. foreach ($oldData as $key => $value) {
  52. $addressArr = explode('|',$value->address);
  53. $address = isset($addressArr[0])?$addressArr[0]:'';
  54. $doorplate = isset($addressArr[1])?$addressArr[1]:'';
  55. $newData[] = [
  56. 'id' => $value->id,
  57. 'user_id' => $value->user_id ?? 0,
  58. 'user_name' => $value->user_name == 'undefined'? '': $value->user_name,
  59. 'address' => $address,
  60. 'gender' => $value->sex ?? 0,
  61. 'lat' => $value->lat ?? '',
  62. 'lng' => $value->lng ?? '',
  63. 'tel' => $value->tel == 'undefined'? '': $value->tel,
  64. 'doorplate' => $doorplate,
  65. 'is_default' => $value->is_default ?? 0,
  66. 'tags' => json_encode([]),
  67. 'created_at' => time(),
  68. 'updated_at' => time(),
  69. ];
  70. $bar->advance();
  71. }
  72. // insert new data to new table
  73. DB::table('lanzu_user_address')->insert($newData);
  74. $bar->finish();
  75. return 0;
  76. }
  77. }