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; } } // TODO 根据传入的$orderHeaders 同步订单 public function syncOrderCommodity(&$orderHeaders) { /** * @var OwnerService $ownerService * @var CommodityService $commodityService * @var DataHandlerService $dataHandlerService */ $ownerService = app('OwnerService'); $commodityService = app('CommodityService'); $dataHandlerService = app('DataHandlerService'); $orderNos = data_get($orderHeaders,'*.orderno'); $owner_codes = [];$sku_codes = []; $count = 0; foreach ($orderHeaders as $orderHeader) { $actAllocationDetails = $orderHeader->actAllocationDetails; foreach ($actAllocationDetails as $actAllocationDetail) { $owner_codes[$actAllocationDetail['customerid']] = $actAllocationDetail['customerid']; $sku_codes[$actAllocationDetail['sku']] = $actAllocationDetail['sku']; $count++; } } $owners = $ownerService->getOwnerByCodes($owner_codes); $owner_id_maps = $dataHandlerService->dataHeader(['id'],$owners); $commodities = $commodityService->get_(data_get($owners,'*.id'),$sku_codes,[],true); $commodity_map = []; foreach ($commodities as $commodity) { $owner = $dataHandlerService->getKeyValue(['id'=>$commodity->owner_id],$owner_id_maps); $key = "_owner_code_{$owner['code']}_sku_{$commodity['sku']}"; $commodity_map[$key] = $commodity; } $orderCommodities = OrderCommodity::query()->with(['order','commodity'])->whereIn('order_id',function ($query)use($orderNos){ $query->from('orders')->select('id')->whereIn('code',$orderNos); })->get(); $orders = Order::query()->with('packages.commodities')->whereIn('code',$orderNos)->get(); $order_code_map = $dataHandlerService->dataHeader(['code'],$orders); $ActAllocationDetail_maps = $this->getRegroupActAllocationDetails($orderHeaders); $orderCommodity_maps = $this->getRegroupOrderCommodities($orderCommodities,$owner_id_maps); $delete_ids = $this->getDeleteIds($ActAllocationDetail_maps,$orderCommodity_maps); $this->filterHasExist($ActAllocationDetail_maps,$orderCommodity_maps); $create_params = $this->getCreateParams($ActAllocationDetail_maps,$orderCommodity_maps); $inner_params = $this->getInnerParamsByParams($create_params,$order_code_map,$commodity_map); if(count($inner_params)>0){ $inner_params = array_chunk($inner_params,4000); foreach ($inner_params as $inner_param) { $this->insert($inner_param); } } if(count($delete_ids)==0)return; OrderCommodity::query()->whereIn('id',$delete_ids)->delete(); app('LogService')->log(__METHOD__,__FUNCTION__,"delete OrderCommodity ".json_encode($delete_ids)); } // TODO 将拉取的OrderHeader 重组为符合当前基准的数组 public function getRegroupActAllocationDetails(&$orderHeaders) { $map = []; if($orderHeaders->count()==0) return $map; $orderHeaders->each(function($orderHeader)use(&$map){ $orderHeader->actAllocationDetails->each(function ($details)use(&$map){ $key = "order_{$details['orderno']}_owner_code_{$details['customerid']}_sku_{$details['sku']}_location_{$details['location']}"; if(empty($map[$key]))$map[$key]=[]; $map[$key][] = [ 'code' => $details['orderno'], 'sku' => $details['sku'], 'owner_code'=> $details['customerid'], 'amount' => $details['qty_each'], 'location' => $details['location'] ]; }); }); return $map; } // TODO 过滤已有 public function filterHasExist(&$ActAllocationDetail_maps,&$orderCommodity_maps) { foreach ($ActAllocationDetail_maps as $key=>$actAllocationDetail_map) { if(isset($orderCommodity_maps[$key]))unset($ActAllocationDetail_maps[$key]); } } // TODO 将传入OrderCommodities 重组为符合当前基准的数组 public function getRegroupOrderCommodities(&$orderCommodities,$owner_id_maps) { /** @var DataHandlerService $dataHandlerService */ $dataHandlerService = app(DataHandlerService::class); $map = []; if($orderCommodities->count() == 0)return $map; $orderCommodities->each(function($orderCommodity)use(&$map,$owner_id_maps,$dataHandlerService){ $order = $orderCommodity->order; $owner = $dataHandlerService->getKeyValue(['id'=>$order->owner_id ?? ''],$owner_id_maps); $key = "order_{$order->code}_owner_code_{$owner->code}_sku_{$orderCommodity->commodity->sku}_location_{$orderCommodity->location}"; if(empty($map[$key]))$map[$key]=[]; $map[$key][] =[ 'id' => $orderCommodity->id, 'code' => $orderCommodity->order->code ?? '', 'sku' => $orderCommodity->commodity->sku, 'owner_code' => $owner->code ?? '', 'amount' => $orderCommodity->amount, 'location' => $orderCommodity->location, ]; }); return $map; } // TODO 返回修改删除的 public function getDeleteIds(&$ActAllocationDetail_maps,&$orderCommodity_maps) { $ids = []; foreach ($orderCommodity_maps as $key=>$orderCommodity_map) { if(isset($ActAllocationDetail_maps[$key]))continue; foreach ($orderCommodity_map as $key1=>$orderCommodity) { $ids[] = $orderCommodity['id']; unset($orderCommodity_map[$key1]); } } return $ids; } // TODO 返回创建的数组 public function getCreateParams(&$ActAllocationDetail_maps,&$orderCommodity_maps) { $params = []; foreach ($ActAllocationDetail_maps as $key=>$actAllocationDetail_map) { if(isset($orderCommodity_maps[$key]))continue; foreach ($actAllocationDetail_map as $actAllocationDetail) { $params[] = $actAllocationDetail; } } return $params; } // TODO 根据创建数组返回可以使用的插入数组 public function getInnerParamsByParams(&$create_params,&$order_code_map,&$commodity_map) { /** @var DataHandlerService $dataHandlerService */ $dataHandlerService = app(DataHandlerService::class); $inner_params = []; $date = (string) Carbon::now(); foreach ($create_params as $item) { $order = $dataHandlerService->getKeyValue(['code'=>$item['code']],$order_code_map); $key = "_owner_code_{$item['owner_code']}_sku_{$item['sku']}"; $commodity = $commodity_map[$key] ?? null; $inner_params[] = [ 'order_id' => $order['id'] ?? '', 'commodity_id' => $commodity['id'] ?? '', 'amount' => $item['amount'], 'location' => $item['location'], 'created_at' => $date, 'updated_at' => $date ]; } return $inner_params; } }