123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <?php
- namespace App\Service;
- use App\Model\Employee;
- use App\Model\OrdersProduct;
- use App\Model\OrdersProductProcess;
- use App\Model\Scrapp;
- use App\Model\SystemL;
- use App\Model\Team;
- use Illuminate\Support\Facades\DB;
- /**
- * 设备相关设置报表
- * Class ReportFormsService
- * @package App\Service
- */
- class ReportFormsService extends Service
- {
- /**
- * 数据分析图
- * @param $data
- * @return array
- */
- public function deviceStatisticsReportChart($data){
- if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
- $day = $this->returnDays($data['time'], false);
- if($day > 31) return [false, '查询时间仅支持范围区间在31天内'];
- $process_time = [];
- $result = SystemL::where('time','>=',$data['time'][0])
- ->where('time','<',$data['time'][1])
- ->where('data_point_name',SystemL::standBy)
- ->select('device_name','time','value')
- ->get()->toArray();
- //所有的时间
- $time_all = [];
- foreach ($result as $value){
- $time = date('Y-m-d',$value['time'] / 1000);
- //工作 运行次数
- if(isset($process_time[$value['device_name']][$time])){
- $process_time[$value['device_name']][$time] += 1;
- }else{
- $process_time[$value['device_name']][$time] = 1;
- }
- if(! in_array($time, $time_all)) $time_all[] = $time;
- }
- sort($time_all);
- //数据结构模型
- foreach (SystemL::$device as $k => $v){
- if(isset($process_time[$k])){
- foreach ($time_all as $t){
- if(! isset($process_time[$k][$t])){
- $process_time[$k][$t] = 0;
- }
- }
- }else{
- foreach ($time_all as $t){
- $process_time[$k][$t] = 0;
- }
- }
- }
- $return = [];
- foreach ($process_time as $key => $value){
- $tmp['title'] = $key;
- $tmp['list'] = [];
- foreach ($value as $k => $v){
- $tmp['list'][] = [
- 'time' => $k,
- 'num' => $v
- ];
- }
- $return[] = $tmp;
- }
- return [true, $return];
- }
- /**
- * 数据OEE分析图
- * @param $data
- * @return array
- */
- public function deviceStatisticsReportOEEChart($data){
- if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
- $day = $this->returnDays($data['time'], false);
- if($day > 31) return [false, '查询时间仅支持范围区间在31天内'];
- //获取数据
- $result = SystemL::where('time','>=',$data['time'][0])
- ->where('time','<',$data['time'][1])
- ->select('device_name','time','value','data_point_name')
- ->get()->toArray();
- if(empty($result)) return [true,[]];
- $device_name = array_values(array_unique(array_column($result,'device_name')));
- $run_time = $process_time = $fault = $time_all = [];
- foreach ($result as $value){
- $time = date("Y-m-d",$value['time'] / 1000);
- if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::standBy){
- //运行次数
- if(isset($run_time[$value['device_name']][$time])){
- $run_time[$value['device_name']][$time] += 1;
- }else{
- $run_time[$value['device_name']][$time] = 1;
- }
- }
- if($value['data_point_name'] == SystemL::standBy){
- //工作次数
- if(isset($process_time[$value['device_name']][$time])){
- $process_time[$value['device_name']][$time] += 1;
- }else{
- $process_time[$value['device_name']][$time] = 1;
- }
- }
- if($value['data_point_name'] == SystemL::stop){
- //故障次数
- if(isset($fault[$value['device_name']][$time])){
- $fault[$value['device_name']][$time] += 1;
- }else{
- $fault[$value['device_name']][$time] = 1;
- }
- }
- if(! in_array($time, $time_all)) $time_all[] = $time;
- }
- sort($time_all);
- //组织模型 返回大致的数据结构
- $models = [];
- foreach (SystemL::$device as $k => $v){
- foreach ($time_all as $t){
- $models[$k][$t] = 0;
- }
- }
- //填充模型里的数据
- foreach ($device_name as $value){//设备名
- foreach ($time_all as $d_val){
- //运行次数
- $run_num = $run_time[$value][$d_val] ?? 0;
- //工作次数
- $process_num = $process_time[$value][$d_val] ?? 0;
- //故障次数
- $fault_tmp = $fault[$value][$d_val] ?? 0;
- //运行时间
- $run_time_tmp = $this->calTimeReturnMin($run_num);
- //工作时间
- $process_time_tmp = $this->calTimeReturnMin($process_num);
- //故障时间
- $fault_time_tmp = $this->calTimeReturnMin($fault_tmp);
- //计划运行时间 工作时间 - 计划停机
- //实际运行时间 计划运行时间 -故障停机 - 设备调整
- $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(isset($models[$value][$d_val])){
- $models[$value][$d_val] = $oee;
- }
- }
- }
- //返回结果
- $final = [];
- foreach ($models as $key => $value){
- $tmp['title'] = $key;
- $tmp['list'] = [];
- foreach ($value as $k => $v){
- $tmp['list'][] = [
- 'time' => $k,
- 'num' => $v
- ];
- }
- $final[] = $tmp;
- }
- return [true,$final];
- }
- /**
- * 用于计算时间
- * @param $minute
- * @return string
- */
- public function calTimeReturnMin($minute){
- return number_format($minute * 1.5 / 60,2);
- }
- function returnDays($time = [], $is_mirco_time = true){
- // 示例时间戳
- $timestamp1 = $time[0];
- $timestamp2 = $time[1];
- // 计算时间差
- $difference = abs($timestamp2 - $timestamp1);
- // 将时间差转换为天数
- $days = floor($difference / (60 * 60 * 24));
- if($is_mirco_time) $days = $days / 1000;
- return $days;
- }
- }
|