对接连续任务列表接口和连续任务下发,便捷操作中暂停任务的对接,底部导航tab项的设置根据用户角色显示和设置

This commit is contained in:
2026-11-11 16:51:21 +08:00
parent 47f518f94c
commit 985bcf4b55
54 changed files with 5017 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
import 'dart:convert';
import 'package:dio/dio.dart';
import '../../../../core/consts/http_api_consts.dart';
import '../../../../core/logging/i_logger_service.dart';
import '../../domain/entities/work_history_record.dart';
import '../models/work_history_model.dart';
class WorkHistoryRemoteDatasource {
WorkHistoryRemoteDatasource(this.dio, this.logger);
final Dio dio;
final ILoggerService logger;
Future<WorkHistoryPage> load({
required int siteId,
required int orgId,
required int pageNum,
required int pageSize,
}) async {
final body = {
'siteId': siteId,
'orgId': orgId,
'pageNum': pageNum,
'pageSize': pageSize,
};
final url = HttpApiConsts.siteDeviceTaskHistory;
final clock = Stopwatch()..start();
logger.logWithLevel(
'[作业记录] 请求: POST $url, 参数: ${jsonEncode(body)}',
shouldLog: true,
);
try {
final response = await dio.post(url, data: body);
final data = response.data;
if (response.statusCode != 200 ||
data is! Map ||
'${data['code']}' != '200') {
throw Exception(data is Map ? data['msg'] ?? '作业记录查询失败' : '作业记录响应格式错误');
}
final rows = data['rows'];
final total = int.tryParse('${data['total']}');
if (rows is! List || total == null || total < 0) {
throw const FormatException('作业记录响应缺少有效 rows 或 total');
}
final records = <int, WorkHistoryRecord>{};
for (final row in rows) {
if (row is! Map) throw const FormatException('作业记录行格式错误');
if (row['delFlag'] == true ||
row['delFlag'] == 1 ||
row['delFlag'] == '1')
continue;
final record = WorkHistoryModel.fromJson(
Map<String, dynamic>.from(row),
);
if (record.siteId != null && record.siteId != siteId) continue;
if (record.orgId != null && record.orgId != orgId) continue;
records[record.id] = record;
}
final sorted = records.values.toList()
..sort((a, b) {
final dateOrder = (b.recordDate ?? DateTime(1970)).compareTo(
a.recordDate ?? DateTime(1970),
);
return dateOrder != 0 ? dateOrder : b.id.compareTo(a.id);
});
logger.logWithLevel(
'[作业记录] 响应: HTTP ${response.statusCode}, code=${data['code']}, msg=${data['msg']}, total=$total, rows=${rows.length}, 当前场站有效记录=${sorted.length}, 耗时=${clock.elapsedMilliseconds}ms',
shouldLog: true,
);
return WorkHistoryPage(total: total, records: sorted);
} catch (error) {
logger.logWithLevel(
'[作业记录] 异常: $error, 参数: ${jsonEncode(body)}',
shouldLog: true,
);
rethrow;
} finally {
clock.stop();
}
}
}

View File

@@ -0,0 +1,44 @@
import '../../domain/entities/work_history_record.dart';
class WorkHistoryModel {
static int? _int(dynamic value) => int.tryParse('$value');
static String? _text(dynamic value) {
final text = value?.toString().trim();
return text == null || text.isEmpty ? null : text;
}
static WorkHistoryRecord fromJson(Map<String, dynamic> json) {
final id = _int(json['id']);
if (id == null || id <= 0) throw const FormatException('作业历史缺少有效任务 ID');
final accepted = json['taskAccepted'];
return WorkHistoryRecord(
id: id,
deviceId: _text(json['deviceId']) ?? '',
deviceAlias: _text(json['deviceAlias']),
workName: _text(json['workName']) ?? '',
// 后端原字段为 taskStaus(缺少 t),优先按真实接口解析。
status: _text(json['taskStaus'] ?? json['taskStatus']) ?? '',
statusLabel:
_text(json['taskStausTranslate'] ?? json['taskStatusTranslate']) ??
'',
siteId: _int(json['siteId']),
orgId: _int(json['orgId']),
planId: _int(json['planId']),
routeId: _int(json['routeId']),
createdAt: _text(json['createTime']),
updatedAt: _text(json['updateTime']),
startedAt: _text(json['startTime']),
finishedAt: _text(json['finishTime']),
taskType: _text(json['taskType']),
routeType: _text(json['routeType']),
remark: _text(json['remark']),
accepted: accepted is bool
? accepted
: accepted == 1 || accepted == '1'
? true
: accepted == 0 || accepted == '0'
? false
: null,
);
}
}

View File

@@ -0,0 +1,31 @@
import 'package:fpdart/fpdart.dart';
import '../../../../core/error/failure.dart';
import '../../domain/entities/work_history_record.dart';
import '../../domain/repositories/work_history_repository.dart';
import '../datasources/work_history_remote_datasource.dart';
class WorkHistoryRepositoryImpl implements WorkHistoryRepository {
WorkHistoryRepositoryImpl(this.remote);
final WorkHistoryRemoteDatasource remote;
@override
Future<Either<Failure, WorkHistoryPage>> load({
required int siteId,
required int orgId,
required int pageNum,
required int pageSize,
}) async {
try {
return Right(
await remote.load(
siteId: siteId,
orgId: orgId,
pageNum: pageNum,
pageSize: pageSize,
),
);
} catch (error) {
return Left(Failure(error.toString()));
}
}
}

View File

@@ -0,0 +1,56 @@
class WorkHistoryRecord {
const WorkHistoryRecord({
required this.id,
required this.deviceId,
required this.workName,
required this.status,
required this.statusLabel,
this.deviceAlias,
this.siteId,
this.orgId,
this.planId,
this.routeId,
this.createdAt,
this.updatedAt,
this.startedAt,
this.finishedAt,
this.taskType,
this.routeType,
this.remark,
this.accepted,
});
final int id;
final String deviceId;
final String? deviceAlias;
final String workName;
final String status;
final String statusLabel;
final int? siteId;
final int? orgId;
final int? planId;
final int? routeId;
final String? createdAt;
final String? updatedAt;
final String? startedAt;
final String? finishedAt;
final String? taskType;
final String? routeType;
final String? remark;
final bool? accepted;
String get normalizedStatus => status.trim().toUpperCase();
bool get isCompleted => normalizedStatus == 'FINISH';
bool get isCanceled =>
const {'CANCELED', 'CANCELLED'}.contains(normalizedStatus);
DateTime? get recordDate => DateTime.tryParse(startedAt ?? createdAt ?? '');
}
class WorkHistoryPage {
WorkHistoryPage({
required this.total,
required List<WorkHistoryRecord> records,
}) : records = List.unmodifiable(records);
final int total;
final List<WorkHistoryRecord> records;
}

View File

@@ -0,0 +1,12 @@
import 'package:fpdart/fpdart.dart';
import '../../../../core/error/failure.dart';
import '../entities/work_history_record.dart';
abstract class WorkHistoryRepository {
Future<Either<Failure, WorkHistoryPage>> load({
required int siteId,
required int orgId,
required int pageNum,
required int pageSize,
});
}

View File

@@ -0,0 +1,87 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../domain/entities/work_history_record.dart';
import '../../domain/usecases/load_site_work_history_usecase.dart';
class WorkHistoryState {
const WorkHistoryState({
this.loading = false,
this.loaded = false,
this.total = 0,
this.records = const [],
this.error,
});
final bool loading;
final bool loaded;
final int total;
final List<WorkHistoryRecord> records;
final String? error;
}
/// 仅查询历史;不依赖任务池,也不下发或变更机器人任务。
class WorkHistoryCubit extends Cubit<WorkHistoryState> {
WorkHistoryCubit(this._load) : super(const WorkHistoryState());
final LoadSiteWorkHistoryUseCase _load;
(int, int)? _scope;
int _generation = 0;
Future<void> load({required int? siteId, required int? orgId}) async {
if (isClosed) return;
if (siteId == null || orgId == null || siteId <= 0 || orgId <= 0) {
++_generation;
_scope = null;
emit(const WorkHistoryState(error: '请先在首页选择有效场站'));
return;
}
final scope = (siteId, orgId);
if (_scope == scope && state.loading) return;
final keep = _scope == scope && state.loaded;
_scope = scope;
final generation = ++_generation;
emit(
WorkHistoryState(
loading: true,
loaded: keep,
records: keep ? state.records : const [],
total: keep ? state.total : 0,
),
);
try {
final result = await _load(siteId: siteId, orgId: orgId);
if (isClosed || generation != _generation) return;
result.fold(
(failure) => emit(
WorkHistoryState(
loaded: keep,
records: keep ? state.records : const [],
total: keep ? state.total : 0,
error: failure.message,
),
),
(page) => emit(
WorkHistoryState(
loaded: true,
total: page.total,
records: page.records,
),
),
);
} catch (error) {
if (!isClosed && generation == _generation) {
emit(
WorkHistoryState(
loaded: keep,
records: keep ? state.records : const [],
total: keep ? state.total : 0,
error: error.toString(),
),
);
}
}
}
@override
Future<void> close() {
++_generation;
return super.close();
}
}