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

188 lines
7.6 KiB

  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\DB;
  5. class MigrateOrder extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'migrateData:order';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = 'migrate 迁移订单数据';
  19. /**
  20. * Create a new command instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. }
  28. /**
  29. * Execute the console command.
  30. *
  31. * @return int
  32. */
  33. public function handle()
  34. {
  35. $oldOrderMain = 'ims_cjdc_order_main';
  36. $oldOrder = 'ims_cjdc_order';
  37. $oldOrderGood = 'ims_cjdc_order_goods';
  38. $newOrderMain = 'lanzu_order_main';
  39. $newOrder = 'lanzu_order';
  40. $newOrderGood = 'lanzu_order_goods';
  41. // main order
  42. $orderMain = DB::table($oldOrderMain)->where('id',1)->get();
  43. $bar = $this->output->createProgressBar(count($orderMain));
  44. $bar->start();
  45. $mainIds = '已存在的订单id:';
  46. $childIds = '已存在的订单id:';
  47. foreach ($orderMain as $k => $main) {
  48. DB::beginTransaction();
  49. try {
  50. // 查询记录是否存在
  51. $exist_main = DB::table($newOrderMain)->where('id',$main->id)->exists();
  52. if($exist_main){
  53. $mainIds .= ','.$main->id;
  54. continue;
  55. }
  56. // 主订单
  57. $dataMain = [
  58. 'id' => $main->id,
  59. 'market_id' => $main->market_id ?? 0,
  60. 'order_num' => $main->order_num ?? '',
  61. 'global_order_id' => $main->global_order_id ? $main->global_order_id : time().mt_rand(100000,999999),
  62. 'user_id' => $main->user_id ?? 0,
  63. 'pay_type' => $main->pay_type ?? 0,
  64. 'type' => $main->type ?? 0,
  65. 'order_type' => $main->order_type ?? 0,
  66. 'shipping_type' => $main->dada_status == 0 ? 1 : 2,
  67. 'money' => $main->money ?? 0,
  68. 'total_money' => $main->total_money ?? 0,
  69. 'services_money' => 0,
  70. 'coupon_money' => $main->yhq_money2 ?? 0,
  71. 'delivery_money' => $main->dada_fee ?? 0,
  72. 'state' => $main->state ?? 0,
  73. 'pay_time' => $main->pay_time ? strtotime($main->pay_time) : 0,
  74. 'receive_time' => $main->jd_time ? strtotime($main->jd_time) : 0,
  75. 'delivery_time' => 0,
  76. 'complete_time' => $main->complete_time ? strtotime($main->complete_time) : 0,
  77. 'cancel_time' => $main->cancel_time ? strtotime($main->cancel_time) : 0,
  78. 'refund_time' => $main->refund_time ?? 0,
  79. 'tel' => $main->tel ?? '',
  80. 'address' => $main->address ?? '',
  81. 'lat' => $main->lat ?? '',
  82. 'lng' => $main->lng ?? '',
  83. 'name' => $main->name ?? '',
  84. 'print_num' => $main->print_num ?? 0,
  85. 'plat' => $main->plat ?? 0,
  86. 'refuse_refund_note' => $main->refuse_refund_note ?? '',
  87. 'delivery_time_note' => $main->delivery_time ?? '',
  88. 'total_refund_note' => $main->total_refund_note ?? '',
  89. 'note' => $main->note ?? '',
  90. 'shipping_name' => $main->shipping_name ?? '',
  91. 'horseman_id' => $main->horseman_id ?? 0,
  92. 'delivery_distance' => $main->delivery_distance ?? 0,
  93. 'created_at' => $main->time_add ?? 0,
  94. 'updated_at' => time(),
  95. ];
  96. $newMainId = DB::table($newOrderMain)->insertGetId($dataMain);
  97. // 子订单
  98. $orderChildren = DB::table($oldOrder)->where(['order_main_id' => $main->id])->get();
  99. foreach ($orderChildren as $kChild => $child) {
  100. // 查询记录是否存在
  101. $exist_main = DB::table($newOrder)->where('id',$child->id)->exists();
  102. if($exist_main){
  103. $childIds .= ','.$child->id;
  104. DB::rollBack();
  105. continue;
  106. }
  107. // 子订单
  108. $dataChild = [
  109. 'id' => $child->id,
  110. 'order_main_id' => $dataMain['global_order_id'] ?? 0,
  111. 'user_id' => $child->user_id ?? 0,
  112. 'store_id' => $child->store_id ?? 0,
  113. 'refund_time' => 0,
  114. 'order_num' => $child->order_num ?? '',
  115. 'money' => $child->money ?? 0,
  116. 'oid' => $child->oid ?? 0,
  117. 'refuse_refund_note' => '',
  118. 'refund_note' => '',
  119. 'note' => $child->note ?? '',
  120. 'created_at' => $child->time_add ? strtotime($child->time_add) : 0,
  121. 'updated_at' => time(),
  122. ];
  123. if($child->state == 9){
  124. $dataChild['status'] = 3;
  125. }else if($child->state == 8){
  126. $dataChild['status'] = 2;
  127. }else{
  128. $dataChild['status'] = 1;
  129. }
  130. $newChildId = DB::table($newOrder)->insertGetId($dataChild);
  131. // 订单商品
  132. $orderGoods = DB::table($oldOrderGood)->where(['order_id' => $child->id])->get();
  133. if(count($orderGoods) > 0){
  134. $dataGoods = [];
  135. foreach ($orderGoods as $kGoods => $goods) {
  136. // 订单商品
  137. $dataGoods[] = [
  138. 'order_id' => $newChildId ?? 0,
  139. 'goods_id' => $goods->good_id ?? 0,
  140. 'number' => $goods->number ?? 0,
  141. 'status' => $dataChild['status'],
  142. 'price' => $goods->money ?? 0,
  143. 'original_price' => $goods->money2 ?? 0,
  144. 'vip_price' => $goods->vip_money ?? 0,
  145. 'name' => $goods->name ?? '',
  146. 'goods_unit' => $goods->good_unit ?? '',
  147. 'cover_img' => $goods->img ?? '',
  148. 'spec' => json_encode([]),
  149. 'refund_time' => 0,
  150. 'created_at' => time(),
  151. 'updated_at' => time(),
  152. 'refuse_refund_note' => '',
  153. 'note' => '',
  154. ];
  155. }
  156. DB::table($newOrderGood)->insert($dataGoods);
  157. }
  158. }
  159. $bar->advance();
  160. DB::commit();
  161. } catch (\Exception $e) {
  162. $this->info(json_encode(['order_id' => $main->id, 'exception' => $e->getMessage()]));
  163. DB::rollBack();
  164. }
  165. }
  166. var_dump($mainIds);
  167. var_dump($childIds);
  168. $bar->finish();
  169. return 0;
  170. }
  171. }