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

93 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 MigrateShoppingCart extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'migrateData:shoppingCart';
  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_shopcar';
  37. $oldStoreTableName = 'ims_cjdc_store';
  38. $newTableName = 'lanzu_shopping_cart';
  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. $cartId = $value->id;
  56. // 判断是否存在
  57. $exist = DB::table($newTableName)->where('id',$cartId)->exists();
  58. if($exist){
  59. continue;
  60. }
  61. $newData =[
  62. 'id'=>$cartId,
  63. 'market_id'=> isset($oldStoreData[$value->store_id])?$oldStoreData[$value->store_id]:0,
  64. 'store_id'=>$value->store_id,
  65. 'goods_id'=>$value->good_id,
  66. 'user_id'=>$value->user_id,
  67. 'num'=>$value->num,
  68. 'activity_type'=>1,
  69. 'created_at' => time(),
  70. 'updated_at' => time(),
  71. ];
  72. $res = DB::table($newTableName)->insert($newData);
  73. if(!$res){
  74. $error[] = ['id'=>$cartId];
  75. break;
  76. }
  77. $bar->advance();
  78. }
  79. $bar->finish();
  80. var_dump([time()-$startTime]);
  81. var_dump($error);
  82. return 0;
  83. }
  84. }