<?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";
        if($cityId != NULL && $districtId == NULL){
            $selectName = "districtId as id,districtName as value";
            $where .= " and cityId =".$cityId;
            $groupBy = " group by districtId";
        }else if($cityId != NULL && $districtId != NULL){
            $selectName = "plateId as id,plateName as value";
            $where .= " and cityId = ".$cityId." and districtId = ".$districtId;
            $groupBy = "";
        }
        $result = $wpdb->get_results('select '.$selectName.' from '.TCSync::DIC_CITY_TABLE.$where.$groupBy);
        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 = ".$cityId;
        }
        $result = $wpdb->get_results('select id,priceValue as value from '.TCSync::DIC_AREA_TABLE.$where);
        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 = ".$cityId;
        }
        $result = $wpdb->get_results('select id,priceValue as value from '.TCSync::DIC_UNITPRICERANGE_TABLE.$where);
        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('select id,priceValue as value from '.TCSync::DIC_TOTALPRICE_TABLE.$where);
        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('select Id as id,Name as name from '.TCSync::TOSPUR_ORGANIZATION_TABLE.$where);
        return $result;
    }

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

    public static function getWorkUser(){

    }
}