Ver código fonte

控制台重构

ANG YU 5 anos atrás
pai
commit
67767e50ed

+ 1 - 1
app/Http/Controllers/InventoryAccountController.php

@@ -85,7 +85,7 @@ class InventoryAccountController extends Controller
         $paginateParams = $request->input();
         $queryParam = $request->all();
         $inventoryAccounts = app('inventoryAccountService')->paginate($queryParam);
-        $owners = $ownerService->getSelection();
+        $owners = $ownerService->getIntersectPermitting();
         return view('inventory.stockInventory.mission', compact('owners', 'inventoryAccounts', 'paginateParams'));
     }
 

+ 1 - 1
app/Http/Controllers/InventoryCompareController.php

@@ -92,7 +92,7 @@ class InventoryCompareController extends Controller
     }
     function inventoryCompare(Request $request,OwnerService $ownerService){
         if (!Gate::allows('库存管理-库存-库存对比')){return redirect(url('/')); }
-        $owners = $ownerService->getSelection();
+        $owners = $ownerService->getIntersectPermitting();
         $inventoryCompares=app('InventoryCompareService')->getInventoryCompare($request->all());
         $param = $request->input();
         return view('inventory.statement.inventoryCompare',compact('owners','inventoryCompares','param'));

+ 1 - 1
app/Http/Controllers/InventoryController.php

@@ -90,7 +90,7 @@ class InventoryController extends Controller
         if (!Gate::allows('库存管理-库存体积')){return redirect(url('/')); }
         /** @var InventoryDailyLogService*/
         $inventoryDailyLogs = app('InventoryDailyLogService')->paginate($request->input());
-        $owners = $ownerService->getSelection();
+        $owners = $ownerService->getIntersectPermitting();
         $param = $request->input();
         return view('inventory.statement.dailyLog',compact('inventoryDailyLogs','owners','param'));
     }

+ 1 - 1
app/Http/Controllers/PackageController.php

@@ -26,7 +26,7 @@ class PackageController extends Controller
         /** @var PackageService $application */
         $application = app('PackageService');
         $packages = $application->paginate($request);
-        return view('weight.package.index',['packages'=>$packages,'owners'=>$ownerService->getSelection(),'paginateParams'=>$paginateParams]);
+        return view('weight.package.index',['packages'=>$packages,'owners'=>$ownerService->getIntersectPermitting(),'paginateParams'=>$paginateParams]);
     }
 
     public function create()

+ 1 - 1
app/Http/Controllers/ProcessController.php

@@ -43,7 +43,7 @@ class ProcessController extends Controller
         if(!Gate::allows('二次加工管理-查询')){ return redirect(url('/'));  }
         $paginateParams = $request->input();
         $processes = app('ProcessService')->paginate($paginateParams);
-        $owners=$ownerService->getSelection();
+        $owners=$ownerService->getIntersectPermitting();
         return view('process.index',['processes'=>$processes,'owners'=>$owners,'paginateParams'=>$paginateParams]);
     }
 

+ 1 - 1
app/Http/Controllers/RejectedController.php

@@ -53,7 +53,7 @@ class RejectedController extends Controller
         $application = app('RejectedService');
         $rejectedBills= $application->paginate($request);
 //        $issueIds = $application->返回有问题件的ID($request);
-        $owners = $ownerService->getSelection();
+        $owners = $ownerService->getIntersectPermitting();
         $qualityLabels = $qualityLabelService->get();
         $logistics=$logisticService->getSelection();
         return view('rejected.search.general',compact('rejectedBills','owners','logistics',

+ 8 - 11
app/Http/Controllers/TestController.php

@@ -173,17 +173,14 @@ class TestController extends Controller
         }
     }
     public function test2(){
-        $b = Logistic::query()->first();
-        $a = OrderPackage::query()->with("order")->first();
-        $a->bulk = 521;
-        $a->save();
-        if (!$a->order) $a->order = new Order();
-        dd($a);
-        if (!$a->order->logistic)$a->order->logistic = $b;
-        dd($a->order->logistic);
-        dd($a);
-        $a->save();
-        dd($a);
+        $date=['startAt'=>Carbon::now()->subDays(2),'endAt'=>Carbon::now()->addDays(2)];
+        $startAt = Carbon::parse($date['startAt']);
+        $endAt = Carbon::parse($date['endAt']);
+        $dayArray=collect($startAt->yearsUntil($endAt, 1)->toArray())
+            ->map(function(Carbon $date){
+                return $date->firstOfYear();
+            });
+        dd($startAt,$endAt,$dayArray);
     }
 
     function packageFromLog(Request $request)

+ 2 - 2
app/Http/Controllers/WaybillController.php

@@ -56,7 +56,7 @@ class WaybillController extends Controller
         return view('waybill.index', [
             'waybills' => $waybills,
             'logistics' => $logisticService->getSelection(["id","name"],"物流"),
-            'owners' => $ownerService->getSelection(),
+            'owners' => $ownerService->getIntersectPermitting(),
             'paginateParams'=>$paginateParams,
             'uriType'=>$request->uriType??'']);
     }
@@ -68,7 +68,7 @@ class WaybillController extends Controller
         $type=$request->type ?? "";
         if ($type==='ZF')$type='直发车';
         if ($type==='ZX')$type='专线';
-        return view('waybill.create',['owners'=>$ownerService->getSelection(),'type'=>$type]);
+        return view('waybill.create',['owners'=>$ownerService->getIntersectPermitting(),'type'=>$type]);
     }
 
     public function store(Request $request)

+ 138 - 89
app/Services/NewOrderCountingRecordService.php

@@ -7,34 +7,17 @@ namespace App\Services;
 use App\Order;
 use App\OrderCountingRecord;
 use Carbon\Carbon;
-use Illuminate\Support\Arr;
 use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Str;
 
 class NewOrderCountingRecordService
 {
-    public function orderCountingRecords($start, $end, $unit)
+    public function orderCountingRecords($start, $end, $unit, $ownerIds)
     {
-        $key = 'orderCountingRecords_' . $start . '_' . $end . '_' . $unit . '_' . auth()->id();
-        return \cache()->remember($key, 600, function () use ($start, $end, $unit) {
-            $result = collect();
-            switch ($unit) {
-                case '日':
-                    $result = $this->orderCountingRecordsDay($start, $end, $unit);
-                    break;
-                case '周':
-                    $result = $this->orderCountingRecordsWeek($start, $end, $unit);
-                    break;
-                case '月':
-                    $result = $this->orderCountingRecordsMonth($start, $end, $unit);
-                    break;
-                case '年':
-                    $result = $this->orderCountingRecordsYear($start, $end, $unit);
-                    break;
-                default:
-                    break;
-
-            }
-            return $result;
+        $key = 'orderCountingRecords_' . $start . '_' . $end . '_' . $unit . '_' . json_encode($ownerIds);
+        $queryCondition = $this->transfersToCondition($start, $end, $unit, $ownerIds);
+        return cache()->remember($key, 600, function () use ($queryCondition) {
+            return $this->getOrderCountingRecords($queryCondition);
         });
     }
 
@@ -57,27 +40,27 @@ class NewOrderCountingRecordService
         });
     }
 
-    public function dataFromCache($start, $end, $unit)
+    public function getFromCache($queryCondition)
     {
-        $result = [];
-        $unQueryCondition = [];
-        $dataFromCache = collect();
-        $queryCondition = $this->dateUtils($start, $end, $unit);
-        foreach ($queryCondition as $dateStr => $ownerIds) {
+        $lackingCondition = [];
+        $orderCountingRecords_FromCache = collect();
+        $lackingCondition['unit'] = $queryCondition['unit'];
+        foreach ($queryCondition['data'] as $dateStr => $ownerIds) {
             foreach ($ownerIds as $ownerId) {
-                $key = 'order_counting_records_' . $dateStr . '_' . $ownerId . '_' . $unit . '_' . auth()->id();
+                $key = 'order_counting_records_' . $dateStr . '_' . $ownerId . '_' . $queryCondition['unit'];
                 $items = cache()->get($key);
                 if (empty($items)) {
-                    if (empty($unQueryCondition[$dateStr])) $unQueryCondition[$dateStr] = [];
-                    $unQueryCondition[$dateStr][] = $ownerId;
+                    if (empty($lackingCondition['data'][$dateStr])) {
+                        $lackingCondition['data'][$dateStr] = [];
+                    }
+                    $lackingCondition['data'][$dateStr][] = $ownerId;
                 } else {
-                    $dataFromCache = $dataFromCache->concat($items);
+                    $orderCountingRecords_FromCache = $orderCountingRecords_FromCache->concat($items);
                 }
             }
         }
-        $result['dataFromCache'] = $dataFromCache;
-        $result['unQueryCondition'] = $unQueryCondition;
-        return $result;
+        return ['orderCountingRecords_FromCache' => $orderCountingRecords_FromCache,
+            'lackingCondition' => $lackingCondition];
     }
 
     public function dataFromMiddleTable($unQueryCondition, $unit)
@@ -98,54 +81,103 @@ class NewOrderCountingRecordService
         return $result;
     }
 
-    public function orderCountingRecordsDay($start, $end, $unit)
+    public function getOrderCountingRecords($queryCondition)
     {
-        $dataFromCacheResult = $this->dataFromCache($start, $end, $unit);
-        $dataFromCache = $dataFromCacheResult['dataFromCache'];
-        $unQueryCondition = $dataFromCacheResult['unQueryCondition'];
-        if (!empty($unQueryCondition)) {
-            $dataFromMiddleTableResult = $this->dataFromMiddleTable($unQueryCondition, $unit);
-            $dataFromMiddleTable = $dataFromMiddleTableResult['dataFromMiddleTable'];
-            $unQueryCondition = $dataFromMiddleTableResult['unQueryCondition'];
-            if (!empty($unQueryCondition)) {
-                $dataFromOrder = $this->dataFromOrder($unQueryCondition, $unit);
-            }
+
+        list($orderCountingRecords_fromCache, $lackingCondition)
+            = $this->getFromCache($queryCondition);
+        if (empty($lackingCondition['data'])) {
+            return $orderCountingRecords_fromCache;
         }
-        $dataFromCache = $dataFromCache ?? collect();
-        $dataFromMiddleTable = $dataFromMiddleTable ?? collect();
-        $dataFromOrder = $dataFromOrder ?? collect();
-        return $dataFromCache->concat($dataFromMiddleTable)->concat($dataFromOrder);
+
+        list($orderCountingRecords_fromSelfTable, $lackingCondition)
+            = $this->dataFromMiddleTable($lackingCondition);
+
+        if (empty($lackingCondition['data'])) {
+            return $orderCountingRecords_fromCache->concat($orderCountingRecords_fromSelfTable);
+        }
+        list($orderCountingRecords_combinedByLower, $lackingCondition)
+            = $this->getByLowerUnit($lackingCondition);
+
+        $orderCountingRecords_FromOrder = $this->dataFromOrder($lackingCondition);
+
+        return $orderCountingRecords_FromOrder
+            ->concat($orderCountingRecords_fromCache)
+            ->concat($orderCountingRecords_fromSelfTable)
+            ->concat($orderCountingRecords_combinedByLower);
     }
 
-    public function dateUtils($start, $end, $unit)
+
+    public function getByLowerUnit($queryCondition)
     {
-        $ownerIds = $this->getCountingOwnerIds();
-        $dataArray = [];
-        switch ($unit) {
-            case '日';
-                foreach (Carbon::parse($start)->daysUntil($end, 1)->toArray() as $item) {
-                    $dataArray[$item->toDateString()] = $ownerIds;
-                }
+        switch ($queryCondition['unit']) {
+            case '年':
                 break;
-            case '周';
-                foreach (Carbon::parse($start)->weeksUntil($end, 1)->toArray() as $item) {
-                    $dataArray[$item->year . '-' . $item->week . ''] = $ownerIds;
-                }
+            case '月':
                 break;
-            case '月';
-                foreach (Carbon::parse($start)->monthsUntil($end, 1)->toArray() as $item) {
-                    $dataArray[$item->year . '-' . $item->format('m') . ''] = $ownerIds;
+            case '周':
+                $conditionClone = ['unit' => '日', 'data' => collect([])];
+                foreach ($queryCondition['data'] as $date => $ownerIds) {
+                    $startAt = $date;
+                    $endAt = Carbon::parse($date)->endOfWeek()->toDateString();
+                    $conditionClone['data']->concat(
+                        $this->transfersToCondition(
+                            $startAt, $endAt, $queryCondition['unit'], $ownerIds
+                        )['data']
+                    );
                 }
+                $conditionClone['data'] = $conditionClone['data']->toArray();
+                $orderCountingRecords_days = $this->getOrderCountingRecords($conditionClone);
+                //TODO: func turnDaysToWeeks($orderCountingRecords_days): 变成 ..._weeks
+                $orderCountingRecords_combinedByLower = $this->turnDaysToWeeks($orderCountingRecords_days);
                 break;
-            case '年';
-                foreach (Carbon::parse($start)->yearsUntil($end, 1)->toArray() as $item) {
-                    $dataArray[$item->year] = $ownerIds;
-                }
+            case '日':
+                return [[], $queryCondition];
                 break;
             default:
+        }
+        return [[], $orderCountingRecords_combinedByLower];
+    }
+
+    public function transfersToCondition($start, $end, $unit, $ownerIds)
+    {
+        $condition = [
+            'data' => [],
+            'unit' => $unit,];
+        $startAt = Carbon::parse($start);
+        $dates = [];
+        switch ($unit) {
+            case '年':
+                $dates = collect($startAt->yearsUntil($end, 1)->toArray())
+                    ->map(function (Carbon $date) {
+                        return $date->firstOfYear();
+                    });
+                break;
+            case '月':
+                $dates = collect($startAt->monthsUntil($end, 1)->toArray())
+                    ->map(function (Carbon $date) {
+                        return $date->firstOfMonth();
+                    });
                 break;
+            case '周':
+                $dates = collect($startAt->weeksUntil($end, 1)->toArray())
+                    ->map(function (Carbon $date) {
+                        return $date->startOfWeek();
+                    });
+                break;
+            case '日':
+                $dates = collect($startAt->daysUntil($end, 1)->toArray())
+                    ->map(function (Carbon $date) {
+                        return $date->startOfDay();
+                    });
+                break;
+            default:
+        }
+
+        foreach ($dates as $day) {
+            $condition['data'][$day->toDateString()] = $ownerIds;
         }
-        return $dataArray;
+        return $condition;
     }
 
     public function dataFromOrder($unQueryCondition, $unit)
@@ -164,24 +196,6 @@ class NewOrderCountingRecordService
         return $dataFromMiddleTable;
     }
 
-    public function orderCountingRecordsWeek($start, $end, $unit)
-    {
-        //TODO
-        return collect();
-    }
-
-    public function orderCountingRecordsMonth($start, $end, $unit)
-    {
-        //TODO
-        return collect();
-    }
-
-    public function orderCountingRecordsYear($start, $end, $unit)
-    {
-        //TODO
-        return collect();
-    }
-
     public function insertIntoMiddleTable($data, $unit)
     {
         $data->filter(function ($item) {
@@ -243,6 +257,10 @@ class NewOrderCountingRecordService
         //当日当周当月当年的数据不能插入到中间表
         $dateTime = Carbon::now()->toDateTimeString();
         foreach ($dataFromOrder as $order) {
+            $carbon = Carbon::parse($order->date_target);
+            $year = $carbon->year;
+            $week = $carbon->week;
+            $month = $carbon->month;
             $dataFromMiddleTable->push(new OrderCountingRecord([
                 'owner_id' => $order->owner_id,
                 'shop_id' => $order->shop_id,
@@ -251,6 +269,9 @@ class NewOrderCountingRecordService
                 'date_target' => $order->date_target,
                 'counting_unit' => $order->counting_unit,
                 'amount' => $order->amounts,
+                'week' => $year . '-' . $week,
+                'month' => $year . '-' . $month,
+                'year' => $year,
                 'created_at' => $dateTime,
                 'updated_at' => $dateTime,
             ]));
@@ -258,4 +279,32 @@ class NewOrderCountingRecordService
         return $dataFromMiddleTable;
     }
 
+    public function dateSwitch($unQueryCondition, $currentUnit, $targetUnit)
+    {
+        foreach ($unQueryCondition as $date => $ownerIds) {
+            switch ($currentUnit . '-' . $targetUnit) {
+                case '周-日':
+                    $week = Str::of($date)->after('-');
+                    $year = Str::of($date)->before('-');
+
+                    break;
+                case '月-周':
+                    break;
+                case '年-月':
+                    break;
+                default:
+                    break;
+            }
+        }
+        return [];
+    }
+
+    public function turnDaysToWeeks($orderCountingRecords_days)
+    {
+        $orderCountingRecords_combinedByLower = collect();
+        foreach ($orderCountingRecords_days as $orderCountingRecord) {
+            $date_target = $orderCountingRecord->date_target;
+        }
+    }
+
 }

+ 1 - 1
app/Services/OwnerService.php

@@ -21,7 +21,7 @@ Class OwnerService
      * array | string $column
      * 默认一些select字段,可传递string 或 array来指定select字段
      */
-    public function getSelection(array $column = ['id', 'name'])
+    public function getIntersectPermitting(array $column = ['id', 'name'])
     {
         $ownerIds=app('UserService')->getPermittingOwnerIds(Auth::user());
         return $this->cacheService->getOrExecute('OwnersAll_IdName'.md5(json_encode($column).json_encode($ownerIds)),function()use($column,$ownerIds){

+ 51 - 17
tests/Services/NewOrderCountingRecordService/NewOrderCountingRecordServiceTest.php

@@ -39,13 +39,14 @@ class NewOrderCountingRecordServiceTest extends TestCase
     /**
      * @test
      */
-    public function dateUtils_day()
+    public function transfersToConditions()
     {
         factory(Owner::class)->times(2)->create();
         $start = Carbon::now()->subDays(2)->toDateString();
         $end = Carbon::now()->toDateString();
         $unit = '日';
-        $dateArray = $this->newOrderCountingRecordService->dateUtils($start, $end, $unit);
+        $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
+        $dateArray = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
         $this->assertEquals([
             Carbon::now()->subDays(2)->toDateString() => [0 => 1, 1 => 2],
             Carbon::now()->subDays(1)->toDateString() => [0 => 1, 1 => 2],
@@ -53,6 +54,26 @@ class NewOrderCountingRecordServiceTest extends TestCase
         ], $dateArray);
     }
 
+    /**
+     * @test
+     */
+    public function dateUtils_week()
+    {
+        factory(Owner::class)->times(2)->create();
+        $start = Carbon::now()->subWeeks(2)->toDateString();
+        $end = Carbon::now()->toDateString();
+        $unit = '周';
+        $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
+        $dateArray = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
+        dd($dateArray);
+//        $this->assertEquals([
+//            Carbon::now()->subWeeks(2)->toDateString() => [0 => 1, 1 => 2],
+//            Carbon::now()->subWeeks(1)->toDateString() => [0 => 1, 1 => 2],
+//            Carbon::now()->toDateString() => [0 => 1, 1 => 2],
+//        ], $dateArray);
+    }
+
+
     /**
      * @test
      */
@@ -79,14 +100,16 @@ class NewOrderCountingRecordServiceTest extends TestCase
         $end = Carbon::now()->toDateString();
         $unit = '日';
         factory(Owner::class)->times(2)->create();
-        $queryCondition = $this->newOrderCountingRecordService->dateUtils($start, $end, $unit);
+        $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
+        $queryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
+
         foreach ($queryCondition as $dateStr => $ownerIds) {
             foreach ($ownerIds as $ownerId) {
                 $key = 'order_counting_records_' . $dateStr . '_' . $ownerId . '_' . $unit . '_' . auth()->id();
                 cache()->put($key, collect()->push(factory(Owner::class)->create()));
             }
         }
-        $result = $this->newOrderCountingRecordService->dataFromCache($start, $end, $unit);
+        $result = $this->newOrderCountingRecordService->getFromCache($queryCondition, $unit);
         $this->assertCount(6, $result['dataFromCache']);
     }
 
@@ -99,7 +122,11 @@ class NewOrderCountingRecordServiceTest extends TestCase
         $end = Carbon::now()->toDateString();
         $unit = '日';
         factory(Owner::class)->times(2)->create();
-        $result = $this->newOrderCountingRecordService->dataFromCache($start, $end, $unit);
+        $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
+        $queryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
+
+
+        $result = $this->newOrderCountingRecordService->getFromCache($queryCondition, $unit);
         $this->assertEquals([
             Carbon::now()->subDays(2)->toDateString() => [0 => 1, 1 => 2],
             Carbon::now()->subDays(1)->toDateString() => [0 => 1, 1 => 2],
@@ -121,8 +148,10 @@ class NewOrderCountingRecordServiceTest extends TestCase
         factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->toDateString(), 'owner_id' => $owner2->id]);
         factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->subDays(1)->toDateString(), 'owner_id' => $owner1->id]);
         factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->subDays(1)->toDateString(), 'owner_id' => $owner2->id]);
-        $dateArray = $this->newOrderCountingRecordService->dateUtils($start, $end, $unit);
-        $result = $this->newOrderCountingRecordService->dataFromMiddleTable($dateArray, $unit);
+        $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
+
+        $queryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
+        $result = $this->newOrderCountingRecordService->dataFromMiddleTable($queryCondition, $unit);
 
         $this->assertCount(8, $result['dataFromMiddleTable']);
         $this->assertCount(0, $result['unQueryCondition'][Carbon::now()->toDateString()]);
@@ -137,10 +166,11 @@ class NewOrderCountingRecordServiceTest extends TestCase
         $start = Carbon::now()->subDays(1)->toDateString();
         $end = Carbon::now()->toDateString();
         $unit = '日';
-        $owner1 = factory(Owner::class)->create();
-        $owner2 = factory(Owner::class)->create();
+        factory(Owner::class)->create();
+        factory(Owner::class)->create();
+        $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
 
-        $dateArray = $this->newOrderCountingRecordService->dateUtils($start, $end, $unit);
+        $dateArray = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
         $result = $this->newOrderCountingRecordService->dataFromMiddleTable($dateArray, $unit);
 
         $this->assertCount(0, $result['dataFromMiddleTable']);
@@ -158,8 +188,9 @@ class NewOrderCountingRecordServiceTest extends TestCase
         $unit = '日';
         $owner1 = factory(Owner::class)->create();
         $owner2 = factory(Owner::class)->create();
+        $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
 
-        $dateArray = $this->newOrderCountingRecordService->dateUtils($start, $end, $unit);
+        $dateArray = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
         factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->toDateString(), 'owner_id' => $owner1->id]);
 
         $result = $this->newOrderCountingRecordService->dataFromMiddleTable($dateArray, $unit);
@@ -179,8 +210,9 @@ class NewOrderCountingRecordServiceTest extends TestCase
         $unit = '日';
         $owner1 = factory(Owner::class)->create();
         $owner2 = factory(Owner::class)->create();
+        $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
 
-        $dateArray = $this->newOrderCountingRecordService->dateUtils($start, $end, $unit);
+        $dateArray = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
         $orderCountingRecord = factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->toDateString(), 'owner_id' => $owner1->id]);
 
         $result = $this->newOrderCountingRecordService->dataFromMiddleTable($dateArray, $unit);
@@ -202,8 +234,9 @@ class NewOrderCountingRecordServiceTest extends TestCase
         $unit = '日';
         $owner1 = factory(Owner::class)->create();
         $owner2 = factory(Owner::class)->create();
+        $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
 
-        $unQueryCondition = $this->newOrderCountingRecordService->dateUtils($start, $end, $unit);
+        $unQueryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
 
         factory(Order::class)->times(1)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner1->id]);
         factory(Order::class)->times(2)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner2->id]);
@@ -228,8 +261,8 @@ class NewOrderCountingRecordServiceTest extends TestCase
         $unit = '日';
         $owner1 = factory(Owner::class)->create();
         $owner2 = factory(Owner::class)->create();
-
-        $unQueryCondition = $this->newOrderCountingRecordService->dateUtils($start, $end, $unit);
+        $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
+        $unQueryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
 
         factory(Order::class)->times(1)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner1->id]);
         factory(Order::class)->times(2)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner2->id]);
@@ -255,7 +288,7 @@ class NewOrderCountingRecordServiceTest extends TestCase
         $owner1 = factory(Owner::class)->create();
         $owner2 = factory(Owner::class)->create();
 
-        $unQueryCondition = $this->newOrderCountingRecordService->dateUtils($start, $end, $unit);
+        $unQueryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit,$this->newOrderCountingRecordService->getCountingOwnerIds());
 
         factory(Order::class)->times(1)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner1->id]);
         factory(Order::class)->times(2)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner2->id]);
@@ -291,8 +324,9 @@ class NewOrderCountingRecordServiceTest extends TestCase
 
         factory(Order::class)->times(3)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner1->id]);
         factory(Order::class)->times(4)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner2->id]);
+        $unQueryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit,$this->newOrderCountingRecordService->getCountingOwnerIds());
 
-        $result = $this->newOrderCountingRecordService->orderCountingRecordsDay($start, $end, $unit);
+        $result = $this->newOrderCountingRecordService->getOrderCountingRecords($unQueryCondition, $unit);
         $this->assertEquals(3, $result->where('date_target', Carbon::now()->toDateString())->reduce(function ($carry, $item) {
             return $carry + $item->amount;
         }));

+ 126 - 0
tests/Services/NewOrderCountingRecordService/TransfersToConditionsTest.php

@@ -0,0 +1,126 @@
+<?php
+
+namespace NewOrderCountingRecordService;
+
+use App\Order;
+use App\OrderCountingRecord;
+use App\Owner;
+use App\Services\NewOrderCountingRecordService;
+use App\User;
+use Carbon\Carbon;
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Support\Arr;
+use Tests\TestCase;
+
+class TransfersToConditionsTest extends TestCase
+{
+    protected $newOrderCountingRecordService;
+    protected $data;
+
+    protected function setUp(): void
+    {
+        parent::setUp(); // TODO: Change the autogenerated stub
+        cache()->flush();
+        $this->newOrderCountingRecordService = new NewOrderCountingRecordService();
+        $this->actingAs(factory(User::class)->create(['name' => 'yang']));
+        $this->data['dates'] = [
+            'right'=>[
+                ['startAt'=>Carbon::now()->subDays(2),'endAt'=>Carbon::now()->addDays(2)],
+                ['startAt'=>Carbon::now()->subYears(1),'endAt'=>Carbon::now()->addYears(1)],
+            ],
+            'wrong'=>[
+                ['startAt'=>Carbon::now()->addDays(2),'endAt'=>Carbon::now()->subDays(2)],
+                ['startAt'=>Carbon::now()->addYears(1),'endAt'=>Carbon::now()->subYears(1)],
+            ]
+        ];
+        $this->data['units'] = ['year'=>'年','month'=>'月','week'=>'周','day'=>'日'];
+        $this->data['owners']=factory(Owner::class)->times(2)->create();
+    }
+
+    protected function tearDown(): void
+    {
+        cache()->flush();
+        Owner::destroy(data_get($this->data['owners'],'*.id'));
+        parent::tearDown(); // TODO: Change the autogenerated stub
+    }
+
+    public function test_transfersToConditions_format()
+    {
+        foreach($this->data['units'] as $unit){
+            foreach($this->data['dates']['right'] as $date){
+                $result=$this->newOrderCountingRecordService->transfersToCondition($date['startAt'],$date['endAt'],$unit,data_get($this->data['owners'],'*.id'));
+                $this->assertIsArray($result);
+            }
+            foreach($this->data['dates']['wrong'] as $date){
+                $result=$this->newOrderCountingRecordService->transfersToCondition($date['startAt'],$date['endAt'],$unit,data_get($this->data['owners'],'*.id'));
+                $this->assertIsArray($result);
+            }
+        }
+    }
+    public function test_transfersToConditions_ifEmpty()
+    {
+        foreach($this->data['units'] as $unit){
+            foreach($this->data['dates']['right'] as $date){
+                $result=$this->newOrderCountingRecordService->transfersToCondition($date['startAt'],$date['endAt'],$unit,data_get($this->data['owners'],'*.id'));
+                $this->assertNotEmpty($result);
+            }
+            foreach($this->data['dates']['wrong'] as $date){
+                $result=$this->newOrderCountingRecordService->transfersToCondition($date['startAt'],$date['endAt'],$unit,data_get($this->data['owners'],'*.id'));
+                $this->assertEmpty($result['data']);
+                $this->assertNotEmpty($result['unit']);
+            }
+        }
+    }
+
+    public function test_transfersToConditions_ifDatesMatches()
+    {
+        foreach($this->data['units'] as $unit){
+            foreach($this->data['dates']['right'] as $date){
+                $result=$this->newOrderCountingRecordService->transfersToCondition($date['startAt'],$date['endAt'],$unit,data_get($this->data['owners'],'*.id'));
+                $datesExpected=[];
+                switch ($unit){
+                    case '年': $datesExpected=collect($date['startAt']->yearsUntil($date['endAt'], 1)->toArray())
+                        ->map(function(Carbon $date){
+                            return $date->firstOfYear();
+                        })->toArray();
+                        break;
+                    case '月': $datesExpected=collect($date['startAt']->monthsUntil($date['endAt'], 1)->toArray())
+                        ->map(function(Carbon $date){
+                            return $date->firstOfMonth();
+                        })->toArray();break;
+                    case '周': $datesExpected=collect($date['startAt']->weeksUntil($date['endAt'], 1)->toArray())
+                        ->map(function(Carbon $date){
+                            return $date->startOfWeek();
+                        })->toArray();break;
+                    case '日': $datesExpected=collect($date['startAt']->daysUntil($date['endAt'], 1)->toArray())
+                        ->map(function(Carbon $date){
+                            return $date->startOfDay();
+                        })->toArray();break;
+                }
+                $datesExpected=array_map(function($date){
+                    return $date->toDateString();
+                },$datesExpected);
+                $actualDays = array_keys($result['data']);
+                $this->assertEquals($datesExpected, $actualDays);
+            }
+        }
+    }
+
+    public function test_transfersToConditions_ifOwnerIdMatches()
+    {
+        foreach($this->data['units'] as $unit){
+            foreach($this->data['dates']['right'] as $date){
+                $result=$this->newOrderCountingRecordService->transfersToCondition($date['startAt'],$date['endAt'],$unit,data_get($this->data['owners'],'*.id'));
+                $ownerIdsExpected=data_get($this->data['owners'],'*.id');
+                $isMatch=true;
+                foreach($result['data'] as $date=>$ownerIds){
+                    if(json_encode($ownerIdsExpected)!=json_encode($ownerIds)){
+                        $isMatch=false; break;
+                    }
+                }
+                $this->assertTrue($isMatch);
+            }
+        }
+    }
+
+}