2024-03-25 14:16:00 +08:00
|
|
|
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';
|
2024-03-26 15:30:43 +08:00
|
|
|
import 'package:sk_base_mobile/apis/index.dart';
|
2024-03-26 16:02:49 +08:00
|
|
|
import 'package:sk_base_mobile/store/auth.store.dart';
|
2024-03-25 14:16:00 +08:00
|
|
|
import 'package:sk_base_mobile/util/logger_util.dart';
|
|
|
|
|
|
|
|
const needCachedKey = [
|
|
|
|
DictTypeEnum.InventoryRoom,
|
|
|
|
DictTypeEnum.InventoryLine,
|
|
|
|
DictTypeEnum.InventoryLineLevel
|
|
|
|
];
|
|
|
|
|
|
|
|
class DictService extends GetxService {
|
2024-03-26 16:02:49 +08:00
|
|
|
static DictService get to => Get.find();
|
2024-03-25 14:16:00 +08:00
|
|
|
Future<DictService> init() async {
|
2024-03-26 16:02:49 +08:00
|
|
|
if (AuthStore.to.userInfo.value.id == null) return this;
|
2024-03-25 14:16:00 +08:00
|
|
|
await getDictTypes();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
RxList<DictTypeModel> dictTypes = RxList([]);
|
|
|
|
Future<void> 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<DictItemModel> getDictItemsByCode(String code) {
|
|
|
|
return dictTypes.firstWhereOrNull((item) => item.code == code)?.dictItems ??
|
|
|
|
[];
|
|
|
|
}
|
|
|
|
}
|