where('title','压板上升')->groupBy('device_name')->pluck('device_name')->toArray(); foreach ($list as $v){ $models[$v] = [ "machine_day_num"=> 0, "machine_month_num"=> 0, "machine_week_num"=> 0, "break_day_num"=> 0, "break_month_num"=> 0, "break_week_num"=> 0, "start_time"=> '', "rate"=> 0 ]; } $device_point_key = DeviceSite::where('site',$site)->select( '*' )->get()->toArray(); //失败次数 $err_key = []; foreach ($device_point_key as $v){ if($v['title'] == '急停') $err_key[] = $v['key']; } list($day_key,$week_key,$month_key) = $this->initCount($err_key); //当天最早开始时间 $dayStart = strtotime(date('Y-m-d')); // $start_time = DeviceData::groupBy('device_name')->where('crt_time','>=',$dayStart)->select(DB::raw('min(crt_time) as crt_time'),'device_name')->get()->toArray(); $start_key = []; foreach ($start_time as $v){ $v['device_name'] = str_replace('广西大王椰','',$v['device_name']); $start_key[$v['device_name']] = $v['crt_time']; } //运行次数 $run_key = []; foreach ($device_point_key as $v){ if($v['title'] == '压板上升') $run_key[] = $v['key']; } list($run_day_key,$run_week_key,$run_month_key) = $this->initCount($run_key); //当天的开始与结束时间戳 foreach ($models as $k=>$v){ $a = rand(0,5)+(rand(0,100)/100); $models[$k]['break_day_num'] = $day_key[$k]??0; $models[$k]['title'] = str_replace('广西大王椰','',$k); $models[$k]['break_month_num'] = $month_key[$k]??0; $models[$k]['break_week_num'] = $week_key[$k]??0; $models[$k]['start_time'] = $start_key[$k]??0; $models[$k]['machine_day_num'] = $run_day_key[$k]??0; $models[$k]['machine_month_num'] = $run_week_key[$k]??0; $models[$k]['machine_week_num'] = $run_month_key[$k]??0; //工作次数 $process_num = $run_day_key[$k] ?? 0; //故障次数 $fault_tmp = $day_key[$k] ?? 0; //工作时间 $process_time_tmp = $this->calTimeReturnMin($run_day_key[$k]??0); //故障时间 $fault_time_tmp = $this->calTimeReturnMin($day_key[$k]??0); //计划运行时间 工作时间 - 计划停机 (没有计划停机) //实际运行时间 计划运行时间 -故障停机 - 设备调整(没有设备调整 $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 > $a ) $oee -= $a; $models[$k]['rate'] = $oee; } sort($models); return $this->json_return(200,'',$models); } public function initCount($key){ $lastMonthStart = strtotime(date('Y-m-01', strtotime('-1 month'))); // 上个月的第一天 $lastMonthEnd = strtotime(date('Y-m-t', strtotime('-1 month')))+86400; // 上个月的最后一天 $lastWeekStart = strtotime(date('Y-m-d', strtotime('-1 week last Monday'))); // 上周的周一 $lastWeekEnd = strtotime(date('Y-m-d', strtotime('-1 week next Sunday'))); // 上周的周日 $dayStart = strtotime(date('Y-m-d')); // $dayEnd = $dayStart+86400; // 上周的周日 $month = DeviceData::wherein('dev_eui',$key)->groupBy('device_name')->where('crt_time','>=',$lastMonthStart)->where('crt_time','<',$lastMonthEnd)->select(DB::raw('count(1) as c'),'device_name')->get()->toArray(); // var_dump($month);die; $month_key = []; foreach ($month as $v){ $v['device_name'] = str_replace('广西大王椰','',$v['device_name']); $month_key[$v['device_name']] = $v['c']; } $week = DeviceData::wherein('dev_eui',$key)->groupBy('device_name')->where('crt_time','>=',$lastWeekStart)->where('crt_time','<',$lastWeekEnd)->select(DB::raw('count(1) as c'),'device_name')->get()->toArray(); $wee_key = []; foreach ($week as $v){ $v['device_name'] = str_replace('广西大王椰','',$v['device_name']); $wee_key[$v['device_name']] = $v['c']; } $day = DeviceData::wherein('dev_eui',$key)->groupBy('device_name')->where('crt_time','>=',$dayStart)->where('crt_time','<',$dayEnd)->select(DB::raw('count(1) as c'),'device_name')->get()->toArray(); $day_key = []; foreach ($day as $v){ $v['device_name'] = str_replace('广西大王椰','',$v['device_name']); $day_key[$v['device_name']] = $v['c']; } return [$day_key,$wee_key,$month_key]; } public function wyOee(){ $models = []; $site = 1; $list = DeviceSite::where('site',$site)->wherein('title',['主缸压力','压力','温度'])->groupBy('device_name')->pluck('device_name')->toArray(); foreach ($list as $v){ $models[$v] = [ "y_day_num"=> 0, "y_month_num"=> 0, "y_week_num"=> 0, "w_day_num"=> 0, "w_month_num"=> 0, "w_week_num"=> 0, "start_time"=> '', "rate_y"=> 0, "rate_w"=> 0, ]; } $device_point_key = DeviceSite::where('site',$site)->select( '*' )->get()->toArray(); //失败次数 $err_key = []; foreach ($device_point_key as $v){ if($v['title'] == '主缸压力'||$v['title'] == '压力') $err_key[] = $v['key']; } list($day_key,$week_key,$month_key) = $this->wyiInitCount($err_key); //运行次数 $run_key = []; foreach ($device_point_key as $v){ if($v['title'] == '温度') $run_key[] = $v['key']; } list($run_day_key,$run_week_key,$run_month_key) = $this->wyiInitCount($run_key); //当天最早开始时间 $dayStart = strtotime(date('Y-m-d')); // $start_time = DeviceData::groupBy('device_name')->where('crt_time','>=',$dayStart)->select(DB::raw('min(crt_time) as crt_time'),'device_name')->get()->toArray(); $y_time = DeviceData::groupBy('device_name')->orderBy('id','desc')->where('crt_time','>=',$dayStart)->wherein('dev_eui',$err_key)->select('happening_data','device_name')->get()->toArray(); $w_time = DeviceData::groupBy('device_name')->orderBy('id','desc')->where('crt_time','>=',$dayStart)->wherein('dev_eui',$run_key)->select('happening_data','device_name')->get()->toArray(); $start_key = []; $now_w_key = []; $now_y_key = []; foreach ($start_time as $v){ $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19); $start_key[$v['device_name']] = $v['crt_time']; } foreach ($y_time as $v){ $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19); $now_y_key[$v['device_name']] = $v['happening_data']; } foreach ($w_time as $v){ $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19); $now_w_key[$v['device_name']] = $v['happening_data']; } //当天的开始与结束时间戳 foreach ($models as $k=>$v){ $models[$k]['y_day_num'] = $day_key[$k]??0; $models[$k]['title'] = str_replace('广西大王椰','',$k); $models[$k]['y_month_num'] = $month_key[$k]??0; $models[$k]['y_week_num'] = $week_key[$k]??0; $models[$k]['start_time'] = $start_key[$k]??0; $models[$k]['w_day_num'] = $run_day_key[$k]??0; $models[$k]['w_month_num'] = $run_week_key[$k]??0; $models[$k]['w_week_num'] = $run_month_key[$k]??0; $models[$k]['rate_w'] = $now_w_key[$k]??0; $models[$k]['rate_y'] = $now_y_key[$k]??0; } sort($models); return $this->json_return(200,'',$models); } public function wyiInitCount($key){ $lastMonthStart = strtotime(date('Y-m-01', strtotime('-1 month'))); // 上个月的第一天 $lastMonthEnd = strtotime(date('Y-m-t', strtotime('-1 month')))+86400; // 上个月的最后一天 $lastWeekStart = strtotime(date('Y-m-d', strtotime('-1 week last Monday'))); // 上周的周一 $lastWeekEnd = strtotime(date('Y-m-d', strtotime('-1 week next Sunday'))); // 上周的周日 $dayStart = strtotime(date('Y-m-d')); // $dayEnd = $dayStart+86400; // 上周的周日 $month = DeviceData::wherein('dev_eui',$key)->groupBy('device_name')->where('crt_time','>=',$lastMonthStart)->where('crt_time','<',$lastMonthEnd)->select(DB::raw('sum(happening_data) as s'),DB::raw('count(1) as c'),'device_name')->get()->toArray(); $month_key = []; foreach ($month as $v){ $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19); $month_key[$v['device_name']] = sprintf('%.2f',($v['s']/$v['c'])); } // var_dump($key); // var_dump($lastWeekStart); // var_dump($lastMonthEnd); $week = DeviceData::wherein('dev_eui',$key)->groupBy('device_name')->where('crt_time','>=',$lastWeekStart)->where('crt_time','<',$lastWeekEnd)->select(DB::raw('sum(happening_data) as s'),DB::raw('count(1) as c'),'device_name')->get()->toArray(); $wee_key = []; foreach ($week as $v){ $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19); $wee_key[$v['device_name']] = sprintf('%.2f',($v['s']/$v['c'])); } $day = DeviceData::wherein('dev_eui',$key)->groupBy('device_name')->where('crt_time','>=',$dayStart)->where('crt_time','<',$dayEnd)->select(DB::raw('sum(happening_data) as s'),DB::raw('count(1) as c'),'device_name')->get()->toArray(); $day_key = []; foreach ($day as $v){ $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19); $day_key[$v['device_name']] = sprintf('%.2f',($v['s']/$v['c'])); } return [$day_key,$wee_key,$month_key]; } /** * 用于计算时间 * @param $minute * @return string */ public function calTimeReturnMin($minute){ return number_format($minute * 1.5 / 60,2); } }