| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- <?php
- namespace App\Services;
- use App\Order;
- use App\OrderCommodity;
- use App\Services\common\DataHandlerService;
- 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 = [];$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
- $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 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;
- }
- }
- public function syncOrderCommodity($orderHeaders)
- {
- /**
- * @var OwnerService $ownerService
- * @var CommodityService $commodityService
- * @var DataHandlerService $dataHandlerService
- */
- $ownerService = app('OwnerService');
- $commodityService = app('CommodityService');
- $dataHandlerService = app('DataHandlerService');
- $map = [];$owner_codes = [];
- foreach ($orderHeaders as $orderHeader) {
- $actAllocationDetails = $orderHeader->actAllocationDetails;
- foreach ($actAllocationDetails as $actAllocationDetail) {
- $key = "owner_code_{$actAllocationDetail['customerid']}_'sku'";
- $owner_codes[$actAllocationDetail['customerid']] = $actAllocationDetail['customerid'];
- $map[$key] = [
- 'owner_code'=>$actAllocationDetail['customerid'],
- 'sku'=>$actAllocationDetail['sku']
- ];
- }
- }
- $owners = $ownerService->getOwnerByCodes($owner_codes);
- $owner_map = $dataHandlerService->dataHeader(['id'],$owners);
- $commodities = $commodityService->getParamsByBasSku($map);
- $commodity_map = [];
- foreach ($commodities as $commodity) {
- $owner = $dataHandlerService->getKeyValue(['id'=>$commodity->id],$owner_map);
- $key = "_owner_code_{$owner['code']}_sku_{$commodity['sku']}";
- $commodity_map[$key] = $commodity;
- }
- }
- 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;
- }
- 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;
- }
- 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;
- }
- public function getCreateParams(&$ActAllocationDetail_maps,&$orderCommodity_maps)
- {
- $params = [];
- foreach ($ActAllocationDetail_maps as $key=>$actAllocationDetail_map) {
- if(empty($orderCommodity_maps[$key]))continue;
- foreach ($actAllocationDetail_map as $actAllocationDetail) {
- $params[] = $actAllocationDetail;
- }
- }
- return $params;
- }
- 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;
- }
- }
|