一次大的提交
This commit is contained in:
@@ -0,0 +1,230 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../domain/entities/alarm_dispatch_entity.dart';
|
||||
import '../../domain/entities/alarm_detail_entity.dart';
|
||||
import '../../domain/usecases/dispatch_workorder_usecase.dart';
|
||||
import '../states/alarm_dispatch_state.dart';
|
||||
import '../../../report/presentation/constants/report_constants.dart';
|
||||
import '../../../home/domain/entities/site_entity.dart';
|
||||
import '../../../home/domain/usecases/get_site_list_usecase.dart';
|
||||
import '../../../home/domain/repositories/site_repository.dart';
|
||||
import '../../../home/data/repositories/site_repository_impl.dart';
|
||||
import '../../../home/data/datasources/site_datasource_impl.dart';
|
||||
import 'package:maibu_satabot_v2/core/app/app_user_cubit.dart';
|
||||
import 'package:maibu_satabot_v2/core/storage/user_storage.dart';
|
||||
|
||||
/// deviceType → ReportType 映射配置(方便后续修改)
|
||||
const Map<String, ReportType> deviceTypeToReportType = {
|
||||
'UAV': ReportType.uavError,
|
||||
'DRONE': ReportType.uavError,
|
||||
'MOWER': ReportType.mowerError,
|
||||
'ROBOT': ReportType.mowerError,
|
||||
};
|
||||
|
||||
class AlarmDispatchCubit extends Cubit<AlarmDispatchState> {
|
||||
final DispatchWorkOrderUseCase dispatchUseCase;
|
||||
final AlarmDetailEntity alarmDetail;
|
||||
final Dio dio;
|
||||
final AppUserCubit appUserCubit;
|
||||
final UserStorage userStorage;
|
||||
|
||||
AlarmDispatchCubit({
|
||||
required this.dispatchUseCase,
|
||||
required this.alarmDetail,
|
||||
required this.dio,
|
||||
required this.appUserCubit,
|
||||
required this.userStorage,
|
||||
}) : super(AlarmDispatchLoading()) {
|
||||
_init();
|
||||
}
|
||||
|
||||
Future<void> _init() async {
|
||||
_initFromAlarm();
|
||||
await _loadSiteName();
|
||||
}
|
||||
|
||||
/// 从告警数据初始化表单
|
||||
void _initFromAlarm() {
|
||||
final deviceType = alarmDetail.deviceType?.toUpperCase() ?? '';
|
||||
final reportType = deviceTypeToReportType[deviceType] ?? ReportType.other;
|
||||
final now = DateTime.now();
|
||||
|
||||
final entity = AlarmDispatchEntity(
|
||||
alarmId: alarmDetail.id,
|
||||
alarmNo: alarmDetail.alarmNo,
|
||||
deviceType: alarmDetail.deviceType,
|
||||
orderType: reportType.orderType,
|
||||
siteId: alarmDetail.siteId,
|
||||
siteName: null,
|
||||
deviceId: alarmDetail.deviceId,
|
||||
deviceName: alarmDetail.deviceInfo,
|
||||
description: alarmDetail.description,
|
||||
problemLevel: _mapAlarmLevelToProblemLevel(alarmDetail.level),
|
||||
planStartTime: now,
|
||||
planEndTime: now.add(const Duration(hours: 4)),
|
||||
);
|
||||
|
||||
emit(AlarmDispatchFormState(
|
||||
alarmId: alarmDetail.id,
|
||||
alarmNo: alarmDetail.alarmNo,
|
||||
entity: entity,
|
||||
selectedReportType: reportType,
|
||||
selectedDevice: alarmDetail.deviceInfo,
|
||||
selectedDeviceId: alarmDetail.deviceId,
|
||||
planStartTime: now,
|
||||
planEndTime: now.add(const Duration(hours: 4)),
|
||||
siteId: alarmDetail.siteId,
|
||||
));
|
||||
}
|
||||
|
||||
/// 加载场站列表,根据 siteId 找到场站名称
|
||||
Future<void> _loadSiteName() async {
|
||||
if (state is! AlarmDispatchFormState) return;
|
||||
final currentState = state as AlarmDispatchFormState;
|
||||
final siteId = currentState.siteId;
|
||||
if (siteId == null) return;
|
||||
|
||||
try {
|
||||
final siteDataSource = SiteDataSourceImpl(
|
||||
dio,
|
||||
userStorage,
|
||||
appUserCubit,
|
||||
);
|
||||
final siteRepository = SiteRepositoryImpl(siteDataSource);
|
||||
final getSiteListUseCase = GetSiteListUseCase(siteRepository);
|
||||
|
||||
final user = appUserCubit.state.user;
|
||||
if (user == null) return;
|
||||
|
||||
final result = await getSiteListUseCase(user.userId);
|
||||
result.fold(
|
||||
(failure) {},
|
||||
(sites) {
|
||||
final matches = sites.where((s) => s.id == siteId);
|
||||
if (matches.isNotEmpty) {
|
||||
final matchedSite = matches.first;
|
||||
emit(currentState.copyWith(
|
||||
siteName: matchedSite.siteName,
|
||||
entity: currentState.entity.copyWith(siteName: matchedSite.siteName),
|
||||
));
|
||||
}
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
// 加载失败不影响表单使用
|
||||
}
|
||||
}
|
||||
|
||||
/// 告警等级 → 问题等级映射
|
||||
String _mapAlarmLevelToProblemLevel(dynamic level) {
|
||||
final label = level.label?.toUpperCase() ?? level.toString().toUpperCase();
|
||||
if (label.contains('DANGER') || label.contains('严重') || label.contains('高危')) {
|
||||
return 'ERROR';
|
||||
}
|
||||
if (label.contains('WARNING') || label.contains('中危')) {
|
||||
return 'WARNING';
|
||||
}
|
||||
return 'INFO';
|
||||
}
|
||||
|
||||
/// 选择上报类型
|
||||
void selectReportType(ReportType type) {
|
||||
final s = state as AlarmDispatchFormState;
|
||||
emit(s.copyWith(
|
||||
selectedReportType: type,
|
||||
entity: s.entity.copyWith(orderType: type.orderType),
|
||||
));
|
||||
}
|
||||
|
||||
/// 选择问题等级
|
||||
void selectProblemLevel(ProblemLevel level) {
|
||||
final s = state as AlarmDispatchFormState;
|
||||
emit(s.copyWith(
|
||||
selectedProblemLevel: level,
|
||||
entity: s.entity.copyWith(problemLevel: level.level),
|
||||
));
|
||||
}
|
||||
|
||||
/// 更新描述
|
||||
void updateDescription(String description) {
|
||||
final s = state as AlarmDispatchFormState;
|
||||
emit(s.copyWith(
|
||||
entity: s.entity.copyWith(description: description),
|
||||
));
|
||||
}
|
||||
|
||||
/// 更新设备
|
||||
void updateDevice(String deviceName, String deviceId) {
|
||||
final s = state as AlarmDispatchFormState;
|
||||
emit(s.copyWith(
|
||||
selectedDevice: deviceName,
|
||||
selectedDeviceId: deviceId,
|
||||
entity: s.entity.copyWith(deviceId: deviceId, deviceName: deviceName),
|
||||
));
|
||||
}
|
||||
|
||||
/// 添加媒体文件
|
||||
void addMediaFile(String filePath) {
|
||||
final s = state as AlarmDispatchFormState;
|
||||
final updatedFiles = List<String>.from(s.mediaFiles)..add(filePath);
|
||||
emit(s.copyWith(
|
||||
mediaFiles: updatedFiles,
|
||||
entity: s.entity.copyWith(mediaUrls: updatedFiles),
|
||||
));
|
||||
}
|
||||
|
||||
/// 移除媒体文件
|
||||
void removeMediaFile(int index) {
|
||||
final s = state as AlarmDispatchFormState;
|
||||
final updatedFiles = List<String>.from(s.mediaFiles)..removeAt(index);
|
||||
emit(s.copyWith(
|
||||
mediaFiles: updatedFiles,
|
||||
entity: s.entity.copyWith(mediaUrls: updatedFiles),
|
||||
));
|
||||
}
|
||||
|
||||
/// 更新计划开始时间
|
||||
void updatePlanStartTime(DateTime time) {
|
||||
final s = state as AlarmDispatchFormState;
|
||||
emit(s.copyWith(
|
||||
planStartTime: time,
|
||||
entity: s.entity.copyWith(planStartTime: time),
|
||||
));
|
||||
}
|
||||
|
||||
/// 更新计划结束时间
|
||||
void updatePlanEndTime(DateTime time) {
|
||||
final s = state as AlarmDispatchFormState;
|
||||
emit(s.copyWith(
|
||||
planEndTime: time,
|
||||
entity: s.entity.copyWith(planEndTime: time),
|
||||
));
|
||||
}
|
||||
|
||||
/// 提交派发
|
||||
Future<void> submitDispatch() async {
|
||||
final s = state;
|
||||
if (s is! AlarmDispatchFormState) return;
|
||||
|
||||
emit(AlarmDispatchSubmitting());
|
||||
|
||||
final result = await dispatchUseCase.execute(s.entity);
|
||||
|
||||
result.fold(
|
||||
(failure) {
|
||||
emit(s.copyWith(toastMessage: failure.message, isSuccess: false));
|
||||
},
|
||||
(success) {
|
||||
emit(s.copyWith(toastMessage: '工单派发成功', isSuccess: true));
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// 清除 toast 消息
|
||||
void clearToast() {
|
||||
final s = state;
|
||||
if (s is AlarmDispatchFormState) {
|
||||
emit(s.copyWith(toastMessage: null));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user