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

108 lines
3.4 KiB

5 years ago
5 years ago
5 years ago
  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 MigrateCouponReceive extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'migrateData:couponReceive';
  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. $oldTableName = 'ims_system_coupon_user_receive';
  37. $newTableName = 'lanzu_coupon_receive';
  38. $newCouponName = 'lanzu_coupon';
  39. // 判断表是否存在
  40. if(!Schema::hasTable($oldTableName)){
  41. var_dump('旧表不存在');
  42. return 0;
  43. }
  44. if(!Schema::hasTable($newTableName)){
  45. var_dump('新表不存在');
  46. return 0;
  47. }
  48. $oldData = DB::table($oldTableName)->get();
  49. $couponData = DB::table($newCouponName)->get();
  50. $bar = $this->output->createProgressBar(count($oldData));
  51. $bar->start();
  52. $newData = [];
  53. foreach ($oldData as $key => $value) {
  54. $newData[] = [
  55. 'id' => $value->id,
  56. 'coupon_id' => $value->system_coupon_user_id ?? 0,
  57. 'user_id' => $value->user_id ?? 0,
  58. 'order_main_id' => $value->order_main_id ?? 0,
  59. 'receive_time' => $value->receive_time ?? 0,
  60. 'number' => $value->number ?? 0,
  61. 'number_remain' => $value->number_remain ?? 0,
  62. 'status' => $value->status ?? 0,
  63. 'update_time' => $value->update_time ?? 0,
  64. 'receive_type' => $value->receive_type ?? 0,
  65. 'rebate_type' => $value->rebate_type ?? 0,
  66. 'send_user_id' => $value->send_user_id ?? 0,
  67. 'phone' => $value->phone ?? '',
  68. 'created_at' => $value->created_at ?? 0,
  69. 'updated_at' => time(),
  70. ];
  71. $bar->advance();
  72. }
  73. // insert new data to new table
  74. DB::table($newTableName)->insert($newData);
  75. // 优惠券添加的字段设置默认值
  76. foreach($couponData as $k => $v){
  77. $upData = [
  78. 'category' => empty($v->category)? 1 : $v->category,
  79. 'category_ids' => empty($v->category_ids)? '[]' : $v->category_ids,
  80. 'market_ids' => empty($v->market_ids)? '[]' : $v->market_ids,
  81. 'is_new_user' => empty($v->is_new_user)? 2 : $v->is_new_user,
  82. 'type' => empty($v->type)? 1 : $v->type,
  83. 'activity_available' => empty($v->activity_available)? '[]' : $v->activity_available,
  84. 'tags' => empty($v->tags)? '[]' : $v->tags,
  85. 'remark' => empty($v->remark)? '' : $v->remark,
  86. 'created_at' => empty($v->created_at)? time() : $v->created_at,
  87. 'updated_at' => time(),
  88. ];
  89. DB::table($newCouponName)->where('id',$v->id)->update($upData);
  90. }
  91. $bar->finish();
  92. return 0;
  93. }
  94. }