<?php

include_once('../common/base.php');

$respone=array();
$departmentid=$config['work_order']['producing_department'];
$workcenters=$config['work_order']['workcenters'];
$producingLines=$config['work_order']['producing_lines'];
$is_producing_line=$config['work_order']['is_producing_line'];

//计算每小时效率
$hourly_eff=array();
//计算当前所在小时
$curTime=strtotime(date('Y-m-d H:30:00', time()));
if ($curTime-time()>0) {
    $curTime=strtotime(date('Y-m-d H:00:00', time()));
}


for ($i=15;$i>=0;$i--) {
    $hourly_eff[]=array(
        'hour_s'=>date('Y-m-d H:i:00', strtotime('-'.($i*30).' minutes', $curTime)),
        'hour_e'=>date('Y-m-d H:i:00', strtotime('-'.(($i-1)*30).' minutes', $curTime)),
        'eff'=>0
    );
}

$conditions=array();
foreach ($hourly_eff as $k=>$v) {

    if(!isset($config['parameters']['time_seq_chart_source']) || $config['parameters']['time_seq_chart_source']==1){
        $sql="select  sum([ElecProWorkNum].[RecordNum]*[ProProcess].[UnitHour]) as  realworktime,sum(datediff(second,[ElecProWorkNum].[CreateTime],[ElecProWorkNum].[ReportTime])) as planworktime  from [ElecProWorkNum] INNER JOIN [ProcessWork] ON  [ElecProWorkNum].[ProWorkId]=[ProcessWork].[ProWorkId]  AND ";
        if ($is_producing_line==1) {
            foreach ($producingLines as $depid=>$wc) {
                if (isset($wc['checked']) && $wc['checked']==1) {
                    $conditions[]=" ([ProcessWork].[DepId]={$depid} AND [ProcessWork].[CenterId]={$wc['CenterId']} )  ";
                }
            }
            $sql.= " (".implode(' OR ', $conditions).") ";
        } else {
            $t_workcenters=implode(",", $workcenters);
            $sql.=" [ProcessWork].[CenterId] IN  ({$t_workcenters}) ";
            // if ($departmentid==0) {
            //     $t_workcenters=implode(",", $workcenters);
            //     $sql.=" [ProcessWork].[CenterId] IN  ({$t_workcenters}) ";
            // } else {
            //     foreach ($workcenters as $wcid) {
            //         $conditions[]=" ([ProcessWork].[DepId]={$departmentid} AND [ProcessWork].[CenterId]={$wcid} )  ";
            //     }
            //     $sql.= " (".implode(' OR ', $conditions).") ";
            // }
        }
      
        $sql.=" INNER JOIN [ProProcess] ON [ProcessWork].[ProProcessId]=[ProProcess].[ProProcessId] where [ElecProWorkNum].[ReportTime]>='{$v['hour_s']}' and [ElecProWorkNum].[ReportTime]<'{$v['hour_e']}' ";
    
    
    }else{

        $sql="select sum(AutoWork.dEquNum*ProProcess.UnitHour),sum(AutoWork.dRunTime+AutoWork.dStopTime+AutoWork.dUndoTime) from AutoWork inner join ProcessWork on AutoWork.ProWorkId=ProcessWork.ProWorkId ";
        $sql.=" inner join  ProProcess on ProcessWork.ProProcessId=ProProcess.ProProcessId  inner join Equipment on Equipment.EquNo=AutoWork.EquNo ";
        $sql.=" where AutoWork.WorkTime>='{$v['hour_s']}' and AutoWork.WorkTime<'{$v['hour_e']}' ";
        if ($is_producing_line==1) {
            foreach ($producingLines as $depid=>$wc) {
                if (isset($wc['checked']) && $wc['checked']==1) {
                    $conditions[]=" ([Equipment].[DepId]={$depid} AND [Equipment].[CenterId]={$wc['CenterId']} )  ";
                }
            }
            $sql.= " (".implode(' OR ', $conditions).") ";
        } else {
            $t_workcenters=implode(",", $workcenters);
            $sql.="  And [Equipment].[CenterId] IN  ({$t_workcenters}) ";
            // if ($departmentid==0) {
            //     $t_workcenters=implode(",", $workcenters);
            //     $sql.=" [ProcessWork].[CenterId] IN  ({$t_workcenters}) ";
            // } else {
            //     foreach ($workcenters as $wcid) {
            //         $conditions[]=" ([ProcessWork].[DepId]={$departmentid} AND [ProcessWork].[CenterId]={$wcid} )  ";
            //     }
            //     $sql.= " (".implode(' OR ', $conditions).") ";
            // }
        }

        if(count($conditions)>0){
            $sql.=" And ";
            $sql.= " (".implode(' OR ', $conditions).") ";
        }
    }


    $stmt=sqlsrv_query($conn, $sql);


    if ($stmt) {
        $row=sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC);
        if ($row[1]!=0) {
            $hourly_eff[$k]['eff']=round($row[0]/$row[1], 4);
        } else {
            $hourly_eff[$k]['eff']=0;
        }
    }


    if ($stmt) {
        sqlsrv_free_stmt($stmt);
    }
}

$eff=array();
$timeline=array();
foreach ($hourly_eff as $k => $v) {
    $eff[]=$hourly_eff[$k]['eff'];
    $timeline[]=date('H时i分', strtotime($hourly_eff[$k]['hour_e']));
}


$response['code']=200;
$response['eff']=$eff;
$response['timeline']=$timeline;
// header('Content-Type:application/json;charset=utf-8;');
echo json_encode($response);
