<?php

require_once(PLUGIN_DIR . 'Config.php');

class CustomerTrackingDao
{
    public static function insert($house_id, $request)
    {
        $page = $request['page'];
        $origin = 0;
        switch ($page) {
            case 'newHouse':
            case 'secHandHouse':
            case 'rentHouse':
                $origin = 1;
                break;
            case 'customer':
                $origin = 2;
                break;
        }
        global $wpdb;
        $array = array(
            'house_id' => $house_id,
            'status_type' => $request['customer_tracking_status'],
            'consultant_id' => wp_get_current_user()->ID,
            'time' => current_time('Y-m-d H:i:s'),
            'description' => $request['description'],
            'origin' => $origin
        );
        $result = $wpdb->insert(Config::TOSPUR_CUSTOMER_TRACKING_TABLE, $array);
        if ($result) {
            return $wpdb->insert_id;
        } else {
            return '新增客户跟踪失败';
        }
    }

    public static function search($house_id, $origin)
    {
        global $wpdb;
        $sql = 'SELECT tct.*,ts.status_name,tc.name FROM ' . Config::TOSPUR_CUSTOMER_TRACKING_TABLE . ' tct' .
            ' left join (SELECT * FROM ' . Config::TOSPUR_STATUS_TABLE . ' where status_type = 3) as ts on tct.status_type = ts.status_id' .
            ' left join ' . Config::TOSPUR_CONSULTANT . ' tc on tct.consultant_id = tc.id' .
            ' where tct.house_id = ' . $house_id . ' and tct.origin = ' . $origin;
        $result =  $wpdb->get_results($sql);
        return $result;
    }

    public static function search_status()
    {
        global $wpdb;
        $sql = 'SELECT * FROM ' . Config::TOSPUR_STATUS_TABLE . ' where status_type = 3';
        return $wpdb->get_results($sql);
    }
}

?>