ScreenController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Model\DeviceData;
  4. use App\Model\DeviceSite;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. class ScreenController extends BaseController
  8. {
  9. public function oee(){
  10. $models = [];
  11. $site = 1;
  12. $list = DeviceSite::where('site',$site)->groupBy('device_name')->pluck('device_name')->toArray();
  13. foreach ($list as $v){
  14. $models[$v] = [
  15. "machine_day_num"=> 0,
  16. "machine_month_num"=> 0,
  17. "machine_week_num"=> 0,
  18. "break_day_num"=> 0,
  19. "break_month_num"=> 0,
  20. "break_week_num"=> 0,
  21. "start_time"=> '',
  22. "rate"=> 0
  23. ];
  24. }
  25. $device_point_key = DeviceSite::where('site',$site)->select(
  26. '*'
  27. )->get()->toArray();
  28. //失败次数
  29. $err_key = [];
  30. foreach ($device_point_key as $v){
  31. if($v['title'] == '急停') $err_key[] = $v['key'];
  32. }
  33. list($day_key,$week_key,$month_key) = $this->initCount($err_key);
  34. //当天最早开始时间
  35. $dayStart = strtotime(date('Y-m-d')); //
  36. $start_time = DeviceData::groupBy('device_name')->where('crt_time','>=',$dayStart)->select(DB::raw('min(crt_time) as crt_time'),'device_name')->get()->toArray();
  37. $start_key = [];
  38. foreach ($start_time as $v){
  39. $start_key[$v['device_name']] = $v['crt_time'];
  40. }
  41. //运行次数
  42. $run_key = [];
  43. foreach ($device_point_key as $v){
  44. if($v['title'] == '压板上升') $run_key[] = $v['key'];
  45. }
  46. list($run_day_key,$run_week_key,$run_month_key) = $this->initCount($run_key);
  47. //当天的开始与结束时间戳
  48. foreach ($models as $k=>$v){
  49. $models[$k]['break_day_num'] = $day_key[$k]??0;
  50. $models[$k]['title'] = str_replace('广西大王椰','',$k);
  51. $models[$k]['break_month_num'] = $month_key[$k]??0;
  52. $models[$k]['break_week_num'] = $week_key[$k]??0;
  53. $models[$k]['start_time'] = $start_key[$k]??0;
  54. $models[$k]['machine_day_num'] = $this->calTimeReturnMin($run_day_key[$k]??0);
  55. $models[$k]['machine_month_num'] = $this->calTimeReturnMin($run_week_key[$k]??0);
  56. $models[$k]['machine_week_num'] = $this->calTimeReturnMin($run_month_key[$k]??0);
  57. //工作次数
  58. $process_num = $run_day_key[$k] ?? 0;
  59. //故障次数
  60. $fault_tmp = $day_key[$k] ?? 0;
  61. //工作时间
  62. $process_time_tmp = $this->calTimeReturnMin($run_day_key[$k]??0);
  63. //故障时间
  64. $fault_time_tmp = $this->calTimeReturnMin($day_key[$k]??0);
  65. //计划运行时间 工作时间 - 计划停机 (没有计划停机)
  66. //实际运行时间 计划运行时间 -故障停机 - 设备调整(没有设备调整
  67. $true_process_time = $process_time_tmp - $fault_time_tmp;
  68. //有效率 实际/计划运行 时间
  69. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  70. //表现性 加工数量/实际运行时间
  71. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  72. //质量指数 加工数量- 废品数量 / 加工数量
  73. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  74. //OEE
  75. $oee = number_format($efficient * $expressive * $quality_index,2);
  76. $models[$k]['rate'] = $oee;
  77. }
  78. sort($models);
  79. return $this->json_return(200,'',$models);
  80. }
  81. public function initCount($key){
  82. $lastMonthStart = strtotime(date('Y-m-01', strtotime('-1 month'))); // 上个月的第一天
  83. $lastMonthEnd = strtotime(date('Y-m-t', strtotime('-1 month')))+86400; // 上个月的最后一天
  84. $lastWeekStart = strtotime(date('Y-m-d', strtotime('-1 week last Monday'))); // 上周的周一
  85. $lastWeekEnd = strtotime(date('Y-m-d', strtotime('-1 week next Sunday'))); // 上周的周日
  86. $dayStart = strtotime(date('Y-m-d')); //
  87. $dayEnd = $dayStart+86400; // 上周的周日
  88. $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();
  89. $month_key = [];
  90. foreach ($month as $v){
  91. $month_key[$v['device_name']] = $v['c'];
  92. }
  93. $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();
  94. $wee_key = [];
  95. foreach ($week as $v){
  96. $wee_key[$v['device_name']] = $v['c'];
  97. }
  98. $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();
  99. $day_key = [];
  100. foreach ($day as $v){
  101. $day_key[$v['device_name']] = $v['c'];
  102. }
  103. return [$day_key,$wee_key,$month_key];
  104. }
  105. /**
  106. * 用于计算时间
  107. * @param $minute
  108. * @return string
  109. */
  110. public function calTimeReturnMin($minute){
  111. return number_format($minute * 1.5 / 60,2);
  112. }
  113. }