<?php
ini_set("display_errors","1");
$mysqli = new mysqli('localhost','root','nancysimon','myquant');

//ajax:  get stock list for select
if($_REQUEST['t']=='stockList'){
    if($_REQUEST['t_vendor']=='方正证券'){
        $sql= <<< EOF
        select distinct(ts_code) ts_code,ts_name from trade_record
        where tprice>0 and (ts_code not like '7%' and ts_code not like '1%')  and length(trade_id)=5 order by ts_code
EOF;
    }elseif($_REQUEST['t_vendor']=='长江证券'){
        //length(0+ts_name)!=length(ts_name) 判断ts_name 不能为纯数字
        $sql= <<< EOF
    select distinct(ts_code) ts_code,ts_name from trade_record_cj
where tprice>0  and ts_code not like '7%' and length(0+ts_name)!=length(ts_name) order by ts_code
EOF;
    }
    $result = $mysqli->query($sql);
    $data = $result->fetch_all(MYSQLI_ASSOC);
    echo json_encode(array(
        "status" => "1",
        'stocks'=> $data,
        "company"=>$_REQUEST['t_vendor'],
    ),JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
}

/**
 * AJAX get finance Data from getFianceData class
 */
if($_REQUEST['t']=='financeData'){
    include_once __DIR__."/getFinanceData.class.php";
    include_once __DIR__."/getBasic.inc.php";
    $years=yearList($_REQUEST['yst'],$_REQUEST['yed']);

    $fina = new getFinance();
    $fina->ts_code=$_REQUEST['ts_code'];
    $dataAll=array();
    foreach($years as $year){
        $fina->finDate=$year;
        $dataAll[] =$fina->getFinanceData();
    }

    for($i=0;$i<count($dataAll);$i++){
        $n=0;
        foreach ($dataAll[$i] as $key=>$val){
            $n++;
            if(strlen($n)<2) $n='0'.$n;
            if($key=='ts_code') {
                $key=$n.'.股票代码';
                $val=tscodeToName($val);
                $n--;
                continue;  //不显示股票代码
            }elseif($key=='period') {
                $key=$n.'.报告期';
                $val=yearToname($val);
                $n--;
                continue; //不显示报告期
            }else $key=$n.'.'.$key;
            if($i==0) $data[$n][]=$key;  //首列加
            $data[$n][]=$val;
        }
    }

    //strip key from array $data
    //dataTable must NOT have any key of json
    $data=array_values($data);
    echo json_encode(array("data"=>$data),JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
}

/*
*esfTBD: 二手房Trade By Day
*/
if($_REQUEST['t']=='esfTBD'){
    $dataTrade=esfTradeDaily();
    $dataList=esfListDaily();
    echo json_encode(array(
        "status" => "1",
        'dataTrade'=> $dataTrade,
        'dataList'=>$dataList
    ),JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
}

/*
*esfListDaily: 二手房每日挂牌数量
*/
if($_REQUEST['t']=='esfListDaily'){
    $data=esfListDaily();

    echo json_encode(array(
        "status" => "1",
        'datas'=> $data
    ),JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
}
function esfTradeDaily(){
    global $mysqli;
    if($_REQUEST['dm']=='Daily'){
        $sql=   <<<EOF
    SELECT 
        distinct(ej.uuid) uid ,
        date_format(tdate,'%Y-%m-%d') td,  
        eje.val district,
        eje1.val qty,
        eje2.val area  
    FROM 
        `estate_json` ej
        left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val='{$_REQUEST['district']}'
        left join  estate_json_ext eje1 on eje1.id=eje.id+3
        left join  estate_json_ext eje2 on eje2.id=eje.id+4
        where ej.data_type='5' and (ej.tdate>='{$_REQUEST['t_start']}' and ej.tdate<='{$_REQUEST['t_end']}')
        order by ej.tdate asc
EOF;
    }
    if($_REQUEST['dm']=='Monthly'){
        $where="";
        if($_REQUEST['t_start'])  $where  .=" and ej.tdate>='{$_REQUEST['t_start']}-01'";
        if($_REQUEST['t_end'])  {
            $endDate = new DateTime($_REQUEST['t_end']);
            $endDate->modify('last day of this month');
            $where    .=" and ej.tdate<='{$endDate->format('Y-m-d')}'";
        }
        $sql=   <<<EOF
    SELECT 
        distinct(ej.uuid) uid ,
        date_format(tdate,'%Y-%m') td,  
        eje.val district,
        sum(eje1.val) qty,
        sum(eje2.val) area  
    FROM 
        `estate_json` ej
        left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val='{$_REQUEST['district']}'
        left join  estate_json_ext eje1 on eje1.id=eje.id+3
        left join  estate_json_ext eje2 on eje2.id=eje.id+4
        where ej.data_type='5'  {$where}
        group by date_format(tdate,'%Y-%m') order by date_format(tdate,'%Y-%m') asc
EOF;
//echo $sql;
    }
    
    $result = $mysqli->query($sql);
    $data = $result->fetch_all(MYSQLI_ASSOC);
    return $data;
}

function esfListDaily(){
    global $mysqli;
    if($_REQUEST['dm']=='Daily'){
        $sql=   <<<EOF
    SELECT 
        distinct(ej.uuid) uid ,
        date_format(tdate,'%Y-%m-%d') td,  
        eje.val district,
        eje1.val qty,
        eje2.val price  
    FROM 
        `estate_json` ej
        left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val='{$_REQUEST['district']}'
        left join  estate_json_ext eje1 on eje1.id=eje.id+3
        left join  estate_json_ext eje2 on eje2.id=eje.id+5
        where ej.data_type='8' and (ej.tdate>='{$_REQUEST['t_start']}' and ej.tdate<='{$_REQUEST['t_end']}')
        order by ej.tdate asc
EOF;
    }
    if($_REQUEST['dm']=='Monthly'){
        $where="";
        if($_REQUEST['t_start'])  $where  .=" and ej.tdate>='{$_REQUEST['t_start']}-01'";
        if($_REQUEST['t_end'])  {
            $endDate = new DateTime($_REQUEST['t_end']);
            $endDate->modify('last day of this month');
            $where    .=" and ej.tdate<='{$endDate->format('Y-m-d')}'";
        }
        $sql=   <<<EOF
    SELECT 
        distinct(ej.uuid) uid ,
        date_format(tdate,'%Y-%m') td,  
        eje.val district,
        sum(eje1.val) qty,
        sum(eje2.val) price  
    FROM 
        `estate_json` ej
        left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val='{$_REQUEST['district']}'
        left join  estate_json_ext eje1 on eje1.id=eje.id+3
        left join  estate_json_ext eje2 on eje2.id=eje.id+5
        where ej.data_type='8' {$where}
        group by date_format(tdate,'%Y-%m') order by date_format(tdate,'%Y-%m') asc
EOF;
//echo $sql;
    }
    $result = $mysqli->query($sql);
    $data = $result->fetch_all(MYSQLI_ASSOC);
    return $data;
}

/*
*newTBD: 新房Trade By Day
*/
if($_REQUEST['t']=='newTBD'){
    if($_REQUEST['dm']=='Daily'){
    $sql=   <<<EOF
    SELECT 
        distinct(ej.uuid) uid ,
        date_format(tdate,'%Y-%m-%d') td,  
        eje.val district,
        eje1.val qty,
        eje2.val area  
    FROM 
        `estate_json` ej
        left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val='{$_REQUEST['district']}'
        left join  estate_json_ext eje1 on eje1.id=eje.id+3
        left join  estate_json_ext eje2 on eje2.id=eje.id+4
        where ej.data_type='1' and (ej.tdate>='{$_REQUEST['t_start']}' and ej.tdate<='{$_REQUEST['t_end']}')
        order by date_format(tdate,'%Y-%m-%d') asc
EOF;
    }
    if($_REQUEST['dm']=='Monthly'){
        $where="";
        if($_REQUEST['t_start'])  $where  .=" and ej.tdate>='{$_REQUEST['t_start']}-01'";
        if($_REQUEST['t_end'])  {
            $endDate = new DateTime($_REQUEST['t_end']);
            $endDate->modify('last day of this month');
            $where    .=" and ej.tdate<='{$endDate->format('Y-m-d')}'";
        }
        $sql=   <<<EOF
        SELECT 
            distinct(ej.uuid) uid ,
            date_format(tdate,'%Y-%m') td,  
            eje.val district,
            sum(eje1.val) qty,
            sum(eje2.val) area  
        FROM 
            `estate_json` ej
            left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val='{$_REQUEST['district']}'
            left join  estate_json_ext eje1 on eje1.id=eje.id+3
            left join  estate_json_ext eje2 on eje2.id=eje.id+4
            where ej.data_type='1' {$where}
            group by date_format(tdate,'%Y-%m') order by date_format(tdate,'%Y-%m') asc
    EOF;
        }

    $result = $mysqli->query($sql);
    $data = $result->fetch_all(MYSQLI_ASSOC);
    if($_REQUEST['dm']=='Daily'){
    $sql=   <<<EOF
    SELECT 
        distinct(ej.uuid) uid ,
        date_format(tdate,'%Y-%m-%d') td,  
        eje.val district,
        eje1.val area,
        eje2.val qty  
    FROM 
        `estate_json` ej
        left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val='{$_REQUEST['district']}'
        left join  estate_json_ext eje1 on eje1.id=eje.id+3
        left join  estate_json_ext eje2 on eje2.id=eje.id+4
        where ej.data_type='4' and (ej.tdate>='{$_REQUEST['t_start']}' and ej.tdate<='{$_REQUEST['t_end']}')
        order by date_format(tdate,'%Y-%m-%d') asc
EOF;
    }
    if($_REQUEST['dm']=='Monthly'){
        $where="";
        if($_REQUEST['t_start'])  $where  .=" and ej.tdate>='{$_REQUEST['t_start']}-01'";
        if($_REQUEST['t_end'])  {
            $endDate = new DateTime($_REQUEST['t_end']);
            $endDate->modify('last day of this month');
            $where    .=" and ej.tdate<='{$endDate->format('Y-m-d')}'";
        }
        $sql=   <<<EOF
        SELECT 
            distinct(ej.uuid) uid ,
            date_format(tdate,'%Y-%m-%d') td,  
            eje.val district,
            max(eje1.val) area,
            max(eje2.val) qty  
        FROM 
            `estate_json` ej
            left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val='{$_REQUEST['district']}'
            left join  estate_json_ext eje1 on eje1.id=eje.id+3
            left join  estate_json_ext eje2 on eje2.id=eje.id+4
            where ej.data_type='4' $where
            group by date_format(tdate,'%Y-%m') order by date_format(tdate,'%Y-%m') asc
    EOF;
        }
    $result = $mysqli->query($sql);
    $data2 = $result->fetch_all(MYSQLI_ASSOC);
    echo json_encode(array(
        "status" => "1",
        'datas'=> $data,
        'dataExt'=> $data2
    ),JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
}
