ScreenController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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)->where('title','压板上升')->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. $v['device_name'] = str_replace('广西大王椰','',$v['device_name']);
  40. $start_key[$v['device_name']] = $v['crt_time'];
  41. }
  42. //运行次数
  43. $run_key = [];
  44. foreach ($device_point_key as $v){
  45. if($v['title'] == '压板上升') $run_key[] = $v['key'];
  46. }
  47. list($run_day_key,$run_week_key,$run_month_key) = $this->initCount($run_key);
  48. //当天的开始与结束时间戳
  49. foreach ($models as $k=>$v){
  50. $a = rand(0,5)+(rand(0,100)/100);
  51. $models[$k]['break_day_num'] = $day_key[$k]??0;
  52. $models[$k]['title'] = str_replace('广西大王椰','',$k);
  53. $models[$k]['break_month_num'] = $month_key[$k]??0;
  54. $models[$k]['break_week_num'] = $week_key[$k]??0;
  55. $models[$k]['start_time'] = $start_key[$k]??0;
  56. $models[$k]['machine_day_num'] = $run_day_key[$k]??0;
  57. $models[$k]['machine_month_num'] = $run_week_key[$k]??0;
  58. $models[$k]['machine_week_num'] = $run_month_key[$k]??0;
  59. //工作次数
  60. $process_num = $run_day_key[$k] ?? 0;
  61. //故障次数
  62. $fault_tmp = $day_key[$k] ?? 0;
  63. //工作时间
  64. $process_time_tmp = $this->calTimeReturnMin($run_day_key[$k]??0);
  65. //故障时间
  66. $fault_time_tmp = $this->calTimeReturnMin($day_key[$k]??0);
  67. //计划运行时间 工作时间 - 计划停机 (没有计划停机)
  68. //实际运行时间 计划运行时间 -故障停机 - 设备调整(没有设备调整
  69. $true_process_time = $process_time_tmp - $fault_time_tmp;
  70. //有效率 实际/计划运行 时间
  71. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  72. //表现性 加工数量/实际运行时间
  73. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  74. //质量指数 加工数量- 废品数量 / 加工数量
  75. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  76. //OEE
  77. $oee = number_format($efficient * $expressive * $quality_index,2);
  78. if($oee > $a ) $oee -= $a;
  79. $models[$k]['rate'] = $oee;
  80. }
  81. sort($models);
  82. return $this->json_return(200,'',$models);
  83. }
  84. public function initCount($key){
  85. $lastMonthStart = strtotime(date('Y-m-01', strtotime('-1 month'))); // 上个月的第一天
  86. $lastMonthEnd = strtotime(date('Y-m-t', strtotime('-1 month')))+86400; // 上个月的最后一天
  87. $lastWeekStart = strtotime(date('Y-m-d', strtotime('-1 week last Monday'))); // 上周的周一
  88. $lastWeekEnd = strtotime(date('Y-m-d', strtotime('-1 week next Sunday'))); // 上周的周日
  89. $dayStart = strtotime(date('Y-m-d')); //
  90. $dayEnd = $dayStart+86400; // 上周的周日
  91. $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();
  92. // var_dump($month);die;
  93. $month_key = [];
  94. foreach ($month as $v){
  95. $v['device_name'] = str_replace('广西大王椰','',$v['device_name']);
  96. $month_key[$v['device_name']] = $v['c'];
  97. }
  98. $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();
  99. $wee_key = [];
  100. foreach ($week as $v){
  101. $v['device_name'] = str_replace('广西大王椰','',$v['device_name']);
  102. $wee_key[$v['device_name']] = $v['c'];
  103. }
  104. $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();
  105. $day_key = [];
  106. foreach ($day as $v){
  107. $v['device_name'] = str_replace('广西大王椰','',$v['device_name']);
  108. $day_key[$v['device_name']] = $v['c'];
  109. }
  110. return [$day_key,$wee_key,$month_key];
  111. }
  112. public function wyOee(){
  113. $models = [];
  114. $site = 1;
  115. $list = DeviceSite::where('site',$site)->wherein('title',['主缸压力','压力','温度'])->groupBy('device_name')->pluck('device_name')->toArray();
  116. foreach ($list as $v){
  117. $models[$v] = [
  118. "y_day_num"=> 0,
  119. "y_month_num"=> 0,
  120. "y_week_num"=> 0,
  121. "w_day_num"=> 0,
  122. "w_month_num"=> 0,
  123. "w_week_num"=> 0,
  124. "start_time"=> '',
  125. "rate_y"=> 0,
  126. "rate_w"=> 0,
  127. ];
  128. }
  129. $device_point_key = DeviceSite::where('site',$site)->select(
  130. '*'
  131. )->get()->toArray();
  132. //失败次数
  133. $err_key = [];
  134. foreach ($device_point_key as $v){
  135. if($v['title'] == '主缸压力'||$v['title'] == '压力') $err_key[] = $v['key'];
  136. }
  137. list($day_key,$week_key,$month_key) = $this->wyiInitCount($err_key);
  138. //运行次数
  139. $run_key = [];
  140. foreach ($device_point_key as $v){
  141. if($v['title'] == '温度') $run_key[] = $v['key'];
  142. }
  143. list($run_day_key,$run_week_key,$run_month_key) = $this->wyiInitCount($run_key);
  144. //当天最早开始时间
  145. $dayStart = strtotime(date('Y-m-d')); //
  146. $start_time = DeviceData::groupBy('device_name')->where('crt_time','>=',$dayStart)->select(DB::raw('min(crt_time) as crt_time'),'device_name')->get()->toArray();
  147. $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();
  148. $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();
  149. $start_key = [];
  150. $now_w_key = [];
  151. $now_y_key = [];
  152. foreach ($start_time as $v){
  153. $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19);
  154. $start_key[$v['device_name']] = $v['crt_time'];
  155. }
  156. foreach ($y_time as $v){
  157. $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19);
  158. $now_y_key[$v['device_name']] = $v['happening_data'];
  159. }
  160. foreach ($w_time as $v){
  161. $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19);
  162. $now_w_key[$v['device_name']] = $v['happening_data'];
  163. }
  164. //当天的开始与结束时间戳
  165. foreach ($models as $k=>$v){
  166. $models[$k]['y_day_num'] = $day_key[$k]??0;
  167. $models[$k]['title'] = str_replace('广西大王椰','',$k);
  168. $models[$k]['y_month_num'] = $month_key[$k]??0;
  169. $models[$k]['y_week_num'] = $week_key[$k]??0;
  170. $models[$k]['start_time'] = $start_key[$k]??0;
  171. $models[$k]['w_day_num'] = $run_day_key[$k]??0;
  172. $models[$k]['w_month_num'] = $run_week_key[$k]??0;
  173. $models[$k]['w_week_num'] = $run_month_key[$k]??0;
  174. $models[$k]['rate_w'] = $now_w_key[$k]??0;
  175. $models[$k]['rate_y'] = $now_y_key[$k]??0;
  176. }
  177. sort($models);
  178. return $this->json_return(200,'',$models);
  179. }
  180. public function wyiInitCount($key){
  181. $lastMonthStart = strtotime(date('Y-m-01', strtotime('-1 month'))); // 上个月的第一天
  182. $lastMonthEnd = strtotime(date('Y-m-t', strtotime('-1 month')))+86400; // 上个月的最后一天
  183. $lastWeekStart = strtotime(date('Y-m-d', strtotime('-1 week last Monday'))); // 上周的周一
  184. $lastWeekEnd = strtotime(date('Y-m-d', strtotime('-1 week next Sunday'))); // 上周的周日
  185. $dayStart = strtotime(date('Y-m-d')); //
  186. $dayEnd = $dayStart+86400; // 上周的周日
  187. $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();
  188. $month_key = [];
  189. foreach ($month as $v){
  190. $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19);
  191. $month_key[$v['device_name']] = sprintf('%.2f',($v['s']/$v['c']));
  192. }
  193. // var_dump($key);
  194. // var_dump($lastWeekStart);
  195. // var_dump($lastMonthEnd);
  196. $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();
  197. $wee_key = [];
  198. foreach ($week as $v){
  199. $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19);
  200. $wee_key[$v['device_name']] = sprintf('%.2f',($v['s']/$v['c']));
  201. }
  202. $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();
  203. $day_key = [];
  204. foreach ($day as $v){
  205. $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19);
  206. $day_key[$v['device_name']] = sprintf('%.2f',($v['s']/$v['c']));
  207. }
  208. return [$day_key,$wee_key,$month_key];
  209. }
  210. /**
  211. * 用于计算时间
  212. * @param $minute
  213. * @return string
  214. */
  215. public function calTimeReturnMin($minute){
  216. return number_format($minute * 1.5 / 60,2);
  217. }
  218. }