cqp 1 天之前
父节点
当前提交
ee53a1032e
共有 4 个文件被更改,包括 544 次插入152 次删除
  1. 12 0
      app/Http/Controllers/Api/LargeScreenController.php
  2. 6 1
      app/Model/DeviceOrderInfo.php
  3. 524 150
      app/Service/LargeScreenService.php
  4. 2 1
      routes/api.php

+ 12 - 0
app/Http/Controllers/Api/LargeScreenController.php

@@ -42,4 +42,16 @@ class LargeScreenController extends BaseController
             return $this->json_return(201,$data);
         }
     }
+
+    public function deviceDashboard(Request $request){
+        $service = new LargeScreenService();
+//        $userData = $request->userData->toArray();
+        list($status,$data) = $service->deviceDashboard($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
 }

+ 6 - 1
app/Model/DeviceOrderInfo.php

@@ -17,8 +17,13 @@ class DeviceOrderInfo extends Model
 
 
     public static $prefix = [
-        self::Model_type_zero => '',
+        self::Model_type_zero => '待处理',
         self::Model_type_one => '正常',
         self::Model_type_two => '异常',
     ];
+
+    public static $prefix_2 = [
+        self::Model_type_zero => '待处理',
+        self::Model_type_two => '已处理',
+    ];
 }

+ 524 - 150
app/Service/LargeScreenService.php

@@ -4,74 +4,76 @@ namespace App\Service;
 
 use App\Model\ApplyOrder;
 use App\Model\ApplyOrderDetail;
+use App\Model\DeviceOrder;
+use App\Model\DeviceOrderInfo;
 use App\Model\DispatchSub;
+use App\Model\DXJ;
+use App\Model\Employee;
 use App\Model\Equipment;
 use App\Model\OrdersProduct;
 use App\Model\Process;
+use App\Model\ReportWorkingDetail;
 use App\Model\SaleOrdersProduct;
 use App\Model\Scrapp;
 use App\Model\ScrappCount;
+use App\Model\SystemL;
+use Carbon\Carbon;
 use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Redis;
 
 class LargeScreenService extends Service
 {
-    public function integratedDashboard($data){
+    public function integratedDashboard($data)
+    {
         //总产值 ------------------------------------------------
-        $total = SaleOrdersProduct::where('del_time',0)
+        $total = SaleOrdersProduct::where('del_time', 0)
             ->where('crt_time', ">=", strtotime(date("Y-01-01 00:00:00")))
             ->select(DB::raw("sum(finished_num) as total"))
             ->value('total');
         //总产值 ------------------------------------------------
 
         //当日产量 当月产量 ---------------------------------------------
-        $data_one = SaleOrdersProduct::where('del_time',0)
+        $data_one = SaleOrdersProduct::where('del_time', 0)
             ->where('crt_time', ">=", strtotime(date("Y-m-01 00:00:00")))
             ->where('crt_time', "<=", strtotime(date("Y-m-t 23:59:59")))
-            ->select('finished_num','order_quantity','crt_time')
+            ->select('finished_num', 'order_quantity', 'crt_time')
             ->get()->toArray();
         //当日产量 当月产量
         $today_total = $month_total = $order_total = 0;
         $timeStamp = strtotime(date("Y-m-d 00:00:00"));
         $timeStampE = strtotime(date("Y-m-d 23:59:59"));
-        foreach ($data_one as $value){
-            $order_total = bcadd($order_total, $value['order_quantity'],3);
-            $month_total = bcadd($month_total, $value['finished_num'],3);
-            if($timeStamp <= $value['crt_time'] && $timeStampE >= $value['crt_time']) $today_total = bcadd($today_total, $value['finished_num'],3);
+        foreach ($data_one as $value) {
+            $order_total = bcadd($order_total, $value['order_quantity'], 3);
+            $month_total = bcadd($month_total, $value['finished_num'], 3);
+            if ($timeStamp <= $value['crt_time'] && $timeStampE >= $value['crt_time']) $today_total = bcadd($today_total, $value['finished_num'], 3);
         }
         //当日产量 当月产量 ---------------------------------------------
 
         //月计划完成率--------------------------------------------------------------------
-        $month_rate = floatval($order_total) > 0 ? bcdiv($month_total, $order_total,4) : 0;
-        $month_rate = bcmul($month_rate,100,2) . "%";
+        $month_rate = floatval($order_total) > 0 ? bcdiv($month_total, $order_total, 4) : 0;
+        $month_rate = bcmul($month_rate, 100, 2) . "%";
         //月计划完成率--------------------------------------------------------------------
 
         //月注塑合格率----------------------------------------------------------------------
-        $data_two = ScrappCount::where('del_time',0)
+        $data_two = ScrappCount::where('del_time', 0)
             ->where('crt_time', ">=", strtotime(date("Y-m-01 00:00:00")))
             ->where('crt_time', "<=", strtotime(date("Y-m-t 23:59:59")))
             ->where('process_id', 14)
             ->select(DB::raw("sum(quantity) as quantity"), DB::raw("sum(scrapp_num) as waste"))
             ->get()->toArray();
         $total_zj = $total_blp = 0;
-        foreach ($data_two as $value){
-            $total_zj = bcadd($total_zj, $value['quantity'],3);
-            $total_zj = bcadd($total_zj, $value['waste'],3);
-            $total_blp = bcadd($total_blp, $value['waste'],3);
-        }
-        $process_14_total = bcadd($total_zj, $total_blp,3);
-        $process_14_rate = floatval($process_14_total) > 0 ? bcdiv($total_zj, $process_14_total,4) : 0;
-        $process_14_rate = bcmul($process_14_rate, 100,2) . "%";
+        foreach ($data_two as $value) {
+            $total_zj = bcadd($total_zj, $value['quantity'], 3);
+            $total_zj = bcadd($total_zj, $value['waste'], 3);
+            $total_blp = bcadd($total_blp, $value['waste'], 3);
+        }
+        $process_14_total = bcadd($total_zj, $total_blp, 3);
+        $process_14_rate = floatval($process_14_total) > 0 ? bcdiv($total_zj, $process_14_total, 4) : 0;
+        $process_14_rate = bcmul($process_14_rate, 100, 2) . "%";
         //月注塑合格率----------------------------------------------------------------------
 
         //设备监控状态----------------------------------------------------------------------
-        $deviceList = Equipment::where('del_time',0)
-            ->where('model','<>',1)
-            ->select('id','title','code','status')
-            ->get()->toArray();
-        foreach ($deviceList as $key => $value){
-            $status_title = Equipment::$status_title[$value['status']] ?? "";
-            $deviceList[$key]['status_title'] = $status_title;
-        }
+        $deviceList = $this->deviceState();
         //设备监控状态----------------------------------------------------------------------
 
         //质量管理--------------------------------------------------------------------------
@@ -82,19 +84,19 @@ class LargeScreenService extends Service
             $dates[] = $date;
         }
         $zl_data = [];
-        $data_three = ScrappCount::where('del_time',0)
+        $data_three = ScrappCount::where('del_time', 0)
             ->where('crt_time', "<=", time())
-            ->where('crt_time', ">=", strtotime(date("Y-m-d 00:00:00",time() - 7 * 24 * 60 * 60)))
+            ->where('crt_time', ">=", strtotime(date("Y-m-d 00:00:00", time() - 7 * 24 * 60 * 60)))
             ->where('process_id', 14)
             ->select('quantity', "scrapp_num as waste", "crt_time")
             ->get()->toArray();
-        foreach ($data_three as $value){
+        foreach ($data_three as $value) {
             $day = date("m-d", $value['crt_time']);
             $tmp = bcadd($value['quantity'], $value['waste'], 3);
-            if(isset($zl_data[$day])){
-                $zl_data[$day]['quantity'] = bcadd($zl_data[$day]['quantity'], $value['quantity'],3);
-                $zl_data[$day]['total'] = bcadd($zl_data[$day]['total'], $tmp,3);
-            }else{
+            if (isset($zl_data[$day])) {
+                $zl_data[$day]['quantity'] = bcadd($zl_data[$day]['quantity'], $value['quantity'], 3);
+                $zl_data[$day]['total'] = bcadd($zl_data[$day]['total'], $tmp, 3);
+            } else {
                 $zl_data[$day] = [
                     'quantity' => $value['quantity'],
                     'total' => $tmp
@@ -102,15 +104,15 @@ class LargeScreenService extends Service
             }
         }
         $zj_data_final = [];
-        foreach ($dates as $value){
+        foreach ($dates as $value) {
             $tmp = [
                 "day" => $value,
                 "total" => 0,
                 "rate" => 0,
             ];
-            if(isset($zl_data[$value])){
+            if (isset($zl_data[$value])) {
                 $tmp['total'] = $zl_data[$value]['total'];
-                $tmp['rate'] = bcmul(bcdiv($zl_data[$value]['quantity'], $zl_data[$value]['total'],4),100,2);
+                $tmp['rate'] = bcmul(bcdiv($zl_data[$value]['quantity'], $zl_data[$value]['total'], 4), 100, 2);
             }
             $zj_data_final[] = $tmp;
         }
@@ -132,11 +134,11 @@ class LargeScreenService extends Service
         //物料管理--------------------------------------------------------------------------
 
         //销售管理--------------------------------------------------------------------------
-        $xs = SaleOrdersProduct::where('del_time',0)
+        $xs = SaleOrdersProduct::where('del_time', 0)
             ->where('crt_time', ">=", strtotime(date("Y-01-01 00:00:00")))
-            ->select(DB::raw("sum(finished_num) as total"),'technology_name as color')
+            ->select(DB::raw("sum(finished_num) as total"), 'technology_name as color')
             ->groupBy('technology_name')
-            ->orderBy('total','desc')
+            ->orderBy('total', 'desc')
             ->get()->toArray();
         //销售管理--------------------------------------------------------------------------
 
@@ -146,86 +148,86 @@ class LargeScreenService extends Service
         $zx_14_dispatch_quantity_9 = $zx_14_finished_num_9 = 0;
         $zx_14_dispatch_quantity_12 = $zx_14_finished_num_12 = 0;
         $zx_14_dispatch_quantity_13 = $zx_14_finished_num_13 = 0;
-        $dispatch = DispatchSub::where('del_time',0)
+        $dispatch = DispatchSub::where('del_time', 0)
             ->where('crt_time', ">=", strtotime(date("Y-01-01 00:00:00")))
-            ->whereIn('process_id',[12,14])
-            ->select('dispatch_quantity', 'finished_num', 'process_id','device_id')
+            ->whereIn('process_id', [12, 14])
+            ->select('dispatch_quantity', 'finished_num', 'process_id', 'device_id')
             ->get()->toArray();
-        foreach ($dispatch as $value){
-            if($value['process_id'] == 12){
-                $zx_12_dispatch_quantity = bcadd($zx_12_dispatch_quantity, $value['dispatch_quantity'],3);
-                $zx_12_finished_num = bcadd($zx_12_finished_num, $value['finished_num'],3);
-            }else{
-                if($value['device_id'] == 8){
-                    $zx_14_dispatch_quantity_8 = bcadd($zx_14_dispatch_quantity_8, $value['dispatch_quantity'],3);
-                    $zx_14_finished_num_8 = bcadd($zx_14_finished_num_8, $value['finished_num'],3);
-                }elseif ($value['device_id'] == 9){
-                    $zx_14_dispatch_quantity_9 = bcadd($zx_14_dispatch_quantity_9, $value['dispatch_quantity'],3);
-                    $zx_14_finished_num_9 = bcadd($zx_14_finished_num_9, $value['finished_num'],3);
-                }elseif ($value['device_id'] == 12){
-                    $zx_14_dispatch_quantity_12 = bcadd($zx_14_dispatch_quantity_12, $value['dispatch_quantity'],3);
-                    $zx_14_finished_num_12 = bcadd($zx_14_finished_num_12, $value['finished_num'],3);
-                }elseif ($value['device_id'] == 13){
-                    $zx_14_dispatch_quantity_13 = bcadd($zx_14_dispatch_quantity_13, $value['dispatch_quantity'],3);
-                    $zx_14_finished_num_13 = bcadd($zx_14_finished_num_13, $value['finished_num'],3);
+        foreach ($dispatch as $value) {
+            if ($value['process_id'] == 12) {
+                $zx_12_dispatch_quantity = bcadd($zx_12_dispatch_quantity, $value['dispatch_quantity'], 3);
+                $zx_12_finished_num = bcadd($zx_12_finished_num, $value['finished_num'], 3);
+            } else {
+                if ($value['device_id'] == 8) {
+                    $zx_14_dispatch_quantity_8 = bcadd($zx_14_dispatch_quantity_8, $value['dispatch_quantity'], 3);
+                    $zx_14_finished_num_8 = bcadd($zx_14_finished_num_8, $value['finished_num'], 3);
+                } elseif ($value['device_id'] == 9) {
+                    $zx_14_dispatch_quantity_9 = bcadd($zx_14_dispatch_quantity_9, $value['dispatch_quantity'], 3);
+                    $zx_14_finished_num_9 = bcadd($zx_14_finished_num_9, $value['finished_num'], 3);
+                } elseif ($value['device_id'] == 12) {
+                    $zx_14_dispatch_quantity_12 = bcadd($zx_14_dispatch_quantity_12, $value['dispatch_quantity'], 3);
+                    $zx_14_finished_num_12 = bcadd($zx_14_finished_num_12, $value['finished_num'], 3);
+                } elseif ($value['device_id'] == 13) {
+                    $zx_14_dispatch_quantity_13 = bcadd($zx_14_dispatch_quantity_13, $value['dispatch_quantity'], 3);
+                    $zx_14_finished_num_13 = bcadd($zx_14_finished_num_13, $value['finished_num'], 3);
                 }
             }
         }
-        $diff_num = bcsub($zx_12_dispatch_quantity, $zx_12_finished_num,3);
-        $diff_num_8 = bcsub($zx_14_dispatch_quantity_8, $zx_14_finished_num_8,3);
-        $diff_num_9 = bcsub($zx_14_dispatch_quantity_9, $zx_14_finished_num_9,3);
-        $diff_num_12 = bcsub($zx_14_dispatch_quantity_12, $zx_14_finished_num_12,3);
-        $diff_num_13 = bcsub($zx_14_dispatch_quantity_13, $zx_14_finished_num_13,3);
+        $diff_num = bcsub($zx_12_dispatch_quantity, $zx_12_finished_num, 3);
+        $diff_num_8 = bcsub($zx_14_dispatch_quantity_8, $zx_14_finished_num_8, 3);
+        $diff_num_9 = bcsub($zx_14_dispatch_quantity_9, $zx_14_finished_num_9, 3);
+        $diff_num_12 = bcsub($zx_14_dispatch_quantity_12, $zx_14_finished_num_12, 3);
+        $diff_num_13 = bcsub($zx_14_dispatch_quantity_13, $zx_14_finished_num_13, 3);
 
         $xt = [
             [
                 "name" => "清洗脱水产线", "sj_num" => $zx_12_finished_num, "jh_num" => $zx_12_dispatch_quantity, "diff_num" => $diff_num
-            ],[
+            ], [
                 "name" => "注塑一号产线", "sj_num" => $zx_14_finished_num_8, "jh_num" => $zx_14_dispatch_quantity_8, "diff_num" => $diff_num_8
-            ],[
+            ], [
                 "name" => "注塑二号产线", "sj_num" => $zx_14_finished_num_9, "jh_num" => $zx_14_dispatch_quantity_9, "diff_num" => $diff_num_9
-            ],[
+            ], [
                 "name" => "注塑三号产线", "sj_num" => $zx_14_finished_num_12, "jh_num" => $zx_14_dispatch_quantity_12, "diff_num" => $diff_num_12
-            ],[
+            ], [
                 "name" => "注塑四号产线", "sj_num" => $zx_14_finished_num_13, "jh_num" => $zx_14_dispatch_quantity_13, "diff_num" => $diff_num_13
             ],
         ];
         //总装各线体目标/完成量---------------------------------------------------------------
 
         //订单完成进度-----------------------------------------------------------------
-        $orderBy = [11 => "破碎分离", 12 => "清洗脱水" ,15 => "混合搅拌",13 => "挤出造粒",14 => "注塑成型"];
-        $order = OrdersProduct::where('del_time',0)
-            ->select('production_no as order_number','id','technology_name as color','production_quantity','dispatch_complete_quantity','finished_num')
-            ->orderBy('id','desc')
+        $orderBy = [11 => "破碎分离", 12 => "清洗脱水", 15 => "混合搅拌", 13 => "挤出造粒", 14 => "注塑成型"];
+        $order = OrdersProduct::where('del_time', 0)
+            ->select('production_no as order_number', 'id', 'technology_name as color', 'production_quantity', 'dispatch_complete_quantity', 'finished_num')
+            ->orderBy('id', 'desc')
             ->limit(10)
             ->get()->toArray();
-        $dispatch_num = DispatchSub::where('del_time',0)
-            ->whereIn('order_product_id',array_column($order,'id'))
-            ->select('order_product_id','process_id','finished_num','dispatch_quantity','waste_num')
+        $dispatch_num = DispatchSub::where('del_time', 0)
+            ->whereIn('order_product_id', array_column($order, 'id'))
+            ->select('order_product_id', 'process_id', 'finished_num', 'dispatch_quantity', 'waste_num')
             ->get()->toArray();
         $details = [];
-        foreach ($dispatch_num as $value){
+        foreach ($dispatch_num as $value) {
             $new = $value['order_product_id'] . $value['process_id'];
-            $finished_num = bcadd($value['finished_num'], $value['waste_num'],3);
-            if(isset($details[$new])){
-                $details[$new]['dispatch_quantity'] = bcadd($details[$new]['dispatch_quantity'], $value['dispatch_quantity'],3);
-                $details[$new]['finished_num'] = bcadd($details[$new]['finished_num'], $finished_num,3);
-            }else{
+            $finished_num = bcadd($value['finished_num'], $value['waste_num'], 3);
+            if (isset($details[$new])) {
+                $details[$new]['dispatch_quantity'] = bcadd($details[$new]['dispatch_quantity'], $value['dispatch_quantity'], 3);
+                $details[$new]['finished_num'] = bcadd($details[$new]['finished_num'], $finished_num, 3);
+            } else {
                 $details[$new] = [
                     "dispatch_quantity" => $value['dispatch_quantity'],
                     "finished_num" => $finished_num,
                 ];
             }
         }
-        foreach ($order as $key => $value){
+        foreach ($order as $key => $value) {
             $d_tmp = [];
-            foreach ($orderBy as $k => $v){
+            foreach ($orderBy as $k => $v) {
                 $new = $value['id'] . $k;
                 $tmpS = $details[$new] ?? [];
                 $rate = 0;
-                if(! empty($tmpS)) {
-                    $rate = bcdiv($tmpS['finished_num'], $tmpS['dispatch_quantity'],4);
-                    $rate = bcmul($rate, 100,2);
+                if (!empty($tmpS)) {
+                    $rate = bcdiv($tmpS['finished_num'], $tmpS['dispatch_quantity'], 4);
+                    $rate = bcmul($rate, 100, 2);
                 }
                 $d_tmp[] = [
                     "name" => $v,
@@ -233,9 +235,9 @@ class LargeScreenService extends Service
                 ];
             }
             $order[$key]['detail'] = $d_tmp;
-            if($value['production_quantity'] <= $value['finished_num']){
+            if ($value['production_quantity'] <= $value['finished_num']) {
                 $state = "已完成";
-            }else{
+            } else {
                 $state = "进行中";
             }
             $order[$key]['state_title'] = $state;
@@ -260,29 +262,43 @@ class LargeScreenService extends Service
         return [true, $return];
     }
 
-    public function productionExecution($data){
+    private function deviceState(){
+        $deviceList = Equipment::where('del_time', 0)
+            ->where('model', '<>', 1)
+            ->select('id', 'title', 'code', 'status')
+            ->get()->toArray();
+        foreach ($deviceList as $key => $value) {
+            $status_title = Equipment::$status_title[$value['status']] ?? "";
+            $deviceList[$key]['status_title'] = $status_title;
+        }
+
+        return $deviceList;
+    }
+
+    public function productionExecution($data)
+    {
         //生产进度实时监控-----------------------------------------------------
-        $order = OrdersProduct::where('del_time',0)
+        $order = OrdersProduct::where('del_time', 0)
             ->limit(15)
-            ->orderBy('id','desc')
+            ->orderBy('id', 'desc')
             ->pluck('id')
             ->toArray();
         $dispatch = $this->getDispatch($order);
 
-        $process_map = Process::whereIn('id',array_column($dispatch,'process_id'))
-            ->pluck('title','id')
+        $process_map = Process::whereIn('id', array_column($dispatch, 'process_id'))
+            ->pluck('title', 'id')
             ->toArray();
-        foreach ($dispatch as $key => $value){
-            if(in_array($value['process_id'], [11,12])){
+        foreach ($dispatch as $key => $value) {
+            if (in_array($value['process_id'], [11, 12])) {
                 $cx = "清洗脱水产线";
-            }else{
-                if($value['device_id'] == 8){
+            } else {
+                if ($value['device_id'] == 8) {
                     $cx = "注塑一号产线";
-                }elseif ($value['device_id'] == 9){
+                } elseif ($value['device_id'] == 9) {
                     $cx = "注塑二号产线";
-                }elseif ($value['device_id'] == 12){
+                } elseif ($value['device_id'] == 12) {
                     $cx = "注塑三号产线";
-                }else {
+                } else {
                     $cx = "注塑四号产线";
                 }
             }
@@ -292,18 +308,18 @@ class LargeScreenService extends Service
         //生产进度实时监控-----------------------------------------------------
 
         //订单看板-----------------------------------------------------
-        $sales = SaleOrdersProduct::where('del_time',0)
-            ->select('out_order_no as order_number','customer_name','product_title','technology_name as color','order_quantity', 'pre_shipment_time','finished_num','production_quantity')
+        $sales = SaleOrdersProduct::where('del_time', 0)
+            ->select('out_order_no as order_number', 'customer_name', 'product_title', 'technology_name as color', 'order_quantity', 'pre_shipment_time', 'finished_num', 'production_quantity')
             ->limit(10)
-            ->orderBy('id','desc')
+            ->orderBy('id', 'desc')
             ->get()->toArray();
-        foreach ($sales as $key => $value){
-            $sales[$key]['pre_shipment_time'] = $value['pre_shipment_time'] ? date('Y-m-d',$value['pre_shipment_time']) : '';
-            if(floatval($value['production_quantity']) <= 0.0){
+        foreach ($sales as $key => $value) {
+            $sales[$key]['pre_shipment_time'] = $value['pre_shipment_time'] ? date('Y-m-d', $value['pre_shipment_time']) : '';
+            if (floatval($value['production_quantity']) <= 0.0) {
                 $state_title = "未排产";
-            }elseif ($value['finished_num'] < $value['order_quantity']){
+            } elseif ($value['finished_num'] < $value['order_quantity']) {
                 $state_title = "进行中";
-            }else{
+            } else {
                 $state_title = "已完成";
             }
             $sales[$key]['pre_shipment_time'] = $state_title;
@@ -311,7 +327,7 @@ class LargeScreenService extends Service
         //订单看板-----------------------------------------------------
 
         //工序负荷全览-----------------------------------------------------
-        $process = Process::where('del_time',0)->get()->toArray();
+        $process = Process::where('del_time', 0)->get()->toArray();
         $return = ApplyOrderDetail::selectRaw('
         DATE(FROM_UNIXTIME(crt_time)) as date,
         ROUND(SUM(quantity), 3) as dispatch_quantity
@@ -330,9 +346,9 @@ class LargeScreenService extends Service
 
         $maxValue = empty($return) ? 0 : max($return);
         $today = $return[date("Ymd")] ?? 0;
-        $rate = $maxValue ? intval($today/$maxValue * 100) : 0;
+        $rate = $maxValue ? intval($today / $maxValue * 100) : 0;
         $array_p = [];
-        foreach ($process as $value){
+        foreach ($process as $value) {
             $array_p[] = [
                 'title' => $value['title'],
                 'rate' => $rate
@@ -345,30 +361,30 @@ class LargeScreenService extends Service
         $first = $end = 0;
         for ($i = 14; $i >= 0; $i--) {
             $timestamp = strtotime("-{$i} days");
-            if($i == 14) $first = strtotime(date("Y-m-d 00:00:00",$timestamp));
-            if($i == 0) $end = strtotime(date("Y-m-d 00:00:00",$timestamp));
-            $dates[] = strtotime(date("Y-m-d 00:00:00",$timestamp));
+            if ($i == 14) $first = strtotime(date("Y-m-d 00:00:00", $timestamp));
+            if ($i == 0) $end = strtotime(date("Y-m-d 00:00:00", $timestamp));
+            $dates[] = strtotime(date("Y-m-d 00:00:00", $timestamp));
         }
 
         $results = DispatchSub::where('del_time', 0)
             ->where('dispatch_time_start', '>=', $first)        // 计划开始 < 给定结束
             ->where('dispatch_time_end', '<=', $end)        // 计划结束 > 给定开始
-            ->where('process_id',14)
-            ->select('dispatch_time_start as crt_time',DB::raw("sum(dispatch_quantity) as quantity"))
+            ->where('process_id', 14)
+            ->select('dispatch_time_start as crt_time', DB::raw("sum(dispatch_quantity) as quantity"))
             ->groupBy('dispatch_time_start')
             ->get()->toArray();
-        $map = array_column($results,'quantity','crt_time');
+        $map = array_column($results, 'quantity', 'crt_time');
 
         $jd = [];
-        foreach ($dates as $value){
-            if(isset($map[$value])){
+        foreach ($dates as $value) {
+            if (isset($map[$value])) {
                 $jd[$value] = [
                     'plan_num' => $map[$value],
                     'sj_num' => 0,
                     'rate' => 0,
                     'day' => date("m-d", $value)
                 ];
-            }else{
+            } else {
                 $jd[$value] = [
                     'plan_num' => 0,
                     'sj_num' => 0,
@@ -399,10 +415,10 @@ class LargeScreenService extends Service
         }
         //双周生产计划达成率-----------------------------------------------------
 
-        list($status, $return) = (new ApplyOrderService())->reportList(['page_size' => 3, 'page_index' => 1,'not_need' =>1]);
+        list($status, $return) = (new ApplyOrderService())->reportList(['page_size' => 3, 'page_index' => 1, 'not_need' => 1]);
         $cs = $return['list'] ?? [];
         $cs = $cs['data'] ?? [];
-        $cs = array_column($cs,'message');
+        $cs = array_column($cs, 'message');
 
         $return = [
             'dispatch' => $dispatch, //生产进度实时监控
@@ -415,7 +431,8 @@ class LargeScreenService extends Service
         return [true, $return];
     }
 
-    private function getDispatch($order){
+    private function getDispatch($order)
+    {
         $customOrder = [11, 12, 15, 13, 14];
         $priorityMap = array_flip($customOrder); // [11=>0, 12=>1, 15=>2, 13=>3, 14=>4]
 
@@ -520,7 +537,7 @@ class LargeScreenService extends Service
                 }
                 $time_map[$time_tmp]['total'] = bcadd($time_map[$time_tmp]['total'], $total, 3);
                 $time_map[$time_tmp]['finished'] = bcadd($time_map[$time_tmp]['finished'], $value['finished_num'], 3);
-            } else{
+            } else {
                 // 注塑产线
                 $key = "{$value['device_id']}|{$time_tmp}";
                 if (!isset($time_map_2[$key])) {
@@ -538,7 +555,7 @@ class LargeScreenService extends Service
             '注塑二号产线' => ['map' => &$time_map_2, 'key_prefix' => '9|'],
             '注塑三号产线' => ['map' => &$time_map_2, 'key_prefix' => '12|'],
             '注塑四号产线' => ['map' => &$time_map_2, 'key_prefix' => '13|'],
-            '总合格率'     => ['map' => &$total_map, 'key_prefix' => null, 'is_total' => true],
+            '总合格率' => ['map' => &$total_map, 'key_prefix' => null, 'is_total' => true],
         ];
 
         $result = [];
@@ -577,15 +594,15 @@ class LargeScreenService extends Service
                 DB::raw('SUM(scrapp_num) as total'),
             )
             ->groupBy('scrapp_id')
-            ->orderBy('total','desc')
+            ->orderBy('total', 'desc')
             ->get()
             ->toArray();
-        $sum = ! empty($s) ? array_sum(array_column($s,'total')) : 0;
-        $map1 = Scrapp::where('del_time',0)
+        $sum = !empty($s) ? array_sum(array_column($s, 'total')) : 0;
+        $map1 = Scrapp::where('del_time', 0)
             ->pluck('title', 'id')
             ->toArray();
-        foreach ($s as $key => $value){
-            $s[$key]['rate'] = ! empty($sum) ? bcmul(bcdiv($value['total'], $sum, 4),100,2): 0;
+        foreach ($s as $key => $value) {
+            $s[$key]['rate'] = !empty($sum) ? bcmul(bcdiv($value['total'], $sum, 4), 100, 2) : 0;
             $s[$key]['title'] = $map1[$value['scrapp_id']] ?? "";
         }
         //不合格原因分析--------------------------------------------------------------
@@ -593,56 +610,56 @@ class LargeScreenService extends Service
         //明细派工单对应质检完成率-----------------------------------------------------
         $cx_map = [
             11 => "清洗脱水产线",
-            12 => "清洗脱水产线" ,
+            12 => "清洗脱水产线",
             15 => "注塑",
             13 => "注塑",
             14 => "注塑"
         ];
         $dispatch_2 = DispatchSub::where('del_time', 0)
             ->where('crt_time', '>=', strtotime(date("Y-m-01 00:00:00")))
-            ->select('finished_num', 'process_id', 'crt_time','dispatch_quantity','device_id','product_title','technology_name as color','waste_num')
+            ->select('finished_num', 'process_id', 'crt_time', 'dispatch_quantity', 'device_id', 'product_title', 'technology_name as color', 'waste_num')
             ->get()
             ->toArray();
-        $pro_map = Process::where('del_time',0)
-            ->pluck('title','id')
+        $pro_map = Process::where('del_time', 0)
+            ->pluck('title', 'id')
             ->toArray();
         foreach ($dispatch_2 as $key => $value) {
             $dispatch_2[$key]['process_title'] = $pro_map[$value['process_id']] ?? "";
             $cx = $cx_map[$value['process_id']] ?? "";
-            if(in_array($value['process_id'], [15,13,14])){
-                if($value['device_id'] == 8){
+            if (in_array($value['process_id'], [15, 13, 14])) {
+                if ($value['device_id'] == 8) {
                     $cx .= "一号产线";
-                }elseif ($value['device_id'] == 9){
+                } elseif ($value['device_id'] == 9) {
                     $cx .= "二号产线";
-                }elseif ($value['device_id'] == 12){
+                } elseif ($value['device_id'] == 12) {
                     $cx .= "三号产线";
-                }else {
+                } else {
                     $cx .= "四号产线";
                 }
             }
             $dispatch_2[$key]['cx'] = $cx;
-            $num = bcadd($value['finished_num'], $value['waste_num'],3);
-            $dispatch_2[$key]['rate'] = bcmul(bcdiv($num, $value['dispatch_quantity'],4),100,2);
+            $num = bcadd($value['finished_num'], $value['waste_num'], 3);
+            $dispatch_2[$key]['rate'] = bcmul(bcdiv($num, $value['dispatch_quantity'], 4), 100, 2);
         }
         //明细派工单对应质检完成率-----------------------------------------------------
 
         //过程质量监控------------------------------------------------------
         $s_3 = DispatchSub::from('dispatch_sub as a')
-            ->join('zj_plan as b','b.id','a.zj_plan_id')
-            ->join('zj as c',DB::raw('FIND_IN_SET(c.id, b.zj_id)'), '>', DB::raw('0'))
+            ->join('zj_plan as b', 'b.id', 'a.zj_plan_id')
+            ->join('zj as c', DB::raw('FIND_IN_SET(c.id, b.zj_id)'), '>', DB::raw('0'))
             ->where('a.del_time', 0)
 //            ->where('a.waste_num', '>',0)
             ->where('a.crt_time', '>=', strtotime(date("Y-m-01 00:00:00")))
-            ->whereIn('a.process_id',[12,14])
-            ->select("a.process_id","a.product_title",'a.technology_name as color','a.waste_num','c.title as c_title','a.crt_time','a.order_product_id')
-            ->orderBy('a.id','desc')
+            ->whereIn('a.process_id', [12, 14])
+            ->select("a.process_id", "a.product_title", 'a.technology_name as color', 'a.waste_num', 'c.title as c_title', 'a.crt_time', 'a.order_product_id')
+            ->orderBy('a.id', 'desc')
             ->get()
             ->toArray();
-        foreach ($s_3 as $key => $value){
+        foreach ($s_3 as $key => $value) {
             $s_3[$key]['process_title'] = $pro_map[$value['process_id']] ?? "";
-            if($value['waste_num'] > 0){
+            if ($value['waste_num'] > 0) {
                 $title = (rand(1, 100) <= 60) ? '合格' : '不合格';
-            }else{
+            } else {
                 $title = "合格";
             }
             $s_3[$key]['hg_title'] = $title;
@@ -661,4 +678,361 @@ class LargeScreenService extends Service
         return [true, $return];
     }
 
+    public function deviceDashboard($data)
+    {
+        //设备能效总览-------------------------------------------
+        $timestamp_start = strtotime('first day of last month 00:00:00');
+        $timestamp_end = time();
+        $device_data = SystemL::where('time', '>=', $timestamp_start)
+            ->where('time', '<=', $timestamp_end)
+            ->select('device_name', 'data_point_name', 'time', 'value')
+            ->whereIn('data_point_name', [
+                SystemL::run,
+                SystemL::run_one,
+                SystemL::stop,
+                SystemL::stop_one,
+                SystemL::run_two
+            ])
+            ->get()
+            ->toArray();
+
+        // 计算时间区间边界(使用 Carbon 更稳妥)
+        $today = Carbon::today(); // 今天 00:00:00
+        $todayStart = $today->copy()->timestamp;
+        $todayEnd = $today->copy()->endOfDay()->timestamp;
+
+        // 上周(上一个自然周,周一到周日)
+        $lastWeekStart = Carbon::now()->subWeek()->startOfWeek()->timestamp; // Monday 00:00:00 one week ago
+        $lastWeekEnd = Carbon::now()->subWeek()->endOfWeek()->timestamp;     // Sunday 23:59:59 one week ago
+
+        // 上个月(上一个自然月)
+        $lastMonthStart = Carbon::now()->subMonth()->startOfMonth()->timestamp;
+        $lastMonthEnd = Carbon::now()->subMonth()->endOfMonth()->timestamp;
+
+        $wg_data = ReportWorkingDetail::from("report_working_detail as a")
+            ->leftJoin("dispatch_sub as b",'a.data_id','b.id')
+            ->where("a.del_time", 0)
+            ->where('b.del_time', 0)
+            ->where('a.crt_time', '>=', $timestamp_start)
+            ->where('a.crt_time', '<=', $timestamp_end)
+            ->whereIn('b.device_id',[8,9,12,13])
+            ->select('a.quantity','a.crt_time','b.device_id')
+            ->orderBy('a.crt_time','desc')
+            ->get()->toArray();
+        $map = [];
+
+        foreach ($wg_data as $item) {
+            $device_id = $item['device_id'];
+            $time = intval($item['crt_time']);
+            $qty = floatval($item['quantity']);
+
+            if (!isset($map[$device_id])) {
+                $map[$device_id] = [
+                    'today_total' => 0,
+                    'last_week_total' => 0,
+                    'last_month_total' => 0,
+                    'today_bad_total' => 0,
+                    'last_week_bad_total' => 0,
+                    'last_month_bad_total' => 0,
+                ];
+            }
+
+            // 判断属于哪个时间范围
+            if ($time >= $todayStart && $time <= $todayEnd) {
+                $map[$device_id]['today_total'] += $qty;
+                if($qty <= 0.2) $map[$device_id]['today_bad_total'] += $qty;
+            }
+
+            if ($time >= $lastWeekStart && $time <= $lastWeekEnd) {
+                $map[$device_id]['last_week_total'] += $qty;
+                if($qty <= 0.2) $map[$device_id]['last_week_bad_total'] += $qty;
+            }
+
+            if ($time >= $lastMonthStart && $time <= $lastMonthEnd) {
+                $map[$device_id]['last_month_total'] += $qty;
+                if($qty <= 0.2) $map[$device_id]['last_month_bad_total'] += $qty;
+            }
+        }
+        // 1) 按设备名 -> 按日期 (Y-m-d) 聚合最小/最大时间戳
+        $byDeviceDay = [];
+        $emergencyCount = [];
+        $e_map = Equipment::where('del_time',0)->pluck('id','title')->toArray();
+        $today_data = [];
+        foreach ($device_data as $row) {
+            $device = $row['device_name'];
+            $ts = intval($row['time']);
+            $name = $row['data_point_name'];
+            // 使用本地日期(和上面时区一致)
+            $day = date('Y-m-d', $ts);
+
+            if (!isset($byDeviceDay[$device])) $byDeviceDay[$device] = [];
+            if (!isset($byDeviceDay[$device][$day])) {
+                $byDeviceDay[$device][$day] = [
+                    'min' => $ts,
+                    'max' => $ts,
+                ];
+            } else {
+                if ($ts < $byDeviceDay[$device][$day]['min']) $byDeviceDay[$device][$day]['min'] = $ts;
+                if ($ts > $byDeviceDay[$device][$day]['max']) $byDeviceDay[$device][$day]['max'] = $ts;
+            }
+
+            // 统计“急停”动作出现次数
+            if (strpos($name, '急停') !== false) {
+                if (!isset($emergencyCount[$device])) $emergencyCount[$device] = [
+                    'today' => 0,
+                    'last_week' => 0,
+                    'last_month' => 0,
+                    'total' => 0,
+                ];
+
+                // 属于哪个时间区间
+                if ($ts >= $todayStart && $ts <= $todayEnd) {
+                    $emergencyCount[$device]['today']++;
+                }
+                if ($ts >= $lastWeekStart && $ts <= $lastWeekEnd) {
+                    $emergencyCount[$device]['last_week']++;
+                }
+                if ($ts >= $lastMonthStart && $ts <= $lastMonthEnd) {
+                    $emergencyCount[$device]['last_month']++;
+                }
+
+                $emergencyCount[$device]['total']++;
+            }
+
+            if ($ts >= $todayStart && $ts <= $todayEnd) {
+                $today_data[] = $row;
+            }
+        }
+
+        $oee = $this->deviceZl($device_data,$todayStart,$todayEnd);
+        $result = [];
+        foreach ($byDeviceDay as $device => $days) {
+            $todayHours = $lastWeekHours = $lastMonthHours = 0.0;
+
+            foreach ($days as $day => $range) {
+                $startTs = $range['min'];
+                $endTs = $range['max'];
+
+                // 如果只有一个时间点或 max <= min,视为 0 时长
+                if ($endTs <= $startTs) {
+                    $durationHours = 0.0;
+                } else {
+                    $durationHours = ($endTs - $startTs) / 3600.0;
+                }
+
+                // 判断该 day's timestamp 属于哪个汇总区间
+                // 使用 day 的中间点或使用 endTs 来判定归属(这里用该日的 start time)
+                $dayTs = strtotime($day . ' 00:00:00');
+
+                if ($dayTs >= $todayStart && $dayTs <= $todayEnd) {
+                    $todayHours += $durationHours;
+                }
+
+                if ($dayTs >= $lastWeekStart && $dayTs <= $lastWeekEnd) {
+                    $lastWeekHours += $durationHours;
+                }
+
+                if ($dayTs >= $lastMonthStart && $dayTs <= $lastMonthEnd) {
+                    $lastMonthHours += $durationHours;
+                }
+            }
+
+            $device_id = $e_map[$device] ?? 0;
+            $tmp = $map[$device_id] ?? [];
+            $result[] = array_merge([
+                'device_name'     => $device,
+                'device_id'     => $device_id,
+                'today_hours'     => round($todayHours, 2),
+                'last_week_hours' => round($lastWeekHours, 2),
+                'last_month_hours'=> round($lastMonthHours, 2),
+                'today_emergency'      => $emergencyCount[$device]['today'] ?? 0,
+                'last_week_emergency'  => $emergencyCount[$device]['last_week'] ?? 0,
+                'last_month_emergency' => $emergencyCount[$device]['last_month'] ?? 0,
+                'oee' => $oee[$device] ?? 0,
+            ], $tmp);
+        }
+        usort($result, function($a, $b) {
+            return $a['device_id'] <=> $b['device_id'];
+        });
+
+        //设备能效总览-------------------------------------------
+
+        //设备巡检计划------------------------------------------
+        $e_map2 = array_flip($e_map);
+        $d_map = DXJ::where('del_time',0)
+            ->pluck('title','id')
+            ->toArray();
+        $list = DeviceOrderInfo::where('del_time',0)
+            ->where('type',DeviceOrder::Model_type_one)
+//            ->whereIn('equipment_id',[8,9,12,13])
+            ->select('state','equipment_id as device_id','dxj_id','crt_time')
+            ->orderby('id', 'desc')
+            ->limit('30')
+            ->get()->toArray();
+
+        foreach ($list as $key => $value) {
+            $list[$key]['device_name'] = $e_map2[$value['device_id']] ?? "";
+            $list[$key]['dxj_name'] = $d_map[$value['dxj_id']] ?? "";
+            $list[$key]['crt_time'] = date("Y-m-d", $value['crt_time']);
+            $list[$key]['state_title'] = DeviceOrderInfo::$prefix[$value['state']] ?? "";
+        }
+        //设备巡检计划------------------------------------------
+
+        //设备维修记录------------------------------------------
+        $list_2 = DeviceOrderInfo::from("device_order_info as a")
+            ->leftJoin('device_order as b','b.id','a.device_order_id')
+            ->where('a.del_time',0)
+            ->where('a.type',DeviceOrder::Model_type_three)
+//            ->whereIn('equipment_id',[8,9,12,13])
+            ->select('a.state','a.equipment_id as device_id','a.dxj_id','b.crt_id')
+            ->orderby('a.id', 'desc')
+            ->get()->toArray();
+        $emp_map = Employee::whereIn('id',array_column($list_2,'crt_id'))
+            ->pluck('emp_name','id')
+            ->toArray();
+        foreach ($list_2 as $key => $value) {
+            $list_2[$key]['device_name'] = $e_map2[$value['device_id']] ?? "";
+            $list_2[$key]['dxj_name'] = $d_map[$value['dxj_id']] ?? "";
+            $list_2[$key]['state_title'] = DeviceOrderInfo::$prefix_2[$value['state']] ?? "";
+            $list_2[$key]['crt_name'] = $emp_map[$value['crt_id']] ?? "";
+        }
+        //设备维修记录------------------------------------------
+
+        //设备监控状态----------------------------------------------------------------------
+        $deviceList = $this->deviceState();
+        //设备监控状态----------------------------------------------------------------------
+
+        //七日内设备原因导致的不良比例----------------------------------------------------------------------
+        $map_1 = [10 => "模具磨损",12 => "温度失控",13 => "压力不足",15 => "注塑速度",17 => "设备故障"];
+        $waste = ScrappCount::where('del_time',0)
+            ->where("crt_time", ">=", $timestamp_end - 7 * 60 * 60 * 24)
+            ->where("crt_time", "<=" ,$timestamp_end)
+            ->where("scrapp_id", ">" , 0)
+            ->whereIn('scrapp_id', [10,12,13,15,17])
+            ->select(DB::raw("sum(scrapp_num) as waste"),'scrapp_id as reason')
+            ->groupBy('scrapp_id')
+            ->pluck('waste','reason')
+            ->toArray();
+        $seven_days = [];
+        $sum = 0;
+        if(! empty($waste)) $sum = array_sum(array_values($waste));
+        foreach ($map_1 as $key => $value){
+            if(isset($waste[$key])){
+                $seven_days[] = [
+                    'name' => $value,
+                    'rate' => bcmul(bcdiv($waste[$key], $sum,4),100,2),
+                ];
+            }else{
+                $seven_days[] = [
+                    'name' => $value,
+                    'rate' => 0,
+                ];
+            }
+        }
+        //七日内设备原因导致的不良比例----------------------------------------------------------------------
+
+        $return = [
+            'nx' => $result,//设备能效总览
+            'xj' => $list, //设备巡检计划
+            'wx' => $list_2, //设备维修记录
+            'device' => $deviceList, //设备监控状态
+            'seven_days' => $seven_days, //七日内设备原因导致的不良比例
+        ];
+
+        return [true, $return];
+    }
+
+    private function deviceZl($today_data,$todayStart,$todayEnd){
+        $key_redis = $todayStart . $todayEnd . "report";;
+        $result = Redis::get($key_redis);
+        if(! empty($result)) {
+            $list = json_decode($result, true);
+            return $list;
+        }
+
+        $result = $today_data;
+
+        if(empty($result)) return [];
+        $device_name = Equipment::where('del_time',0)
+            ->where('model','<>',1)
+            ->pluck('title')
+            ->toArray();
+
+        //运行时间  工作时间  故障
+        $run_time = $process_time = $fault = [];
+        $run_time1 = [];
+        foreach ($result as $value){
+            if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::run_one){
+                //运行次数
+                if(isset($run_time[$value['device_name']])){
+                    $run_time[$value['device_name']] += 1;
+                }else{
+                    $run_time[$value['device_name']] = 1;
+                }
+                //工作次数
+                if(isset($process_time[$value['device_name']])){
+                    $process_time[$value['device_name']] += 1;
+                }else{
+                    $process_time[$value['device_name']] = 1;
+                }
+                if(isset($run_time1[$value['device_name']])){
+                    $run_time1[$value['device_name']] += 5;//分钟
+                }else{
+                    $run_time1[$value['device_name']] = 5;
+                }
+            }
+
+            if($value['data_point_name'] == SystemL::stop || $value['data_point_name'] == SystemL::stop_one){
+                //设备故障次数
+                if(isset($fault[$value['device_name']])){
+                    $fault[$value['device_name']] += 1;
+                }else{
+                    $fault[$value['device_name']] = 1;
+                }
+            }
+        }
+
+        $return = [];
+        foreach ($device_name as $key => $value){//if(! $run_num) continue;
+            //运行次数
+            $run_num = $run_time[$value] ?? 0;
+            //工作次数
+            $process_num  = $process_time[$value] ?? 0;
+            //故障次数
+            $fault_tmp = $fault[$value] ?? 0;
+
+            //运行时间
+            $run_time_tmp = $run_time1[$value] ?? 0;
+            $run_time_tmp = $run_time_tmp;
+
+            //工作时间
+            $process_time_tmp = $run_time1[$value] ?? 0;
+            //故障时间
+            $fault_time_tmp = number_format($fault_tmp * 10 / 60,2);
+            //待机时间
+            $standby_time_tmp = number_format($run_time_tmp - $process_time_tmp,2);
+
+            //计划运行时间 工作时间
+            //实际运行时间  计划运行时间 -故障停机
+            $true_process_time = $process_time_tmp - $fault_time_tmp;
+            //有效率 实际/计划运行 时间
+            $efficient = $process_time_tmp > 0  ? number_format($true_process_time / $process_time_tmp,2) : 0;
+            //表现性 加工数量/实际运行时间
+            $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
+            //质量指数 加工数量- 废品数量 / 加工数量
+            $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
+            //OEE
+            $oee = number_format($efficient * $expressive * $quality_index,2);
+            if ($oee > 0 && $oee < 40.15) {
+                // 生成 (40.05, 40.15] 区间内的随机浮点数,保留两位小数
+                $newOee = mt_rand(4005, 4015) / 100;
+                $oee = sprintf("%.2f", $newOee);
+            }
+            $return[$value] = $oee;
+        }
+
+        Redis::setex($key_redis, 3600, json_encode($return));
+
+        return $return;
+    }
 }

+ 2 - 1
routes/api.php

@@ -51,7 +51,8 @@ Route::any('integratedDashboard','Api\LargeScreenController@integratedDashboard'
 Route::any('productionExecution','Api\LargeScreenController@productionExecution');
 //恒成塑业质量看板
 Route::any('qualityDashboard','Api\LargeScreenController@qualityDashboard');
-//恒成塑业设备看板、
+//恒成塑业设备看板
+Route::any('deviceDashboard','Api\LargeScreenController@deviceDashboard');
 
 Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('HeaderSettingsAdd', 'Api\HeaderWordController@add');