cqp 2 dienas atpakaļ
vecāks
revīzija
5b5eafeb3e

+ 24 - 1
app/Http/Controllers/Api/LargeScreenController.php

@@ -7,7 +7,6 @@ use Illuminate\Http\Request;
 
 class LargeScreenController extends BaseController
 {
-    //综合大屏
     public function integratedDashboard(Request $request){
         $service = new LargeScreenService();
 //        $userData = $request->userData->toArray();
@@ -19,4 +18,28 @@ class LargeScreenController extends BaseController
             return $this->json_return(201,$data);
         }
     }
+
+    public function productionExecution(Request $request){
+        $service = new LargeScreenService();
+//        $userData = $request->userData->toArray();
+        list($status,$data) = $service->productionExecution($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function qualityDashboard(Request $request){
+        $service = new LargeScreenService();
+//        $userData = $request->userData->toArray();
+        list($status,$data) = $service->qualityDashboard($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
 }

+ 22 - 8
app/Service/ApplyOrderService.php

@@ -4554,7 +4554,7 @@ class ApplyOrderService extends Service
             ->orderBy('id','desc');
 
         $list = $this->limit($model,'',$data);
-        $list = $this->fillRData($list);
+        $list = $this->fillRData($list, $data);
 
         $count = ReportMessage::where('del_time',0)
             ->where('is_use',0)
@@ -4563,7 +4563,7 @@ class ApplyOrderService extends Service
         return [true, ['list'=> $list, 'count' => $count]];
     }
 
-    public function fillRData($data){
+    public function fillRData($data, $erg){
         if(empty($data['data'])) return $data;
 
         $dispatch = DispatchSub::whereIn('id',array_unique(array_column($data['data'],'order_id')))
@@ -4584,6 +4584,7 @@ class ApplyOrderService extends Service
         $order_map = ReportWorking::whereIn('id',array_filter(array_column($data['data'],'report_working_id')))
             ->pluck('order_number','id')
             ->toArray();
+        $no_need = isset($erg['not_need']) ? 1 : 0;
         foreach ($data['data'] as $key => $value){
             $time = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
             $data['data'][$key]['crt_time'] = $time;
@@ -4598,14 +4599,27 @@ class ApplyOrderService extends Service
             }else{
                 $p_t .= ":抽检";
             }
-            if(empty($value['report_working_id'])){
-                $str = "派工单:" . $t['dispatch_no'] . "于" . $time . "由($e)完成工序($p_t)报工,数量{$value['quantity']}吨,请及时处理";
-            }else{
-                $tmp = $order_map[$value['report_working_id']] ?? "";
-                if(empty($tmp)){
+            if(! $no_need){
+                if(empty($value['report_working_id'])){
                     $str = "派工单:" . $t['dispatch_no'] . "于" . $time . "由($e)完成工序($p_t)报工,数量{$value['quantity']}吨,请及时处理";
                 }else{
-                    $str = "派工单:" . $t['dispatch_no'] . "于" . $time . "由($e)完成工序($p_t)报工({$tmp}),数量{$value['quantity']}吨,请及时处理";
+                    $tmp = $order_map[$value['report_working_id']] ?? "";
+                    if(empty($tmp)){
+                        $str = "派工单:" . $t['dispatch_no'] . "于" . $time . "由($e)完成工序($p_t)报工,数量{$value['quantity']}吨,请及时处理";
+                    }else{
+                        $str = "派工单:" . $t['dispatch_no'] . "于" . $time . "由($e)完成工序($p_t)报工({$tmp}),数量{$value['quantity']}吨,请及时处理";
+                    }
+                }
+            }else{
+                if(empty($value['report_working_id'])){
+                    $str = "派工单:" . $t['dispatch_no'] . "由($e)完成工序报工,数量{$value['quantity']}吨,请及时处理";
+                }else{
+                    $tmp = $order_map[$value['report_working_id']] ?? "";
+                    if(empty($tmp)){
+                        $str = "派工单:" . $t['dispatch_no'] . "由($e)完成工序报工,数量{$value['quantity']}吨,请及时处理";
+                    }else{
+                        $str = "派工单:" . $t['dispatch_no'] . "由($e)完成工序报工({$tmp}),数量{$value['quantity']}吨,请及时处理";
+                    }
                 }
             }
             $data['data'][$key]['message'] = $str;

+ 406 - 0
app/Service/LargeScreenService.php

@@ -2,10 +2,14 @@
 
 namespace App\Service;
 
+use App\Model\ApplyOrder;
+use App\Model\ApplyOrderDetail;
 use App\Model\DispatchSub;
 use App\Model\Equipment;
 use App\Model\OrdersProduct;
+use App\Model\Process;
 use App\Model\SaleOrdersProduct;
+use App\Model\Scrapp;
 use App\Model\ScrappCount;
 use Illuminate\Support\Facades\DB;
 
@@ -255,4 +259,406 @@ class LargeScreenService extends Service
 
         return [true, $return];
     }
+
+    public function productionExecution($data){
+        //生产进度实时监控-----------------------------------------------------
+        $order = OrdersProduct::where('del_time',0)
+            ->limit(15)
+            ->orderBy('id','desc')
+            ->pluck('id')
+            ->toArray();
+        $dispatch = $this->getDispatch($order);
+
+        $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])){
+                $cx = "清洗脱水产线";
+            }else{
+                if($value['device_id'] == 8){
+                    $cx = "注塑一号产线";
+                }elseif ($value['device_id'] == 9){
+                    $cx = "注塑二号产线";
+                }elseif ($value['device_id'] == 12){
+                    $cx = "注塑三号产线";
+                }else {
+                    $cx = "注塑四号产线";
+                }
+            }
+            $dispatch[$key]['cx'] = $cx;
+            $dispatch[$key]['process_name'] = $process_map[$value['process_id']] ?? '';
+        }
+        //生产进度实时监控-----------------------------------------------------
+
+        //订单看板-----------------------------------------------------
+        $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')
+            ->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){
+                $state_title = "未排产";
+            }elseif ($value['finished_num'] < $value['order_quantity']){
+                $state_title = "进行中";
+            }else{
+                $state_title = "已完成";
+            }
+            $sales[$key]['pre_shipment_time'] = $state_title;
+        }
+        //订单看板-----------------------------------------------------
+
+        //工序负荷全览-----------------------------------------------------
+        $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
+    ')
+            ->where('del_time', 0)
+            ->where('type', ApplyOrder::type_one)
+            ->groupBy('date')
+            ->orderBy('date', 'desc') // 可选:按日期倒序
+            ->get()
+            ->map(function ($item) {
+                $item->date = str_replace('-', '', $item->date); // 转为 Ymd 格式字符串
+                return $item;
+            })
+            ->pluck('dispatch_quantity', 'date')
+            ->toArray();
+
+        $maxValue = empty($return) ? 0 : max($return);
+        $today = $return[date("Ymd")] ?? 0;
+        $rate = $maxValue ? intval($today/$maxValue * 100) : 0;
+        $array_p = [];
+        foreach ($process as $value){
+            $array_p[] = [
+                'title' => $value['title'],
+                'rate' => $rate
+            ];
+        }
+        //工序负荷全览-----------------------------------------------------
+
+        //双周生产计划达成率-----------------------------------------------------
+        $dates = [];
+        $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));
+        }
+
+        $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"))
+            ->groupBy('dispatch_time_start')
+            ->get()->toArray();
+        $map = array_column($results,'quantity','crt_time');
+
+        $jd = [];
+        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{
+                $jd[$value] = [
+                    'plan_num' => 0,
+                    'sj_num' => 0,
+                    'rate' => 0,
+                    'day' => date("m-d", $value)
+                ];
+            }
+        }
+
+        $pending = 0; // 待完成的任务量(滚存)
+        foreach ($jd as $timestamp => &$item) {
+            $totalWork = $pending + $item['plan_num']; // 可用任务总量(含滚存)
+
+            // 当天最多完成
+            $item['sj_num'] = min(42, $totalWork);
+
+            // 剩余未完成任务,滚入下一天
+            $pending = bcsub($totalWork, $item['sj_num'], 3);
+
+            // 计算 rate
+            if ($item['plan_num'] == 0) {
+                // 没有计划 → 完成率 0%
+                $item['rate'] = 0;
+            } else {
+                // 有计划 → 完成率 = 实际 / 计划
+                $item['rate'] = round(($item['sj_num'] / $item['plan_num']) * 100, 2);
+            }
+        }
+        //双周生产计划达成率-----------------------------------------------------
+
+        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');
+
+        $return = [
+            'dispatch' => $dispatch, //生产进度实时监控
+            'sales' => $sales, //订单看板
+            'array_p' => $array_p, //工序负荷全览
+            'jd' => array_values($jd), // 生产进度实时监控
+            'cs' => $cs
+        ];
+
+        return [true, $return];
+    }
+
+    private function getDispatch($order){
+        $customOrder = [11, 12, 15, 13, 14];
+        $priorityMap = array_flip($customOrder); // [11=>0, 12=>1, 15=>2, 13=>3, 14=>4]
+
+        // 先从数据库取出数据(只需按 id 倒序即可)
+        $dispatch = DispatchSub::where('del_time', 0)
+            ->whereIn('order_product_id', $order)
+            ->where('dispatch_quantity', '>', 1)
+            ->select(
+                'process_id',
+                'dispatch_quantity',
+                'finished_num',
+                DB::raw('finished_num as bg_num'),
+                DB::raw('finished_num as hg_num'),
+                'waste_num',
+                'device_id',
+                'order_product_id',
+                'id',
+                'technology_name as color',
+                'product_title'
+            )
+            ->orderBy('id', 'desc')
+            ->get()
+            ->toArray();
+
+        // === 在 PHP 中分组并排序 ===
+        $grouped = [];
+        foreach ($dispatch as $item) {
+            $grouped[$item['order_product_id']][] = $item;
+        }
+
+        // 对每一组按 process_id 自定义顺序排序
+        foreach ($grouped as &$group) {
+            usort($group, function ($a, $b) use ($priorityMap) {
+                $posA = $priorityMap[$a['process_id']] ?? 999;
+                $posB = $priorityMap[$b['process_id']] ?? 999;
+                return $posA <=> $posB; // 升序:11(0), 12(1), 15(2), 13(3), 14(4)
+            });
+        }
+        unset($group);
+
+        // 合并成扁平数组(保持 order_product_id 之间的顺序为 id 倒序)
+        $sortedDispatch = [];
+        $seenOrderIds = [];
+
+        // 按原始 id 倒序决定 order_product_id 的出现顺序
+        foreach ($dispatch as $item) {
+            $orderId = $item['order_product_id'];
+            if (!in_array($orderId, $seenOrderIds)) {
+                $seenOrderIds[] = $orderId;
+            }
+        }
+
+        // 按首次出现的 order_product_id 顺序(即 id 倒序)拼接每组数据
+        foreach ($seenOrderIds as $orderId) {
+            if (isset($grouped[$orderId])) {
+                foreach ($grouped[$orderId] as $item) {
+                    $sortedDispatch[] = $item;
+                }
+            }
+        }
+
+        return $sortedDispatch;
+    }
+
+    public function qualityDashboard($data)
+    {
+        //质量趋势图 以及 合格率----------------------------------------------------
+        // 近15天日期戳
+        $dates = [];
+        for ($i = 14; $i >= 0; $i--) {
+            $timestamp = strtotime("-{$i} days");
+            $dates[] = strtotime(date("Y-m-d 00:00:00", $timestamp));
+        }
+
+        $dispatch = ScrappCount::where('del_time', 0)
+            ->where('crt_time', '>=', strtotime(date("Y-m-01 00:00:00")))
+            ->whereIn('process_id', [12, 14])
+            ->select('scrapp_num as dispatch_quantity', 'quantity as finished_num', 'process_id', 'equipment_id as device_id', 'crt_time')
+            ->get()
+            ->toArray();
+
+        // 数据聚合
+        $time_map = [];      // 清洗脱水线
+        $time_map_2 = [];    // 注塑多条产线
+        $total_map = [];     // 所有产线总汇总
+
+        foreach ($dispatch as $value) {
+            $time_tmp = strtotime(date("Ymd 00:00:00", $value['crt_time']));
+            $total = bcadd($value['finished_num'], $value['dispatch_quantity'], 3);
+
+            // ✅ 所有产线的汇总
+            if (!isset($total_map[$time_tmp])) {
+                $total_map[$time_tmp] = ['total' => 0, 'finished' => 0];
+            }
+            $total_map[$time_tmp]['total'] = bcadd($total_map[$time_tmp]['total'], $total, 3);
+            $total_map[$time_tmp]['finished'] = bcadd($total_map[$time_tmp]['finished'], $value['finished_num'], 3);
+
+            if ($value['process_id'] == 12) {
+                // 清洗脱水产线
+                if (!isset($time_map[$time_tmp])) {
+                    $time_map[$time_tmp] = ['total' => 0, 'finished' => 0];
+                }
+                $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{
+                // 注塑产线
+                $key = "{$value['device_id']}|{$time_tmp}";
+                if (!isset($time_map_2[$key])) {
+                    $time_map_2[$key] = ['total' => 0, 'finished' => 0];
+                }
+                $time_map_2[$key]['total'] = bcadd($time_map_2[$key]['total'], $total, 3);
+                $time_map_2[$key]['finished'] = bcadd($time_map_2[$key]['finished'], $value['finished_num'], 3);
+            }
+        }
+
+        // 各产线配置
+        $lines = [
+            '清洗脱水产线' => ['map' => &$time_map, 'key_prefix' => null],
+            '注塑一号产线' => ['map' => &$time_map_2, 'key_prefix' => '8|'],
+            '注塑二号产线' => ['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],
+        ];
+
+        $result = [];
+        foreach ($lines as $name => $cfg) {
+            $map = $cfg['map'];
+            $prefix = $cfg['key_prefix'] ?? null;
+            $arr = [];
+
+            foreach ($dates as $date_ts) {
+                $key = $prefix ? "{$prefix}{$date_ts}" : $date_ts;
+                $a = $map[$key]['total'] ?? 0;
+                $b = $map[$key]['finished'] ?? 0;
+
+                $rate = 0.00;
+                if (floatval($a) > 0.0) {
+                    $rate = bcmul(bcdiv($b, $a, 4), 100, 2);
+                }
+                $arr[date("m-d", $date_ts)] = $rate;
+            }
+
+            $result[] = [
+                'name' => $name,
+                'array' => $arr,
+            ];
+        }
+        $end = array_pop($result);
+        $end = $end["array"];
+        //质量趋势图 end 以及 合格率 $result ----------------------------------------------------
+
+        //不合格原因分析--------------------------------------------------------------
+        $s = ScrappCount::where('del_time', 0)
+            ->where('crt_time', '>=', strtotime(date("Y-m-01 00:00:00")))
+            ->where('scrapp_id', '>', 0)
+            ->select(
+                'scrapp_id',
+                DB::raw('SUM(scrapp_num) as total'),
+            )
+            ->groupBy('scrapp_id')
+            ->orderBy('total','desc')
+            ->get()
+            ->toArray();
+        $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;
+            $s[$key]['title'] = $map1[$value['scrapp_id']] ?? "";
+        }
+        //不合格原因分析--------------------------------------------------------------
+
+        //明细派工单对应质检完成率-----------------------------------------------------
+        $cx_map = [
+            11 => "清洗脱水产线",
+            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')
+            ->get()
+            ->toArray();
+        $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){
+                    $cx .= "一号产线";
+                }elseif ($value['device_id'] == 9){
+                    $cx .= "二号产线";
+                }elseif ($value['device_id'] == 12){
+                    $cx .= "三号产线";
+                }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);
+        }
+        //明细派工单对应质检完成率-----------------------------------------------------
+
+        //过程质量监控------------------------------------------------------
+        $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'))
+            ->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')
+            ->get()
+            ->toArray();
+        foreach ($s_3 as $key => $value){
+            $s_3[$key]['process_title'] = $pro_map[$value['process_id']] ?? "";
+            if($value['waste_num'] > 0){
+                $title = (rand(1, 100) <= 60) ? '合格' : '不合格';
+            }else{
+                $title = "合格";
+            }
+            $s_3[$key]['hg_title'] = $title;
+
+        }
+        //过程质量监控------------------------------------------------------
+
+        $return = [
+            'zl' => $end, //质量趋势图
+            'zl_rate' => $result, //质量合格率
+            'reason' => $s, //不合格原因分析
+            'detail' => $dispatch_2, // 明细派工单对应质检完成率
+            'procedure' => $s_3, //过程质量监控
+        ];
+
+        return [true, $return];
+    }
+
 }

+ 6 - 1
routes/api.php

@@ -45,8 +45,13 @@ Route::any('saleOrdersFromAddress','Api\ScreenController@saleOrdersFromAddress')
 Route::any('production_status','Api\ScreenController@production_status');
 Route::any('deviceProduction','Api\ScreenController@deviceProduction');
 
-//综合大屏
+//恒成塑业综合看板
 Route::any('integratedDashboard','Api\LargeScreenController@integratedDashboard');
+//恒成塑业生产看板
+Route::any('productionExecution','Api\LargeScreenController@productionExecution');
+//恒成塑业质量看板
+Route::any('qualityDashboard','Api\LargeScreenController@qualityDashboard');
+//恒成塑业设备看板、
 
 Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('HeaderSettingsAdd', 'Api\HeaderWordController@add');