<?php

function getEqus($pid, $type, &$equs,$page=null,$pageSize=20)
{
    global $conn;
    if ($type=='workcenter') {
        $pid="(".implode(",",$pid).")";
        $sql="select WorkCenter.CenterNo,EquNo,EquName,IsCNC,MachineId  from Equipment inner join WorkCenter on WorkCenter.CenterId=Equipment.CenterId ";
        $sql.=" where Equipment.CenterId IN {$pid} order by WorkCenter.CenterNo asc,Equipment.EquNo asc";
     
    } else {
        $sql="select EquNo,EquName,IsCNC,MachineId from Equipment ";
        $sql.=" where DepId={$pid} ";
    }


    $stmt=myQuery($conn, $sql);
    if ($stmt) {
        while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
            $equs[$row['EquNo']]=$row;
        }
        sqlsrv_free_stmt($stmt);
    }
  

    if ($type=='department') {
        //获取子部门
        $sql_departments='SELECT [DepId],[DepCode],[DepName],[PDepId],[Property] FROM [dbo].[Department] where  [PDepId]='.$pid.' Order By [Sequence] ';
        $stmt_departments=sqlsrv_query($conn, $sql_departments);
        if ($stmt_departments) {
            while ($row = sqlsrv_fetch_array($stmt_departments, SQLSRV_FETCH_ASSOC)) {
                if ($row!=null) {
                    getEqus($row['DepId'], 'department', $equs);
                }
            }
            sqlsrv_free_stmt($stmt_departments);
        }
    }
}


function timeToHMS($time){
    $hour=floor($time/3600);
    $time2=($time%3600);
    $min=floor($time2/60);
    $time3=($time2%60);

    return $hour.':'.$min.'\''.$time3.'\'\'';
}

function myQuery($conn,$sql){
    $stmt=sqlsrv_query($conn,$sql);
    if($stmt==false ){
        $error=sqlsrv_errors();
        throw new Exception(implode(",",array('msg'=>$error[0]['message'],'sql'=>$sql)));
    }
    
    return $stmt;
}

function myGetCol($row,$col,$default=null){
    if($row==null || $row==false){
        return $default;
    }

    return $row[$col];
}
