Преглед изворни кода

控制台当天数据优化

ANG YU пре 5 година
родитељ
комит
662593e340

+ 28 - 8
app/Services/OrderCountingRecordService.php

@@ -30,7 +30,7 @@ class OrderCountingRecordService
     /**
      * @param $start
      * @param $end
-     * @param null $ownerIds
+     * @param null $ownerIds
      * @param string $unit
      * @param $user
      * @return mixed
@@ -49,7 +49,7 @@ class OrderCountingRecordService
     public function orderCountingRecords($start, $end, $ownerIds = null, $unit = '日', $user = null)
     {
         $key = 'orderCountingRecords_' . $start . '_' . $end . '_' . $unit . '_' . Auth::user()->id;
-        return Cache::remember($key, 600, function () use ($start, $end, $unit, $ownerIds, $user) {
+        return Cache::remember($key, 1, function () use ($start, $end, $unit, $ownerIds, $user) {
             $orders = $this->get($start, $end, null, $unit, null);
             $dataList = collect();
             $orders->groupBy('date_target')->each(function ($items) use (&$dataList, $unit) {
@@ -203,7 +203,8 @@ class OrderCountingRecordService
         foreach ($targetOwnerIdsUnderDates as $dateStr => $ownerIds) {
             foreach ($ownerIds as $key => $ownerId) {
                 $key1 = 'owner_id=' . $ownerId . ' date_target' . $dateStr;
-                if (isset($map[$key1])) {
+                if (isset($map[$key1])&&$this->isNotCurrentDate($dateStr)) {
+                    //中间表查找到数据,并且时间不是当前日期
                     unset($targetOwnerIdsUnderDates[$dateStr][$key]);
                 }
             }
@@ -228,15 +229,14 @@ class OrderCountingRecordService
                 break;
         }
         $result = collect();
-
+        $isHasCurrentDate = false;
+        $currentDate = null;
         foreach ($targetOwnerIdsUnderDates as $dateStr => $ownerIds) {
-            if ($dateStr != Carbon::now()->format('Y-m-d')
-                || $dateStr != Carbon::now()->year . '-' . Carbon::now()->week
-                ||$dateStr != Carbon::now()->year . '-' . Carbon::now()->month)
+            if ($this->isNotCurrentDate($dateStr))
             {
                 foreach ($ownerIds as $ownerId) {
                     if ($resultOrders->where('date_target', $dateStr)->where('owner_id', $ownerId)->count()==0) {
-                        $result->push([
+                       $result->push([
                             'owner_id' =>$ownerId,
                             'shop_id' => null,
                             'warehouse_id' => null,
@@ -247,6 +247,10 @@ class OrderCountingRecordService
                         ]);
                     }
                 }
+            } else {
+                //查询范围包含当天
+                $isHasCurrentDate = true;
+                $currentDate = $dateStr;
             }
         }
         $resultOrders->each(function ($order) use (&$result, $unit) {
@@ -261,6 +265,11 @@ class OrderCountingRecordService
             ]);
             $result->push($orderCountingRecord);
         });
+        if ($isHasCurrentDate) {
+            //删除OrderCountingRecord表中与当前日期相关的数据,之后的批量插入重新插入这部分数据
+            dump($currentDate,$unit);
+            OrderCountingRecord::query()->where('date_target', $currentDate)->where('counting_unit',$unit)->delete();
+        }
         OrderCountingRecord::query()->insert($result->toArray());
         return ['resultOrders' => $result];
     }
@@ -358,4 +367,15 @@ class OrderCountingRecordService
         }
         return $orderSqlBuilder->groupBy(['owner_id', 'warehouse_id', 'shop_id', 'logistic_id', 'date_target'])->get();
     }
+
+    /**
+     * @param $dateStr
+     * @return bool
+     */
+    public function isNotCurrentDate($dateStr): bool
+    {
+        return $dateStr != Carbon::now()->format('Y-m-d')
+            && $dateStr != Carbon::now()->year . '-' . Carbon::now()->week
+            && $dateStr != Carbon::now()->year . '-' . Carbon::now()->month;
+    }
 }

+ 1 - 1
app/Services/RealtimePendingOrdersService.php

@@ -40,7 +40,7 @@ class RealtimePendingOrdersService
         $start = $start ?? date('Y-m-d 00:00:00');
         $end = $end ?? date('Y-m-d 23:59:59');
         $ownerIds = app('OwnerService')->getSelectionId();
-        $builders = Order::query()->selectRaw("warehouses.name," .
+        $builders = Order::query()->selectRaw("warehouses.code," .
             "count(case wms_status when '创建订单' then 1 end) as createOrder, " .
             "count(case wms_status when '分配完成' then 1 end) as assignedComplete, " .
             "count(case wms_status when '部分分配' then 1 end) as partialAllocation, " .

+ 1 - 1
package-lock.json

@@ -6254,7 +6254,7 @@
         },
         "minimist": {
             "version": "1.2.0",
-            "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz",
+            "resolved": "https://registry.npm.taobao.org/minimist/download/minimist-1.2.0.tgz",
             "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=",
             "dev": true
         },

+ 67 - 4
tests/Services/OrderCountingRecordService/OrderCountingRecordServiceGetTest.php

@@ -2,12 +2,19 @@
 
 namespace Tests\Services\OrderCountingRecordService;
 
+use App\Order;
+use App\Owner;
 use App\Services\CacheService;
 use App\Services\OrderCountingRecordService;
+use App\User;
+use Carbon\Carbon;
+use Illuminate\Foundation\Testing\RefreshDatabase;
 use Tests\TestCase;
 
 class OrderCountingRecordServiceGetTest extends TestCase
 {
+    use  RefreshDatabase;
+
     /** @var OrderCountingRecordService $orderCountingRecordService */
     public $orderCountingRecordService;
 
@@ -19,12 +26,68 @@ class OrderCountingRecordServiceGetTest extends TestCase
     }
 
 
-    public function testGet()
+    /**
+     * @test
+     */
+    public function current_date()
+    {
+        $start = Carbon::now()->subDays(2)->format('Y-m-d');
+        $end = (new Carbon())->toDateString();
+
+        $this->actingAs(factory(User::class)->create(['name' => 'yang']));
+
+        $owner = factory(Owner::class)->create();
+        $orders = factory(Order::class)->times(20)->create([
+            'created_at' => Carbon::now(),
+            'owner_id' => $owner->id,
+            'wms_status'=>'订单完成']);
+        $orders = factory(Order::class)->times(10)->create([
+            'created_at' => Carbon::now()->subDays(1),
+            'owner_id' => $owner->id,
+            'wms_status'=>'订单完成']);
+        $result = $this->orderCountingRecordService->orderCountingRecords($start, $end)->toArray();
+        $this->assertEquals([0,10,20], array_column($result, 'counter'));
+        cache()->flush();
+        $orders = factory(Order::class)->times(20)->create([
+            'created_at' => Carbon::now(),
+            'owner_id' => $owner->id,
+            'wms_status'=>'订单完成']);
+        $result = $this->orderCountingRecordService->orderCountingRecords($start, $end)->toArray();
+        $this->assertEquals([0,10,40], array_column($result, 'counter'));
+        cache()->flush();
+
+    }
+
+    /**
+     * @test
+     */
+    public function set_null()
     {
-        $start = '2020-11-05';
-        $end = '2020-11-06';
-        dd($this->orderCountingRecordService->get($start, $end, null));
+        $dateStr = '2020-11-26';
+        $this->assertTrue($dateStr != Carbon::now()->format('Y-m-d'));
+    }
+
+    /**
+     * @test
+     */
+    public function isNotCurrentDate()
+    {
+        $day_ture =$this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->subDay()->format('Y-m-d'));
+        $day_false =$this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->format('Y-m-d'));
+
+        $week_ture =$this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->subWeek()->year . '-' . Carbon::now()->subWeek()->week);
+        $week_false =$this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->year . '-' . Carbon::now()->week);
+
+        $month_true =$this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->subMonth()->year.'-'. Carbon::now()->subMonth()->month);
+        $month_false =$this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->year.'-'. Carbon::now()->month);
+
+        $this->assertTrue($day_ture);
+        $this->assertFalse($day_false);
 
+        $this->assertTrue($week_ture);
+        $this->assertFalse($week_false);
 
+        $this->assertTrue($month_true);
+        $this->assertFalse($month_false);
     }
 }