<?php

require_once(PLUGIN_DIR . 'Config.php');

class TospurDao
{
    //insert 预约看房
    public static function insert_view_house($house_id, $user_id, $date, $index, $time, $consultant_id)
    {
        $insert_view_house_array = array(
            'house_id' => $house_id,
            'user_id' => $user_id,
            'date' => $date,
            'time_index' => $index,
            'time' => $time,
            'consultant_id' => $consultant_id,
            'handle' => 0
        );
        global $wpdb;
        $result = $wpdb->insert(Config::TOSPUR_VIEW_HOUSE_TABLE, $insert_view_house_array);
        return $result;
    }

    public static function update_view_house($id)
    {
        global $wpdb;
        $result = $wpdb->update(Config::TOSPUR_VIEW_HOUSE_TABLE, array(
            'handle' => 1,
            'handle_date' => current_time('Y-m-d H:i:s')
        ), array(
            'id' => $id
        ));
        return $result;
    }

    public static function update_view_house_consultant($house_id, $consultant_id)
    {
        global $wpdb;
        $result = $wpdb->update(Config::TOSPUR_VIEW_HOUSE_TABLE, array(
            'consultant_id' => $consultant_id
        ), array(
            'id' => $house_id
        ));
        return $result;
    }

    //search 预约看房 user_id
    public static function search_view_house_by_user_id($user_id)
    {
        global $wpdb;
        $sql = 'select v.house_id,v.date,v.time,v.consultant_id,v.handle,h.name from ' . Config::TOSPUR_VIEW_HOUSE_TABLE . ' v ' .
            'left join ' . Config::TOSPUR_HOUSE_TABLE . ' h on v.house_id = h.id ' .
            'where v.user_id = ' . $user_id . ' order by v.date desc';
        return $wpdb->get_results($sql);
    }

    //search 预约看房 consultant_id
    public static function search_view_house_by_consultant_id($consultant_id)
    {
        global $wpdb;
        $sql = 'select v.id,v.house_id,v.date,v.time,v.user_id,v.handle,h.name from ' . Config::TOSPUR_VIEW_HOUSE_TABLE . ' v ' .
            'left join ' . Config::TOSPUR_HOUSE_TABLE . ' h on v.house_id = h.id ' .
            'where v.consultant_id = ' . $consultant_id . ' order by v.date desc';
        return $wpdb->get_results($sql);
    }

    //insert 置业顾问评分
    public static function insert_consultant_score($consultant_id, $user_id, $score, $valid = 1)
    {
        $insert_consultant_score_array = array(
            'consultant_id' => $consultant_id,
            'user_id' => $user_id,
            'score' => $score,
            'valid' => $valid
        );
        global $wpdb;
        $result = $wpdb->insert(Config::TOSPUR_CONSULTANT_SCORE_TABLE, $insert_consultant_score_array);
        return $result;
    }

    //search 置业顾问信息
    public static function search_consultant_info($consultant_id)
    {
        global $wpdb;
        $sql = 'select c.*,u.display_name,s.average_score from ' . Config::TOSPUR_CONSULTANT . ' c ' .
            'left join ' . Config::WP_USERS_TABLE . ' u on c.id = u.ID ' .
            'left join (select consultant_id,sum(score)/count(score) as average_score from ' . Config::TOSPUR_CONSULTANT_SCORE_TABLE . ' group by consultant_id) s ' .
            'on c.id = s.consultant_id where c.id = ' . $consultant_id;
        return $wpdb->get_row($sql);
    }

    public static function update_consultant_score($score_id, $valid)
    {
        global $wpdb;
        $result = $wpdb->update(Config::TOSPUR_CONSULTANT_SCORE_TABLE, array(
            'valid' => $valid
        ), array(
            'id' => $score_id
        ));
        return $result;
    }

    public static function search_tospur_verify($phone, $code)
    {
        global $wpdb;
        $sql = 'select code from ' . Config::TOSPUR_VERIFY_TABLE . ' where phone = ' . $phone . ' and status = 0 ' .
            'and create_time > NOW() - INTERVAL 30 MINUTE ' .
            'order by create_time desc limit 1';
        return $wpdb->get_var($sql);
    }

    public static function insert_tospur_verify($phone, $code)
    {
        $insert_tospur_verify_array = array(
            'phone' => $phone,
            'code' => $code,
            'create_time' => current_time('Y-m-d H:i:s'),
            'status' => 0
        );
        global $wpdb;
        $result = $wpdb->insert(Config::TOSPUR_VERIFY_TABLE, $insert_tospur_verify_array);
        return $result;
    }

    public static function update_tospur_verify($phone)
    {
        global $wpdb;
        $wpdb->update(Config::TOSPUR_VERIFY_TABLE, array(
            'status' => 1
        ), array(
            'phone' => $phone
        ));
    }

    public static function search_consultant_by_city($city_id, $index)
    {
        global $wpdb;
        $consultant_sql = 'select c.*,u.display_name,s.average_score from ' . Config::TOSPUR_CONSULTANT . ' c ' .
            'left join ' . Config::WP_USERS_TABLE . ' u on c.id = u.ID ' .
            'left join (select consultant_id,sum(score)/count(score) as average_score from ' . Config::TOSPUR_CONSULTANT_SCORE_TABLE . ' where valid=1 group by consultant_id) s on c.id = s.consultant_id ' .
            'where cityid = ' . $city_id . ' LIMIT ' . $index . ',20;';
        return $wpdb->get_results($consultant_sql);
    }

    public static function search_house_image($house_ids)
    {
        $string = '(' . implode(',', array_map('intval', $house_ids)) . ')';
        $sql = 'select * from (select ahi.house_id,ti.path,ti.image_type,case ti.image_type ' .
            'when 1 then 1 ' .
            'when 5 then 1 ' .
            'ELSE 2 ' .
            'end as type from ' . Config::A_HOUSE_IMAGE_TABLE . ' ahi ' .
            'left join ' . Config::TOSPUR_IMAGE_TABLE . ' ti on ahi.image_id = ti.id ' .
            'where house_id in ' . $string . ' order by type) as image group by house_id;';
        global $wpdb;
        return $wpdb->get_results($sql);
    }

    public static function search_house_tag($house_ids)
    {
        $string = '(' . implode(',', array_map('intval', $house_ids)) . ')';
        $sql = 'select aht.house_id,tt.name from ' . Config::A_HOUSE_TAG_TABLE . ' aht' .
            ' left join ' . Config::TOSPUR_TAG_TABLE . ' tt on aht.tag_id = tt.id' .
            ' where house_id in ' . $string;
        global $wpdb;
        return $wpdb->get_results($sql);
    }
}

?>