<?php
//完工站设备的当前的AutoWork记录
include_once('../common/base.php');
include_once('../common/func.php');



$response=array();
$tdata=array();
$total=0;
$pageIndex=isset($_POST['page'])?$_POST['page']:1;
$pageSize=10;

//工作站，默认是设置里面的完工站
$wcids=$config['workcenter_of_device'];
$wcids=implode(",",$wcids);
if (isset($_POST['wcids']) && !empty($_POST['wcids'])) {
    $wcids=$_POST['wcids'];
}

//设备号
$equNos='';
if(isset($_POST['equNos']) && !empty($_POST['equNos'])){
    $equNos=$_POST['equNos'];
}


//时间区间
$startTime=date('Y-m-d 00:00:00');
$endTime=date('Y-m-d 00:00:00',strtotime('next day'));
if(isset($_POST['startTime']) && !empty($_POST['startTime'])){
    $startTime=$_POST['startTime'];
    $endTime=$_POST['endTime'];
}


$sSql="select max(_id) from AutoWork where EquNo in (select EquNo from Equipment where CenterId in ({$wcids})) and cast(WorkTime as datetime)>='{$startTime}' and cast(WorkTime as datetime)<'{$endTime}' ";
if(!empty($equNos)){
    $sSql.="  and  EquNo in ({$equNos}) ";
}
$sSql.="  group by EquNo ,cast( Convert(nvarchar(100),cast(WorkTime as datetime),101) as datetime) ";

$sql="select count(*) from AutoWork  where _id in ({$sSql}) ";

$stmt=myQuery($conn,$sql);
if($stmt){
    while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC)){
        $total=$row[0];
        if($total==null){
            $total=0;
        }
    }

    
    sqlsrv_free_stmt($stmt);
}



if (($pageIndex-1)*$pageSize>=$total) {
    $pageIndex=1;
}


$sql="select ROW_NUMBER() over ( order by _id asc) as rownum,AutoWork.*,WorkCenter.CenterName,Equipment.MachineId,";
$sql.="case when AutoWork.AllTime<>0 then  cast(AutoWork.RunTime as float)/AutoWork.AllTime else 0 end as TimeRunRate,case when Equipment.Capacity<>0 then  cast(AutoWork.RunTime as float)/(Equipment.Capacity*3600) else 0 end as CapacityUsageRate  ";
$sql.=" from AutoWork inner join Equipment on AutoWork.EquNo=Equipment.EquNo inner join WorkCenter on Equipment.CenterId= WorkCenter.CenterId where _id in ({$sSql}) ";
$sql="select top {$pageSize} t.* from ({$sql}) t   where  rownum>".($pageIndex-1)*$pageSize;
$stmt=myQuery($conn,$sql);

if($stmt){
    while($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)){
        $row['TimeRunRate']=(round($row['TimeRunRate'],4)*100).'%';
        $row['CapacityUsageRate']=(round($row['CapacityUsageRate'],4)*100).'%';
        $sql2="select cast(sum(dEquNum) as int) from AutoWork where EquNo='{$row['EquNo']}' and datediff(day,WorkTime,'{$row['WorkTime']}')=0";
        $row['WorkTime']=date('Y-m-d',strtotime($row['WorkTime']));
        $stmt2=myQuery($conn,$sql2);
        if($stmt2){
            $row2=sqlsrv_fetch_array($stmt2,SQLSRV_FETCH_NUMERIC);
            $row['TodayEquNum']=($row2[0]==null?0:$row2[0]);
            sqlsrv_free_stmt($stmt2);
        }
        $tdata[]=$row;
    }
    sqlsrv_free_stmt($stmt);
}





if ($total==0) {
    $pageIndex=0;
}

$response['tdata']=$tdata;
$response['code']=1;
$response['pageSize']=$pageSize;
$response['cur_page']=$pageIndex;
$response['total_page']=ceil($total/$pageSize);
$response['total']=$total;


echo json_encode($response);

