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

$data=array();
if(isset($_GET['date'])){
    $st1=$_GET['date'];
    $st1=date('Y-m-d 00:00:00',strtotime($st1));
}else{
    $st1=date('Y-m-d 00:00', time());
}
$en1=date('Y-m-d 00:00:00',strtotime("next day",strtotime($st1)));

try{
    
    $equNo=$_GET['equNo'];


    $existMdc=true;
    $haveMdcRow=true;
    //判断mdcs数据源是否存在
    $sql="select count (*) from sys.tables where name='mdc_CNCStatus' and type='U' ";
    $stmt=myQuery($conn,$sql);
    if($stmt){
        $row=sqlsrv_fetch_array($stmt,SQLSRV_FETCH_NUMERIC);
        if($row[0]==null || $row[0]==0){
            $existMdc=false;
        }
    }else{
        throw new Exception(sqlsrv_errors());
    }

    $statuses=array();
    $status='';

    if($existMdc){
  
        $sql="select * from Equipment where EquNo='{$equNo}' ";
        $stmt=myQuery($conn,$sql);
        $machineId=0;
        if($stmt){
            $row=sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC);
            $machineId=$row['MachineId'];
        }else{
            throw new Exception(sqlsrv_errors());
        }
        //开始时间 高度 //结束时间  //机台状态
        $statuses[]=array($st1,1,null,'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');
        $data['cncStatuses']=$statuses;
        sqlsrv_free_stmt($stmt);
        
        if(count($statuses)<=2){
            $haveMdcRow=false;
        }
    }
    
    
    if($existMdc==false || $haveMdcRow==false){
        $statuses=[];
 
        //切换到autowork
        //开始时间 高度 //结束时间  //机台状态
        //显示绿色，表示正常1主机运转中；显示红色，表示2报警停机；显示黄色，表示3\4停止停机；0断点停机 灰色
        $statuses[]=array($st1,1,null,'OFF');
        $sql="select AutoWork.* from AutoWork inner join Equipment on AutoWork.EquNo=Equipment.EquNo where AutoWork.EquNo='{$equNo}'  and cast(AutoWork.WorkTime as datetime)>='{$st1}' and cast(AutoWork.WorkTime as datetime)<'{$en1}'  order by WorkTime asc";
    
        $stmt=myQuery($conn,$sql);
        while($row=sqlsrv_fetch_array($stmt,SQLSRV_FETCH_BOTH)){
            switch(myGetCol($row,'Status')){
                case 0:
                    $row['cncStatus']='OFF';
                    break;
                case 1:
                    $row['cncStatus']='RUN';
                    break;
                case 2:
                    $row['cncStatus']='EMERGENCY';
                    break;              
                case 3:
                    $row['cncStatus']='WARNING';
                    break;

                case 4:
                    $row['cncStatus']='WARNING';
                    break;            
                default:
                    $row['cncStatus']='OFF';
            }

            $count=count($statuses);
            if($count>0){                     
                $lastItem=$statuses[$count-1];
                if($lastItem[3]!=myGetCol($row,'cncStatus')){
                    $lastItem[2]=myGetCol($row,'WorkTime');
                    $statuses[$count-1]=$lastItem;
                    $item=array(
                        myGetCol($row,'WorkTime'),
                        1,
                        null,
                        myGetCol($row,'cncStatus')
                    );
    
                    $statuses[]=$item;               
                }else{
                    continue;
                }
              
            }else{
                $item=array(
                    myGetCol($row,'WorkTime'),
                    1,
                    null,
                    myGetCol($row,'cncStatus')
                );
                $statuses[]=$item;
            }    
        }
        if(($count=count($statuses))>0){
            $statuses[$count-1][2]=$en1;
        }

        $statuses[]=array($en1,1,$en1,'OFF');
        $data['cncStatuses']=$statuses;
        sqlsrv_free_stmt($stmt); 
    }



    echo json_encode(array('code'=>1,'data'=>$data,'frommdc'=>($existMdc && $haveMdcRow)));

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



