ANG YU 5 лет назад
Родитель
Сommit
e86e188c45

+ 13 - 11
app/Services/NewOrderCountingRecordService.php

@@ -107,7 +107,7 @@ class NewOrderCountingRecordService
         return $this->getOrderCountingRecords($queryCondition);
     }
 
-    public function getFromCache($queryCondition)
+    public function getFromCache($queryCondition): array
     {
         $lackingCondition = [];
         $orderCountingRecords_FromCache = collect();
@@ -131,9 +131,8 @@ class NewOrderCountingRecordService
         return [$orderCountingRecords_FromCache, $lackingCondition];
     }
 
-    public function dataFromMiddleTable($queryCondition)
+    public function getFromMiddleTable($queryCondition): array
     {
-        $result = [];
         $orderSqlBuilder = OrderCountingRecord::query();
         $unit = $queryCondition['unit'];
         foreach ($queryCondition['data'] as $date => $owners) {
@@ -158,12 +157,13 @@ class NewOrderCountingRecordService
         }
 
         list($orderCountingRecords_fromSelfTable, $lackingCondition)
-            = $this->dataFromMiddleTable($lackingCondition);
+            = $this->getFromMiddleTable($lackingCondition);
         if (empty($lackingCondition['data'])) {
             return $orderCountingRecords_fromCache->concat($orderCountingRecords_fromSelfTable);
         }
-        list($orderCountingRecords_combinedByLower, $lackingCondition)
-            = $this->getByLowerUnit($lackingCondition);
+        if ($lackingCondition['unit']!='日') {//日为最低粒度,不用进入此方法
+            $orderCountingRecords_combinedByLower = $this->getFromLowerUnit($lackingCondition);
+        }
         if (!empty($lackingCondition)) {
             $orderCountingRecords_FromOrder = $this->dataFromOrder($lackingCondition);
         }
@@ -177,14 +177,14 @@ class NewOrderCountingRecordService
         if (!$orderCountingRecords_fromSelfTable->isEmpty()) {
             $result = $result->concat($orderCountingRecords_fromSelfTable);
         }
-        if (count($orderCountingRecords_combinedByLower) != 0) {
+        if (isset($orderCountingRecords_combinedByLower)) {
             $result = $result->concat($orderCountingRecords_combinedByLower);
         }
         return $result;
     }
 
 
-    public function getByLowerUnit($queryCondition)
+    public function getFromLowerUnit($queryCondition)
     {
         switch ($queryCondition['unit']) {
             case '年':
@@ -203,6 +203,7 @@ class NewOrderCountingRecordService
                 }
                 $orderCountingRecords_days = $this->getOrderCountingRecords($conditionClone);
                 $orderCountingRecords_combinedByLower = $this->turnGradingUpToLow($orderCountingRecords_days, $lowUnit, 'year');
+                $this->insertIntoMiddleTable($orderCountingRecords_combinedByLower);
                 break;
             case '月':
                 $lowUnit = '日';
@@ -220,15 +221,16 @@ class NewOrderCountingRecordService
                 }
                 $orderCountingRecords_days = $this->getOrderCountingRecords($conditionClone);
                 $orderCountingRecords_combinedByLower = $this->turnGradingUpToLow($orderCountingRecords_days, $lowUnit, 'month');
+                $this->insertIntoMiddleTable($orderCountingRecords_combinedByLower);
                 break;
             case '日':
                 return [[], $queryCondition];
             default:
         }
-        return [$orderCountingRecords_combinedByLower, []];
+        return $orderCountingRecords_combinedByLower;
     }
 
-    public function transfersToCondition($start, $end, $unit, $ownerIds)
+    public function transfersToCondition($start, $end, $unit, $ownerIds): array
     {
         $condition = [
             'data' => [],
@@ -268,7 +270,7 @@ class NewOrderCountingRecordService
         $orderSqlBuilder = Order::query()->selectRaw("owner_id,warehouse_id,shop_id,logistic_id,count(1) as amounts ,DATE_FORMAT(created_at,'%Y-%m-%d') as date_target");
         foreach ($queryCondition['data'] as $dateStr => $ownerIds) {
             $orderSqlBuilder->orWhere(function ($query) use ($ownerIds, $dateStr) {
-                $query->whereIn('owner_id', $ownerIds)->where('created_at', '>', $dateStr)->where('created_at', '<', Carbon::parse($dateStr)->addDay()->toDateString())->where('wms_status', '订单完成');
+                $query->whereIn('owner_id', $ownerIds)->where('created_at', '>=', $dateStr)->where('created_at', '<', Carbon::parse($dateStr)->addDay()->toDateString())->where('wms_status', '订单完成');
             });
         }
         $dataFromOrder = $orderSqlBuilder->groupBy(['owner_id', 'warehouse_id', 'shop_id', 'logistic_id', 'date_target'])->get();

+ 106 - 0
tests/Services/NewOrderCountingRecordService/GetFromCacheTest.php

@@ -0,0 +1,106 @@
+<?php
+
+namespace NewOrderCountingRecordService;
+
+
+use App\Order;
+use App\OrderCountingRecord;
+use App\Owner;
+use App\Services\NewOrderCountingRecordService;
+use App\User;
+use Carbon\Carbon;
+use Tests\TestCase;
+
+class GetFromCacheTest extends TestCase
+{
+    protected $newOrderCountingRecordService;
+    protected $queryConditionDay;
+    protected $queryConditionWeek;
+    protected $queryConditionMonth;
+    protected $queryConditionYear;
+    protected $ownerIds;
+    protected $cache_key = 'order_counting_records_';
+    protected $step_length = 1;
+    protected $orderCountingRecordIds = [];
+    protected $units = ['日', '月', '年'];
+    protected $orderIds;
+
+
+    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']));
+        $owners = factory(Owner::class)->times(2)->create();
+        $this->ownerIds = array_column($owners->toArray(), 'id');
+        $this->queryConditionDay = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subDays($this->step_length)->toDateString(), Carbon::now()->toDateString(), '日', $this->ownerIds);
+        $this->queryConditionMonth = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subMonths($this->step_length)->toDateString(), Carbon::now()->toDateString(), '月', $this->ownerIds);
+        $this->queryConditionYear = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subYears($this->step_length)->toDateString(), Carbon::now()->toDateString(), '年', $this->ownerIds);
+    }
+
+    protected function tearDown(): void
+    {
+        Owner::destroy($this->ownerIds);
+        OrderCountingRecord::destroy($this->orderCountingRecordIds);
+        Order::destroy($this->orderIds);
+        OrderCountingRecord::query()->whereIn('owner_id', $this->ownerIds)->delete();
+        parent::tearDown();
+    }
+
+    /**
+     * 缓存没有任何数据
+     * @test
+     */
+    public function cache_data_empty()
+    {
+        $result = $this->newOrderCountingRecordService->getFromCache($this->queryConditionDay);
+        $this->assertEquals([
+            0 => collect(),
+            ['unit' => '日',
+                'data' => [
+                    Carbon::now()->subDays(1)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],
+                    Carbon::now()->subDays(0)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],
+                ]]], $result);
+    }
+
+
+    /**
+     * 缓存命中全部数据
+     * @test
+     */
+    public function cache_data_all()
+    {
+        foreach ($this->ownerIds as $owner_id) {
+            for ( $i = 1; $i >= 0; $i--) {
+                $key = 'order_counting_records_' . Carbon::now()->subDays($i)->toDateString() . '_' . $owner_id . '_' . '日';
+                cache()->put($key, collect(['aaa']));
+            }
+        }
+        $result = $this->newOrderCountingRecordService->getFromCache($this->queryConditionDay);
+        $this->assertEquals([
+            0 => collect(['aaa','aaa','aaa','aaa']),
+            ['unit' => '日']], $result);
+    }
+
+    /**
+     * 缓存命中部分数据
+     * @test
+     */
+    public function cache_data_some()
+    {
+        foreach ($this->ownerIds as $owner_id) {
+            for ( $i = 0; $i >= 0; $i--) {
+                $key = 'order_counting_records_' . Carbon::now()->subDays($i)->toDateString() . '_' . $owner_id . '_' . '日';
+                cache()->put($key, collect(['aaa']));//缓存中放入当天的数据
+            }
+        }
+        $result = $this->newOrderCountingRecordService->getFromCache($this->queryConditionDay);
+        $this->assertEquals([
+            0 => collect(['aaa','aaa']),
+            ['unit' => '日',
+                'data' => [
+                    Carbon::now()->subDays(1)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],//昨天的数据没放在缓存,应该在data中返回
+                ]]], $result);
+    }
+}

+ 181 - 0
tests/Services/NewOrderCountingRecordService/GetFromLowerUnitTest.php

@@ -0,0 +1,181 @@
+<?php
+
+namespace NewOrderCountingRecordService;
+
+
+use App\Order;
+use App\OrderCountingRecord;
+use App\Owner;
+use App\Services\NewOrderCountingRecordService;
+use App\User;
+use Carbon\Carbon;
+use Tests\TestCase;
+
+class GetFromLowerUnitTest extends TestCase
+{
+    protected $newOrderCountingRecordService;
+    protected $queryConditionDay;
+    protected $queryConditionWeek;
+    protected $queryConditionMonth;
+    protected $queryConditionYear;
+    protected $ownerIds;
+    protected $cache_key = 'order_counting_records_';
+    protected $step_length = 1;
+    protected $orderCountingRecordIds = [];
+    protected $units = ['日', '月', '年'];
+    protected $orderIds;
+
+
+    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']));
+        $owners = factory(Owner::class)->times(2)->create();
+        $this->ownerIds = array_column($owners->toArray(), 'id');
+        $this->queryConditionDay = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subDays($this->step_length)->toDateString(), Carbon::now()->toDateString(), '日', $this->ownerIds);
+        $this->queryConditionMonth = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subMonths($this->step_length)->toDateString(), Carbon::now()->toDateString(), '月', $this->ownerIds);
+        $this->queryConditionYear = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subYears($this->step_length)->toDateString(), Carbon::now()->toDateString(), '年', $this->ownerIds);
+    }
+
+    protected function tearDown(): void
+    {
+        Owner::destroy($this->ownerIds);
+        OrderCountingRecord::destroy($this->orderCountingRecordIds);
+        Order::destroy($this->orderIds);
+        OrderCountingRecord::query()->whereIn('owner_id', $this->ownerIds)->delete();
+        parent::tearDown();
+    }
+
+    /**
+     * unit为月,中间表日为空,查询orders
+     * @test
+     */
+    public function unit_month_from_order()
+    {
+        $orders = collect();
+        foreach ($this->ownerIds as $ownerId) {
+            for ($i = 1; $i >= 0; $i--) {
+                $orders->push(factory(Order::class)->create([
+                    'created_at' => Carbon::now()->subMonths($i)->toDateString(),
+                    'owner_id' => $ownerId,
+                    'wms_status' => '订单完成',
+                ]));
+            }
+        }
+        $this->orderIds = array_column($orders->toArray(), 'id');
+
+        $result = $this->newOrderCountingRecordService->getFromLowerUnit($this->queryConditionMonth);
+        $this->assertEquals([1, 1, 1, 1],$result->pluck('amount')->toArray());
+    }
+
+    /**
+     * unit为年,中间表日为空,查询orders
+     * @test
+     */
+    public function unit_year_from_order()
+    {
+        $orders = collect();
+        foreach ($this->ownerIds as $ownerId) {
+            for ($i = 1; $i >= 0; $i--) {
+                $orders->push(factory(Order::class)->create([
+                    'created_at' => Carbon::now()->subYears($i)->toDateString(),
+                    'owner_id' => $ownerId,
+                    'wms_status' => '订单完成',
+                ]));
+            }
+        }
+        $this->orderIds = array_column($orders->toArray(), 'id');
+
+        $result = $this->newOrderCountingRecordService->getFromLowerUnit($this->queryConditionYear);
+        $this->assertEquals([1, 1, 1, 1],$result->pluck('amount')->toArray());
+    }
+
+    /**
+     * 插入中间表测试 月
+     * @test
+     */
+    public function unit_month_from_order_insert()
+    {
+        $orders = collect();
+        foreach ($this->ownerIds as $ownerId) {
+            for ($i = 1; $i >= 0; $i--) {
+                $orders->push(factory(Order::class)->create([
+                    'created_at' => Carbon::now()->subMonths($i)->toDateString(),
+                    'owner_id' => $ownerId,
+                    'wms_status' => '订单完成',
+                ]));
+            }
+        }
+        $this->orderIds = array_column($orders->toArray(), 'id');
+
+        $this->assertDatabaseMissing('order_counting_records', [
+            'date_target' =>Carbon::now()->subMonths(1)->startOfMonth()->toDateString(),
+            'counting_unit' => '月',
+            'owner_id' => $this->ownerIds[0],
+        ]);
+        $this->assertDatabaseMissing('order_counting_records', [
+            'date_target' =>Carbon::now()->subMonths(1)->startOfMonth()->toDateString(),
+            'counting_unit' => '月',
+            'owner_id' => $this->ownerIds[1],
+        ]);
+
+        $this->newOrderCountingRecordService->getFromLowerUnit($this->queryConditionMonth);
+
+        $this->assertDatabaseHas('order_counting_records', [
+            'date_target' =>Carbon::now()->subMonths(1)->startOfMonth()->toDateString(),
+            'counting_unit' => '月',
+            'owner_id' => $this->ownerIds[0],
+        ]);
+
+        $this->assertDatabaseHas('order_counting_records', [
+            'date_target' =>Carbon::now()->subMonths(1)->startOfMonth()->toDateString(),
+            'counting_unit' => '月',
+            'owner_id' => $this->ownerIds[1],
+        ]);
+    }
+
+    /**
+     * 插入中间表测试 年
+     * @test
+     */
+    public function unit_year_from_order_insert()
+    {
+        $orders = collect();
+        foreach ($this->ownerIds as $ownerId) {
+            for ($i = 1; $i >= 0; $i--) {
+                $orders->push(factory(Order::class)->create([
+                    'created_at' => Carbon::now()->subYears($i)->toDateString(),
+                    'owner_id' => $ownerId,
+                    'wms_status' => '订单完成',
+                ]));
+            }
+        }
+        $this->orderIds = array_column($orders->toArray(), 'id');
+
+        $this->assertDatabaseMissing('order_counting_records', [
+            'date_target' =>Carbon::now()->subYears(1)->startOfYear()->toDateString(),
+            'counting_unit' => '年',
+            'owner_id' => $this->ownerIds[0],
+        ]);
+        $this->assertDatabaseMissing('order_counting_records', [
+            'date_target' =>Carbon::now()->subYears(1)->startOfYear()->toDateString(),
+            'counting_unit' => '年',
+            'owner_id' => $this->ownerIds[1],
+        ]);
+
+        $this->newOrderCountingRecordService->getFromLowerUnit($this->queryConditionYear);
+        $this->assertDatabaseHas('order_counting_records', [
+            'date_target' =>Carbon::now()->subYears(1)->startOfYear()->toDateString(),
+            'counting_unit' => '年',
+            'owner_id' => $this->ownerIds[0],
+        ]);
+
+        $this->assertDatabaseHas('order_counting_records', [
+            'date_target' =>Carbon::now()->subYears(1)->startOfYear()->toDateString(),
+            'counting_unit' => '年',
+            'owner_id' => $this->ownerIds[1],
+        ]);
+    }
+}

+ 119 - 0
tests/Services/NewOrderCountingRecordService/GetFromMiddleTableTest.php

@@ -0,0 +1,119 @@
+<?php
+
+namespace NewOrderCountingRecordService;
+
+
+use App\Order;
+use App\OrderCountingRecord;
+use App\Owner;
+use App\Services\NewOrderCountingRecordService;
+use App\User;
+use Carbon\Carbon;
+use Illuminate\Database\Eloquent\Collection;
+use Tests\TestCase;
+
+class GetFromMiddleTableTest extends TestCase
+{
+    protected $newOrderCountingRecordService;
+    protected $queryConditionDay;
+    protected $queryConditionWeek;
+    protected $queryConditionMonth;
+    protected $queryConditionYear;
+    protected $ownerIds;
+    protected $cache_key = 'order_counting_records_';
+    protected $step_length = 1;
+    protected $orderCountingRecordIds = [];
+    protected $units = ['日', '月', '年'];
+    protected $orderIds;
+
+
+    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']));
+        $owners = factory(Owner::class)->times(2)->create();
+        $this->ownerIds = array_column($owners->toArray(), 'id');
+        $this->queryConditionDay = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subDays($this->step_length)->toDateString(), Carbon::now()->toDateString(), '日', $this->ownerIds);
+        $this->queryConditionMonth = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subMonths($this->step_length)->toDateString(), Carbon::now()->toDateString(), '月', $this->ownerIds);
+        $this->queryConditionYear = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subYears($this->step_length)->toDateString(), Carbon::now()->toDateString(), '年', $this->ownerIds);
+    }
+
+    protected function tearDown(): void
+    {
+        Owner::destroy($this->ownerIds);
+        OrderCountingRecord::destroy($this->orderCountingRecordIds);
+        Order::destroy($this->orderIds);
+        OrderCountingRecord::query()->whereIn('owner_id', $this->ownerIds)->delete();
+        parent::tearDown();
+    }
+
+    /**
+     * 中间表没有任何数据
+     * @test
+     */
+    public function data_empty()
+    {
+        $result = $this->newOrderCountingRecordService->getFromMiddleTable($this->queryConditionDay);
+        $this->assertEquals([
+            0 => new Collection(),
+            ['unit' => '日',
+                'data' => [
+                    Carbon::now()->subDays(1)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],
+                    Carbon::now()->subDays(0)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],
+                ]]], $result);
+    }
+
+
+    /**
+     * 中间表命中全部数据
+     * @test
+     */
+    public function data_all()
+    {
+        $orderCountingRecords = collect();
+        foreach ($this->ownerIds as $owner_id) {
+            for ($i = 1; $i >= 0; $i--) {
+                $orderCountingRecords->push(factory(OrderCountingRecord::class)->create([
+                    'date_target' => Carbon::now()->subDays($i)->toDateString(),
+                    'owner_id' => $owner_id,
+                ]));
+            }
+        }
+        $this->orderCountingRecordIds = array_column($orderCountingRecords->toArray(), 'id');
+        $result = $this->newOrderCountingRecordService->getFromMiddleTable($this->queryConditionDay);
+        $orderCountingRecords = OrderCountingRecord::query()->whereIn('owner_id', $this->ownerIds)->orderByDesc('owner_id')->get();
+
+        $this->assertEquals([
+            0 => $orderCountingRecords,
+            ['unit' => '日','data'=>[]]], $result);
+    }
+
+    /**
+     * 中间表命中部分数据
+     * @test
+     */
+    public function data_some()
+    {
+        $orderCountingRecords = collect();
+        foreach ($this->ownerIds as $owner_id) {
+            for ($i = 0; $i >= 0; $i--) {
+                $orderCountingRecords->push(factory(OrderCountingRecord::class)->create([
+                    'date_target' => Carbon::now()->subDays($i)->toDateString(),
+                    'owner_id' => $owner_id,
+                ]));
+            }
+        }
+        $this->orderCountingRecordIds = array_column($orderCountingRecords->toArray(), 'id');
+
+        $result = $this->newOrderCountingRecordService->getFromMiddleTable($this->queryConditionDay);
+        $orderCountingRecords = OrderCountingRecord::query()->whereIn('owner_id', $this->ownerIds)->orderByDesc('owner_id')->get();
+
+        $this->assertEquals([
+            0 => $orderCountingRecords,
+            ['unit' => '日','data' => [
+                Carbon::now()->subDays(1)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],//昨天的数据没放在缓存,应该在data中返回
+            ]]], $result);
+    }
+}