dict_service.dart 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import 'package:lszlgl/model/rsp/dict_rsp.dart';
  2. enum DictType {
  3. // 天气情况
  4. tqqk('tqqk'),
  5. // 监测类别
  6. jclb('jclb'),
  7. // 样品等级
  8. ypdj('ypdj'),
  9. // 小麦品种优质类型
  10. xmpzyzlx('xmpzyzlx'),
  11. // 玉米品种优质类型
  12. ympzyzlx('ympzyzlx'),
  13. // 稻谷品种优质类型
  14. dgpzyzlx('dgpzyzlx'),
  15. // 大豆品种优质类型
  16. ddyzpzlx('ddyzpzlx'),
  17. // 工厂排污类型
  18. gcpwlx('gcpwlx'),
  19. // 污染物类型
  20. wrwlx('wrwlx'),
  21. // 病虫害类型
  22. bchlx('bchlx'),
  23. // 真菌毒素污染类型
  24. zjdswrlx('zjdswrlx'),
  25. // 重金属污染类型
  26. zjswrlx('zjswrlx'),
  27. // 采样品种
  28. cypz('cypz');
  29. const DictType(this.type);
  30. final String type;
  31. }
  32. class DictService {
  33. DictService._();
  34. static List<DictRsp>? _dictList;
  35. static List<DictRsp> get dictList => _dictList ??= [];
  36. static final Map<String, Map<String, DictRsp>> _dictMap = {};
  37. static void saveDictList(List<DictRsp>? list) {
  38. if (list == null) return;
  39. _dictList = list;
  40. // 按类型存储字典
  41. _dictMap.clear();
  42. for (DictRsp dict in list) {
  43. if (dict.dictType == null || dict.value == null) continue;
  44. Map<String, DictRsp> map = _dictMap[dict.dictType!] ??= {};
  45. map[dict.value!] = dict;
  46. }
  47. }
  48. static DictRsp? getDict(DictType? type, {num? value, String? valueStr}) {
  49. if (type == null) return null;
  50. String? v;
  51. if (valueStr == null) {
  52. if (value == null) return null;
  53. v = value.toString();
  54. } else {
  55. v = valueStr;
  56. }
  57. return _dictMap[type.type]?[v];
  58. }
  59. static String? getLabel(DictType type, {num? value, String? valueStr}) {
  60. return getDict(type, value: value, valueStr: valueStr)?.label;
  61. }
  62. static List<DictRsp>? getDictList(DictType? type) {
  63. if (type == null) return null;
  64. return _dictMap[type.type]?.values.toList();
  65. }
  66. static num? getDictFirstNum(DictType type) {
  67. var list = getDictList(type);
  68. return list?.first.value != null ? num.parse(list!.first.value!) : null;
  69. }
  70. }