Files
flutterApp/lib/features/regions/data/models/region_model.dart
2026-11-06 08:44:05 +08:00

46 lines
1.6 KiB
Dart

import '../../domain/entities/region_draft.dart';
/// 列表接口字段可为数字或字符串,空编码/备注在领域内统一为空串。
class RegionModel {
static RegionDraft fromJson(Map<String, dynamic> json) {
int? integer(dynamic value) => int.tryParse('$value');
double? number(dynamic value) =>
value is num ? value.toDouble() : double.tryParse('$value');
final id = integer(json['id']);
final siteId = integer(json['siteId']);
if (id == null || id <= 0 || siteId == null || siteId <= 0) {
throw const FormatException('分区 ID 或场站 ID 无效');
}
final points = <RegionPoint>[];
for (final raw
in json['scope'] is List ? json['scope'] as List : const []) {
if (raw is! Map) continue;
final lat = number(raw['lat']);
final lng = number(raw['lng']);
if (lat == null || lng == null) continue;
final point = RegionPoint(lat: lat, lng: lng);
if (point.isValid) points.add(point);
}
return RegionDraft(
id: id,
siteId: siteId,
name: json['regionName']?.toString() ?? '',
code: json['regionCode']?.toString() ?? '',
remark: json['remark']?.toString() ?? '',
scope: points,
enabled:
json['status'] == null ||
json['status'] == true ||
'${json['status']}' == '1',
defaultRouteId: integer(json['defaultRoute']),
boundRouteIds: [
for (final raw
in json['bindRoutes'] is List
? json['bindRoutes'] as List
: const [])
if (integer(raw) != null && integer(raw)! > 0) integer(raw)!,
],
);
}
}