|
|
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\Schema;
class MigrateGoods extends Command{ /** * The name and signature of the console command. * * @var string */ protected $signature = 'migrateData:goods';
/** * 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_goods_new'; $oldSpecTableName = 'ims_cjdc_spec_combination_new'; $oldStoreTableName = 'ims_cjdc_store_new';
$newTableName = 'lanzu_goods_new'; $newBannerTableName = 'lanzu_goods_banners_new';
// 判断表是否存在
if(!Schema::hasTable($oldTableName)){ var_dump('旧商品表不存在'); return 0; } if(!Schema::hasTable($oldSpecTableName)){ var_dump('旧规格表不存在'); return 0; } if(!Schema::hasTable($oldStoreTableName)){ var_dump('旧的店铺表不存在'); return 0; } if(!Schema::hasTable($newTableName)){ var_dump('新商品表不存在'); return 0; } if(!Schema::hasTable($newBannerTableName)){ var_dump('新商品的banners表不存在'); 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(); $total = 0; $error = []; foreach ($oldData as $value){ $goodsId = $value->id; // 判断店铺是否存在,如果商品的店铺不在,则不存这个商品
$storeExist = DB::table($oldStoreTableName)->where('id',$value->store_id)->exists(); if(!$storeExist){ continue; } $goodsData =[ // 'id'=>$value->id,
'market_id' => $oldStoreData[$value->store_id], 'store_id'=>$value->store_id, 'on_sale'=> $value->is_show == 1 ? 1 : 0, 'sort'=>$value->num, 'price'=>$value->money, 'original_price'=> ($value->money2 < $value->money)?$value->money:$value->money2, 'vip_price'=>$value->vip_money, 'sales'=>$value->sales, 'start_num'=>$value->start_num, 'restrict_num'=>$value->restrict_num, 'is_infinite'=> $value->is_max == 1 ? 1 :0, 'inventory'=>$value->inventory, 'name'=>$value->name, 'cover_img'=>$value->logo, 'goods_unit'=>$value->good_unit, 'content'=> strip_tags($value->content), 'details'=> strip_tags($value->details),
'created_at' => time(), 'updated_at' => time(), ]; $specs = DB::table($oldSpecTableName)->where('good_id',$goodsId)->get(); if(count($specs) > 0){ foreach($specs as $vs){ $spec = ['spec_key'=>'净含量','spec_value'=>$vs->combination]; if(strstr($vs->combination,'辣')){ $spec['spec_key'] = '口味'; }else if(strstr($vs->combination,'馅')){ $spec['spec_key'] = '馅料'; }else if(strstr($vs->combination,'mm')){ $spec['spec_key'] = '尺寸'; } $goodsData['spec'] = json_encode([$spec]); $newData = $goodsData; $newBanner = [ 'type' =>1, 'path' =>$value->logo, 'created_at' =>time(), 'updated_at' =>time(), ]; $id = DB::table($newTableName)->insertGetId($newData); if(!$id){ $error[] = ['id'=>$goodsId]; break; } $newBanner['goods_id'] = $id; DB::table($newBannerTableName)->insertGetId($newBanner); $total++; } }else{ $newData = $goodsData; $res= $id = DB::table($newTableName)->insertGetId($newData); if(!$res){ $error[] = ['id'=>$goodsId]; break; } $newBanner['goods_id'] = $id; DB::table($newBannerTableName)->insertGetId($newBanner); $total++; } $bar->advance(); }
$bar->finish(); var_dump([time()-$startTime]); var_dump($total); var_dump($error); return 0; }}
|