| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 | 
							- <?php
 
- namespace App\Http\Controllers\Api;
 
- use App\Model\DeviceData;
 
- use App\Model\DeviceSite;
 
- use Illuminate\Http\Request;
 
- use Illuminate\Support\Facades\DB;
 
- class ScreenController extends BaseController
 
- {
 
-     public function oee(){
 
-         $models = [];
 
-         $site = 1;
 
-         $list = DeviceSite::where('site',$site)->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){
 
-             $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){
 
-             $models[$k]['break_day_num'] = $day_key[$k]??0;
 
-             $models[$k]['title'] = $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'] = $this->calTimeReturnMin($run_day_key[$k]??0);
 
-             $models[$k]['machine_month_num'] = $this->calTimeReturnMin($run_week_key[$k]??0);
 
-             $models[$k]['machine_week_num'] = $this->calTimeReturnMin($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);
 
-             $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','<',$lastWeekEnd)->select(DB::raw('count(1) as c'),'device_name')->get()->toArray();
 
-         $month_key = [];
 
-         foreach ($month as $v){
 
-             $month_key[$v['device_name']] = $v['c'];
 
-         }
 
-         $week = DeviceData::wherein('dev_eui',$key)->groupBy('device_name')->where('crt_time','>=',$lastWeekStart)->where('crt_time','<',$lastMonthEnd)->select(DB::raw('count(1) as c'),'device_name')->get()->toArray();
 
-         $wee_key = [];
 
-         foreach ($week as $v){
 
-             $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){
 
-             $day_key[$v['device_name']] = $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);
 
-     }
 
- }
 
 
  |