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

160 lines
5.3 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 MigrateGoods extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'migrateData:goods';
  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_goods_new';
  37. $oldSpecTableName = 'ims_cjdc_spec_combination_new';
  38. $oldStoreTableName = 'ims_cjdc_store_new';
  39. $newTableName = 'lanzu_goods_new';
  40. $newBannerTableName = 'lanzu_goods_banners_new';
  41. // 判断表是否存在
  42. if(!Schema::hasTable($oldTableName)){
  43. var_dump('旧商品表不存在');
  44. return 0;
  45. }
  46. if(!Schema::hasTable($oldSpecTableName)){
  47. var_dump('旧规格表不存在');
  48. return 0;
  49. }
  50. if(!Schema::hasTable($oldStoreTableName)){
  51. var_dump('旧的店铺表不存在');
  52. return 0;
  53. }
  54. if(!Schema::hasTable($newTableName)){
  55. var_dump('新商品表不存在');
  56. return 0;
  57. }
  58. if(!Schema::hasTable($newBannerTableName)){
  59. var_dump('新商品的banners表不存在');
  60. return 0;
  61. }
  62. $oldData = DB::table($oldTableName)->orderBy('id','asc')->get();
  63. $oldStoreData = DB::table($oldStoreTableName)->pluck('market_id','id');
  64. $bar = $this->output->createProgressBar(count($oldData));
  65. $bar->start();
  66. $startTime = time();
  67. $total = 0;
  68. $error = [];
  69. foreach ($oldData as $value){
  70. $goodsId = $value->id;
  71. // 判断店铺是否存在,如果商品的店铺不在,则不存这个商品
  72. $storeExist = DB::table($oldStoreTableName)->where('id',$value->store_id)->exists();
  73. if(!$storeExist){
  74. continue;
  75. }
  76. $goodsData =[
  77. // 'id'=>$value->id,
  78. 'market_id' => $oldStoreData[$value->store_id],
  79. 'store_id'=>$value->store_id,
  80. 'on_sale'=> $value->is_show == 1 ? 1 : 0,
  81. 'sort'=>$value->num,
  82. 'price'=>$value->money,
  83. 'original_price'=> ($value->money2 < $value->money)?$value->money:$value->money2,
  84. 'vip_price'=>$value->vip_money,
  85. 'sales'=>$value->sales,
  86. 'start_num'=>$value->start_num,
  87. 'restrict_num'=>$value->restrict_num,
  88. 'is_infinite'=> $value->is_max == 1 ? 1 :0,
  89. 'inventory'=>$value->inventory,
  90. 'name'=>$value->name,
  91. 'cover_img'=>$value->logo,
  92. 'goods_unit'=>$value->good_unit,
  93. 'content'=> strip_tags($value->content),
  94. 'details'=> strip_tags($value->details),
  95. 'created_at' => time(),
  96. 'updated_at' => time(),
  97. ];
  98. $specs = DB::table($oldSpecTableName)->where('good_id',$goodsId)->get();
  99. if(count($specs) > 0){
  100. foreach($specs as $vs){
  101. $spec = ['spec_key'=>'净含量','spec_value'=>$vs->combination];
  102. if(strstr($vs->combination,'辣')){
  103. $spec['spec_key'] = '口味';
  104. }else if(strstr($vs->combination,'馅')){
  105. $spec['spec_key'] = '馅料';
  106. }else if(strstr($vs->combination,'mm')){
  107. $spec['spec_key'] = '尺寸';
  108. }
  109. $goodsData['spec'] = json_encode([$spec]);
  110. $newData = $goodsData;
  111. $newBanner = [
  112. 'type' =>1,
  113. 'path' =>$value->logo,
  114. 'created_at' =>time(),
  115. 'updated_at' =>time(),
  116. ];
  117. $id = DB::table($newTableName)->insertGetId($newData);
  118. if(!$id){
  119. $error[] = ['id'=>$goodsId];
  120. break;
  121. }
  122. $newBanner['goods_id'] = $id;
  123. DB::table($newBannerTableName)->insertGetId($newBanner);
  124. $total++;
  125. }
  126. }else{
  127. $newData = $goodsData;
  128. $res= $id = DB::table($newTableName)->insertGetId($newData);
  129. if(!$res){
  130. $error[] = ['id'=>$goodsId];
  131. break;
  132. }
  133. $newBanner['goods_id'] = $id;
  134. DB::table($newBannerTableName)->insertGetId($newBanner);
  135. $total++;
  136. }
  137. $bar->advance();
  138. }
  139. $bar->finish();
  140. var_dump([time()-$startTime]);
  141. var_dump($total);
  142. var_dump($error);
  143. return 0;
  144. }
  145. }