<?php

require_once(PLUGIN_DIR . 'Config.php');

class CustomerTrackingDao
{
    public static function insert($house_id, $request)
    {
        if(isset($request['customer_tracking_status'])){
            $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 = DBManager::insert(Config::TOSPUR_CUSTOMER_TRACKING_TABLE, $array);
            if ($result) {
                return $wpdb->insert_id;
            } else {
                return '新增客户跟踪失败';
            }
        }else{
            return 0;
        }
    }

    public static function search($house_id, $origin)
    {
        $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 = DBManager::get_results($sql);
        return $result;
    }

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

?>