import 'package:get/get.dart'; import 'package:sk_base_mobile/constants/dict_enum.dart'; import 'package:sk_base_mobile/models/dict_item.model.dart'; import 'package:sk_base_mobile/models/dict_type.model.dart'; import 'package:sk_base_mobile/apis/index.dart'; import 'package:sk_base_mobile/store/auth.store.dart'; import 'package:sk_base_mobile/util/logger_util.dart'; const needCachedKey = [ DictTypeEnum.InventoryRoom, DictTypeEnum.InventoryLine, DictTypeEnum.InventoryLineLevel ]; class DictService extends GetxService { static DictService get to => Get.find(); Future init() async { if (AuthStore.to.userInfo.value.id == null) return this; await getDictTypes(); return this; } RxList dictTypes = RxList([]); Future getDictTypes() async { try { final response = await Api.getDictTypeAll({ 'storeCodes': needCachedKey, 'withItems': true, }); dictTypes.value = (response.data as List) .map((item) => DictTypeModel.fromJson(item)) .toList(); } catch (e) { LoggerUtil().error('getDictTypes error: $e'); } } List getDictItemsByCode(String code) { return dictTypes.firstWhereOrNull((item) => item.code == code)?.dictItems ?? []; } }