链街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. return 0;
  49. $oldData = DB::table($oldTableName)->orderBy('id','asc')->get();
  50. $oldStoreData = DB::table($oldStoreTableName)->pluck('market_id','id');
  51. $bar = $this->output->createProgressBar(count($oldData));
  52. $bar->start();
  53. $startTime = time();
  54. $error = [];
  55. foreach ($oldData as $key => $value){
  56. $cartId = $value->id;
  57. // 判断是否存在
  58. $exist = DB::table($newTableName)->where('id',$cartId)->exists();
  59. if($exist){
  60. continue;
  61. }
  62. $newData =[
  63. 'id'=>$cartId,
  64. 'market_id'=> isset($oldStoreData[$value->store_id])?$oldStoreData[$value->store_id]:0,
  65. 'store_id'=>$value->store_id,
  66. 'goods_id'=>$value->good_id,
  67. 'user_id'=>$value->user_id,
  68. 'num'=>$value->num,
  69. 'activity_type'=>1,
  70. 'created_at' => time(),
  71. 'updated_at' => time(),
  72. ];
  73. $res = DB::table($newTableName)->insert($newData);
  74. if(!$res){
  75. $error[] = ['id'=>$cartId];
  76. break;
  77. }
  78. $bar->advance();
  79. }
  80. $bar->finish();
  81. var_dump([time()-$startTime]);
  82. var_dump($error);
  83. return 0;
  84. }
  85. }