ajun 5 lat temu
rodzic
commit
5d10eae327

+ 1 - 1
app/Console/Kernel.php

@@ -44,7 +44,7 @@ class Kernel extends ConsoleKernel
         $schedule->command('createOwnerBillReport')->monthlyOn(1);
         $schedule->command('createOwnerAreaReport')->monthlyOn(25);
         $schedule->command('sync:batch')->everyMinute();
-        $schedule->command('sync:order')->everyMinute();
+//        $schedule->command('sync:order')->everyMinute();
     }
 
     /**

+ 38 - 153
app/Services/OrderCommodityService.php

@@ -9,149 +9,6 @@ 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;
@@ -167,7 +24,8 @@ Class OrderCommodityService
         }
     }
 
-    public function syncOrderCommodity($orderHeaders)
+    // TODO 根据传入的$orderHeaders 同步订单
+    public function syncOrderCommodity(&$orderHeaders)
     {
         /**
          * @var OwnerService $ownerService
@@ -177,7 +35,7 @@ Class OrderCommodityService
         $ownerService = app('OwnerService');
         $commodityService = app('CommodityService');
         $dataHandlerService = app('DataHandlerService');
-
+        $orderNos = data_get($orderHeaders,'*.code');
         $map = [];$owner_codes = [];
         foreach ($orderHeaders as $orderHeader) {
             $actAllocationDetails  = $orderHeader->actAllocationDetails;
@@ -191,7 +49,7 @@ Class OrderCommodityService
             }
         }
         $owners = $ownerService->getOwnerByCodes($owner_codes);
-        $owner_map = $dataHandlerService->dataHeader(['id'],$owners);
+        $owner_id_maps = $dataHandlerService->dataHeader(['id'],$owners);
         $commodities = $commodityService->getParamsByBasSku($map);
         $commodity_map = [];
         foreach ($commodities as $commodity) {
@@ -200,8 +58,30 @@ Class OrderCommodityService
             $commodity_map[$key] = $commodity;
         }
 
+        $orderCommodities = OrderCommodity::query()->with('order')->whereHas('order',function ($qurey)use($orderNos){
+            $qurey->whereIn('code',$orderNos);
+        })->get();
+        $orders = Order::query()->whereIn('code',$orderNos)->get();
+        $order_code_map = $dataHandlerService->dataHeader(['code'],$orders);
+        $ActAllocationDetail_maps =  $this->getRegroupActAllocationDetails($orderHeaders);
+        $orderCommodity_maps = $this->getRegroupOrderCommodities($orderCommodities,$owner_id_maps);
+
+        $this->filterHasExist($ActAllocationDetail_maps,$orderCommodity_maps);
+        $create_params = $this->getCreateParams($ActAllocationDetail_maps,$orderCommodity_maps);
+        $delete_ids =  $this->getDeleteIds($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 = [];
@@ -221,7 +101,14 @@ Class OrderCommodityService
         });
         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 */
@@ -244,7 +131,7 @@ Class OrderCommodityService
         });
         return $map;
     }
-
+    // TODO 返回修改删除的
     public function getDeleteIds(&$ActAllocationDetail_maps,&$orderCommodity_maps)
     {
         $ids = [];
@@ -257,7 +144,7 @@ Class OrderCommodityService
         }
         return $ids;
     }
-
+    // TODO 返回创建的数组
     public function getCreateParams(&$ActAllocationDetail_maps,&$orderCommodity_maps)
     {
         $params = [];
@@ -269,8 +156,8 @@ Class OrderCommodityService
         }
         return $params;
     }
-
-    public function getInnerParamsByParams($create_params,$order_code_map,$commodity_map)
+    // TODO 根据创建数组返回可以使用的插入数组
+    public function getInnerParamsByParams(&$create_params,&$order_code_map,&$commodity_map)
     {
         /** @var DataHandlerService  $dataHandlerService */
         $dataHandlerService = app(DataHandlerService::class);
@@ -291,7 +178,5 @@ Class OrderCommodityService
         }
         return $inner_params;
     }
-
-
 }
 

+ 4 - 4
database/factories/OrcaleDOCOrderHeaderFactory.php

@@ -8,11 +8,11 @@ use Faker\Generator as Faker;
 $factory->define(OracleDOCOrderHeader::class, function (Faker $faker) {
     return [
         'orderno' => $faker->uuid,
-        'customerid' => '',
-        'waveno' => '',
+        'customerid' => $faker->name,
+        'waveno' => $faker->name,
         'ordertime' =>$faker->time(),
         'soreference1' => $faker->uuid,
-        'consigneeid' => '',
+        'consigneeid' => $faker->name,
         'c_contact' =>$faker->title(10),
         'consigneename' => $faker->title(11),
         'c_address1' => $faker->address,
@@ -27,7 +27,7 @@ $factory->define(OracleDOCOrderHeader::class, function (Faker $faker) {
         'soreference5' => $faker->uuid,
         'c_tel2' => $faker->phoneNumber,
         'transportation' => $faker->phoneNumber,
-        'warehouseid' => '',
+        'warehouseid' => $faker->name,
         'sostatus' => '99',
         'c_tel1' => $faker->phoneNumber,
         'c_district' => $faker->city,

+ 5 - 1
database/factories/OrderCommodityFactory.php

@@ -7,6 +7,10 @@ use Faker\Generator as Faker;
 
 $factory->define(OrderCommodity::class, function (Faker $faker) {
     return [
-        //
+        'order_id' => 1,
+        'commodity_id' => 1,
+        'amount' => rand(1,100),
+        'wms_ptltaskid' => $faker->name,
+        'location' => $faker->name
     ];
 });

+ 67 - 0
tests/Services/OrderCommodityService/GetReGroupActAllocationDetailsTest.php

@@ -0,0 +1,67 @@
+<?php
+
+namespace Tests\Services\OrderCommodityService;
+
+use App\OracleActAllocationDetails;
+use App\OracleDOCOrderHeader;
+use App\Owner;
+use App\Services\OrderCommodityService;
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Foundation\Testing\WithFaker;
+use Tests\TestCase;
+
+class GetReGroupActAllocationDetailsTest extends TestCase
+{
+//    use RefreshDatabase;
+
+    /** @var OrderCommodityService  $service */
+    private $service;
+    private $data;
+
+    public function setUp(): void
+    {
+        parent::setUp(); // TODO: Change the autogenerated stub
+        $this->service = app('OrderCommodityService');
+//        $owner = factory(Owner::class)->create();
+        $orderHeaders  = collect();
+        $orderHeader = factory(OracleDOCOrderHeader::class)->make();
+        $oracleActAllocationDetails = collect();
+        for ($i=0;$i<=10;$i++){
+            $oracleActAllocationDetails->push( factory(OracleActAllocationDetails::class)->make(['orderno'=>$orderHeader['orderno'],'customerid' => $orderHeader['customerid']]));
+        }
+        $orderHeader->setRelation('actAllocationDetails',$oracleActAllocationDetails);
+        $orderHeaders->push($orderHeader);
+        $this->data['orderHeaders'] = $orderHeaders;
+    }
+
+    /**
+     * @test
+     */
+    public function getRegroupActAllocationDetails()
+    {
+        $orderHeaders = $this->data['orderHeaders'];
+        $oracleActAllocationDetails = $orderHeaders->first()->actAllocationDetails;
+
+        $regroupActAllocationDetails = $this->service->getRegroupActAllocationDetails($orderHeaders);
+
+        $this->assertNotNull($oracleActAllocationDetails);
+        $this->assertNotNull($regroupActAllocationDetails);
+
+        $this->assertEquals(count($orderHeaders),$this->count($regroupActAllocationDetails));
+        foreach ($regroupActAllocationDetails as $items) {
+            foreach ($items as $item) {
+                $details = $oracleActAllocationDetails
+                    ->where('location',$item['location'])
+                    ->where('customerid',$item['owner_code'])
+                    ->where('sku',$item['sku']);
+                $this->assertNotEmpty($details);
+                $this->assertEquals(1, $details->count());
+            }
+        }
+    }
+
+    public function tearDown(): void
+    {
+        parent::tearDown(); // TODO: Change the autogenerated stub
+    }
+}