ajun 5 vuotta sitten
vanhempi
commit
a80d1cf450

+ 15 - 14
app/Services/OrderCommodityService.php

@@ -35,40 +35,40 @@ Class OrderCommodityService
         $ownerService = app('OwnerService');
         $commodityService = app('CommodityService');
         $dataHandlerService = app('DataHandlerService');
-        $orderNos = data_get($orderHeaders,'*.code');
-        $map = [];$owner_codes = [];
+        $orderNos = data_get($orderHeaders,'*.orderno');
+        $owner_codes = [];$sku_codes = [];
+        $count = 0;
         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']
-                ];
+                $sku_codes[$actAllocationDetail['sku']] = $actAllocationDetail['sku'];
+                $count++;
             }
         }
+
         $owners = $ownerService->getOwnerByCodes($owner_codes);
         $owner_id_maps = $dataHandlerService->dataHeader(['id'],$owners);
-        $commodities = $commodityService->getParamsByBasSku($map);
+        $commodities = $commodityService->get_(data_get($owners,'*.id'),$sku_codes,[],true);
+
         $commodity_map = [];
         foreach ($commodities as $commodity) {
-            $owner = $dataHandlerService->getKeyValue(['id'=>$commodity->id],$owner_map);
+            $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')->whereHas('order',function ($qurey)use($orderNos){
-            $qurey->whereIn('code',$orderNos);
+        $orderCommodities = OrderCommodity::query()->with(['order','commodity'])->whereHas('order',function ($query)use($orderNos){
+            $query->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);
 
+        $delete_ids =  $this->getDeleteIds($ActAllocationDetail_maps,$orderCommodity_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);
@@ -118,7 +118,7 @@ Class OrderCommodityService
         $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)}";
+            $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,
@@ -142,6 +142,7 @@ Class OrderCommodityService
                 unset($orderCommodity_map[$key1]);
             }
         }
+
         return $ids;
     }
     // TODO 返回创建的数组
@@ -149,7 +150,7 @@ Class OrderCommodityService
     {
         $params = [];
         foreach ($ActAllocationDetail_maps as $key=>$actAllocationDetail_map) {
-            if(empty($orderCommodity_maps[$key]))continue;
+            if(isset($orderCommodity_maps[$key]))continue;
             foreach ($actAllocationDetail_map as $actAllocationDetail) {
                 $params[]  = $actAllocationDetail;
             }

+ 93 - 0
tests/Services/OrderCommodityService/SyncOrderCommodityTest.php

@@ -0,0 +1,93 @@
+<?php
+
+namespace Tests\Services\OrderCommodityService;
+
+use App\Commodity;
+use App\OracleActAllocationDetails;
+use App\OracleDOCOrderHeader;
+use App\Order;
+use App\OrderCommodity;
+use App\Owner;
+use App\Services\CommodityService;
+use App\Services\OrderCommodityService;
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Foundation\Testing\WithFaker;
+use Tests\TestCase;
+
+class SyncOrderCommodityTest 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');
+
+        $owners = factory(Owner::class)->times(3)->create();
+        $this->data['owners'] = $owners;
+        $commodities = collect();
+        foreach ($owners as $owner) {
+            $counts = rand(1,20);
+            for($i=0;$i<=$counts;$i++){
+                $commodities->push(factory(Commodity::class)->create(['owner_id'=>$owner->id]));
+            }
+        }
+        $this->data['commodities'] = $commodities;
+
+        $orderHeaders = collect();$orders = collect();
+        for ($i=0;$i<2000;$i++) {
+            $owner = $owners->random();
+            $commodity_list = $commodities->where('owner_id',$owner->id);
+            $order = factory(Order::class)->create(['owner_id'=>$owner->id]);
+            $orderHeader = factory(OracleDOCOrderHeader::class)->make(['orderno' => $order->code, 'customerid' => $owner->code]);
+            $oracleActAllocationDetails = collect();
+            for($count=1;$count<=4;$count++){
+                $commodity = $commodity_list->random();
+                $oracleActAllocationDetail  = factory(OracleActAllocationDetails::class)->make([
+                    'orderno'=>$orderHeader['orderno'],
+                    'customerid' => $orderHeader['customerid'],
+                    'sku' => $commodity->sku
+                    ]);
+                $oracleActAllocationDetails->push($oracleActAllocationDetail);
+            }
+            $orderHeader->setRelation('actAllocationDetails',$oracleActAllocationDetails);
+            $orderHeaders->push($orderHeader);
+            $orders->push($order);
+        }
+        $this->data['orders'] = $orders;
+        $this->data['orderHeaders'] = $orderHeaders;
+
+        $this->mock(CommodityService::class,function($mock)use($commodities){
+            $mock->shouldReceive('get_')->andReturn($commodities);
+        });
+    }
+
+    /**
+     * @test
+     */
+    public function syncOrderCommodity()
+    {
+        for ($i = 0 ;$i<4; $i++) {
+            $this->service->syncOrderCommodity($this->data['orderHeaders']);
+        }
+        $counts = OrderCommodity::query()->whereIn('order_id',data_get($this->data['orders'],'*.id'))->get()->count();
+        $actAllocationDetailsCounts  = 0;
+         foreach ($this->data['orderHeaders'] as $orderHeader) {
+            $actAllocationDetails = $orderHeader->actAllocationDetails;
+            $actAllocationDetailsCounts  += $actAllocationDetails->count();
+        }
+        $this->assertEquals($counts,$actAllocationDetailsCounts);
+     }
+
+    public function tearDown(): void
+    {
+        cache()->flush();
+//        Order::query()->whereIn('id',data_get($this->data['orders'],'*.id'))->delete();
+//        Owner::query()->whereIn('id',data_get($this->data['owners'],'*.id'))->delete();
+//        Commodity::query()->whereIn('id',data_get($this->data['commodities'],'*.id'))->delete();
+        parent::tearDown(); // TODO: Change the autogenerated stub
+    }
+}