一次大的提交
This commit is contained in:
@@ -1,43 +1,174 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../domain/entities/report_entity.dart';
|
||||
import '../../domain/usecases/submit_report_usecase.dart';
|
||||
import '../constants/report_constants.dart';
|
||||
import '../states/report_state.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';
|
||||
import 'package:maibu_satabot_v2/core/consts/http_api_consts.dart';
|
||||
import '../../../device_list/data/models/robot_data_model.dart';
|
||||
import '../../../device_list/domain/entities/drone_station_entity.dart';
|
||||
|
||||
/// 上报页面 Cubit
|
||||
class ReportCubit extends Cubit<ReportState> {
|
||||
final SubmitReportUseCase submitReportUseCase;
|
||||
final Dio dio;
|
||||
final AppUserCubit appUserCubit;
|
||||
final UserStorage userStorage;
|
||||
|
||||
ReportCubit({required this.submitReportUseCase})
|
||||
: super(ReportFormState(report: ReportEntity()));
|
||||
ReportCubit({
|
||||
required this.submitReportUseCase,
|
||||
required this.dio,
|
||||
required this.appUserCubit,
|
||||
required this.userStorage,
|
||||
}) : super(ReportFormState(report: ReportEntity())) {
|
||||
_loadSiteList();
|
||||
}
|
||||
|
||||
Future<void> _loadSiteList() async {
|
||||
if (state is ReportFormState) {
|
||||
final currentState = state as ReportFormState;
|
||||
emit(currentState.copyWith(sites: []));
|
||||
|
||||
try {
|
||||
final siteDataSource = SiteDataSourceImpl(
|
||||
dio,
|
||||
userStorage,
|
||||
appUserCubit,
|
||||
);
|
||||
final siteRepository = SiteRepositoryImpl(siteDataSource);
|
||||
final getSiteListUseCase = GetSiteListUseCase(siteRepository);
|
||||
|
||||
final user = appUserCubit.state.user;
|
||||
if (user == null) {
|
||||
emit(currentState.copyWith(sites: []));
|
||||
return;
|
||||
}
|
||||
|
||||
final result = await getSiteListUseCase(user.userId);
|
||||
result.fold(
|
||||
(failure) {
|
||||
emit(currentState.copyWith(sites: []));
|
||||
},
|
||||
(sites) {
|
||||
SiteEntity? selectedSite;
|
||||
if (sites.isNotEmpty) {
|
||||
selectedSite = sites.first;
|
||||
}
|
||||
emit(
|
||||
currentState.copyWith(
|
||||
sites: sites,
|
||||
selectedSite: selectedSite,
|
||||
location: selectedSite?.siteName ?? '请选择场站',
|
||||
report: currentState.report.copyWith(
|
||||
siteId: selectedSite?.id,
|
||||
siteName: selectedSite?.siteName,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
emit(currentState.copyWith(sites: []));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> loadDeviceList(int siteId) async {
|
||||
if (state is ReportFormState) {
|
||||
final currentState = state as ReportFormState;
|
||||
emit(ReportDevicesLoading());
|
||||
|
||||
try {
|
||||
final robotResponse = await dio.get(
|
||||
HttpApiConsts.getRobotList,
|
||||
queryParameters: {'siteId': siteId, 'pageSize': 9999, 'pageNum': 1},
|
||||
);
|
||||
|
||||
final droneResponse = await dio.get(
|
||||
HttpApiConsts.getSiteUAVList,
|
||||
queryParameters: {'siteId': siteId},
|
||||
);
|
||||
|
||||
List<RobotDataModel> robots = [];
|
||||
List<DroneStationEntity> drones = [];
|
||||
|
||||
if (robotResponse.statusCode == 200 &&
|
||||
robotResponse.data['code'] == 200) {
|
||||
final List<dynamic> rows = robotResponse.data['rows'] ?? [];
|
||||
robots = rows.map((item) => RobotDataModel.fromJson(item)).toList();
|
||||
}
|
||||
|
||||
if (droneResponse.statusCode == 200 &&
|
||||
droneResponse.data['code'] == 200) {
|
||||
final List<dynamic> rows = droneResponse.data['rows'] ?? [];
|
||||
drones = rows
|
||||
.map((item) => DroneStationEntity.fromJson(item))
|
||||
.toList();
|
||||
}
|
||||
|
||||
emit(currentState.copyWith(robots: robots, drones: drones));
|
||||
} catch (e) {
|
||||
emit(currentState.copyWith(robots: [], drones: []));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 选择上报类型
|
||||
void selectReportType(ReportType type) {
|
||||
if (state is ReportFormState) {
|
||||
final currentState = state as ReportFormState;
|
||||
emit(
|
||||
currentState.copyWith(
|
||||
selectedReportType: type,
|
||||
report: currentState.report.copyWith(reportType: type.labelKey),
|
||||
report: currentState.report.copyWith(reportType: type.orderType),
|
||||
selectedDevice: null,
|
||||
selectedDeviceId: null,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 选择设备
|
||||
void selectDevice(String device) {
|
||||
void selectSite(SiteEntity site) {
|
||||
if (state is ReportFormState) {
|
||||
final currentState = state as ReportFormState;
|
||||
emit(
|
||||
currentState.copyWith(
|
||||
selectedDevice: device,
|
||||
report: currentState.report.copyWith(deviceId: device),
|
||||
selectedSite: site,
|
||||
location: site.siteName,
|
||||
report: currentState.report.copyWith(
|
||||
siteId: site.id,
|
||||
siteName: site.siteName,
|
||||
),
|
||||
selectedDevice: null,
|
||||
selectedDeviceId: null,
|
||||
robots: [],
|
||||
drones: [],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void selectDevice(String deviceName, String deviceId) {
|
||||
if (state is ReportFormState) {
|
||||
final currentState = state as ReportFormState;
|
||||
emit(
|
||||
currentState.copyWith(
|
||||
selectedDevice: deviceName,
|
||||
selectedDeviceId: deviceId,
|
||||
report: currentState.report.copyWith(
|
||||
deviceId: deviceId,
|
||||
deviceName: deviceName,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 更新位置
|
||||
void updateLocation(String location) {
|
||||
if (state is ReportFormState) {
|
||||
final currentState = state as ReportFormState;
|
||||
@@ -50,7 +181,6 @@ class ReportCubit extends Cubit<ReportState> {
|
||||
}
|
||||
}
|
||||
|
||||
/// 更新问题描述
|
||||
void updateDescription(String description) {
|
||||
if (state is ReportFormState) {
|
||||
final currentState = state as ReportFormState;
|
||||
@@ -62,20 +192,18 @@ class ReportCubit extends Cubit<ReportState> {
|
||||
}
|
||||
}
|
||||
|
||||
/// 选择问题等级
|
||||
void selectProblemLevel(ProblemLevel level) {
|
||||
if (state is ReportFormState) {
|
||||
final currentState = state as ReportFormState;
|
||||
emit(
|
||||
currentState.copyWith(
|
||||
selectedProblemLevel: level,
|
||||
report: currentState.report.copyWith(problemLevel: level.labelKey),
|
||||
report: currentState.report.copyWith(problemLevel: level.level),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 添加媒体文件
|
||||
void addMediaFile(String filePath) {
|
||||
if (state is ReportFormState) {
|
||||
final currentState = state as ReportFormState;
|
||||
@@ -90,7 +218,6 @@ class ReportCubit extends Cubit<ReportState> {
|
||||
}
|
||||
}
|
||||
|
||||
/// 删除媒体文件
|
||||
void removeMediaFile(int index) {
|
||||
if (state is ReportFormState) {
|
||||
final currentState = state as ReportFormState;
|
||||
@@ -105,43 +232,68 @@ class ReportCubit extends Cubit<ReportState> {
|
||||
}
|
||||
}
|
||||
|
||||
/// 提交上报
|
||||
Future<void> submitReport() async {
|
||||
if (state is! ReportFormState) return;
|
||||
|
||||
final currentState = state as ReportFormState;
|
||||
|
||||
// 验证必填项
|
||||
if (currentState.selectedSite == null) {
|
||||
emit(currentState.copyWith(errorMessage: '请选择场站'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentState.selectedReportType == null) {
|
||||
emit(currentState.copyWith(errorMessage: '请选择上报类型'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentState.selectedDevice == null ||
|
||||
currentState.selectedDevice!.isEmpty) {
|
||||
emit(const ReportFailure('请选择设备'));
|
||||
emit(currentState.copyWith(errorMessage: '请选择设备'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentState.report.description == null ||
|
||||
currentState.report.description!.isEmpty) {
|
||||
emit(const ReportFailure('请填写问题描述'));
|
||||
emit(currentState.copyWith(errorMessage: '请填写问题描述'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentState.selectedProblemLevel == null) {
|
||||
emit(const ReportFailure('请选择问题等级'));
|
||||
emit(currentState.copyWith(errorMessage: '请选择问题等级'));
|
||||
return;
|
||||
}
|
||||
|
||||
// 开始提交
|
||||
emit(ReportSubmitting());
|
||||
|
||||
final result = await submitReportUseCase.execute(currentState.report);
|
||||
|
||||
result.fold(
|
||||
(failure) => emit(ReportFailure(failure.message)),
|
||||
(success) => emit(ReportSuccess()),
|
||||
(failure) => emit(currentState.copyWith(errorMessage: failure.message)),
|
||||
(success) {
|
||||
// 提交成功:清空表单,保留场站列表
|
||||
emit(currentState.copyWith(
|
||||
report: ReportEntity(),
|
||||
selectedReportType: null,
|
||||
selectedProblemLevel: null,
|
||||
selectedDevice: null,
|
||||
selectedDeviceId: null,
|
||||
mediaFiles: [],
|
||||
successMessage: '提交成功',
|
||||
));
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// 重置表单
|
||||
void clearMessages() {
|
||||
if (state is ReportFormState) {
|
||||
final s = state as ReportFormState;
|
||||
emit(s.copyWith(errorMessage: null, successMessage: null));
|
||||
}
|
||||
}
|
||||
|
||||
void resetForm() {
|
||||
emit(ReportFormState(report: ReportEntity()));
|
||||
_loadSiteList();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user