<?php
require_once(PLUGIN_DIR . 'Config.php');
class TCSync {
    public static function user_sync(){
        global $wpdb;
        $response = wp_remote_post( Config::user_url );
        $res = array(
            'code' => 200,
            'msg' => "success"
        );
        //判断请求是否成功
        if( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ){
            $res['code'] = 500;
            $res['msg'] = "get user info error";
        }else{
            set_time_limit(0) ;
            //获取请求内容
            $result = wp_remote_retrieve_body( $response );
            $result = json_decode($result,true);
            foreach($result['data'] as $item){
                if(username_exists($item['WorkNum'])){
                    $user = get_user_by( "login", $item['WorkNum'] );
                    $userdata = array(
                        'ID' => $user->data->ID,
                        'display_name' => $item['Name']
                    );
                    $user_id = wp_update_user( $userdata ) ;
                }else{
                    $userdata = array(
                        'user_login'  =>  $item['WorkNum'],
                        'user_pass'   =>  $item['WorkNumAndID'],  // When creating an user, `user_pass` is expected.
                        'role' => 'editor',
                        'wp_user_level' => 7,
                        'display_name' => $item['Name']
                    );
                    $user_id = wp_insert_user( $userdata ) ;
                }
                if(is_numeric($user_id)){
                    $info = array(
                        'id' => $user_id,
                        'cityId' => $item['CityId'],
                        'imageUrl' => $item['ImageUrl'],
                        'mobile' => $item['Mobile'],
                        'name' => $item['Name']
                    );
                    $wpdb->query(TCSync::create_insert_update_sql(Config::TOSPUR_CONSULTANT,$info,array('id')));
                }
            }
        }
        return $res;
    }

    public static function other_sync(){
        global $wpdb;
        $response = wp_remote_post( Config::other_url );
        $res = array(
            'code' => 200,
            'msg' => "success"
        );
        //判断请求是否成功
        if( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) {
            $res['code'] = 500;
            $res['msg'] = "get info error";
        }else{
            set_time_limit(0) ;
            //获取请求内容
            $result = wp_remote_retrieve_body( $response );
            $result = json_decode($result,true);
            //print_r($result['data']['CityList']);
            foreach($result['data']['CityList'] as $item){
                $wpdb->query(TCSync::create_insert_update_sql(Config::DIC_CITY_TABLE,$item,array('plateId')));
                //print_r(TCSync::create_insert_update_sql(Config::CITY_TABLE,$item,array('plateId')));print_r("<br />");
            }

            foreach($result['data']['RoomStyle'] as $item){
                $wpdb->query(TCSync::create_insert_update_sql(Config::DIC_ROOM_TABLE,$item,array('id')));
                //print_r(TCSync::create_insert_update_sql(Config::DIC_ROOM_TABLE,$item,array('id')));print_r("<br />");
            }

            foreach($result['data']['BulidProperty'] as $item){
                $wpdb->query(TCSync::create_insert_update_sql(Config::DIC_BUILDPROPERTY_TABLE,$item,array('id')));
                //print_r(TCSync::create_insert_update_sql(Config::DIC_BUILDPROPERTY_TABLE,$item,array('id')));print_r("<br />");
            }

            foreach($result['data']['AreaRange'] as $item){
                $wpdb->query(TCSync::create_insert_update_sql(Config::DIC_AREA_TABLE,$item,array('id'),array('maxValue'=>'max','minValue'=>'min')));
                //print_r(TCSync::create_insert_update_sql(Config::DIC_AREA,$item,array('id'),array('maxValue'=>'max','minValue'=>'min')));print_r("<br />");
            }

            foreach($result['data']['TotalPrice'] as $item){
                $wpdb->query(TCSync::create_insert_update_sql(Config::DIC_TOTALPRICE_TABLE,$item,array('id'),array('maxValue'=>'max','minValue'=>'min')));
                //print_r(TCSync::create_insert_update_sql(Config::DIC_AREA,$item,array('id'),array('maxValue'=>'max','minValue'=>'min')));print_r("<br />");
            }

            foreach($result['data']['UnitPriceRange'] as $item){
                $wpdb->query(TCSync::create_insert_update_sql(Config::DIC_UNITPRICERANGE_TABLE,$item,array('id'),array('maxValue'=>'max','minValue'=>'min')));
                //print_r(TCSync::create_insert_update_sql(Config::DIC_AREA,$item,array('id'),array('maxValue'=>'max','minValue'=>'min')));print_r("<br />");
            }

        }
    }

    public static function organization_sync(){
        global $wpdb;
        $response = wp_remote_post( Config::organization_url );
        $res = array(
            'code' => 200,
            'msg' => "success"
        );
        //判断请求是否成功
        if( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) {
            $res['code'] = 500;
            $res['msg'] = "get info error";
        }else{
            set_time_limit(0) ;
            $result = wp_remote_retrieve_body( $response );
            $result = json_decode($result,true);
            foreach($result['data'] as $item){
                $wpdb->query(TCSync::create_insert_update_sql(Config::TOSPUR_ORGANIZATION_TABLE,$item,array('Id')));
                //print_r(TCSync::create_insert_update_sql(Config::DIC_AREA,$item,array('id'),array('maxValue'=>'max','minValue'=>'min')));print_r("<br />");
            }
        }
    }

    private static function create_insert_update_sql($table,$item,$primary=array(),$change=array()){
        $keys = "";
        $values = "";
        $update = "";
        foreach($item as $key => $value){
            if(strlen($keys)>0)
                $keys .= ",";
            $keys .= $change[$key]?$change[$key]:$key;
            if(strlen($values)>0)
                $values .= ",";
            $value = is_numeric($value)?$value:"'".$value."'";
            $values .= $value;
            if(!in_array($key,$primary)){
                if(strlen($update)>0)
                    $update .= ",";
                $update .= ($change[$key]?$change[$key]:$key)."=".$value;
            }
        }
        $sql = "insert into ".$table."(".$keys.") values(".$values.") ON DUPLICATE KEY UPDATE ".$update;
        return $sql;
    }
} 