ReportFormsService.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Employee;
  4. use App\Model\OrdersProduct;
  5. use App\Model\OrdersProductProcess;
  6. use App\Model\Scrapp;
  7. use App\Model\SystemL;
  8. use App\Model\Team;
  9. use Illuminate\Support\Facades\DB;
  10. /**
  11. * 设备相关设置报表
  12. * Class ReportFormsService
  13. * @package App\Service
  14. */
  15. class ReportFormsService extends Service
  16. {
  17. /**
  18. * 数据分析图
  19. * @param $data
  20. * @return array
  21. */
  22. public function deviceStatisticsReportChart($data){
  23. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  24. $day = $this->returnDays($data['time'], false);
  25. if($day > 31) return [false, '查询时间仅支持范围区间在31天内'];
  26. $process_time = [];
  27. $result = SystemL::where('time','>=',$data['time'][0])
  28. ->where('time','<',$data['time'][1])
  29. ->where('data_point_name',SystemL::standBy)
  30. ->select('device_name','time','value')
  31. ->get()->toArray();
  32. //所有的时间
  33. $time_all = [];
  34. foreach ($result as $value){
  35. $time = date('Y-m-d',$value['time'] / 1000);
  36. //工作 运行次数
  37. if(isset($process_time[$value['device_name']][$time])){
  38. $process_time[$value['device_name']][$time] += 1;
  39. }else{
  40. $process_time[$value['device_name']][$time] = 1;
  41. }
  42. if(! in_array($time, $time_all)) $time_all[] = $time;
  43. }
  44. sort($time_all);
  45. //数据结构模型
  46. foreach (SystemL::$device as $k => $v){
  47. if(isset($process_time[$k])){
  48. foreach ($time_all as $t){
  49. if(! isset($process_time[$k][$t])){
  50. $process_time[$k][$t] = 0;
  51. }
  52. }
  53. }else{
  54. foreach ($time_all as $t){
  55. $process_time[$k][$t] = 0;
  56. }
  57. }
  58. }
  59. $return = [];
  60. foreach ($process_time as $key => $value){
  61. $tmp['title'] = $key;
  62. $tmp['list'] = [];
  63. foreach ($value as $k => $v){
  64. $tmp['list'][] = [
  65. 'time' => $k,
  66. 'num' => $v
  67. ];
  68. }
  69. $return[] = $tmp;
  70. }
  71. return [true, $return];
  72. }
  73. /**
  74. * 数据OEE分析图
  75. * @param $data
  76. * @return array
  77. */
  78. public function deviceStatisticsReportOEEChart($data){
  79. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  80. $day = $this->returnDays($data['time'], false);
  81. if($day > 31) return [false, '查询时间仅支持范围区间在31天内'];
  82. //获取数据
  83. $result = SystemL::where('time','>=',$data['time'][0])
  84. ->where('time','<',$data['time'][1])
  85. ->select('device_name','time','value','data_point_name')
  86. ->get()->toArray();
  87. if(empty($result)) return [true,[]];
  88. $device_name = array_values(array_unique(array_column($result,'device_name')));
  89. $run_time = $process_time = $fault = $time_all = [];
  90. foreach ($result as $value){
  91. $time = date("Y-m-d",$value['time'] / 1000);
  92. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::standBy){
  93. //运行次数
  94. if(isset($run_time[$value['device_name']][$time])){
  95. $run_time[$value['device_name']][$time] += 1;
  96. }else{
  97. $run_time[$value['device_name']][$time] = 1;
  98. }
  99. }
  100. if($value['data_point_name'] == SystemL::standBy){
  101. //工作次数
  102. if(isset($process_time[$value['device_name']][$time])){
  103. $process_time[$value['device_name']][$time] += 1;
  104. }else{
  105. $process_time[$value['device_name']][$time] = 1;
  106. }
  107. }
  108. if($value['data_point_name'] == SystemL::stop){
  109. //故障次数
  110. if(isset($fault[$value['device_name']][$time])){
  111. $fault[$value['device_name']][$time] += 1;
  112. }else{
  113. $fault[$value['device_name']][$time] = 1;
  114. }
  115. }
  116. if(! in_array($time, $time_all)) $time_all[] = $time;
  117. }
  118. sort($time_all);
  119. //组织模型 返回大致的数据结构
  120. $models = [];
  121. foreach (SystemL::$device as $k => $v){
  122. foreach ($time_all as $t){
  123. $models[$k][$t] = 0;
  124. }
  125. }
  126. //填充模型里的数据
  127. foreach ($device_name as $value){//设备名
  128. foreach ($time_all as $d_val){
  129. //运行次数
  130. $run_num = $run_time[$value][$d_val] ?? 0;
  131. //工作次数
  132. $process_num = $process_time[$value][$d_val] ?? 0;
  133. //故障次数
  134. $fault_tmp = $fault[$value][$d_val] ?? 0;
  135. //运行时间
  136. $run_time_tmp = $this->calTimeReturnMin($run_num);
  137. //工作时间
  138. $process_time_tmp = $this->calTimeReturnMin($process_num);
  139. //故障时间
  140. $fault_time_tmp = $this->calTimeReturnMin($fault_tmp);
  141. //计划运行时间 工作时间 - 计划停机
  142. //实际运行时间 计划运行时间 -故障停机 - 设备调整
  143. $true_process_time = $process_time_tmp - $fault_time_tmp;
  144. //有效率 实际/计划运行 时间
  145. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  146. //表现性 加工数量/实际运行时间
  147. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  148. //质量指数 加工数量- 废品数量 / 加工数量
  149. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  150. //OEE
  151. $oee = number_format($efficient * $expressive * $quality_index,2);
  152. //模型里赋值
  153. if(isset($models[$value][$d_val])){
  154. $models[$value][$d_val] = $oee;
  155. }
  156. }
  157. }
  158. //返回结果
  159. $final = [];
  160. foreach ($models as $key => $value){
  161. $tmp['title'] = $key;
  162. $tmp['list'] = [];
  163. foreach ($value as $k => $v){
  164. $tmp['list'][] = [
  165. 'time' => $k,
  166. 'num' => $v
  167. ];
  168. }
  169. $final[] = $tmp;
  170. }
  171. return [true,$final];
  172. }
  173. /**
  174. * 用于计算时间
  175. * @param $minute
  176. * @return string
  177. */
  178. public function calTimeReturnMin($minute){
  179. return number_format($minute * 1.5 / 60,2);
  180. }
  181. function returnDays($time = [], $is_mirco_time = true){
  182. // 示例时间戳
  183. $timestamp1 = $time[0];
  184. $timestamp2 = $time[1];
  185. // 计算时间差
  186. $difference = abs($timestamp2 - $timestamp1);
  187. // 将时间差转换为天数
  188. $days = floor($difference / (60 * 60 * 24));
  189. if($is_mirco_time) $days = $days / 1000;
  190. return $days;
  191. }
  192. }