瀏覽代碼

实时待处理订单 根据货主权限筛选 添加缓存

ANG YU 5 年之前
父節點
當前提交
80f7ea5d91
共有 1 個文件被更改,包括 22 次插入14 次删除
  1. 22 14
      app/Services/RealtimePendingOrdersService.php

+ 22 - 14
app/Services/RealtimePendingOrdersService.php

@@ -5,25 +5,33 @@ namespace App\Services;
 
 
 use App\Order;
+use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\DB;
 
 class RealtimePendingOrdersService
 {
     public function warehousesOrders($start = null, $end = null)
     {
-        $start = $start ?? date('Y-m-d 00:00:00');
-        $end = $end ?? date('Y-m-d 23:59:59');
-        return Order::query()->selectRaw("warehouses.name," .
-            "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, " .
-            "count(case wms_status when '部分装箱' then 1 end) as partPacking, " .
-            "count(case wms_status when '播种完成' then 1 end) as sowComplete, " .
-            "count(1) as total"
-        )->whereBetween('orders.created_at', [$start, $end,])
-            ->whereIn('wms_status', ['创建订单', '分配完成', '部分分配', '部分装箱', '播种完成'])
-            ->leftJoin('warehouses', 'orders.warehouse_id', 'warehouses.id')
-            ->groupBy('warehouse_id')
-            ->get();
+        return Cache::get('RealTimePendingOrders' . Auth::id(), function () use ($start, $end) {
+            $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," .
+                "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, " .
+                "count(case wms_status when '部分装箱' then 1 end) as partPacking, " .
+                "count(case wms_status when '播种完成' then 1 end) as sowComplete, " .
+                "count(1) as total"
+            )->whereBetween('orders.created_at', [$start, $end,])
+                ->whereIn('wms_status', ['创建订单', '分配完成', '部分分配', '部分装箱', '播种完成'])
+                ->leftJoin('warehouses', 'orders.warehouse_id', 'warehouses.id')
+                ->groupBy('warehouse_id');
+            if ($ownerIds) $builders->whereIn('owner_id', $ownerIds);
+            $builders = $builders->get();
+            Cache::put('RealTimePendingOrders' . Auth::id(), $builders);
+            return $builders;
+        });
     }
 }