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

139 lines
4.6 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 MigrateCoupon extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'migrateData:coupon';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'migrate 迁移优惠券数据';
  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. $oldSetTableName = 'ims_system_coupon_setting';
  37. $oldTableName = 'ims_system_coupon_user';
  38. $oldTypeTableName = 'ims_system_coupon_user_receivetype';
  39. $newSetTableName = 'lanzu_coupon_setting';
  40. $newTableName = 'lanzu_coupon';
  41. $newTypeTableName = 'lanzu_coupon_receive_type';
  42. // 判断表是否存在
  43. if(!Schema::hasTable($oldTableName)){
  44. var_dump('旧表不存在');
  45. return 0;
  46. }
  47. if(!Schema::hasTable($newTableName)){
  48. var_dump('新表不存在');
  49. return 0;
  50. }
  51. $oldSetData = DB::table($oldSetTableName)->get();
  52. $oldData = DB::table($oldTableName)->get();
  53. $oldTypeData = DB::table($oldTypeTableName)->get();
  54. $bar = $this->output->createProgressBar(count($oldData));
  55. $bar->start();
  56. $newSetData = [];
  57. $newData = [];
  58. $newTypeData = [];
  59. foreach ($oldSetData as $key => $value) {
  60. $newSetData[] = [
  61. 'id' => $value->id,
  62. 'name' => $value->name ?? 0,
  63. 'category' => $value->category ?? 0,
  64. 'value' => $value->value ?? 0,
  65. 'desc' => $value->desc ?? '',
  66. 'sort' => $value->sort ?? 0,
  67. 'status' => $value->status ?? 0,
  68. 'created_at' => time(),
  69. 'updated_at' => time(),
  70. ];
  71. }
  72. foreach ($oldData as $key => $value) {
  73. $newData[] = [
  74. 'id' => $value->id,
  75. 'status' => $value->status ?? 0,
  76. 'active_type' => $value->active_type ?? 0,
  77. 'type' => $value->type ?? 0,
  78. 'title' => $value->title ?? '',
  79. 'introduce' => $value->introduce ?? '',
  80. 'start_time' => $value->start_time ?? 0,
  81. 'end_time' => $value->end_time,
  82. 'full_amount' => $value->full_amount,
  83. 'discounts' => $value->discounts ?? 0,
  84. 'is_new_user' => $value->is_new_user ?? 0,
  85. 'inventory' => $value->inventory,
  86. 'inventory_use' => $value->inventory_use ?? 0,
  87. 'market_ids' => json_encode([]),
  88. 'category_ids' => json_encode([]),
  89. 'category' => $value->category ?? 0,
  90. 'discount_type' => $value->discount_type ?? 0,
  91. 'activity_available' => json_encode([]),
  92. 'weigh' => $value->weigh ?? 0,
  93. 'usable_number' => $value->usable_number ?? 0,
  94. 'usable_start_time' => $value->usable_start_time ?? 0,
  95. 'usable_end_time' => $value->usable_end_time ?? 0,
  96. 'remark' => $value->remark ?? 0,
  97. 'add_user_id' => $value->add_user_id ?? 0,
  98. 'update_user_id' => $value->update_user_id ?? 0,
  99. 'tags' => json_encode([]),
  100. 'created_at' => $value->addtime ?? 0,
  101. 'updated_at' => time(),
  102. 'deleted_at' => $value->status == -1 ? time() : $value->deleted_at ?? null,
  103. ];
  104. }
  105. foreach ($oldTypeData as $key => $value) {
  106. $newTypeData[] = [
  107. 'id' => $value->id,
  108. 'coupon_id' => $value->system_coupon_user_id ?? 0,
  109. 'receive_type' => $value->receive_type ?? 0,
  110. 'one_receive_number' => $value->one_receive_number ?? 0,
  111. 'created_at' => $value->add_time ?? 0,
  112. 'updated_at' => time(),
  113. ];
  114. $bar->advance();
  115. }
  116. // insert new data to new table
  117. DB::table($newSetTableName)->insert($newSetData);
  118. DB::table($newTableName)->insert($newData);
  119. DB::table($newTypeTableName)->insert($newTypeData);
  120. $bar->finish();
  121. return 0;
  122. }
  123. }