package com.tofly.ghrq_route.module.user.service;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.url.NetRequestURL;
import com.tofly.ghrq_route.base.url.NetRequestURL.newApi_dyrq;
import com.tofly.ghrq_route.base.URLResponseCode;
import com.tofly.ghrq_route.base.URLResponseCode.ErrorCode;
import com.tofly.ghrq_route.base.URLResponseCode.URLResponse;
import com.tofly.ghrq_route.module.pipeAnalyze.domain.FieldsBean;
import com.tofly.ghrq_route.module.sqlite.DBDaoHelper;
import com.tofly.ghrq_route.module.user.controller.UserCallBack;
import com.tofly.ghrq_route.module.user.domain.CarBean;
import com.tofly.ghrq_route.module.user.domain.CompanyBean;
import com.tofly.ghrq_route.module.user.domain.CompanyUserBean;
import com.tofly.ghrq_route.module.user.domain.DepartListBean;
import com.tofly.ghrq_route.module.user.domain.DepartUserBean;
import com.tofly.ghrq_route.module.user.domain.DepartUserListBean;
import com.tofly.ghrq_route.module.user.domain.DepartmentBean;
import com.tofly.ghrq_route.module.user.domain.DepartmentUserBean;
import com.tofly.ghrq_route.module.user.domain.DeviceBean;
import com.tofly.ghrq_route.module.user.domain.MessageUserBean;
import com.tofly.ghrq_route.module.user.domain.RegisterInfoBean;
import com.tofly.ghrq_route.module.user.domain.UserBean;
import com.tofly.ghrq_route.module.user.domain.UserMapDataBean;
import com.tofly.ghrq_route.module.user.domain.UserMenuBean;
import com.tofly.ghrq_route.module.user.domain.UserOnlineMapDataBean;
import com.tofly.ghrq_route.common.util.net.volley.JsonRequestConstant;
import com.tofly.ghrq_route.common.util.net.volley.MultiPartStringRequest;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Amu on 2017/2/9.
 */

public class UserServiceImpl implements IUserService {
    private Context mContext;
    private DBDaoHelper dbDaoHelper;

    public UserServiceImpl(Context context){
        mContext = context;
        dbDaoHelper = DBDaoHelper.getInstance();
    }

    @Override
    public void login(final String userName, final String token, final boolean forceLogin,final UserCallBack userCallBack) {
        final Handler handler = new Handler(Looper.getMainLooper()){
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == ErrorCode.SUCCESS) {
                    String errMsg = (String) msg.obj;
                    URLResponse urlResponse = new URLResponse(errMsg);
                    String result = urlResponse.getResult();
                    try {
                        Gson gson = new Gson();
                        UserBean user = gson.fromJson(result, UserBean.class);
                        if (user != null){
                            userCallBack.onLogin(true, msg.what, errMsg, user);
                        }else  {
                            errMsg = "用户解析失败";
                            userCallBack.onLogin(false, -1, errMsg, null);
                        }
                    }catch (Exception e) {
                        errMsg = "用户解析失败";
                        userCallBack.onLogin(false, -1, errMsg, null);
                    }
                } else {
                    String errMsg = null;
                    if (msg.obj != null){
                        errMsg = (String)msg.obj;
                        URLResponse urlResponse = new URLResponse(errMsg);
                        if (urlResponse.getMessage() != null){
                            errMsg = urlResponse.getMessage();
                        }
                    }
                    userCallBack.onLogin(false, msg.what, errMsg, null);
                }
            }
        };

        String url = NetRequestURL.newApi_dyrq.getLoginUrl();
        //Http post请求,用户登录
        StringRequest postRequest = new StringRequest(Request.Method.POST,
                url, response -> {
                    URLResponse responseCode = new URLResponse(response);
                    handler.sendMessage(handler.obtainMessage(responseCode.getCode(), response));
                }, error -> {
                    //登录失败
                    if (error.networkResponse != null) {
                        try {
                            String errString = new String(error.networkResponse.data, "UTF-8");
                            handler.sendMessage(handler.obtainMessage(error.networkResponse.statusCode, errString));
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            handler.sendMessage(handler.obtainMessage(error.networkResponse.statusCode, null));
                        }
                    }else {
                        handler.sendMessage(handler.obtainMessage(ErrorCode.HTTPCONN_TIMEOUT,
                                ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT)));
                    }
                }){
            @Override
            protected Map<String, String> getParams(){
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<>();
/*                params.put("loginid", userName);
                params.put("loginpass", token);
                params.put("relogin", (forceLogin ? 1 : 0) + "");
                String imei = BaseApp.getInstance().getDevieImei();
                if (imei != null)
                    params.put("imei", imei);
                String app_ver_name = BaseApp.getAppVersionName();
                if (app_ver_name != null)
                    params.put("software_ver", app_ver_name);*/
                params.put("username", userName);
                params.put("password", token);
                return params;
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest);
    }

    @Override
    public void getAccountInfo(final String username, final String token, final UserCallBack userCallBack) {
        final Handler handler = new Handler(Looper.getMainLooper()){
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == ErrorCode.SUCCESS) {
                    String errMsg = (String) msg.obj;
                    URLResponse urlResponse = new URLResponse(errMsg);
                    String result = urlResponse.getResult();
                    Gson gson = new Gson();
                    UserBean user = gson.fromJson(result, UserBean.class);
                    if (user != null){
                        userCallBack.onGetAccountInfo(true, msg.what, errMsg, user);
                    }else {
                        errMsg = "用户解析失败";
                        userCallBack.onGetAccountInfo(false, -1, errMsg, null);
                    }
                } else {
                    String errMsg = null;
                    if (msg.obj != null){
                        errMsg = (String)msg.obj;
                        URLResponse urlResponse = new URLResponse(errMsg);
                        if (urlResponse.getMessage() != null){
                            errMsg = urlResponse.getMessage();
                        }
                    }
                    userCallBack.onGetAccountInfo(false, msg.what, errMsg, null);
                }
            }
        };

        String url = newApi_dyrq.getAccountInfoUrl();
        StringRequest postRequest = new StringRequest(Request.Method.GET,
                url, response -> {
                    URLResponse responseCode = new URLResponse(response);
                    handler.sendMessage(handler.obtainMessage(responseCode.getCode(), response));
                }, error -> {
                    //登录失败
                    if (error.networkResponse != null) {
                        try {
                            String errString = new String(error.networkResponse.data, "UTF-8");
                            handler.sendMessage(handler.obtainMessage(error.networkResponse.statusCode, errString));
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            handler.sendMessage(handler.obtainMessage(error.networkResponse.statusCode, null));
                        }
                    }else {
                        handler.sendMessage(handler.obtainMessage(ErrorCode.HTTPCONN_TIMEOUT,
                                ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT)));
                    }
                }){
            @Override
            public Map<String, String> getHeaders(){
                //参数拼装:表单提交方式
                return newApi_dyrq.getHeaderParams();
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest);
    }

    @Override
    public void getUserMenus(Context context, UserCallBack userCallBack) {
        String url = newApi_dyrq.getUserMenusUrl();
        StringRequest postRequest = new StringRequest(Request.Method.GET,
                url , response -> {
                    URLResponse responseCode = new URLResponse(response);
                    if (responseCode.getCode() == ErrorCode.SUCCESS) {
                        String result = responseCode.getResult();
                         List<UserMenuBean> menuList = null;
                        int resCode = -1;
                        boolean ret = false;
                        String parseErrMsg = "用户解析失败";
                        if (result != null){
                            try {
                                Gson gson = new Gson();
                                List<UserMenuBean> userMenus = gson.fromJson(result, new TypeToken<List<UserMenuBean>>(){}.getType());
                                if (userMenus != null && userMenus.size() >= 0){
                                    menuList = userMenus;
                                    resCode = responseCode.getCode();
                                    parseErrMsg = responseCode.getMessage();
                                    ret = true;

                                    saveUserMenus(menuList);
                                }
                            }catch (Exception e){
                                userCallBack.onGetUserMenus(false, resCode, parseErrMsg, null);
                            }
                        }
                        userCallBack.onGetUserMenus(ret, resCode, parseErrMsg, menuList);
                    } else {
                        userCallBack.onGetUserMenus(false, responseCode.getCode(), responseCode.getMessage(),null);
                    }
                }, error -> {
                    //登录失败
                    if (error.networkResponse != null) {
                        try {
                            String errString = new String(error.networkResponse.data, "UTF-8");
                            userCallBack.onGetUserMenus(false, error.networkResponse.statusCode, errString, null);
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            userCallBack.onGetUserMenus(false, error.networkResponse.statusCode, null, null);
                        }
                    }else {
                        String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                        userCallBack.onGetUserMenus(false, ErrorCode.HTTPCONN_TIMEOUT, errString, null);
                    }
                }){
            @Override
            public Map<String, String> getHeaders(){
                //参数拼装:表单提交方式
                return newApi_dyrq.getHeaderParams();
            }
        };
        BaseApp.addRequest(context.getClass().getName(), postRequest);
    }

    private void saveUserMenus(List<UserMenuBean> userMenus) {
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null) {
            String userid = userBean.getUserid();
            if (userMenus != null && userMenus.size() >= 0) {
                dbDaoHelper.deleteUserMenuList(userid);
                dbDaoHelper.saveUserMenuList(userMenus, userid);
            }
        }
    }

    @Override
    public void updateAccountInfo(String userid, String username, String mobile, String password, String avatar, final UserCallBack userCallBack) {
        String url = newApi_dyrq.getUpdateUserUrl();
        MultiPartStringRequest postRequest = new MultiPartStringRequest(Request.Method.PUT,
                url, response -> {
                    URLResponse responseCode = new URLResponse(response);
                    if (responseCode.getCode() == ErrorCode.SUCCESS) {
                        String errMsg = responseCode.getMessage();
                        userCallBack.onUpdateAccount(true, responseCode.getCode(), errMsg);
                    } else {
                        userCallBack.onUpdateAccount(false, responseCode.getCode(), responseCode.getMessage());
                    }
                }, error -> {
                    //登录失败
                    if (error.networkResponse != null) {
                        String errString = new String(error.networkResponse.data, StandardCharsets.UTF_8);
                        userCallBack.onUpdateAccount(false, error.networkResponse.statusCode, errString);
                    }
                    else {
                        String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                        userCallBack.onUpdateAccount(false, ErrorCode.HTTPCONN_TIMEOUT, errString);
                    }
                }){
            @Override
            public Map<String, String> getHeaders(){
                //参数拼装:表单提交方式
                return newApi_dyrq.getHeaderParams();
            }

            @Override
            public Map<String, String> getStringUploads(){
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<>();
                params.put("id", userid);
                if (username != null && !username.isEmpty())
                    params.put("realName", username);
                if (mobile != null && !mobile.isEmpty())
                    params.put("phone", mobile);
                if (password != null && !password.isEmpty())
                    params.put("password", password);
                return params;
            }

/*            @Override
            public byte[] getBody() {
                if (avatar != null && !avatar.isEmpty()) {
                    Bitmap bitmap = ImageUtil.loadImageFromFilePath(avatar);
                    byte[] bytes = ImageUtil.bitmapToBytes(bitmap);
                    return bytes;
                }
                return null;
            }*/

            @Override
            public Map<String, File> getFileUploads() {
                Map<String, File> params = new HashMap<>();
                if (avatar != null && !avatar.isEmpty())
                    params.put("file", new File(avatar));
                return params;
            }
        };
        BaseApp.addMultiStatckRequest(mContext.getClass().getName(), postRequest);
    }

    @Override
    public void getUserInfo(final String userid, final String token, final UserCallBack userCallBack) {
        String url = NetRequestURL.getUserInfoUrl() + userid;
        StringRequest postRequest = new StringRequest(Request.Method.POST,
               url , response -> {
                   URLResponse responseCode = new URLResponse(response);
                   if (responseCode.getCode() == ErrorCode.SUCCESS) {
                       String errMsg = responseCode.getMessage();
                       String result = responseCode.getResult();
                       Gson gson = new Gson();
                       UserBean user = gson.fromJson(result, UserBean.class);
                       String strDevice = responseCode.queryResult("device");
                       DeviceBean device = null;
                       if (strDevice != null && !strDevice.isEmpty()){
                            device = gson.fromJson(strDevice, DeviceBean.class);
                       }
                       if (user != null){
                           userCallBack.onGetUserInfo(true, responseCode.getCode(), errMsg, user, device);
                       }else {
                           errMsg = "用户解析失败";
                           userCallBack.onGetUserInfo(false, -1, (String) errMsg, null, null);
                       }
                   } else {
                       userCallBack.onGetUserInfo(false, responseCode.getCode(), responseCode.getMessage(), null, null);
                   }
               }, error -> {
                   //登录失败
                   if (error.networkResponse != null) {
                       try {
                           String errString = new String(error.networkResponse.data, "UTF-8");
                           userCallBack.onGetUserInfo(false, error.networkResponse.statusCode, errString, null, null);
                       } catch (UnsupportedEncodingException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                           userCallBack.onGetUserInfo(false, error.networkResponse.statusCode, null, null, null);
                       }
                   }else {
                       String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                       userCallBack.onGetUserInfo(false, ErrorCode.HTTPCONN_TIMEOUT, errString, null, null);
                   }
               }){
            @Override
            protected Map<String, String> getParams(){
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<String, String>();
                return params;
            }

            @Override
            public Map<String, String> getHeaders(){
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<String, String>();
                params.put("token", token);
                return params;
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest);
    }

    @Override
    public void logout(final String token, final UserCallBack userCallBack) {
        String url = NetRequestURL.getLogoutUrl();
        StringRequest postRequest = new StringRequest(Request.Method.POST,
                url , response -> {
                    URLResponse responseCode = new URLResponse(response);
                    if (responseCode.getCode() == ErrorCode.SUCCESS) {
                        String errMsg = responseCode.getMessage();
                        userCallBack.onLogout(true, responseCode.getCode(), errMsg);
                    } else {
                        userCallBack.onLogout(false, responseCode.getCode(), responseCode.getMessage());
                    }
                }, error -> {
                    //登录失败
                    if (error.networkResponse != null) {
                        try {
                            String errString = new String(error.networkResponse.data, "UTF-8");
                            userCallBack.onLogout(false, error.networkResponse.statusCode, errString);
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            userCallBack.onLogout(false, error.networkResponse.statusCode, null);
                        }
                    }else {
                        String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                        userCallBack.onLogout(false, ErrorCode.HTTPCONN_TIMEOUT, errString);
                    }
                }){
            @Override
            public Map<String, String> getHeaders(){
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<String, String>();
                params.put("token", token);
                return params;
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest);
    }

    @Override
    public void getUserMList(final String token, final UserCallBack userCallBack) {
        String url = NetRequestURL.getUserMListUrl();
        StringRequest postRequest = new StringRequest(Request.Method.POST,
                url, response -> {
                    URLResponse responseCode = new URLResponse(response);
                    if (responseCode.getCode() == ErrorCode.SUCCESS){
                        String rowsArray = responseCode.queryResult("rows");
                        List<MessageUserBean> useList = null;
                        int resCode = -1;
                        boolean result = false;
                        String parseErrMsg = "用户解析失败";
                        if (rowsArray != null){
                            try {
                                Gson gson = new Gson();
                                List<MessageUserBean> users = gson.fromJson(rowsArray, new TypeToken<List<MessageUserBean>>(){}.getType());
                                if (users != null && users.size() >= 0){
                                    useList = users;
                                    resCode = responseCode.getCode();
                                    parseErrMsg = responseCode.getMessage();
                                    result = true;
                                }
                            }catch (Exception e){
                                userCallBack.onGetUserMList(false, resCode, parseErrMsg, null);
                            }
                        }
                        userCallBack.onGetUserMList(result, resCode, parseErrMsg, useList);
                    }else {
                        userCallBack.onGetUserMList(false, responseCode.getCode(), responseCode.getMessage(), null);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //登录失败
                if (error.networkResponse != null) {
                    try {
                        String errString = new String(error.networkResponse.data, "UTF-8");
                        userCallBack.onGetUserMList(false, error.networkResponse.statusCode, errString, null);
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        userCallBack.onGetUserMList(false, error.networkResponse.statusCode, null, null);
                    }
                }else {
                    String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                    userCallBack.onGetUserMList(false, ErrorCode.HTTPCONN_TIMEOUT, errString, null);
                }
            }
        }){
            @Override
            protected Map<String, String> getParams(){
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<String, String>();
                return params;
            }

            @Override
            public Map<String, String> getHeaders(){
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<String, String>();
                params.put("token", token);
                return params;
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest);
    }

    @Override
    public void getUserList(final String token, final UserCallBack userCallBack) {
        String url = NetRequestURL.getSysMsgUserList();
        StringRequest postRequest = new StringRequest(Request.Method.GET,
                url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                URLResponse responseCode = new URLResponse(response);
                if (responseCode.getCode() == ErrorCode.SUCCESS){
                    String rowsArray = responseCode.getResult();
                    List<DepartUserBean> useList = null;
                    int resCode = -1;
                    boolean result = false;
                    String parseErrMsg = "用户解析失败";
                    if (rowsArray != null){
                        try {
                            Gson gson = new Gson();
                            List<DepartUserBean> users = gson.fromJson(rowsArray, new TypeToken<List<DepartUserBean>>(){}.getType());
                            if (users != null && users.size() >= 0){
                                useList = users;
                                resCode = responseCode.getCode();
                                parseErrMsg = responseCode.getMessage();
                                result = true;
                                saveUserList(useList);
                            }
                        }catch (Exception e){
                            userCallBack.onGetUserList(false, resCode, parseErrMsg, null);
                        }
                    }
                    userCallBack.onGetUserList(result, resCode, parseErrMsg, useList);
                }else {
                    userCallBack.onGetUserList(false, responseCode.getCode(), responseCode.getMessage(), null);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //登录失败
                if (error.networkResponse != null) {
                    try {
                        String errString = new String(error.networkResponse.data, "UTF-8");
                        userCallBack.onGetUserList(false, error.networkResponse.statusCode, errString, null);
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        userCallBack.onGetUserList(false, error.networkResponse.statusCode, null, null);
                    }
                }else {
                    String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                    userCallBack.onGetUserList(false, ErrorCode.HTTPCONN_TIMEOUT, errString, null);
                }
            }
        }){
            @Override
            protected Map<String, String> getParams(){
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<String, String>();
                return params;
            }

            @Override
            public Map<String, String> getHeaders(){
                //参数拼装:表单提交方式
                Map<String, String> params =NetRequestURL.newApi_dyrq.getHeaderParams();
                return params;
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest);
    }

    private void saveUserList(List<DepartUserBean> users) {
        dbDaoHelper.deleteDepartAndUser();
        for (DepartUserBean data : users) {
            DepartListBean departListBean = new DepartListBean();
            if (data.getId() != 0) {
                departListBean.setId("" + data.getId());
            }
            if (!TextUtils.isEmpty(data.getName())) {
                departListBean.setName(data.getName());
            }
            dbDaoHelper.insertOrUpdateDepartList(departListBean);
            if (data.getUser() != null && data.getUser().size() > 0) {
                for (MessageUserBean messageUserBean : data.getUser()) {
                    DepartUserListBean departUserListBean = new DepartUserListBean();
                    if (data.getId() != 0) {
                        departUserListBean.setDepartmentId("" + data.getId());
                    }
                    if (messageUserBean.getId() != 0) {
                        departUserListBean.setId("" + messageUserBean.getId());
                    }
                    if (!TextUtils.isEmpty(messageUserBean.getRealName())) {
                        departUserListBean.setRealName(messageUserBean.getRealName());
                    }
                    dbDaoHelper.insertOrUpdateDepartUserList(departUserListBean);
                }
            }
        }
    }

    @Override
    public void registerAccount(final DeviceBean deviceBean, final boolean isStandBy, final CompanyBean companyBean, final CompanyUserBean userBean, final UserCallBack userCallBack) {
        String url = NetRequestURL.getRegisterAccountUrl();
        final StringRequest postRequest = new StringRequest(Request.Method.POST,
                url , new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                URLResponse responseCode = new URLResponse(response);
                if (responseCode.getCode() == ErrorCode.SUCCESS) {
                    String errMsg = responseCode.getMessage();
                    userCallBack.onRegisterAccount(true, responseCode.getCode(), errMsg);
                } else {
                    userCallBack.onRegisterAccount(false, responseCode.getCode(), responseCode.getMessage());
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //登录失败
                if (error.networkResponse != null) {
                    try {
                        String errString = new String(error.networkResponse.data, "UTF-8");
                        userCallBack.onRegisterAccount(false, error.networkResponse.statusCode, errString);
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        userCallBack.onRegisterAccount(false, error.networkResponse.statusCode, null);
                    }
                }else {
                    String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                    userCallBack.onRegisterAccount(false, ErrorCode.HTTPCONN_TIMEOUT, errString);
                }
            }
        }){
            @Override
            public Map<String, String> getParams(){
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<String, String>();
                params.put("imei", deviceBean.getImei());
                params.put("padname", deviceBean.getPadname());
                params.put("brand", deviceBean.getBrand());
                params.put("model", deviceBean.getModel());
                params.put("android_version", deviceBean.getAndroidVersion());
                params.put("type", (isStandBy ? 1 : 0) + "");
                params.put("companyid", companyBean.getCompanyid());
                params.put("company_name", companyBean.getCompany_name());
                params.put("userid", userBean.getUserid());
                return params;
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest);
    }

    @Override
    public void getRegisterDeviceInfo(final String imei, final UserCallBack userCallBack) {
        String url = NetRequestURL.getRegisterDeviceInfoUrl();
        StringRequest postRequest = new StringRequest(Request.Method.POST,
                url, response -> {
                    URLResponse responseCode = new URLResponse(response);
                    if (responseCode.getCode() == ErrorCode.SUCCESS){
                        String rowsArray = responseCode.getResult();
                        RegisterInfoBean registerInfo = null;
                        int resCode = -1;
                        boolean result = false;
                        String parseErrMsg = "用户解析失败";
                        if (rowsArray != null){
                            try {
                                Gson gson = new Gson();
                                RegisterInfoBean info = gson.fromJson(rowsArray, RegisterInfoBean.class);
                                if (info != null){
                                    registerInfo = info;
                                    resCode = responseCode.getCode();
                                    parseErrMsg = responseCode.getMessage();
                                    result = true;
                                }
                            }catch (Exception e){
                                userCallBack.onGetRegisterDeviceInfo(false, resCode, parseErrMsg, null);
                            }
                        }
                        userCallBack.onGetRegisterDeviceInfo(result, resCode, parseErrMsg, registerInfo);
                    }else {
                        userCallBack.onGetRegisterDeviceInfo(false, responseCode.getCode(), responseCode.getMessage(), null);
                    }
                }, error -> {
            //登录失败
            if (error.networkResponse != null) {
                try {
                    String errString = new String(error.networkResponse.data, "UTF-8");
                    userCallBack.onGetRegisterDeviceInfo(false, error.networkResponse.statusCode, errString, null);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    userCallBack.onGetRegisterDeviceInfo(false, error.networkResponse.statusCode, null, null);
                }
            }else {
                String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                userCallBack.onGetRegisterDeviceInfo(false, ErrorCode.HTTPCONN_TIMEOUT, errString, null);
            }
        }){
            @Override
            public Map<String, String> getParams(){
                Map<String, String> params = new HashMap<>();
                params.put("imei", imei);
                return params;
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest);
    }

    @Override
    public void getCompanyList(final UserCallBack userCallBack) {
        String url = NetRequestURL.getCompanyListUrl();
        StringRequest postRequest = new StringRequest(Request.Method.POST,
                url, response -> {
                    URLResponse responseCode = new URLResponse(response);
                    if (responseCode.getCode() == ErrorCode.SUCCESS){
                        String rowsArray = responseCode.getResult();
                        List<CompanyBean> companyList = null;
                        int resCode = -1;
                        boolean result = false;
                        String parseErrMsg = "用户解析失败";
                        if (rowsArray != null){
                            try {
                                Gson gson = new Gson();
                                List<CompanyBean> companys = gson.fromJson(rowsArray, new TypeToken<List<CompanyBean>>(){}.getType());
                                if (companys != null && companys.size() > 0){
                                    companyList = companys;
                                    resCode = responseCode.getCode();
                                    parseErrMsg = responseCode.getMessage();
                                    result = true;
                                }
                            }catch (Exception e){
                                userCallBack.onGetCompanyList(false, resCode, parseErrMsg, null);
                            }
                        }
                        userCallBack.onGetCompanyList(result, resCode, parseErrMsg, companyList);
                    }else {
                        userCallBack.onGetCompanyList(false, responseCode.getCode(), responseCode.getMessage(), null);
                    }
                }, error -> {
                    //登录失败
                    if (error.networkResponse != null) {
                        try {
                            String errString = new String(error.networkResponse.data, "UTF-8");
                            userCallBack.onGetCompanyList(false, error.networkResponse.statusCode, errString, null);
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            userCallBack.onGetCompanyList(false, error.networkResponse.statusCode, null, null);
                        }
                    }else {
                        String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                        userCallBack.onGetCompanyList(false, ErrorCode.HTTPCONN_TIMEOUT, errString, null);
                    }
                });
        BaseApp.addRequest(mContext.getClass().getName(), postRequest);
    }

    @Override
    public void getCompanyUserList(final String companyid, final UserCallBack userCallBack) {
        String url = NetRequestURL.getCompanyUserListUrl();
        StringRequest postRequest = new StringRequest(Request.Method.POST,
                url, response -> {
                    URLResponse responseCode = new URLResponse(response);
                    if (responseCode.getCode() == ErrorCode.SUCCESS){
                        String rowsArray = responseCode.getResult();
                        List<CompanyUserBean> userList = null;
                        int resCode = -1;
                        boolean result = false;
                        String parseErrMsg = "用户解析失败";
                        if (rowsArray != null){
                            try {
                                Gson gson = new Gson();
                                List<CompanyUserBean> users = gson.fromJson(rowsArray, new TypeToken<List<CompanyUserBean>>(){}.getType());
                                if (users != null && users.size() >= 0){
                                    userList = users;
                                    resCode = responseCode.getCode();
                                    parseErrMsg = responseCode.getMessage();
                                    result = true;
                                }
                            }catch (Exception e){
                                userCallBack.onGetCompanyUserList(false, resCode, parseErrMsg, null);
                            }
                        }
                        userCallBack.onGetCompanyUserList(result, resCode, parseErrMsg, userList);
                    }else {
                        userCallBack.onGetCompanyUserList(false, responseCode.getCode(), responseCode.getMessage(), null);
                    }
                }, error -> {
                    //登录失败
                    if (error.networkResponse != null) {
                        try {
                            String errString = new String(error.networkResponse.data, "UTF-8");
                            userCallBack.onGetCompanyUserList(false, error.networkResponse.statusCode, errString, null);
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            userCallBack.onGetCompanyUserList(false, error.networkResponse.statusCode, null, null);
                        }
                    }else {
                        String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                        userCallBack.onGetCompanyUserList(false, ErrorCode.HTTPCONN_TIMEOUT, errString, null);
                    }
                }){
            @Override
            public Map<String, String> getParams(){
                Map<String, String> params = new HashMap<>();
                params.put("companyid", companyid);
                return params;
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest);
    }

    @Override
    public void getDepartmentList(Context context, int pageindex, int pagesize, boolean filtered, UserCallBack userCallBack) {
        String url = newApi_dyrq.getDepartmentListUrl(filtered) + "current=" + pageindex + "&size=" + pagesize;
        StringRequest postRequest = new StringRequest(Request.Method.GET,
                url, response -> {
            URLResponse responseCode = new URLResponse(response);
            if (responseCode.getCode() == ErrorCode.SUCCESS){
                String rowsArray = responseCode.getResult();
                List<DepartmentBean> departmentList = null;
                int resCode = -1;
                boolean result = false;
                String parseErrMsg = "用户解析失败";
                if (rowsArray != null){
                    try {
                        Gson gson = new Gson();
                        List<DepartmentBean> departments = gson.fromJson(rowsArray, new TypeToken<List<DepartmentBean>>(){}.getType());
                        if (departments != null && departments.size() > 0){
                            departmentList = departments;
                            resCode = responseCode.getCode();
                            parseErrMsg = responseCode.getMessage();
                            result = true;
                        }
                    }catch (Exception e){
                        userCallBack.onGetDepartmentList(false, resCode, parseErrMsg, null);
                    }
                }
                userCallBack.onGetDepartmentList(result, resCode, parseErrMsg, departmentList);
            }else {
                userCallBack.onGetDepartmentList(false, responseCode.getCode(), responseCode.getMessage(), null);
            }
        }, error -> {
            //登录失败
            if (error.networkResponse != null) {
                String errString = new String(error.networkResponse.data, StandardCharsets.UTF_8);
                userCallBack.onGetDepartmentList(false, error.networkResponse.statusCode, errString, null);
            }else {
                String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                userCallBack.onGetDepartmentList(false, ErrorCode.HTTPCONN_TIMEOUT, errString, null);
            }
        }){
            @Override
            public Map<String, String> getHeaders() {
                return newApi_dyrq.getHeaderParams();
            }
        };
        BaseApp.addRequest(context.getClass().getName(), postRequest);
    }

    @Override
    public void getDepartmentUserList(Context context, String departId, int pageindex, int pagesize, UserCallBack userCallBack) {
        String url = newApi_dyrq.getDepartmentUserListUrl() + "?current=" + pageindex + "&size=" + pagesize;
        if (departId != null && !departId.isEmpty()) {
           url += "&departmentId=" + departId;
        }
        StringRequest postRequest = new StringRequest(Request.Method.GET,
                url, response -> {
            URLResponse responseCode = new URLResponse(response);
            if (responseCode.getCode() == ErrorCode.SUCCESS){
                String rowsArray = responseCode.queryResult("records");
                List<DepartmentUserBean> userList = null;
                int resCode = -1;
                boolean result = false;
                String parseErrMsg = "用户解析失败";
                if (rowsArray != null){
                    try {
                        Gson gson = new Gson();
                        List<DepartmentUserBean> users = gson.fromJson(rowsArray, new TypeToken<List<DepartmentUserBean>>(){}.getType());
                        if (users != null && users.size() >= 0){
                            userList = users;
                            resCode = responseCode.getCode();
                            parseErrMsg = responseCode.getMessage();
                            result = true;
                        }
                    }catch (Exception e){
                        userCallBack.onGetDepartmentUserList(false, resCode, parseErrMsg, null);
                    }
                }
                userCallBack.onGetDepartmentUserList(result, resCode, parseErrMsg, userList);
            }else {
                userCallBack.onGetDepartmentUserList(false, responseCode.getCode(), responseCode.getMessage(), null);
            }
        }, error -> {
            //登录失败
            if (error.networkResponse != null) {
                String errString = new String(error.networkResponse.data, StandardCharsets.UTF_8);
                userCallBack.onGetDepartmentUserList(false, error.networkResponse.statusCode, errString, null);
            }else {
                String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                userCallBack.onGetDepartmentUserList(false, ErrorCode.HTTPCONN_TIMEOUT, errString, null);
            }
        }){
            @Override
            public Map<String, String> getHeaders() {
                return newApi_dyrq.getHeaderParams();
            }
        };
        BaseApp.addRequest(context.getClass().getName(), postRequest);
    }

    @Override
    public void getCarList(final String token, final UserCallBack userCallBack) {
        String url = NetRequestURL.getCarListUrl();
        StringRequest postRequest = new StringRequest(Request.Method.POST,
                url, response -> {
                    URLResponse responseCode = new URLResponse(response);
                    if (responseCode.getCode() == ErrorCode.SUCCESS){
                        String rowsArray = responseCode.getResult();
                        List<CarBean> carList = null;
                        int resCode = -1;
                        boolean result = false;
                        String parseErrMsg = "用户解析失败";
                        if (rowsArray != null){
                            try {
                                Gson gson = new Gson();
                                List<CarBean> cars = gson.fromJson(rowsArray, new TypeToken<List<CarBean>>(){}.getType());
                                if (cars != null && cars.size() >= 0){
                                    carList = cars;
                                    resCode = responseCode.getCode();
                                    parseErrMsg = responseCode.getMessage();
                                    result = true;
                                }
                            }catch (Exception e){
                                userCallBack.onGetCarList(false, resCode, parseErrMsg, null);
                            }
                        }
                        userCallBack.onGetCarList(result, resCode, parseErrMsg, carList);
                    }else {
                        userCallBack.onGetCarList(false, responseCode.getCode(), responseCode.getMessage(), null);
                    }
                }, error -> {
                    //登录失败
                    if (error.networkResponse != null) {
                        try {
                            String errString = new String(error.networkResponse.data, "UTF-8");
                            userCallBack.onGetCarList(false, error.networkResponse.statusCode, errString, null);
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            userCallBack.onGetCarList(false, error.networkResponse.statusCode, null, null);
                        }
                    }else {
                        String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                        userCallBack.onGetCarList(false, ErrorCode.HTTPCONN_TIMEOUT, errString, null);
                    }
                }){
            @Override
            public Map<String, String> getHeaders(){
                Map<String, String> params = new HashMap<String, String>();
                params.put("token", token);
                return params;
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest);
    }

    @Override
    public void userBindCar(final String token, final String carid, final UserCallBack userCallBack) {
        String url = NetRequestURL.getUserBindCarUrl();
        StringRequest postRequest = new StringRequest(Request.Method.POST,
                url, response -> {
                    URLResponse responseCode = new URLResponse(response);
                    if (responseCode.getCode() == ErrorCode.SUCCESS){
                        userCallBack.onUserBindCar(true, responseCode.getCode(), responseCode.getMessage());
                    }else {
                        userCallBack.onUserBindCar(false, responseCode.getCode(), responseCode.getMessage());
                    }
                }, error -> {
                    //登录失败
                    if (error.networkResponse != null) {
                        try {
                            String errString = new String(error.networkResponse.data, "UTF-8");
                            userCallBack.onUserBindCar(false, error.networkResponse.statusCode, errString);
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            userCallBack.onUserBindCar(false, error.networkResponse.statusCode, null);
                        }
                    }else {
                        String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                        userCallBack.onUserBindCar(false, ErrorCode.HTTPCONN_TIMEOUT, errString);
                    }
                }){

            @Override
            public Map<String, String> getParams(){
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<String, String>();
                params.put("carid", carid);
                return params;
            }

            @Override
            public Map<String, String> getHeaders(){
                Map<String, String> params = new HashMap<>();
                params.put("token", token);
                return params;
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest);
    }

    @Override
    public void userUnBindCar(final String token, final String carid, final UserCallBack userCallBack) {
        String url = NetRequestURL.getUserUnbindCarUrl();
        StringRequest postRequest = new StringRequest(Request.Method.POST,
                url, response -> {
                    URLResponse responseCode = new URLResponse(response);
                    if (responseCode.getCode() == ErrorCode.SUCCESS){
                        userCallBack.onUserUnBindCar(true, responseCode.getCode(), responseCode.getMessage());
                    }else {
                        userCallBack.onUserUnBindCar(false, responseCode.getCode(), responseCode.getMessage());
                    }
                }, error -> {
                    //登录失败
                    if (error.networkResponse != null) {
                        try {
                            String errString = new String(error.networkResponse.data, "UTF-8");
                            userCallBack.onUserUnBindCar(false, error.networkResponse.statusCode, errString);
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            userCallBack.onUserUnBindCar(false, error.networkResponse.statusCode, null);
                        }
                    }else {
                        String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                        userCallBack.onUserUnBindCar(false, ErrorCode.HTTPCONN_TIMEOUT, errString);
                    }
                }){
            @Override
            public Map<String, String> getParams(){
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<String, String>();
                params.put("carid", carid);
                return params;
            }

            @Override
            public Map<String, String> getHeaders(){
                Map<String, String> params = new HashMap<String, String>();
                params.put("token", token);
                return params;
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest);
    }

    @Override
    public void getMapConfigData(String userid, final UserCallBack userCallBack) {
        String url = newApi_dyrq.getOnlineMapDataUrl();
        StringRequest postRequest = new StringRequest(Request.Method.GET,
                url, response -> {
                    URLResponse responseCode = new URLResponse(response);
                    if (responseCode.getCode() == URLResponseCode.ErrorCode.SUCCESS){
                        String rowsArray = responseCode.getResult();
                        List<UserOnlineMapDataBean> mapUrlConfigData = null;
                        List<UserMapDataBean> userMapDataList = null;
                        int resCode = -1;
                        boolean result = false;
                        String parseErrMsg = "用户解析失败";
                        if (rowsArray != null){
                            try {
                                Gson gson = new Gson();
                                List<UserOnlineMapDataBean> urlConfigData = gson.fromJson(rowsArray, new TypeToken<List<UserOnlineMapDataBean>>(){}.getType());
                                if (urlConfigData != null){
                                    mapUrlConfigData = urlConfigData;
                                    resCode = responseCode.getCode();
                                    parseErrMsg = responseCode.getMessage();
                                    result = true;

                                    String strUserData = responseCode.queryResult("data");
                                    /*if (strUserData != null && !strUserData.isEmpty()){
                                        List<UserMapDataBean> userMapDatas = gson.fromJson(strUserData, new TypeToken<List<UserMapDataBean>>(){}.getType());
                                        if (userMapDatas != null){
                                            userMapDataList = userMapDatas;
                                        }
                                    }*/

                                    saveMapConfigData(mapUrlConfigData, userMapDataList);
                                }
                            }catch (Exception e){
                                userCallBack.onGetMapConfigData(false, resCode, parseErrMsg, null, null);
                            }
                        }
                        userCallBack.onGetMapConfigData(result, resCode, parseErrMsg, mapUrlConfigData, userMapDataList);
                    }else {
                        userCallBack.onGetMapConfigData(false, responseCode.getCode(), responseCode.getMessage(), null, null);
                    }
                }, error -> {
                    //登录失败
                    if (error.networkResponse != null) {
                        try {
                            String errString = new String(error.networkResponse.data, "UTF-8");
                            userCallBack.onGetMapConfigData(false, error.networkResponse.statusCode, errString, null, null);
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            userCallBack.onGetMapConfigData(false, error.networkResponse.statusCode, null, null, null);
                        }
                    }else {
                        String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                        userCallBack.onGetMapConfigData(false, ErrorCode.HTTPCONN_TIMEOUT, errString, null, null);
                    }
                }){
            @Override
            public Map<String, String> getHeaders(){
                return newApi_dyrq.getHeaderParams();
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest);
    }

    private void saveMapConfigData(List<UserOnlineMapDataBean> userOnlineMapDataList, List<UserMapDataBean> userMapDataBeanList) {
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null) {
            String userid = userBean.getUserid();
            if (userOnlineMapDataList != null) {
                dbDaoHelper.deleteUserOnlineMapDataList(userid);
                dbDaoHelper.saveUserOnlineMapDataList(userOnlineMapDataList, userid);
            }

            if (userMapDataBeanList != null && userMapDataBeanList.size() >= 0) {
                dbDaoHelper.deleteUserMapDataList(userid);
                dbDaoHelper.saveUserMapDataList(userMapDataBeanList, userid);
            }
        }
    }

    @Override
    public void getMapLayerIndexConfigData(final String token, final UserCallBack userCallBack) {
        String url = newApi_dyrq.getFieldConfigUrl();
        StringRequest postRequest = new StringRequest(Request.Method.GET,
                url, response -> {
                    URLResponse responseCode = new URLResponse(response);
                    if (responseCode.getCode() == ErrorCode.SUCCESS){
                        String rowsArray = responseCode.getResult();
                        List<FieldsBean> mapConfigList = null;
                        int resCode = -1;
                        boolean result = false;
                        String parseErrMsg = "用户解析失败";
                        if (rowsArray != null){
                            try {
                                mapConfigList = new ArrayList<>();
                                Gson gson = new Gson();
                                JsonObject datas = new JsonParser().parse(rowsArray).getAsJsonObject();
                                for (Map.Entry<String, JsonElement> entry : datas.entrySet()) {
                                    JsonArray value = entry.getValue().getAsJsonArray();
                                    List<FieldsBean> configs = gson.fromJson(value, new TypeToken<List<FieldsBean>>(){}.getType());
                                    if (configs != null) {
                                        mapConfigList.addAll(configs);
                                    }
                                }
                                resCode = responseCode.getCode();
                                parseErrMsg = responseCode.getMessage();
                                result = true;

                                saveMapLayerIndexConfigData(mapConfigList);
                            }catch (Exception e){
                                userCallBack.onGetMapLayerIndexConfigData(false, resCode, parseErrMsg, null);
                            }
                        }
                        userCallBack.onGetMapLayerIndexConfigData(result, resCode, parseErrMsg, mapConfigList);
                    }else {
                        userCallBack.onGetMapLayerIndexConfigData(false, responseCode.getCode(), responseCode.getMessage(), null);
                    }
                }, error -> {
                    //登录失败
                    if (error.networkResponse != null) {
                        try {
                            String errString = new String(error.networkResponse.data, "UTF-8");
                            userCallBack.onGetMapLayerIndexConfigData(false, error.networkResponse.statusCode, errString, null);
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            userCallBack.onGetMapLayerIndexConfigData(false, error.networkResponse.statusCode, null, null);
                        }
                    }else {
                        String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                        userCallBack.onGetMapLayerIndexConfigData(false, ErrorCode.HTTPCONN_TIMEOUT, errString, null);
                    }
                }){
            @Override
            public Map<String, String> getHeaders(){
                return newApi_dyrq.getHeaderParams();
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest);
    }

    private void saveMapLayerIndexConfigData(List<FieldsBean> fieldsBeans) {
        if (fieldsBeans != null && fieldsBeans.size() >= 0) {
            dbDaoHelper.saveMapFieldConfigList(fieldsBeans);
        }
    }

    @Override
    public void getMapLayersData(String token, UserCallBack userCallBack) {

    }

    @Override
    public void checkPdaBinded(Context context, UserCallBack userCallBack) {
        String url = newApi_dyrq.getCheckPdaBindUrl() ;
        StringRequest postRequest = new StringRequest(Request.Method.GET,
                url , response -> {
            URLResponse responseCode = new URLResponse(response);
            if (responseCode.getCode() == ErrorCode.SUCCESS) {
                int status = 0;
                String strTotal = responseCode.getResult();
                if (strTotal != null && !strTotal.isEmpty()) {
                    status = Integer.parseInt(strTotal);
                }
                userCallBack.onCheckPdaBind(true, responseCode.getCode(), responseCode.getMessage(), status);
            } else {
                userCallBack.onCheckPdaBind(false, responseCode.getCode(), responseCode.getMessage(),0);
            }
        }, error -> {
            //登录失败
            if (error.networkResponse != null) {
                try {
                    String errString = new String(error.networkResponse.data, "UTF-8");
                    userCallBack.onCheckPdaBind(false, error.networkResponse.statusCode, errString, 0);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    userCallBack.onCheckPdaBind(false, error.networkResponse.statusCode, null, 0);
                }
            }else {
                String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                userCallBack.onCheckPdaBind(false, ErrorCode.HTTPCONN_TIMEOUT, errString, 0);
            }
        }){
            @Override
            public Map<String, String> getHeaders(){
                //参数拼装:表单提交方式
                return newApi_dyrq.getHeaderParams();
            }
        };
        BaseApp.addRequest(mContext.getClass().getName(), postRequest);
    }

    @Override
    public void bindPdaDevice(Context context, final DeviceBean deviceBean, final UserCallBack userCallBack) {
        String url = newApi_dyrq.getPdaDeviceBindUrl();
        final StringRequest postRequest = new StringRequest(Request.Method.POST,
                url , response -> {
                    URLResponse responseCode = new URLResponse(response);
                    if (responseCode.getCode() == ErrorCode.SUCCESS) {
                        userCallBack.onPdaDeviceBinded(true, responseCode.getCode(), responseCode.getMessage());
                    } else {
                        userCallBack.onPdaDeviceBinded(false, responseCode.getCode(), responseCode.getMessage());
                    }
                }, error -> {
                    //登录失败
                    if (error.networkResponse != null) {
                        try {
                            String errString = new String(error.networkResponse.data, "UTF-8");
                            userCallBack.onPdaDeviceBinded(false, error.networkResponse.statusCode, errString);
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            userCallBack.onPdaDeviceBinded(false, error.networkResponse.statusCode, null);
                        }
                    }else {
                        String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                        userCallBack.onPdaDeviceBinded(false, ErrorCode.HTTPCONN_TIMEOUT, errString);
                    }
                }){
            /*@Override
            public Map<String, String> getParams(){
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<>();
*//*                params.put("imei", deviceBean.getImei());
                params.put("padname", deviceBean.getPadname());
                params.put("brand", deviceBean.getBrand());
                params.put("model", deviceBean.getModel());
                params.put("androidVersion", deviceBean.getAndroid_version());*//*
                Gson gson = new Gson();
                String pdaDevice = gson.toJson(deviceBean);
                params.put("pdaDevice", pdaDevice);
                return params;
            }*/

            @Override
            public byte[] getBody() {
                Gson gson = new Gson();
                String pdaDevice = gson.toJson(deviceBean);
                return pdaDevice.getBytes();
            }

            @Override
            public String getBodyContentType() {
                return JsonRequestConstant.PROTOCOL_CONTENT_TYPE;
            }

            @Override
            public Map<String, String> getHeaders() {
                return newApi_dyrq.getHeaderParams();
            }
        };

        BaseApp.addRequest(context.getClass().getName(), postRequest);
    }

    @Override
    public void getUserGisToken(Context context, UserCallBack userCallBack) {
        String url = newApi_dyrq.getUserGisTokenUrl() ;
        StringRequest postRequest = new StringRequest(Request.Method.GET,
                url , response -> {
            URLResponse responseCode = new URLResponse(response);
            if (responseCode.getCode() == ErrorCode.SUCCESS) {
                String gisToken = responseCode.queryResult("gisToken");
                userCallBack.onGetUserGisToken(true, responseCode.getCode(), responseCode.getMessage(), gisToken);
            } else {
                userCallBack.onGetUserGisToken(false, responseCode.getCode(), responseCode.getMessage(),null);
            }
        }, error -> {
            //登录失败
            if (error.networkResponse != null) {
                try {
                    String errString = new String(error.networkResponse.data, "UTF-8");
                    userCallBack.onGetUserGisToken(false, error.networkResponse.statusCode, errString, null);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    userCallBack.onGetUserGisToken(false, error.networkResponse.statusCode, null, null);
                }
            }else {
                String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                userCallBack.onGetUserGisToken(false, ErrorCode.HTTPCONN_TIMEOUT, errString, null);
            }
        }){
            @Override
            public Map<String, String> getHeaders(){
                //参数拼装:表单提交方式
                return newApi_dyrq.getHeaderParams();
            }
        };
        BaseApp.addRequest(context.getClass().getName(), postRequest);
    }
}
