package com.tofly.ghrq_route.module.user;

import android.content.Context;

import com.mgq.base.utils.logger.LogRecorder;
import com.mgq.base.utils.progressdialog.ProgressManager;
import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.C;
import com.tofly.ghrq_route.base.MainModule;
import com.tofly.ghrq_route.base.URLResponseCode;
import com.tofly.ghrq_route.module.OfflineWork.OfflineWorkTool;
import com.tofly.ghrq_route.module.pipeAnalyze.domain.FieldsBean;
import com.tofly.ghrq_route.module.plan.domain.PlanBean;
import com.tofly.ghrq_route.module.sqlite.DBDaoHelper;
import com.tofly.ghrq_route.module.user.controller.UserCallBack;
import com.tofly.ghrq_route.module.user.controller.UserController;
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.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.RegisterInfoBean;
import com.tofly.ghrq_route.module.user.domain.UserBean;
import com.tofly.ghrq_route.module.user.domain.UserBean.PDA_BIND_STATUS;
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.sharedPreference.SharePreUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by Amu on 2017/4/26.
 */

public class UserInfoTool {

    private static final String TAG = UserInfoTool.class.getName();

    private static UserInfoTool userInfoTool = null;

    private UserController userController;

    private DBDaoHelper dbDaoHelper;

    private Context mContext;

    private int pageindex = 1;
    private final int pagesize = C.DEFAULT_MAX_LOAD_PAGESIZE;

    private UserInfoTool(){
        dbDaoHelper = DBDaoHelper.getInstance();
    }

    public static synchronized UserInfoTool getInstance(){
        if (userInfoTool == null){
            userInfoTool = new UserInfoTool();
        }
        return userInfoTool;
    }

    public void checkLogin(Context context){
        boolean hasToken = false;
        boolean hasName = false;
        boolean bRemembered = false;
        String token = "";
        String name = "";
        String userid = "";

        mContext = context;

        /*判断用户是否登陆过，并且拿到数据库中的用户token，否则则视为未登录过*/
        UserBean rememberedUser = SharePreUtil.getLoginInfo(mContext);
        bRemembered = SharePreUtil.getIsLogined(mContext);

        if (rememberedUser != null){
            String saveName = rememberedUser.getUsername();
            if (saveName != null && !saveName.isEmpty()){
                hasName = true;
                name = saveName;
            }
            String saveUserId = rememberedUser.getUserid();
            if (saveUserId != null && !saveUserId.isEmpty()){
                userid = saveUserId;
                UserBean savedUser = dbDaoHelper.queryUserByUserId(userid);
                if (savedUser != null){
                    hasToken = true;
                    token = savedUser.getToken();
                    BaseApp.getInstance().setLoginUser(savedUser);
                }
            }
        }

        /*如果获取到token，向服务器请求用户信息*/
        if (hasToken && hasName){
            getUserInfo(context, name, token, null);
        }
    }

    public void userLogin(final Context context, final String userid, final String password, final boolean forceLogin,
                          final int login_mode, final UserCallBack userCallBack){
        if (userController == null)
            userController = new UserController(context);
        userController.login(userid, password, forceLogin, login_mode, new UserCallBack() {
            @Override
            public void onLogin(boolean isSuccess, int errorCode, String errMsg, UserBean user) {
                super.onLogin(isSuccess, errorCode, errMsg, user);
                if (isSuccess) {
                    if (errorCode == URLResponseCode.ErrorCode.SUCCESS){
                        OfflineWorkTool.setOffline(false);
                        OfflineWorkTool.setLoginServer(true);
                        //getDeviceRegisterInfo(context, BaseApp.getInstance().getDevieImei(), null);
                    }else if (errorCode == URLResponseCode.ErrorCode.SUCCESS_OFFLINE){
                        OfflineWorkTool.setOffline(true);
                    }
                    if (user != null){
                        /*如果登录的账号和之前的账号不一致，则清除上一个账号的本地数据*/
                        UserBean rememberedUser = SharePreUtil.getLoginInfo(mContext);
                        if (rememberedUser != null) {
                            String oldUserId = rememberedUser.getUserid();
                            String userId = user.getUserid();
                            if (!oldUserId.equals(userId)) {
                                dbDaoHelper.clearAllButUser();
                            }
                        }
                        user.setPassword(password);
                        saveUserInfo(user);
                        SharePreUtil.saveLoginInfo(context, user.getUsername(), user.getUserid(), password);
                    }
                    checkPdaBinded(context, userCallBack);
                } else {
                    if (login_mode != UserController.LOGIN_TEST_SERVER){
                        errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                        ToastUtil.showToast(context, errMsg);
                    }
                }

                if (userCallBack != null)
                    userCallBack.onLogin(isSuccess, errorCode, errMsg, user);
            }
        });
    }

    public void userLogin(final Context context, final String userid, final String password,
                          final boolean forceLogin, final UserCallBack userCallBack){
        userLogin(context, userid, password, forceLogin, UserController.LOGIN_UNCERTAIN, userCallBack);
    }

    public void userLoginOnline(final Context context, final String userid, final String password,
                          final boolean forceLogin, final UserCallBack userCallBack){
        userLogin(context, userid, password, forceLogin, UserController.LOGIN_ONLINE, userCallBack);
    }

    public void userLoginTestServer(final Context context, final String userid, final String password,
                                final boolean forceLogin, final UserCallBack userCallBack){
        userLogin(context, userid, password, forceLogin, UserController.LOGIN_TEST_SERVER, userCallBack);
    }

    public void userLogin(final Context context){
        boolean isOffline = OfflineWorkTool.isOffline();
        boolean isNetConnnceted = BaseApp.isNetworkConnected();
        if (isOffline && isNetConnnceted){
            boolean isLoginAlready = OfflineWorkTool.isLoginServer();
            if (isLoginAlready){
                getUserInfoOnline(context, null);
            }else {
                String password = "";
                UserBean rememberedUser = SharePreUtil.getLoginInfo(context);
                if (rememberedUser != null){
                    String savePassword = rememberedUser.getPassword();
                    if (savePassword != null && !savePassword.isEmpty()){
                        //password = BaseApp.AESDecodeString(savePassword);
                        password = savePassword;
                    }
                }
                if (!password.isEmpty()){
                    UserBean userBean = BaseApp.getInstance().getLoginUser();
                    String username = userBean.getUsername();
                    userLoginOnline(context, username, password,true, null);
                }
            }
        }
    }

    public void getUserInfo(final Context context, String username, final String token, int login_mode, final UserCallBack userCallBack){
        if (userController == null)
            userController = new UserController(context);
        userController.getAccountInfo(username, token, login_mode, new UserCallBack(){
            @Override
            public void onGetAccountInfo(boolean isSuccess, int errorCode, String errMsg, UserBean user) {
                super.onGetAccountInfo(isSuccess, errorCode, errMsg, user);
                if (isSuccess){
                    if (errorCode == URLResponseCode.ErrorCode.SUCCESS ||
                            errorCode == URLResponseCode.ErrorCode.SUCCESS_OFFLINE){
                        if (user != null){
                            String password = SharePreUtil.getLoginPassword(context);
                            user.setPassword(password);
                            user.setToken(token);
                            saveUserInfo(user);
                            SharePreUtil.saveLoginInfo(context, user.getUsername(), user.getUserid());
                        }

                        if (errorCode == URLResponseCode.ErrorCode.SUCCESS)
                            OfflineWorkTool.setOffline(false);
                    }
                }else {
                    errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                    ToastUtil.showToast(mContext, errMsg);
                }
                if (userCallBack != null)
                    userCallBack.onGetAccountInfo(isSuccess, errorCode, errMsg, user);
            }
        });
    }

    public void getUserInfo(Context context, String username, final String token, final UserCallBack userCallBack){
        getUserInfo(context, username, token, UserController.LOGIN_UNCERTAIN, userCallBack);
    }

    public void getUserInfoOnline(Context context, final UserCallBack userCallBack){
        getUserInfo(context, UserController.LOGIN_ONLINE, userCallBack);
    }

    public void getUserInfo(Context context, final UserCallBack userCallBack){
        getUserInfo(context, UserController.LOGIN_UNCERTAIN, userCallBack);
    }

    public void getUserInfo(Context context, int login_mode, UserCallBack userCallBack){
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        String username = userBean.getUserid();
        String token = userBean.getToken();
        getUserInfo(context, username, token, login_mode, userCallBack);
    }

    private void saveUserInfo(UserBean user) {
        BaseApp.getInstance().setLoginUser(user);
        dbDaoHelper.insertOrUpdateUser(user);
    }

    /**
     * 设备绑定校验 返回结果 0[未绑定],1[已绑定],2[绑定错误]
     */
    public void checkPdaBinded(Context context, UserCallBack userCallBack) {
        String strUploading = context.getString(R.string.logining_wait);
        ProgressManager.getInstance().showProgress(context, null, strUploading, false);
        if (userController == null)
            userController = new UserController(context);
        userController.checkPdaBinded(context, new UserCallBack(){
            @Override
            public void onCheckPdaBind(boolean isSuccess, int errorCode, String errMsg, int bind_status) {
                super.onCheckPdaBind(isSuccess, errorCode, errMsg, bind_status);
                ProgressManager.getInstance().cancelProgress();
                if (isSuccess) {
                    if (bind_status == PDA_BIND_STATUS.PDA_BINDED.getStatus()){  //设备已绑定
                        SharePreUtil.setDeviceBinded(context, true);
                    }else {
                        SharePreUtil.setDeviceBinded(context, false);
                        if (bind_status != PDA_BIND_STATUS.PDA_UNBIND.getStatus()) {
                            if (bind_status == PDA_BIND_STATUS.PDA_BINDED_OTHER.getStatus()) {
                                errMsg = context.getString(R.string.ui_login_bind_other);
                            }
                            ToastUtil.showToast(context, errMsg);
                        }
                    }
                }else {
                    errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                    ToastUtil.showToast(context, errMsg);
                }

                if (userCallBack != null)
                    userCallBack.onCheckPdaBind(isSuccess, errorCode, errMsg, bind_status);
            }
        });
    }

    public void bindPdaDevice(Context context , DeviceBean deviceBean, UserCallBack userCallBack) {
        if (userController == null)
            userController = new UserController(context);
        userController.bindPdaDevice(context, deviceBean, new UserCallBack(){
            @Override
            public void onPdaDeviceBinded(boolean isSuccess, int errorCode, String errMsg) {
                super.onPdaDeviceBinded(isSuccess, errorCode, errMsg);
                if (isSuccess) {
                    SharePreUtil.setDeviceBinded(context, true);
                }else {
                    errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                    ToastUtil.showToast(context, errMsg);
                }

                if (userCallBack != null)
                    userCallBack.onPdaDeviceBinded(isSuccess, errorCode, errMsg);
            }
        });
    }

    public void getUserMenus(Context context, UserCallBack userCallBack) {
        String strUploading = context.getString(R.string.logining_wait);
        ProgressManager.getInstance().showProgress(context, null, strUploading, false);
        if (userController == null)
            userController = new UserController(context);
        userController.getUserMenus(context, new UserCallBack(){
            @Override
            public void onGetUserMenus(boolean isSuccess, int errorCode, String errMsg, List<UserMenuBean> menus) {
                super.onGetUserMenus(isSuccess, errorCode, errMsg, menus);
                ProgressManager.getInstance().cancelProgress();
                if (isSuccess){
                    MainModule mainModule = MainModule.getInstance();
                    mainModule.setModulesEnable(menus);
                }else {
                    errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                    ToastUtil.showToast(context, errMsg);
                }
                if (userCallBack != null)
                    userCallBack.onGetUserMenus(isSuccess, errorCode, errMsg, menus);
            }
        });
    }

    public List<MainModule.MainModuleBean> getUserMainModules() {
        return getUserModules(MainModule.MODULE_TYPE.MAIN_MODULE_TYPE);
    }

    public List<MainModule.MainModuleBean> getUserModules(MainModule.MODULE_TYPE module_type) {
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        List<MainModule.MainModuleBean> moduleBeans = new ArrayList<>();

        List<UserMenuBean> userMenus = dbDaoHelper.getUserMenuList(userBean.getUserid());
        Map<String, MainModule.MainModuleBean> mainModules = MainModule.getInstance().getMainModules();
        for (UserMenuBean userMenu : userMenus) {
            MainModule.MainModuleBean module = mainModules.get(userMenu.getCode());
            if (module != null && module.getModule_type() == module_type) {
                moduleBeans.add(module);
            }
        }
        return moduleBeans;
    }

    public List<MainModule.SubModuleBean> getTroubleModules() {
        String main_code = MainModule.MainModuleBean.MIAN_MODULE_INDEX.MIAN_MODULE_DANGER.getCode();
        List<MainModule.SubModuleBean> modules = getSubModules(main_code);
        Collections.sort(modules);
        return modules;

/*        List<MainModule.SubModuleBean> modules = new ArrayList<>();

        Map<String, MainModule.SubModuleBean> subModules = MainModule.getInstance().getSubTroubleModules();
        for (Map.Entry<String, MainModule.SubModuleBean> entry : subModules.entrySet()) {
            MainModule.SubModuleBean subModuleBean = entry.getValue();
            modules.add(subModuleBean);
        }

        Collections.sort(modules);

        return modules;*/
    }

    public List<MainModule.SubModuleBean> getEmergencyModules() {
        String main_code = MainModule.MainModuleBean.MIAN_MODULE_INDEX.MIAN_MODULE_EMERGENCY_JOB.getCode();
        List<MainModule.SubModuleBean> modules = getSubModules(main_code);

/*        Map<String, MainModule.SubModuleBean> subModules = MainModule.getInstance().getSubEmergencyModules();
        for (Map.Entry<String, MainModule.SubModuleBean> entry : subModules.entrySet()) {
            MainModule.SubModuleBean subModuleBean = entry.getValue();
            modules.add(subModuleBean);
        }*/

        Collections.sort(modules);

        return modules;
    }

    public List<MainModule.SubModuleBean> getSubModules(String main_code) {
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        List<MainModule.SubModuleBean> modules = new ArrayList<>();

        List<UserMenuBean> userMenus = dbDaoHelper.getSubModuleMenuList(userBean.getUserid(), main_code);

        Map<String, MainModule.SubModuleBean> subModules = MainModule.getInstance().getSubModules();
        for (UserMenuBean userMenu : userMenus) {
            MainModule.SubModuleBean module = subModules.get(userMenu.getCode());
            if (module != null) {
                modules.add(module);
            }
        }
        return modules;
    }

    public void updateAccount(final Context context, final String name, String mobile, String password, String avatar, final UserCallBack userCallBack){
        if (userController == null)
            userController = new UserController(context);
        final UserBean user = BaseApp.getInstance().getLoginUser();
        final String token = user.getToken();
        userController.updateAccountInfo(user.getUserid(), name, mobile, password, avatar, new UserCallBack(){
            @Override
            public void onUpdateAccount(boolean isSuccess, int errorCode, String errMsg) {
                super.onUpdateAccount(isSuccess, errorCode, errMsg);
                if (isSuccess) {
                    String username = user.getUsername();
                    getUserInfo(context, username, token, userCallBack);
                    if (password != null && !password.isEmpty()) {
                        /*下次需要重新登陆*/
                        SharePreUtil.setIsLogined(context, true);
                        SharePreUtil.setIsNeedReLogin(context, true);
                    }
                    ToastUtil.showToast(mContext, R.string.userinfo_update_user_ok);
                } else {
                    errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                    ToastUtil.showToast(mContext, errMsg);
                }
                if (userCallBack != null)
                    userCallBack.onUpdateAccount(isSuccess, errorCode, errMsg);
            }
        });
    }

    public void getUserMapConfigData(Context context, final UserCallBack userCallBack) {
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null) {
            if (userController == null)
                userController = new UserController(context);
            userController.getMapConfigData(userBean.getUserid(), new UserCallBack() {
                @Override
                public void onGetMapConfigData(boolean isSuccess, int errorCode, String errMsg, List<UserOnlineMapDataBean> urlConfigDataBean, List<UserMapDataBean> userMapDataBeanList) {
                    super.onGetMapConfigData(isSuccess, errorCode, errMsg, urlConfigDataBean, userMapDataBeanList);
                    if (userCallBack != null){
                        userCallBack.onGetMapConfigData(isSuccess, errorCode, errMsg, urlConfigDataBean, userMapDataBeanList);
                    }
                }
            });
        }
    }

    public boolean hasLocalUserMapConfig(String className){
        boolean hasConfig = false;
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null){
            List<UserMapDataBean> userMapDataList = dbDaoHelper.getUserMapDataList(userBean.getUserid());
            if (userMapDataList != null){
                int mapSize = userMapDataList.size();
                hasConfig = mapSize > 0;
                Context context = BaseApp.getInstance().getApplicationContext();
                String content = String.format("%s - 用户id：%s 用户地图配置个数：%d", className, userBean.getUserid(), mapSize);
                LogRecorder.writeDeviceToolLog(context, content, userBean.getUserid());
            }
        }
        return hasConfig;
    }

    public void getMapLayerIndexConfig(Context context, UserCallBack userCallBack){
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null){
            if (userController == null)
                userController = new UserController(context);
            userController.getMapLayerIndexConfigData(userBean.getToken(), new UserCallBack(){
                @Override
                public void onGetMapLayerIndexConfigData(boolean isSuccess, int errorCode, String errMsg, List<FieldsBean> configBeans) {
                    super.onGetMapLayerIndexConfigData(isSuccess, errorCode, errMsg, configBeans);
                    if (isSuccess){
                    }
                    if (userCallBack != null) {
                        userCallBack.onGetMapLayerIndexConfigData(isSuccess, errorCode, errMsg, configBeans);
                    }
                }
            });
        }
    }

    public void userBindCar(final Context context, final String carno, final UserCallBack userCallBack) {
        final UserBean user = BaseApp.getInstance().getLoginUser();
        if (userController == null)
            userController = new UserController(context);
        userController.userBindCar(user.getToken(), carno, new UserCallBack() {
            @Override
            public void onUserBindCar(boolean isSuccess, int errorCode, String errMsg) {
                super.onUserBindCar(isSuccess, errorCode, errMsg);
                if (isSuccess) {
                    BaseApp.getInstance().setCarNo(carno);
                    getUserInfo(context, user.getUsername(), user.getToken(), userCallBack);
                } else {
                    errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                    ToastUtil.showToast(context, errMsg);
                }
                if (userCallBack != null)
                    userCallBack.onUserBindCar(isSuccess, errorCode, errMsg);
            }
        });
    }

    public void userUnBindCar(final Context context, final UserCallBack userCallBack) {
        final UserBean user = BaseApp.getInstance().getLoginUser();
        if (userController == null)
            userController = new UserController(context);
        userController.userUnBindCar(user.getToken(), user.getCarid(), new UserCallBack() {
            @Override
            public void onUserUnBindCar(boolean isSuccess, int errorCode, String errMsg) {
                super.onUserUnBindCar(isSuccess, errorCode, errMsg);
                if (isSuccess) {
                    getUserInfo(context, user.getUsername(), user.getToken(), userCallBack);
                } else {
                    errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                    ToastUtil.showToast(context, errMsg);
                }
                if (userCallBack != null)
                    userCallBack.onUserUnBindCar(isSuccess, errorCode, errMsg);
            }
        });
    }

    /**
     * 用户注册部分
     */

    //获取用户注册时的公司信息
    public void getCompaniesForRegister(final Context context, final UserCallBack userCallBack){
        if (userController == null)
            userController = new UserController(context);
        userController.getCompanyList(new UserCallBack(){
            @Override
            public void onGetCompanyList(boolean isSuccess, int errorCode, String errMsg, List<CompanyBean> companyList) {
                super.onGetCompanyList(isSuccess, errorCode, errMsg, companyList);
                if (isSuccess){
                }else {
                    errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                    ToastUtil.showToast(context, errMsg);
                }
                if (userCallBack != null)
                    userCallBack.onGetCompanyList(isSuccess, errorCode, errMsg, companyList);
            }
        });
    }

    //获取该公司可以注册绑定的用户
    public void getCompanyUsersForRegister(final Context context, String company_id, final UserCallBack userCallBack){
        if (company_id != null){
            if (userController == null)
                userController = new UserController(context);
            userController.getCompanyUserList(company_id, new UserCallBack(){
                @Override
                public void onGetCompanyUserList(boolean isSuccess, int errorCode, String errMsg, List<CompanyUserBean> userList) {
                    super.onGetCompanyUserList(isSuccess, errorCode, errMsg, userList);
                    if (isSuccess){
                    }else {
                        errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                        ToastUtil.showToast(context, errMsg);
                    }
                    if (userCallBack != null)
                        userCallBack.onGetCompanyUserList(isSuccess, errorCode, errMsg, userList);
                }
            });
        }
    }

    //获取当前设备的注册信息
    public void getDeviceRegisterInfo(final Context context, String imei, final UserCallBack userCallBack){
        if (imei != null && !imei.isEmpty()){
            if (userController == null)
                userController = new UserController(context);
            userController.getRegisterDeviceInfo(imei, new UserCallBack(){
                @Override
                public void onGetRegisterDeviceInfo(boolean isSuccess, int errorCode, String errMsg, RegisterInfoBean infoBean) {
                    super.onGetRegisterDeviceInfo(isSuccess, errorCode, errMsg, infoBean);
                    if (isSuccess){
                        if (infoBean != null) {
                            boolean isStandBy = infoBean.getType() == 1;
                            SharePreUtil.setDeviceIsStandBy(context, isStandBy);
                        }
                    }else {
                        errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                        ToastUtil.showToast(context, errMsg);
                    }
                    if (userCallBack != null)
                        userCallBack.onGetRegisterDeviceInfo(isSuccess, errorCode, errMsg, infoBean);
                }
            });
        }
    }

    //注册并绑定设备
    public void registerAccount(final Context context, DeviceBean deviceInfo, boolean isStandBy, CompanyBean companyBean,
                                CompanyUserBean userBean, final UserCallBack userCallBack){
        if (deviceInfo != null){
            if (userController == null)
                userController = new UserController(context);
            userController.registerAccount(deviceInfo, isStandBy, companyBean, userBean, new UserCallBack(){
                @Override
                public void onRegisterAccount(boolean isSuccess, int errorCode, String errMsg) {
                    super.onRegisterAccount(isSuccess, errorCode, errMsg);
                    if (isSuccess){
                    }else {
                        errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                        ToastUtil.showToast(context, errMsg);
                    }
                    if (userCallBack != null)
                        userCallBack.onRegisterAccount(isSuccess, errorCode, errMsg);
                }
            });
        }
    }

    /**
     * 用户注册部分
     */

    //获取部门信息
    public void getDepartments(final Context context, boolean filtered, final UserCallBack userCallBack){
        if (userController == null)
            userController = new UserController(context);
        int pageindex = 1;
        int pagesize = C.DEFAULT_MAX_LOAD_PAGESIZE;
        userController.getDepartmentList(context, pageindex, pagesize, filtered, new UserCallBack(){
            @Override
            public void onGetDepartmentList(boolean isSuccess, int errorCode, String errMsg, List<DepartmentBean> departmentList) {
                super.onGetDepartmentList(isSuccess, errorCode, errMsg, departmentList);
                if (isSuccess){
                }else {
                    errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                    ToastUtil.showToast(context, errMsg);
                }
                if (userCallBack != null)
                    userCallBack.onGetDepartmentList(isSuccess, errorCode, errMsg, departmentList);
            }
        });
    }

    //获取该部门的用户
    public void getDepartmentUsers(final Context context, String departId, final UserCallBack userCallBack){
        int pageindex = 1;
        int pagesize = C.DEFAULT_MAX_LOAD_PAGESIZE;
        if (userController == null)
            userController = new UserController(context);
        userController.getDepartmentUserList(context, departId, pageindex, pagesize, new UserCallBack(){
            @Override
            public void onGetDepartmentUserList(boolean isSuccess, int errorCode, String errMsg, List<DepartmentUserBean> userList) {
                super.onGetDepartmentUserList(isSuccess, errorCode, errMsg, userList);
                if (isSuccess){
                }else {
                    errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                    ToastUtil.showToast(context, errMsg);
                }
                if (userCallBack != null)
                    userCallBack.onGetDepartmentUserList(isSuccess, errorCode, errMsg, userList);
            }
        });
    }

    /*清除当前用户已过期的本地的计划数据*/
    public void removePlansOverTime(){
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null) {
            List<PlanBean> pastPlans = dbDaoHelper.getPlanOverTime(userBean.getUserid());
            if (pastPlans != null && pastPlans.size() > 0){
                dbDaoHelper.deletePlans(pastPlans);
            }

            removePlanOfflinePointsOverTime();
        }
    }

    /*清除当前用户已过期的计划的离线点数据*/
    private void removePlanOfflinePointsOverTime(){
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null)
            dbDaoHelper.clearOfflinePathPastDeadline(userBean.getUserid());
    }


    // 清除历史保存的轨迹(最多保存45天)
    public void removeLocalLocationGpsData(){
        dbDaoHelper.clearLocationGPSData();
    }

    // 清除历史保存的签到记录(最多保存45天)
    public void removeLocalSignHistoryData(){
        dbDaoHelper.clearSignHistoryData();
    }

    public void getUserGisToken(Context context, final UserCallBack userCallBack) {
        if (userController == null)
            userController = new UserController(context);
        userController.getUserGisToken(context, new UserCallBack() {
            @Override
            public void onGetUserGisToken(boolean isSuccess, int errorCode, String errMsg, String gisToken) {
                super.onGetUserGisToken(isSuccess, errorCode, errMsg, gisToken);
                if (isSuccess) {
                    if (gisToken != null && !gisToken.isEmpty()) {
                        SharePreUtil.setGisToken(context, gisToken);
                    }
                } else {
                    errMsg = BaseApp.formatErrorMessage(errorCode, errMsg);
                    ToastUtil.showToast(context, errMsg);
                }
                if (userCallBack != null)
                    userCallBack.onGetUserGisToken(isSuccess, errorCode, errMsg, gisToken);
            }
        });
    }
}
