<?php

require_once('const.php');

class dao
{
    //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(tospur_view_house_table, $insert_view_house_array);
        return $result;
    }

    public static function update_view_house($id)
    {
        global $wpdb;
        $result = $wpdb->update(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(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 ' . tospur_view_house_table . ' v ' .
            'left join ' . 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 ' . tospur_view_house_table . ' v ' .
            'left join ' . 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(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 ' . tospur_consultant_table . ' c ' .
            'left join ' . wp_users_table . ' u on c.id = u.ID ' .
            'left join (select consultant_id,sum(score)/count(score) as average_score from ' . 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(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 ' . 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(tospur_verify_table, $insert_tospur_verify_array);
        return $result;
    }

    public static function update_tospur_verify($phone)
    {
        global $wpdb;
        $wpdb->update(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 ' . tospur_consultant_table . ' c ' .
            'left join ' . wp_users_table . ' u on c.id = u.ID ' .
            'left join (select consultant_id,sum(score)/count(score) as average_score from ' . 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 ' . a_house_image_table . ' ahi ' .
            'left join ' . 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 ' . a_house_tag_table . ' aht' .
            ' left join ' . tospur_tag_table . ' tt on aht.tag_id = tt.id' .
            ' where house_id in ' . $string;
        global $wpdb;
        return $wpdb->get_results($sql);
    }
}

?>