98 lines
2.5 KiB
Dart
98 lines
2.5 KiB
Dart
class RegionPoint {
|
||
const RegionPoint({required this.lat, required this.lng});
|
||
final double lat;
|
||
final double lng;
|
||
|
||
bool get isValid =>
|
||
lat.isFinite &&
|
||
lng.isFinite &&
|
||
lat >= -90 &&
|
||
lat <= 90 &&
|
||
lng >= -180 &&
|
||
lng <= 180;
|
||
}
|
||
|
||
/// 分区的业务数据;JSON 字段、type 和 status 的接口格式由数据层负责。
|
||
class RegionDraft {
|
||
RegionDraft({
|
||
this.id,
|
||
required this.siteId,
|
||
this.name = '',
|
||
this.code = '',
|
||
this.remark = '',
|
||
this.enabled = true,
|
||
this.defaultRouteId,
|
||
List<int> boundRouteIds = const [],
|
||
List<RegionPoint> scope = const [],
|
||
}) : boundRouteIds = List.unmodifiable(boundRouteIds),
|
||
scope = List.unmodifiable(scope);
|
||
|
||
final int? id;
|
||
final int siteId;
|
||
final String name;
|
||
final String code;
|
||
final String remark;
|
||
final bool enabled;
|
||
final int? defaultRouteId;
|
||
final List<int> boundRouteIds;
|
||
final List<RegionPoint> scope;
|
||
|
||
RegionDraft withScope(List<RegionPoint> points) => RegionDraft(
|
||
id: id,
|
||
siteId: siteId,
|
||
name: name,
|
||
code: code,
|
||
remark: remark,
|
||
enabled: enabled,
|
||
defaultRouteId: defaultRouteId,
|
||
boundRouteIds: boundRouteIds,
|
||
scope: points,
|
||
);
|
||
|
||
RegionDraft withId(int? value) => RegionDraft(
|
||
id: value,
|
||
siteId: siteId,
|
||
name: name,
|
||
code: code,
|
||
remark: remark,
|
||
enabled: enabled,
|
||
defaultRouteId: defaultRouteId,
|
||
boundRouteIds: boundRouteIds,
|
||
scope: scope,
|
||
);
|
||
|
||
String? get scopeError {
|
||
if (scope.length != 4) return '分区范围需要恰好 4 个打点';
|
||
if (scope.any((p) => !p.isValid)) return '分区范围包含无效经纬度';
|
||
if (scope.map((p) => (p.lat, p.lng)).toSet().length != 4) {
|
||
return '分区的 4 个打点不能重复';
|
||
}
|
||
return null;
|
||
}
|
||
|
||
String? get validationError {
|
||
if (id != null && id! <= 0) return '分区 ID 无效';
|
||
if (siteId <= 0) return '请先选择场站';
|
||
if (name.trim().isEmpty) return '请填写分区名称';
|
||
if (scopeError != null) return scopeError;
|
||
if (defaultRouteId != null && defaultRouteId! <= 0 ||
|
||
boundRouteIds.any((id) => id <= 0)) {
|
||
return '请选择有效路线';
|
||
}
|
||
return null;
|
||
}
|
||
}
|
||
|
||
class RegionRouteOption {
|
||
const RegionRouteOption({required this.id, required this.name});
|
||
final int id;
|
||
final String name;
|
||
}
|
||
|
||
class RegionSaveResult {
|
||
const RegionSaveResult({this.id});
|
||
|
||
/// 仅为明确返回的分区主键。data: 1 的影响行数不能作为分区 ID。
|
||
final int? id;
|
||
}
|