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.
|
|
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\Schema;
class MigrateUserCollection extends Command{ /** * The name and signature of the console command. * * @var string */ protected $signature = 'migrateData:userCollection';
/** * The console command description. * * @var string */ protected $description = 'Command 迁移用户收藏店铺数据';
/** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); }
/** * Execute the console command. * * @return int */ public function handle() { $oldTableName = 'ims_cjdc_collection'; $oldStoreTableName = 'ims_cjdc_store'; $newTableName = 'lanzu_user_collection'; // 判断表是否存在
if(!Schema::hasTable($oldTableName)){ var_dump('旧表不存在'); return 0; } if(!Schema::hasTable($newTableName)){ var_dump('新表不存在'); return 0; }
$oldData = DB::table($oldTableName)->orderBy('id','asc')->get(); $oldStoreData = DB::table($oldStoreTableName)->pluck('market_id','id'); $bar = $this->output->createProgressBar(count($oldData)); $bar->start(); $startTime = time(); $error = []; foreach ($oldData as $key => $value){ $collectionId = $value->id; // 判断是否存在
$exist = DB::table($newTableName)->where('id',$collectionId)->exists(); if($exist){ continue; } // 店铺存在
if(isset($oldStoreData[$value->store_id])){ $newData =[ 'id'=>$collectionId, 'store_id'=>$value->store_id, 'user_id'=>$value->user_id, 'market_id'=> isset($oldStoreData[$value->store_id])?$oldStoreData[$value->store_id]:0, 'created_at' => $value->time, 'updated_at' => time(), ]; $res = DB::table($newTableName)->insert($newData); if(!$res){ $error[] = ['id'=>$collectionId]; break; } } $bar->advance(); } $bar->finish(); var_dump([time()-$startTime]); var_dump($error); return 0; }}
|