<?php
include_once('../common/base.php');
$range=$_GET['range'];
$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'];

$starttime=date('Y-m-d 0:0:0');
$endtime=date('Y-m-d H:i:s');
$mode="今日";
//缺陷情况
// switch ($config['work_order']['defect_search_mode']) {
//     //2:查本周计划
//     case '2':
//         $starttime=date('Y-m-d 00:00:00', strtotime('Monday this week'));
//         $endtime=date('Y-m-d 00:00:00', strtotime('Monday next week '));
// 		$mode="本周";
//         break;
//     //3:查本月计划
//     case '3':
//         $starttime=date('Y-m-d 00:00:00', strtotime('first day of this month'));
//         $endtime=date('Y-m-d 00:00:00', strtotime('first day of next month'));
// 		$mode="本月";
//         break;
//     //3:查本年计划
//     case '4':
//         $starttime=date('Y-m-d 00:00:00', strtotime('first day of this year'));
//         $endtime=date('Y-m-d 00:00:00', strtotime('first day of next year'));
// 		$mode="本年";
//         break;    
//     //默认仅查当日
//     default:
//         $starttime=date('Y-m-d 00:00:00');
//         $endtime=date('Y-m-d 00:00:00', strtotime('next day'));
// }

switch ($range) {
    //2:查本周计划
    case '周':
        $starttime=date('Y-m-d 00:00:00', strtotime('Monday this week'));
        $endtime=date('Y-m-d 00:00:00', strtotime('Monday next week '));
		$mode="本周";
        break;
    //3:查本月计划
    case '月':
        $starttime=date('Y-m-01 00:00:00', strtotime('first day of this month'));
        $endtime=date('Y-m-01 00:00:00', strtotime('first day of next month'));
		$mode="本月";
        break;
    //3:查本年计划
    case '年':
        $starttime=date('Y-01-01 00:00:00', strtotime('first day of this year'));
        $endtime=date('Y-01-01 00:00:00', strtotime('first day of next year'));
		$mode="本年";
        
        break;    
    //默认仅查当日
    default:
        $starttime=date('Y-m-d 00:00:00');
        $endtime=date('Y-m-d 00:00:00', strtotime('next day'));
}


$sql=' SELECT TOP 5  [InspDetail].[DefName],[InspDetail].[DefCode],cast(sum([InspDetail].[InspNum]) as int) as [InspNumSum]  FROM  [dbo].[InspRecord] INNER 

JOIN [dbo].[InspDetail] ON [dbo].[InspRecord].[InspId]=[dbo].[InspDetail].[InspId] ';
$conditions=array();
if(count($config['currentProdLines'])>0){
    foreach ($config['currentProdLines'] as $depid) {
        $conditions[]=$depid;
    }
    $t_pdls=implode(",", $conditions);
    $sql.="  WHERE  InspRecord.EquNo in (select EquNo from Equipment where Equipment.DepId IN  ({$t_pdls}) )   ";
}else{
    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'].')';
            }
        }
        $t_pdls=implode(" Or ", $conditions);
        $sql.="  WHERE  InspRecord.EquNo in (select EquNo from Equipment where $t_pdls )   ";
     
    } else {
        $t_workcenters=implode(",", $workcenters);
     
        $sql.=" WHERE [dbo].[InspRecord].[CenterId] IN  ({$t_workcenters}) ";
    }
}




$sql.=" AND  [dbo].[InspRecord].[Status]=2   AND [dbo].[InspRecord].[TestTime]>='{$starttime}' AND [dbo].[InspRecord].[TestTime]<'{$endtime}' Group By 

[InspDetail].[DefCode],[InspDetail].[DefName] Order By InspNumSum Desc";


$stmt=sqlsrv_query($conn, $sql);
$insp=array();
$insp['items']=array();
$insp['data']=array();
$total=0;
if ($stmt) {
    while ($row=sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC)) {
        $insp['items'][]=$row[0];
        $total+=$row[2];
        $insp['data'][]=array('name'=>$row[0],'value'=>round($row[2], 0));
    }
}

if ($stmt) {
    sqlsrv_free_stmt($stmt);
}
$insp['data2']=array();
foreach ($insp['data'] as $k=>$v) {
    if ($total!=0) {
        $insp['data2'][$k]['value']=round($insp['data'][$k]['value']/$total*100, 2);
    } else {
        $insp['data2'][$k]['value']=0;
    }
}
$response['code']=200;
$response['insp']=$insp;
$response['mode']=$mode;
echo json_encode($response);
