where('id',1)->get(); $bar = $this->output->createProgressBar(count($orderMain)); $bar->start(); $mainIds = '已存在的订单id:'; $childIds = '已存在的订单id:'; foreach ($orderMain as $k => $main) { DB::beginTransaction(); try { // 查询记录是否存在 $exist_main = DB::table($newOrderMain)->where('id',$main->id)->exists(); if($exist_main){ $mainIds .= ','.$main->id; continue; } // 主订单 $dataMain = [ 'id' => $main->id, 'market_id' => $main->market_id ?? 0, 'order_num' => $main->order_num ?? '', 'global_order_id' => $main->global_order_id ? $main->global_order_id : time().mt_rand(100000,999999), 'user_id' => $main->user_id ?? 0, 'pay_type' => $main->pay_type ?? 0, 'type' => $main->type ?? 0, 'order_type' => $main->order_type ?? 0, 'shipping_type' => $main->dada_status == 0 ? 1 : 2, 'money' => $main->money ?? 0, 'total_money' => $main->total_money ?? 0, 'services_money' => 0, 'coupon_money' => $main->yhq_money2 ?? 0, 'delivery_money' => $main->dada_fee ?? 0, 'state' => $main->state ?? 0, 'pay_time' => $main->pay_time ? strtotime($main->pay_time) : 0, 'receive_time' => $main->jd_time ? strtotime($main->jd_time) : 0, 'delivery_time' => 0, 'complete_time' => $main->complete_time ? strtotime($main->complete_time) : 0, 'cancel_time' => $main->cancel_time ? strtotime($main->cancel_time) : 0, 'refund_time' => $main->refund_time ?? 0, 'tel' => $main->tel ?? '', 'address' => $main->address ?? '', 'lat' => $main->lat ?? '', 'lng' => $main->lng ?? '', 'name' => $main->name ?? '', 'print_num' => $main->print_num ?? 0, 'plat' => $main->plat ?? 0, 'refuse_refund_note' => $main->refuse_refund_note ?? '', 'delivery_time_note' => $main->delivery_time ?? '', 'total_refund_note' => $main->total_refund_note ?? '', 'note' => $main->note ?? '', 'shipping_name' => $main->shipping_name ?? '', 'horseman_id' => $main->horseman_id ?? 0, 'delivery_distance' => $main->delivery_distance ?? 0, 'created_at' => $main->time_add ?? 0, 'updated_at' => time(), ]; $newMainId = DB::table($newOrderMain)->insertGetId($dataMain); // 子订单 $orderChildren = DB::table($oldOrder)->where(['order_main_id' => $main->id])->get(); foreach ($orderChildren as $kChild => $child) { // 查询记录是否存在 $exist_main = DB::table($newOrder)->where('id',$child->id)->exists(); if($exist_main){ $childIds .= ','.$child->id; DB::rollBack(); continue; } // 子订单 $dataChild = [ 'id' => $child->id, 'order_main_id' => $dataMain['global_order_id'] ?? 0, 'user_id' => $child->user_id ?? 0, 'store_id' => $child->store_id ?? 0, 'refund_time' => 0, 'order_num' => $child->order_num ?? '', 'money' => $child->money ?? 0, 'oid' => $child->oid ?? 0, 'refuse_refund_note' => '', 'refund_note' => '', 'note' => $child->note ?? '', 'created_at' => $child->time_add ? strtotime($child->time_add) : 0, 'updated_at' => time(), ]; if($child->state == 9){ $dataChild['status'] = 3; }else if($child->state == 8){ $dataChild['status'] = 2; }else{ $dataChild['status'] = 1; } $newChildId = DB::table($newOrder)->insertGetId($dataChild); // 订单商品 $orderGoods = DB::table($oldOrderGood)->where(['order_id' => $child->id])->get(); if(count($orderGoods) > 0){ $dataGoods = []; foreach ($orderGoods as $kGoods => $goods) { // 订单商品 $dataGoods[] = [ 'order_id' => $newChildId ?? 0, 'goods_id' => $goods->good_id ?? 0, 'number' => $goods->number ?? 0, 'status' => $dataChild['status'], 'price' => $goods->money ?? 0, 'original_price' => $goods->money2 ?? 0, 'vip_price' => $goods->vip_money ?? 0, 'name' => $goods->name ?? '', 'goods_unit' => $goods->good_unit ?? '', 'cover_img' => $goods->img ?? '', 'spec' => json_encode([]), 'refund_time' => 0, 'created_at' => time(), 'updated_at' => time(), 'refuse_refund_note' => '', 'note' => '', ]; } DB::table($newOrderGood)->insert($dataGoods); } } $bar->advance(); DB::commit(); } catch (\Exception $e) { $this->info(json_encode(['order_id' => $main->id, 'exception' => $e->getMessage()])); DB::rollBack(); } } var_dump($mainIds); var_dump($childIds); $bar->finish(); return 0; } }