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

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class MigrateUserAddr extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'migrateData:userAddr';
/**
* The console command description.
*
* @var string
*/
protected $description = 'migrate 迁移用户地址数据';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$oldTableName = 'ims_cjdc_useradd';
$newTableName = 'lanzu_user_address';
// 判断表是否存在
if(!Schema::hasTable($oldTableName)){
var_dump('旧表不存在');
return 0;
}
if(!Schema::hasTable($newTableName)){
var_dump('新表不存在');
return 0;
}
$oldData = DB::table($oldTableName)->get();
$bar = $this->output->createProgressBar(count($oldData));
$bar->start();
$newData = [];
foreach ($oldData as $key => $value) {
$addressArr = explode('|',$value->address);
$address = isset($addressArr[0])?$addressArr[0]:'';
$doorplate = isset($addressArr[1])?$addressArr[1]:'';
$newData[] = [
'id' => $value->id,
'user_id' => $value->user_id ?? 0,
'user_name' => $value->user_name == 'undefined'? '': $value->user_name,
'address' => $address,
'gender' => $value->sex ?? 0,
'lat' => $value->lat ?? '',
'lng' => $value->lng ?? '',
'tel' => $value->tel == 'undefined'? '': $value->tel,
'doorplate' => $doorplate,
'is_default' => $value->is_default ?? 0,
'tags' => json_encode([]),
'created_at' => time(),
'updated_at' => time(),
];
$bar->advance();
}
// insert new data to new table
DB::table('lanzu_user_address')->insert($newData);
$bar->finish();
return 0;
}
}