getOwnerByCodes($customer_ids); $owner_code_maps = []; if(count($owners) == 0)return $owner_code_maps; foreach ($owners as $owner) { $owner_code_maps[$owner['code']]= $owner['id']; $owners_id_maps[$owner['id']] = $owner['code']; } return $owner_code_maps; })(); // 重组OrderDetails $orderDetails_map = (function()use(&$orderHeaders,&$owner_code_maps,&$commodities_map){ $map = []; if(count($orderHeaders)==0)return $map; foreach ($orderHeaders as $orderHeader) { $Order_Details = $orderHeader->oracleDOCOrderDetails; $Order_Details->each(function($item)use(&$map,&$owner_code_maps,&$commodities_map){ if(!empty($item['customerid']) && !empty($item['sku'])){ $key = "owner_code_{$item['customerid']}_sku_{$item['sku']}"; $commodities_map[$key] = ['owner_code'=>$item['customerid'],'sku'=>$item['sku']]; } $key = "orderno_{$item['orderno']}_sku{$item['sku']}_each_{$item['qtyordered']}_location_{$item['location']}"; if(empty($map[$key]))$map[$key]=[]; $map[$key][] = [ 'code' => $item['orderno'], 'sku' => $item['sku'], 'owner_id'=>$owner_code_maps[$item['customerid']], 'amount' => $item['qtyordered'], 'location' => $item['location'] ]; }); } return $map; })(); $commodities = $commodityService->getCommoditiesByMap($commodities_map); $commodities_maps = (function()use($commodities,$owners_id_maps){ $map = []; if(count($commodities) == 0)return $map; foreach ($commodities as $commodity) { $owner_code = $owners_id_maps[$commodity['owner_id']]; $key ="owner_code_{$owner_code}_sku_{$commodity['sku']}"; $map[$key] = $commodity; } return $map; })(); $order_id_map = []; $orders_map = (function()use($order_nos,&$order_id_map){ $map = []; if(count($order_nos)==0)return $map; $orders = Order::query()->whereIn('code',$order_nos)->get(); $orders->each(function($item)use(&$map,&$order_id_map){ $map[$item['code']] = ['id'=> $item['id'], 'owner_id' => $item['owner_id']]; $order_id_map[$item['id']] = ['code'=> $item['code'], 'owner_id' => $item['owner_id']]; }); return $map; })(); $orderCommodities = OrderCommodity::query()->with('commodity','order')->whereHas('order',function($query)use($order_nos){ $query->whereIn('code',$order_nos); })->get(); $orderCommodities_map = $this->regroupOrderCommodities($orderCommodities); // 重组orderCommodities $inner_params = $this->filterInnerParams($orderDetails_map,$orderCommodities_map); $del_ids = $this->filterDeleteParams($orderDetails_map,$orderCommodities_map); if(count($inner_params)>0){ $inner_arr = array_chunk($inner_params,4000); foreach ($inner_arr as $item) { $created_params = $this->createByInnerParams($item,$orders_map,$commodities_maps,$owners_id_maps); $this->insert($created_params); } } if(count($del_ids)>0){ $orderCommodities = OrderCommodity::query()->whereIn('id',$del_ids)->get(); $this->batchDelete($orderCommodities); } } private function regroupOrderCommodities(&$orderCommodities) { $map = []; if(count($orderCommodities)==0)return $map; foreach ($orderCommodities as $orderCommodity) { $key = "orderno_{$orderCommodity['order']['code']}_sku{$orderCommodity['commodity']['sku']}_each_{$orderCommodity['amount']}_location_{$orderCommodity['location']}"; if(empty($map[$key]))$map[$key] =[]; $map[$key][] = [ 'id' =>$orderCommodity['id'], 'code' => $orderCommodity['order']['orderno'], 'sku' => $orderCommodity['commodity']['sku'], 'owner_id' => $orderCommodity['order']['owner_id'], 'amount' => $orderCommodity['amount'], 'location' => $orderCommodity['location'] ?? '' ]; } return $map; } private function filterInnerParams(&$orderDetails_map,&$orderCommodities_map) { $inner_params = []; if(count($orderDetails_map) == 0)return $inner_params; foreach ($orderDetails_map as $key=>&$map) { if(empty($orderCommodities_map[$key])){ foreach ($map as &$item) { $inner_params[] = $item; } }elseif(count($map)>count($orderCommodities_map[$key])){ foreach ($map as &$obj) { if($orderCommodities_map[$key]>0)array_shift($orderCommodities_map[$key]); elseif($orderCommodities_map[$key]==0)$inner_params[] = $obj; } } elseif(count($map)==count($orderCommodities_map[$key]))continue; unset($orderDetails_map[$key]); $map = null; } return $inner_params; } private function filterDeleteParams(&$orderDetails_map,&$orderCommodities_map) { $del_ids = []; if(count($orderDetails_map) == 0)return $del_ids; foreach ($orderCommodities_map as $key=>$map) { if(empty($orderDetails_map[$key])){ foreach ($map as &$item) { $del_ids[] = $item['id']; } }elseif(count($map)>count($orderDetails_map[$key])){ foreach ($map as $key1=>&$obj) { if(count($orderDetails_map[$key]) == 0){ $del_ids [] = $obj['id']; unset($map[$key1]); } else{ array_shift($orderDetails_map[$key]); } } } } return $del_ids; } private function createByInnerParams(&$inner_params,&$orders_map,&$commodities_maps,&$owner_id_maps) { $created_params = []; if(count($inner_params) == 0)return $created_params; $data = Carbon::now(); foreach ($inner_params as &$item) { $order = $orders_map[$item['code']] ?? false; if(!$order)continue; $owner_code = $owner_id_maps[$item['owner_id']]; $key = "owner_code_{$owner_code}_sku_{$item['sku']}"; $commodity = $commodities_maps[$key]; $created_params[] = [ 'order_id' =>$order['id'], 'commodity_id' =>$commodity['id'], 'amount' =>$item['amount'], 'location' =>$item['location'], 'created_at' =>$data, 'updated_at' => $data ]; } return $created_params; } public function batchDelete($orderCommodities) { if(count($orderCommodities) == 0)return true; try { $bool = OrderCommodity::destroy(data_get($orderCommodities, '*.id')); if($bool)app('LogService')->log(__METHOD__,__FUNCTION__,'批量删除 OrderCommodities'.' || '.count($orderCommodities).' || '.json_encode($orderCommodities)); else app('LogService')->log(__METHOD__,__FUNCTION__,'批量删除 OrderCommodities FAULT'.' || '.count($orderCommodities).' || '.json_encode($orderCommodities).' || '); return $bool; } catch (\Exception $e) { app('LogService')->log(__METHOD__,__FUNCTION__,'批量删除 OrderCommodities ERROR'.' || '.count($orderCommodities).' || '.json_encode($orderCommodities).' || '.json_encode($e->getMessage()).' || '.json_encode($e->getTraceAsString())); return false; } } public function insert($innerParams){ if(!$innerParams)return false; if(count($innerParams)==0)return false; try { $bool = OrderCommodity::query()->insert($innerParams); if ($bool) app('LogService')->log(__METHOD__, __FUNCTION__, '批量添加 OrderCommodities SUCCESS' . ' || ' . count($innerParams) . ' || ' . json_encode($innerParams)); else app('LogService')->log(__METHOD__, __FUNCTION__, '批量添加 OrderCommodities FAULT' . ' || ' . count($innerParams) . ' || ' . json_encode($innerParams)); return $bool; } catch (\Exception $e) { app('LogService')->log(__METHOD__, __FUNCTION__, '批量添加 OrderCommodities ERROR'. ' || ' . count($innerParams) . ' || ' . json_encode($innerParams).' || '.json_encode($e->getMessage()).' || '.json_encode($e->getTraceAsString())); return false; } } }