<?php
include_once('../common/base.php');
include_once('../common/func.php');

$data=array();
$starttime=null;
$todaySt=date('Y-m-d 00:00', time());
$todayEn=date('Y-m-d 23:59', time());
try{
    
    $machineId=$_GET['machineId'];
    if(empty($machineId)){
        throw new Exception('machineId不能为空');
    }

    //获取设备号
    $data['machineId']=$machineId;

    //获取状态
    $sql="select top 1 cncStatus from mdc_CNCStatus where machineID='".$machineId."' order by time desc ";
    $stmt=myQuery($conn,$sql);
    $row=sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC);
    $data['cncStatus']=myGetCol($row,'cncStatus');

    //获取程序信息
    $sql="select top 1 mainPrgmName from mdc_ProgramInfo where machineID='".$machineId."' order by time desc ";
    $stmt=myQuery($conn,$sql);
    $row=sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC);
    $data['mainPrgmName']=myGetCol($row,'mainPrgmName');

    //获取刀号
    $sql="select top 1 toolNumber from mdc_CurrentToolNumber where machineID='".$machineId."' order by time desc ";
    $stmt=myQuery($conn,$sql);
    $row=sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC);
    $data['toolNumber']=myGetCol($row,'toolNumber');

    //获取负载
    $sql="select top 1 spindleLoad from mdc_Load where machineID='".$machineId."' order by time desc ";
    $stmt=myQuery($conn,$sql);
    $row=sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC);
    $data['spindleLoad']=myGetCol($row,'spindleLoad');

    //获取进给,转速,进给倍率,主轴倍率
    $sql="select top 1 actFeed,actSpindle,overFeed,overSpindle from mdc_FeedAndSpindle where machineID='".$machineId."' order by time desc ";
    $stmt=myQuery($conn,$sql);
    $row=sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC);
    $data['actFeed']=myGetCol($row,'actFeed');
    $data['actSpindle']=myGetCol($row,'actSpindle');
    $data['overFeed']=myGetCol($row,'overFeed');
    $data['overSpindle']=myGetCol($row,'overSpindle');

    //产量
    $sql="select top 1 cumulativeCount from mdc_Count where machineID='".$machineId."' order by time desc ";
    $stmt=myQuery($conn,$sql);
    $row=sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC);
    $data['cumulativeCount']=myGetCol($row,'cumulativeCount');

    //开动率
    $sql="select top 1 availability from mdc_OEE where machineID='".$machineId."' order by time desc ";
    $stmt=myQuery($conn,$sql);
    $row=sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC);
    $data['availability']=myGetCol($row,'availability');      

    //报警列表
    $sql="select *  from mdc_AlarmHistory where machineID='{$machineId}' and time>='{$todaySt}' and time<'{$todayEn}' order by time desc ";
    $stmt=myQuery($conn,$sql);
    $data['alarmMsgs']=array();
    while($row=sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)){
        $row['alarmTime']=isset($row['alarmTime'])?$row['alarmTime']->format('Y-m-d H:i:s'):'';
        $row['time']=$row['time']->format('Y-m-d H:i:s');
        $data['alarmMsgs'][]=$row;
    }

    
    //机床状态
    $st1=strtotime('-24hours');
    $st1=date('Y-m-d H:i:s',$st1);
    $en1=date('Y-m-d H:i:s',time());
    $t=$st1;

    $statuses=array();
    $status='';
    // $sql="select top 1 * from mdc_CNCStatus where machineId='{$machineId}'  and time>='{$t}' and time<'{$en1}' and cast(cncStatus as nvarchar(100))<>'{$status}' order by time asc";
    // $stmt=myQuery($conn,$sql);
    // $row=null;
    // $tcount=0;
    // while($row=sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)){

    //     if(($count=count($statuses))>0){           
    //         $lastItem=$statuses[$count-1];
    //         $lastItem[2]=myGetCol($row,'time')->format('Y-m-d H:i:s');
    //         $statuses[$count-1]=$lastItem;
    //     }
    //     $item=array(
    //         myGetCol($row,'time')->format('Y-m-d H:i:s'),
    //         1,
    //         null,
    //         myGetCol($row,'cncStatus'),
    //     );

    //     $tcount++;
    //     if($tcount>50) break;
    //     $statuses[]=$item;
    //     $t=myGetCol($row,'time')->format('Y-m-d H:i:s');
    //     $status=myGetCol($row,'cncStatus');
    //     sqlsrv_free_stmt($stmt);
    //     $sql="select top 1 * from mdc_CNCStatus where machineId='{$machineId}'  and time>='{$t}' and time<'{$en1}' and cast(cncStatus as nvarchar(100))<>'{$status}' order by time asc";
    //     $stmt=myQuery($conn,$sql);
        
    // }

    // if(($count=count($statuses))>0){
    //     $statuses[$count-1][2]=$en1;
    // }
    $statuses[]=array($st1,1,null,'OFF','OFF');
    $sql="select  * from mdc_CNCStatus where machineId='{$machineId}'  and time>='{$st1}' and time<'{$en1}'  order by time asc";
    $stmt=myQuery($conn,$sql);
    while($row=sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)){
        if( stripos(myGetCol($row,'cncStatus'),'MANUAL')!==false){
            $row['cncStatus']='MANUAL';
        } 
        $count=count($statuses);
        if($count>0){                     
            $lastItem=$statuses[$count-1];
            if($lastItem[3]!=myGetCol($row,'cncStatus')){
                $lastItem[2]=myGetCol($row,'time')->format('Y-m-d H:i:s');
                $statuses[$count-1]=$lastItem;
                $item=array(
                    myGetCol($row,'time')->format('Y-m-d H:i:s'),
                    1,
                    null,
                    myGetCol($row,'cncStatus')
                );

                $statuses[]=$item;               
            }else{
                continue;
            }

        }else{
            $item=array(
                myGetCol($row,'time')->format('Y-m-d H:i:s'),
                1,
                null,
                myGetCol($row,'cncStatus')
            );
            $statuses[]=$item;
        }

        
    }

    if(($count=count($statuses))>0){
        $statuses[$count-1][2]=$en1;
    }
    $statuses[]=array($en1,1,$en1,'OFF','OFF');
    sqlsrv_free_stmt($stmt);
    $data['cncStatuses']=$statuses;

    echo json_encode(array('code'=>1,'data'=>$data));

}catch(Exception $e){
    $data=array('code'=>0,'msg'=>$e->getMessage());
    echo json_encode($data);
    die();
}



