import 'package:get/get.dart'; import 'package:star_lock/main/lockDetail/messageWarn/msgNotification/openDoorNotify/openDoorNotify_entity.dart'; import 'package:star_lock/main/lockDetail/messageWarn/notificationMode/notificationMode_data.dart'; import 'package:star_lock/network/api_repository.dart'; import 'package:star_lock/tools/baseGetXController.dart'; import 'nDaysUnopened_state.dart'; class NDaysUnopenedLogic extends BaseGetXController { NDaysUnopenedState state = NDaysUnopenedState(); Future lockNoticeSettingAccountList() async { final OpenDoorNotifyEntity entity = await ApiRepository.to .updateNdaysNotCloseDoorNoticeSetting( lockId: state.getLockId.value, dayNotOpenDoorState: state.isUnOpenNotice.value == true ? 1 : 0, dayNotOpenDoorValue: state.unOpenDoorTime.value, dayNotOpenDoorNoticeWayList: [ {'type': 'mail', 'accounts': getEmailAndSMSAccountList(true)}, {'type': 'sms', 'accounts': getEmailAndSMSAccountList(false)} ]); if (entity.errorCode!.codeIsSuccessful) { showToast('设置成功'.tr); Get.back(result: true); } } //获取到家人信息的请求数组 List getEmailAndSMSAccountList(bool isEmail) { final List list = []; List accountList = []; isEmail ? accountList = state.emailReceiverList.value : accountList = state.phoneReceiverList.value; for (int i = 0; i < accountList.length; i++) { final MsgNoticeModeData item = accountList[i]; final Map map = {}; map['countryCode'] = isEmail ? 0 : item.countryCode; map['account'] = isEmail ? item.receiveEmail : item.receivePhone; list.add(map); } return list; } String getEmailListStr(Map val) { String emailListStr = ''; if (val['emailReceiverList'] != null) { state.emailReceiverList.value = val['emailReceiverList']; final List emailReceiverList = state.emailReceiverList.value; for (int i = 0; i < emailReceiverList.length; i++) { final MsgNoticeModeData item = emailReceiverList[i]; emailListStr += item.receiveEmail; // 检查是否为最后一个元素 if (i < emailReceiverList.length - 1) { emailListStr += ','; } } } return emailListStr; } String getPhoneListStr(Map val) { String phoneListStr = ''; if (val['phoneReceiverList'] != null) { state.phoneReceiverList.value = val['phoneReceiverList']; final List phoneReceiverList = state.phoneReceiverList.value; for (int i = 0; i < phoneReceiverList.length; i++) { final MsgNoticeModeData item = phoneReceiverList[i]; phoneListStr += item.receivePhone; // 检查是否为最后一个元素 if (i < phoneReceiverList.length - 1) { phoneListStr += ','; } } } return phoneListStr; } //根据列表返回值得到邮箱、手机列表 Map> getAccountsMap() { final List mailAccounts = []; final List smsAccounts = []; if (state.msgNoticeInfo.value.dayNotOpenDoorNoticeWayList != null) { for (final NoticeWay item in state.msgNoticeInfo.value.dayNotOpenDoorNoticeWayList!) { if (item.type == 'mail' && item.accounts != null) { for (final Accounts account in item.accounts!) { if (account.account != null) { final MsgNoticeModeData msgNoticeModeData = MsgNoticeModeData(); msgNoticeModeData.receiveEmail = account.account!; mailAccounts.add(msgNoticeModeData); } } } else if (item.type == 'sms' && item.accounts != null) { for (final Accounts account in item.accounts!) { if (account.account != null && account.countryCode != null) { final MsgNoticeModeData msgNoticeModeData = MsgNoticeModeData(); msgNoticeModeData.receivePhone = account.account!; msgNoticeModeData.countryCode = account.countryCode!; smsAccounts.add(msgNoticeModeData); } } } } } return { 'emailReceiverList': mailAccounts, 'phoneReceiverList': smsAccounts, }; } @override onInit() { super.onInit(); state.emailListStr.value = getEmailListStr(getAccountsMap()); state.phontListStr.value = getPhoneListStr(getAccountsMap()); } }