package com.grkj.iscs.util import android.content.Context import com.google.gson.Gson import com.google.gson.reflect.TypeToken import com.grkj.iscs.model.UrlConsts import com.grkj.iscs.model.bo.LoginUserBO import com.grkj.iscs.model.bo.UpdateKeyReturnBO import com.grkj.iscs.model.vo.system.SystemAttributePageRespVO import com.grkj.iscs.model.vo.ticket.LockPointUpdateReqVO import com.grkj.iscs.model.vo.user.UserInfoRespVO import com.grkj.iscs.util.log.LogUtil object SPUtils { private const val SP_NAME = "iscs" private const val SP_CONFIG_NAME = "iscs_config" private const val SP_DATA = "iscs_data" private const val SP_ATTRIBUTE = "iscs_attribute" private const val SP_URL = "iscs_url" private const val KEY_LOGIN_USER_CARD_ID = "card_id" private const val KEY_LOGIN_USER_CARD_CODE = "card_code" private const val KEY_LOGIN_USER_HARDWARE_ID = "hardware_id" private const val KEY_LOGIN_USER_CARD_NFC = "card_nfc" private const val KEY_LOGIN_USER_CARD_TYPE = "card_type" private const val KEY_LOGIN_USER_USER_ID = "user_id" private const val KEY_LOGIN_USER_USER_NAME = "user_name" private const val KEY_LOGIN_USER_KEY_CODE = "key_code" private const val KEY_LOGIN_USER_ROLE_KEY = "role_key" private const val KEY_DOCK_CONFIG = "dock_config" private const val KEY_PORT_CONFIG = "port_config" private const val KEY_STEP_MODE = "step_mode" private const val KEY_DATA_UPDATE_LOCK_POINT = "update_lock_point" private const val KEY_DATA_UPDATE_KEY_RETURN = "update_key_return" private const val KEY_SYSTEM_ATTRIBUTE = "system_attribute" private const val KEY_BASE_URL = "base_url" fun getLoginUser(context: Context): LoginUserBO? { val sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE) if (sp.getLong(KEY_LOGIN_USER_USER_ID, -1) == -1L) { return null } return LoginUserBO( userId = sp.getLong(KEY_LOGIN_USER_USER_ID, 0), userName = sp.getString(KEY_LOGIN_USER_USER_NAME, null), keyCode = sp.getString(KEY_LOGIN_USER_KEY_CODE, null), roleKeyList = sp.getString(KEY_LOGIN_USER_ROLE_KEY, null)?.split(",")?.toMutableList(), userCardList = sp.getString(KEY_LOGIN_USER_CARD_NFC, null)?.split(",")?.toMutableList() ) } fun setLoginUser(context: Context, userInfoRespVO: UserInfoRespVO) { val sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE) val edit = sp.edit() userInfoRespVO.user?.userId?.let { edit.putLong(KEY_LOGIN_USER_USER_ID, it) } userInfoRespVO.user?.userName?.let { edit.putString(KEY_LOGIN_USER_USER_NAME, it) } userInfoRespVO.user?.keyCode?.let { edit.putString(KEY_LOGIN_USER_KEY_CODE, it) } userInfoRespVO.roles?.let { edit.putString(KEY_LOGIN_USER_ROLE_KEY, it.toString().replace("[", "").replace("]", "")) } userInfoRespVO.userCardList?.let { edit.putString(KEY_LOGIN_USER_CARD_NFC, it.toString().replace("[", "").replace("]", "")) } edit.commit() } fun clearLoginUser(context: Context): Boolean { return try { val sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE) val edit = sp.edit() edit.clear() edit.apply() true } catch (e: Exception) { false } } fun saveDockConfig(context: Context, config: String) { val sp = context.getSharedPreferences(SP_CONFIG_NAME, Context.MODE_PRIVATE) val edit = sp.edit() edit.putString(KEY_DOCK_CONFIG, config) edit.commit() } fun getDockConfig(context: Context): String? { val sp = context.getSharedPreferences(SP_CONFIG_NAME, Context.MODE_PRIVATE) return sp.getString(KEY_DOCK_CONFIG, null) } fun savePortConfig(context: Context, config: String) { val sp = context.getSharedPreferences(SP_CONFIG_NAME, Context.MODE_PRIVATE) val edit = sp.edit() edit.putString(KEY_PORT_CONFIG, config) edit.commit() } fun getPortConfig(context: Context): String? { val sp = context.getSharedPreferences(SP_CONFIG_NAME, Context.MODE_PRIVATE) return sp.getString(KEY_PORT_CONFIG, null) } fun saveStepMode(context: Context, mode: Int) { val sp = context.getSharedPreferences(SP_CONFIG_NAME, Context.MODE_PRIVATE) val edit = sp.edit() edit.putInt(KEY_STEP_MODE, mode) edit.commit() } fun getStepMode(context: Context): Int { val sp = context.getSharedPreferences(SP_CONFIG_NAME, Context.MODE_PRIVATE) return sp.getInt(KEY_STEP_MODE, 0) } fun saveUpdateLockPoint(context: Context, list: MutableList) { val sp = context.getSharedPreferences(SP_DATA, Context.MODE_PRIVATE) val edit = sp.edit() val tempList = getUpdateLockPoint(context) tempList.addAll(list) edit.putString(KEY_DATA_UPDATE_LOCK_POINT, Gson().toJson(tempList)) edit.apply() } fun getUpdateLockPoint(context: Context): MutableList { val sp = context.getSharedPreferences(SP_DATA, Context.MODE_PRIVATE) val listJson = sp.getString(KEY_DATA_UPDATE_LOCK_POINT, null) if (listJson.isNullOrEmpty()) { return mutableListOf() } try { val tempList: MutableList = Gson().fromJson( listJson, object : TypeToken>() {}.type ) return tempList } catch (e: Exception) { LogUtil.e("${e.message}") return mutableListOf() } } fun clearUpdateLockPoint(context: Context) { val sp = context.getSharedPreferences(SP_DATA, Context.MODE_PRIVATE) val edit = sp.edit() edit.putString(KEY_DATA_UPDATE_LOCK_POINT, null) edit.apply() } fun saveUpdateKeyReturn(context: Context, returnBO: UpdateKeyReturnBO) { val sp = context.getSharedPreferences(SP_DATA, Context.MODE_PRIVATE) val edit = sp.edit() val tempList = getUpdateKeyReturn(context) tempList.add(returnBO) edit.putString(KEY_DATA_UPDATE_KEY_RETURN, Gson().toJson(tempList)) edit.apply() } fun getUpdateKeyReturn(context: Context): MutableList { val sp = context.getSharedPreferences(SP_DATA, Context.MODE_PRIVATE) val listJson = sp.getString(KEY_DATA_UPDATE_KEY_RETURN, null) if (listJson.isNullOrEmpty()) { return mutableListOf() } try { val tempList: MutableList = Gson().fromJson( listJson, object : TypeToken>() {}.type ) return tempList } catch (e: Exception) { LogUtil.e("${e.message}") return mutableListOf() } } fun clearUpdateKeyReturn(context: Context) { val sp = context.getSharedPreferences(SP_DATA, Context.MODE_PRIVATE) val edit = sp.edit() edit.putString(KEY_DATA_UPDATE_KEY_RETURN, null) edit.apply() } fun saveSystemAttribute( context: Context, recordList: MutableList ) { val sp = context.getSharedPreferences(SP_ATTRIBUTE, Context.MODE_PRIVATE) val edit = sp.edit() edit.putString(KEY_SYSTEM_ATTRIBUTE, Gson().toJson(recordList)) edit.commit() } fun getSystemAttribute(context: Context): MutableList { val sp = context.getSharedPreferences(SP_ATTRIBUTE, Context.MODE_PRIVATE) val listJson = sp.getString(KEY_SYSTEM_ATTRIBUTE, null) if (listJson.isNullOrEmpty()) { return mutableListOf() } try { val tempList: MutableList = Gson().fromJson( listJson, object : TypeToken>() {}.type ) return tempList } catch (e: Exception) { LogUtil.e("${e.message}") return mutableListOf() } } fun getAttributeValue(context: Context, key: String?): String? { val list = getSystemAttribute(context) if (list.isEmpty()) { return null } return list.find { it.sysAttrKey == key }?.sysAttrValue } fun saveBaseUrl(context: Context, url: String) { val sp = context.getSharedPreferences(SP_URL, Context.MODE_PRIVATE) val edit = sp.edit() edit.putString(KEY_BASE_URL, url) edit.commit() } fun getBaseUrl(context: Context): String { val sp = context.getSharedPreferences(SP_URL, Context.MODE_PRIVATE) val spUrl = sp.getString(KEY_BASE_URL, null) if (spUrl.isNullOrEmpty()) { return UrlConsts.BASE_URL } return spUrl } }