<?php
class SearchDao{

    public function __construct() {
        require_once(PLUGIN_DIR . '\TCSync.php');
    }
    public static function ajax_serachCity(){
        wp_send_json(SearchDao::searchCity($_GET["cityId"],$_GET['districtId']));
    }

    public static function searchCity($cityId = NULL,$districtId = NULL){
        global $wpdb;
        $selectName = "cityId as id,cityName as value";
        $where = " where 1=1 ";
        $groupBy = " group by cityId";
        $params = array();
        if($cityId != NULL && $districtId == NULL){
            $selectName = "districtId as id,districtName as value";
            $where .= " and cityId = %d";
            $params[] = $cityId;
            $groupBy = " group by districtId";
        }else if($cityId != NULL && $districtId != NULL){
            $selectName = "plateId as id,plateName as value";
            $where .= " and cityId = %d and districtId = %d";
            $params[] = $cityId;
            $params[] = $districtId;
            $groupBy = "";
        }
        $result = $wpdb->get_results($wpdb->prepare('select '.$selectName.' from '.TCSync::DIC_CITY_TABLE.$where.$groupBy,$params));
        return $result;
    }

    public static function ajax_searchArea(){
        wp_send_json(SearchDao::searchArea($_GET['cityId']));
    }

    public static function searchArea($cityId){
        global $wpdb;
        $where = " where 1=1";
        if(isset($_GET['cityId'])){
            $where .= " and cityId = %d";
        }
        $result = $wpdb->get_results($wpdb->prepare('select id,priceValue as value from '.TCSync::DIC_AREA_TABLE.$where,$cityId));
        return $result;
    }

    public static function ajax_searchBuildProperty(){
        wp_send_json(SearchDao::searchBuildProperty());
    }

    public static function searchBuildProperty(){
        global $wpdb;
        $result = $wpdb->get_results('select value as id,literal as value from '.TCSync::DIC_BUILDPROPERTY_TABLE);
        return $result;
    }

    public static function ajax_searchRoom(){
        wp_send_json(SearchDao::searchRoom());
    }

    public static function searchRoom(){
        global $wpdb;
        $result = $wpdb->get_results('select value as id,literal as value from '.TCSync::DIC_ROOM_TABLE);
        return $result;
    }

    public static function ajax_searchUnitPriceRange(){
        wp_send_json(SearchDao::searchUnitPriceRange($_GET['cityId']));
    }

    public static function searchUnitPriceRange($cityId){
        global $wpdb;
        $where = " where 1=1";
        if(isset($_GET['cityId'])){
            $where .= " and cityId = %d";
        }
        $result = $wpdb->get_results($wpdb->prepare('select id,priceValue as value from '.TCSync::DIC_UNITPRICERANGE_TABLE.$where,$cityId));
        return $result;
    }

    public static function ajax_searchTotalPrice(){
        wp_send_json(SearchDao::searchTotalPrice($_GET['cityId']));
    }

    public static function searchTotalPrice($cityId){
        global $wpdb;
        $where = " where 1=1";
        if(isset($_GET['cityId'])){
            $where .= " and cityId = ".$cityId;
        }
        $result = $wpdb->get_results($wpdb->prepare('select id,priceValue as value from '.TCSync::DIC_TOTALPRICE_TABLE.$where,$cityId));
        return $result;
    }

    public static function ajax_searchOrganization(){
        wp_send_json(SearchDao::searchOrganization($_GET['parentId']));
    }

    public static function searchOrganization($parentId = NULL){
        global $wpdb;
        $where = " where 1=1 ";
        if($parentId != NULL){
            $where .= " and ParentId =".$parentId;
        }
        $result = $wpdb->get_results($wpdb->prepare('select Id as id,Name as name from '.TCSync::TOSPUR_ORGANIZATION_TABLE.$where,$parentId));
        return $result;
    }

    public static function getCityNameWithId($cityId){
        global $wpdb;
        $result = $wpdb->get_var($wpdb->prepare("select cityName from ".TCSync::DIC_CITY_TABLE." where cityId = %d",$cityId));
        if($result){
            return $result;
        }else{
            return "未知";
        }
    }

    public static function ajax_searchHouse(){
        wp_send_json(SearchDao::searchHouse($_GET["cityId"],$_GET["districtId"],$_GET["plateId"],$_GET["buildPropertyId"],$_GET["roomId"],$_GET["acreage"],$_GET["totalPrice"],$_GET["searchText"]));
    }

    public static function searchHouse($cityId = NULL,$districtId = NULL,$plateId = NULL,$buildpropertyId = NULL,$roomId = NULL,$acreage = NULL,$totalPrice = NULL,$searchText = NULL){
        global $wpdb;
        if($acreage == "面积"){
            $acreage = null;
        }
        if($totalPrice == "价格"){
            $totalPrice = null;
        }
        $areaArray = explode("-",$acreage);
        $priceArray = explode("-",$totalPrice);
        $sql = "select  * from tospur_house th
                left join (select buildproperty_id,house_area,image_id,house_id as bph_id from a_district_area) ada on th.id = ada.bph_id
                left join dic_city dc on  th.plate_id = dc.plateId
                left join (select * from a_house_image group by house_id) hi on th.id = hi.house_id
                left join (select id,name as iname,path,creattime as itime,alt,image_type from tospur_image) i on hi.image_id = i.id where 1=1";
        if($cityId!=NULL  ){
            $sql = $sql." and cityId=".$cityId;
        }
        if($districtId != NULL ){
            $sql = $sql." and districtId=".$districtId;
        }
        if($plateId != NULL){
            $sql = $sql." and plateId=".$plateId;
        }
        if($buildpropertyId!=NULL){
            $sql = $sql." and buildproperty_id=".$buildpropertyId;
        }
        if($roomId!=NULL){
            $sql = $sql." and room_id=".$roomId;
        }
        if($acreage!=NULL){
            $sql = $sql." and covered_area between ". $areaArray[0]." and ". $areaArray[1];
        }
        if($totalPrice!=NULL){
            $sql = $sql." and total_price between ". $priceArray[0]." and ". $priceArray[1];
        }
        if($searchText!=NULL){
            $sql = $sql." and name like '%".$searchText."%'";
        }

        $sql = $sql." group by bph_id order by th.creattime DESC";
        $result = $wpdb->get_results($sql);
        return $result;
    }
    public static function getWorkUser(){

    }
}