|
|
@@ -0,0 +1,226 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Services;
|
|
|
+
|
|
|
+use App\Order;
|
|
|
+use App\OrderCommodity;
|
|
|
+use Carbon\Carbon;
|
|
|
+
|
|
|
+Class OrderCommodityService
|
|
|
+{
|
|
|
+ public function syncOrderCommodities(&$orderHeaders)
|
|
|
+ {
|
|
|
+ /**
|
|
|
+ * @var CommodityService $commodityService
|
|
|
+ * @var OwnerService $ownerService
|
|
|
+ */
|
|
|
+ $commodityService = app('CommodityService');
|
|
|
+ $ownerService = app('OwnerService');
|
|
|
+ if(count($orderHeaders) == 0)return ;
|
|
|
+ $order_nos = array_unique(data_get($orderHeaders,'*.orderno'));
|
|
|
+ $commodities_map = [];$orderDetails_map = [];$owners_id_maps = [];
|
|
|
+ $owner_code_maps = (function()use(&$ownerService,&$orderHeaders,&$owners_id_maps){
|
|
|
+ $customer_ids= array_unique(data_get($orderHeaders,'*.customerid'));
|
|
|
+ $owners = $ownerService->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_map = (function()use(&$orderHeaders,&$map,&$orderDetails_map,&$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,&$orderDetails_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($orderDetails_map[$key]))$orderDetails_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;
|
|
|
+ })();
|
|
|
+ $order_commodities_maps = (function()use($order_nos,$owner_code_maps,&$owners_id_maps,$orders_map,&$order_id_map){
|
|
|
+ $orderCommodities = OrderCommodity::query()->with('commodity','order')->whereHas('order',function($query)use($order_nos){
|
|
|
+ $query->whereIn('code',$order_nos);
|
|
|
+ })->get();
|
|
|
+ $map = [];
|
|
|
+ if(count($orderCommodities)==0)return $map;
|
|
|
+ foreach ($orderCommodities as $commodity) {
|
|
|
+ $order = $order_id_map[$commodity['order_id']];
|
|
|
+ $owner_code = $owners_id_maps[$order['owner_id']];
|
|
|
+ $key = "owner_code_{$owner_code}_sku_{$commodity['sku']}";
|
|
|
+ $map[$key] = $commodity;
|
|
|
+ }
|
|
|
+ return $map;
|
|
|
+ })();
|
|
|
+ $orderCommodities_map = $this->regroupOrderCommodities($order_commodities_maps);
|
|
|
+ $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']['orderno']}_sku{$orderCommodity['commodity']['sku']}_each_{$orderCommodity['amount']}_location_{$orderCommodity['location']}";
|
|
|
+ if(empty($map[$key]))continue;
|
|
|
+ $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(count($map)==count($orderDetails_map[$key]))continue;
|
|
|
+ if(empty($orderDetails_map[$key])){
|
|
|
+ foreach ($map as &$item) {
|
|
|
+ $del_ids[] = $item['id'];
|
|
|
+ }
|
|
|
+ }elseif(count($map)>count($orderDetails_map[$key])){
|
|
|
+ foreach ($map as &$obj) {
|
|
|
+ if(count($orderDetails_map[$key]) == 0)$del_ids[] = array_shift($orderCommodities_map[$map])['id'];
|
|
|
+ else array_shift($orderDetails_map[$key]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $del_ids;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function createByInnerParams(&$inner_params,&$orders_map,&$commodities_maps,&$owner_id_maps)
|
|
|
+ {
|
|
|
+ if(count($inner_params) == 0)return;
|
|
|
+ $created_params = [];
|
|
|
+ $data = Carbon::now();
|
|
|
+ foreach ($inner_params as &$item) {
|
|
|
+ $order = $orders_map[$item['code']];
|
|
|
+ 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::query()->where('id', data_get($orderCommodities, '*.id'))->delete();
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|