where('crt_time', ">=", strtotime(date("Y-01-01 00:00:00"))) ->select(DB::raw("sum(finished_num) as total")) ->value('total'); //总产值 ------------------------------------------------ //当日产量 当月产量 --------------------------------------------- $order_total = 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(DB::raw('sum(order_quantity) as order_quantity')) ->value('order_quantity'); $box = new BoxDetail(['channel'=>date("Ymd")]); $data_one = $box->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('num as quantity', 'crt_time') ->get()->toArray(); //当日产量 当月产量 $today_total = $month_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) { $month_total = bcadd($month_total, $value['quantity'], 3); if ($timeStamp <= $value['crt_time'] && $timeStampE >= $value['crt_time']) $today_total = bcadd($today_total, $value['quantity'], 3); } //当日产量 当月产量 --------------------------------------------- //月计划完成率-------------------------------------------------------------------- $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) ->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) . "%"; //月注塑合格率---------------------------------------------------------------------- //设备监控状态---------------------------------------------------------------------- $deviceList = $this->deviceState(); //设备监控状态---------------------------------------------------------------------- //质量管理-------------------------------------------------------------------------- $dates = []; for ($i = 35; $i >= 0; $i--) { $timestamp = strtotime("-{$i} days"); $date = date('m-d', $timestamp); $dates[] = $date; } $zl_data = []; $data_three = ScrappCount::where('del_time', 0) ->where('crt_time', "<=", time()) ->where('crt_time', ">=", strtotime(date("Y-m-d 00:00:00", time() - 35 * 24 * 60 * 60))) ->where('process_id', 14) ->select('quantity', "scrapp_num as waste", "crt_time") ->get()->toArray(); 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 { $zl_data[$day] = [ 'quantity' => $value['quantity'], 'total' => $tmp ]; } } $zj_data_final = []; foreach ($dates as $value) { $tmp = [ "day" => $value, "total" => 0, "rate" => 0, ]; 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); $zj_data_final[] = $tmp; } } //质量管理-------------------------------------------------------------------------- //物料管理-------------------------------------------------------------------------- $thing = [ ["name" => '包装外吨袋', 'rate' => 16.11], ["name" => '包装内吨袋', 'rate' => 18.23], ["name" => '904黑色母', 'rate' => 57.60], ["name" => '118颜料红', 'rate' => 60.00], ["name" => '150高温红', 'rate' => 59.71], ["name" => '151荧光红', 'rate' => 60.57], ["name" => '152颜料暗红', 'rate' => 66.57], ["name" => '153荧光橙', 'rate' => 57.50], ["name" => '263颜料黄', 'rate' => 57.14], ["name" => '265进口颜料黄', 'rate' => 69.05], ["name" => '35溶剂蓝', 'rate' => 49.05], ["name" => '378钛青蓝', 'rate' => 59.29], ["name" => '560颜料黑', 'rate' => 62.14], ["name" => '706分散紫', 'rate' => 57.14], ["name" => '708颜料紫', 'rate' => 56.43], ["name" => '965钛白粉', 'rate' => 64.29], ["name" => '966进口颜料白', 'rate' => 68.86], ["name" => '168抗氧剂', 'rate' => 67.43], ["name" => '工业盐', 'rate' => 119.10], ["name" => 'OB-1增白剂', 'rate' => 93.00], ["name" => '181高胶粉', 'rate' => 107.21], ["name" => '白料', 'rate' => 3.19], ["name" => '黑料', 'rate' => 14.57], ["name" => '灰料', 'rate' => 24.22], ]; //物料管理-------------------------------------------------------------------------- //销售管理-------------------------------------------------------------------------- $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') ->groupBy('technology_name') ->orderBy('total', 'desc') ->get()->toArray(); //销售管理-------------------------------------------------------------------------- //总装各线体目标/完成量--------------------------------------------------------------- $zx_12_dispatch_quantity = $zx_12_finished_num = 0; $zx_14_dispatch_quantity_8 = $zx_14_finished_num_8 = 0; $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) ->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') ->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); } } } $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') ->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') ->get()->toArray(); $details = []; 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 { $details[$new] = [ "dispatch_quantity" => $value['dispatch_quantity'], "finished_num" => $finished_num, ]; } } foreach ($order as $key => $value) { $d_tmp = []; 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); } $d_tmp[] = [ "name" => $v, "rate" => $rate ]; } $order[$key]['detail'] = $d_tmp; if ($value['production_quantity'] <= $value['finished_num']) { $state = "已完成"; } else { $state = "进行中"; } $order[$key]['state_title'] = $state; } //订单完成进度----------------------------------------------------------------- // dd($total, $today_total, $month_total, $month_rate, $process_14_rate, $deviceList, $zj_data_final, $thing, $xs, $xt,$order); $return = [ 'total' => $total, //年总产量 'today_total' => $today_total, //当日产量 'month_total' => $month_total, //当月产量 'month_rate' => $month_rate, // 月计划完成比例 'process_14_rate' => $process_14_rate, // 注塑产线合格率 'device' => $deviceList, // 设备监控状态 'zj_result' => $zj_data_final, //质量管理 'thing' => $thing, //物料管理 'xs' => $xs, //销售管理 'xt' => $xt, //总装各线体目标/完成量 'order' => $order, // 订单完成进度 ]; return [true, $return]; } 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) ->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]['state_title'] = $state_title; $sales[$key]['pre_shipment_time'] = date("Y-m-d", $value['pre_shipment_time']); } //订单看板----------------------------------------------------- //工序负荷全览----------------------------------------------------- $process = Process::where('del_time', 0)->get()->toArray(); $return = ApplyOrderDetail::selectRaw(' DATE_FORMAT(FROM_UNIXTIME(crt_time), "%Y-%m") as month, ROUND(SUM(quantity), 3) as dispatch_quantity ') ->where('del_time', 0) ->where('type', ApplyOrder::type_one) ->groupBy('month') ->orderBy('month', 'desc') // 按月份倒序 ->get() ->map(function ($item) { // 转成 YYYYMM 格式 $item->month = str_replace('-', '', $item->month); return $item; }) ->pluck('dispatch_quantity', 'month') ->toArray(); $maxValue = empty($return) ? 0 : max($return); $today = $return[date("Ym")] ?? 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]); $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) { //质量趋势图 以及 合格率---------------------------------------------------- // 近多少天日期戳 $dates = []; for ($i = 35; $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', "<=", time()) ->where('crt_time', ">=", strtotime(date("Y-m-d 00:00:00", time() - 35 * 24 * 60 * 60))) ->whereIn('process_id', [12, 14]) ->where('scrapp_id','>',0) ->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; if($a <= 0 || $b <= 0) continue; $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]; } 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, ]; } // 判断属于哪个时间范围 if ($time >= $todayStart && $time <= $todayEnd) { $map[$device_id]['today_total'] += $qty; } if ($time >= $lastWeekStart && $time <= $lastWeekEnd) { $map[$device_id]['last_week_total'] += $qty; } if ($time >= $lastMonthStart && $time <= $lastMonthEnd) { $map[$device_id]['last_month_total'] += $qty; } } // 1) 按设备名 -> 按日期 (Y-m-d) 聚合最小/最大时间戳 $byDeviceDay = []; $todayFirstTs = []; $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) { if (!isset($todayFirstTs[$device]) || $ts < $todayFirstTs[$device]) { $todayFirstTs[$device] = $ts; } $today_data[] = $row; } } $oee = $this->deviceZl($today_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, 'today_start' => isset($todayFirstTs[$device]) ? date('Y-m-d H:i:s', $todayFirstTs[$device]) : '', ], $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]['crt_name'] = "王豪亿"; $list[$key]['state_title'] = DeviceOrderInfo::$prefix[$value['state']] ?? ""; $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','b.mark') ->orderby('b.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]['dxj_name'] = $value['mark']; $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), 'num' => $waste[$key] ]; }else{ $seven_days[] = [ 'name' => $value, 'rate' => 0, 'num' => 0 ]; } } //七日内设备原因导致的不良比例---------------------------------------------------------------------- $return = [ 'nx' => $result,//设备能效总览 'xj' => $list, //设备巡检计划 'wx' => $list_2, //设备维修记录 'device' => $deviceList, //设备监控状态 'seven_days' => $seven_days, //七日内设备原因导致的不良比例 'seven_days_total' => $sum, //七日内设备原因导致的不良总数 ]; 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; //工作时间 $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; } }