<?php

require_once(PLUGIN_DIR . 'Config.php');

class SearchDao
{
    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 ' . Config::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 ' . Config::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 ' . Config::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 ' . Config::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 ' . Config::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 ' . Config::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 ' . Config::TOSPUR_ORGANIZATION_TABLE . $where, $parentId));
        return $result;
    }

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

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

    public static function searchHouse($array)
    {
        global $wpdb;
        $params = array();
        $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 (select value as bp_value,literal as bp_literal from dic_buildproperty) dbp on ada.buildproperty_id = dbp.bp_value
 left join (select plateId,plateName from dic_city) dcp on th.plate_id = dcp.plateId
 left join (select districtId,districtName from dic_city) dcd on th.district_id = dcd.districtId
 left join (select cityId,cityName from dic_city) dcc on th.city_id = dcc.cityId
 left join (select id as rid,value as r_value,literal from dic_room) dr on th.room_id = dr.r_value
 where 1=1";
        if ($array['cityId'] > -1) {
            $params[] = $array['cityId'];
            $sql = $sql . " and cityId=%d";
        }
        if ($array['districtId'] > -1) {
            $params[] = $array['districtId'];
            $sql = $sql . " and districtId=%d";
        }
        if ($array['plateId'] > -1) {
            $params[] = $array['plateId'];
            $sql = $sql . " and plateId=%d";
        }
        if ($array['buildPropertyId'] > -1) {
            $params[] = $array['buildPropertyId'];
            $sql = $sql . " and buildproperty_id=%d";
        }
        if ($array['roomId'] > -1) {
            $params[] = $array['roomId'];
            $sql = $sql . " and room_id=%d";
        }
        if ($array['acreage'] > -1) {
            $areaArray = explode("-", $array['acreage']);
            $params[] = $areaArray[0];
            $params[] = $areaArray[1];
            $sql = $sql . " and covered_area between %d and %d";
        }
        if ($array['totalPrice'] > -1) {
            $priceArray = explode("-", $array['totalPrice']);
            $params[] = $priceArray[0];
            $params[] = $priceArray[1];
            $sql = $sql . " and total_price between %d and %d";
        }
        if ($array['searchText'] != null) {
            $params[] = "%" . $array['searchText'] . "%";
            $sql = $sql . " and name like %s";
        }
        if ($array['houseType'] > -1) {
            $params[] = $array['houseType'];
            $sql = $sql . " and house_type = %d";
        }
        if ($array['userType'] > -1 && $array['userId'] > -1) {
            $params[] = $array['userType'];
            $params[] = $array['userId'];
            $sql = $sql . " and id in (select house_id from a_house_user where user_type = %d and user_id = %d)";
        }

        $sql = $sql . " group by bph_id order by th.creattime DESC";

        if ($array['index'] > -1) {
            $params[] = $array['index'];
            $sql = $sql . " limit %d,10";
        }

        $result = $wpdb->get_results($wpdb->prepare($sql, $params));
        return $result;
    }

    public static function ajax_searchPhotoType()
    {
        wp_send_json(SearchDao::searchPhotoType());
    }

    public static function searchPhotoType()
    {
        global $wpdb;
        $result = $wpdb->get_results('select id,photo_name as value from ' . Config::DIC_PHOTOTYPE_TABLE);
        return $result;
    }
}