170 lines
5.6 KiB
Dart
170 lines
5.6 KiB
Dart
import '../../data/models/maintenance_dto.dart';
|
|
import '../../data/repositories/maintenance_repository.dart';
|
|
import '../../utils/constants.dart';
|
|
|
|
/// 유지보수 UseCase (백엔드 스키마 기반)
|
|
/// 백엔드 API와 100% 호환되는 단순한 CRUD 작업만 제공
|
|
class MaintenanceUseCase {
|
|
final MaintenanceRepository _repository;
|
|
|
|
MaintenanceUseCase({required MaintenanceRepository repository})
|
|
: _repository = repository;
|
|
|
|
// 유지보수 목록 조회 (백엔드 스키마 기반)
|
|
Future<MaintenanceListResponse> getMaintenances({
|
|
int page = 1,
|
|
int pageSize = PaginationConstants.defaultPageSize,
|
|
String? sortBy,
|
|
String? sortOrder,
|
|
String? search,
|
|
int? equipmentHistoryId,
|
|
String? maintenanceType,
|
|
}) async {
|
|
return await _repository.getMaintenances(
|
|
page: page,
|
|
pageSize: pageSize,
|
|
sortBy: sortBy,
|
|
sortOrder: sortOrder,
|
|
search: search,
|
|
equipmentHistoryId: equipmentHistoryId,
|
|
maintenanceType: maintenanceType,
|
|
);
|
|
}
|
|
|
|
// 특정 유지보수 조회
|
|
Future<MaintenanceDto> getMaintenance(int id) async {
|
|
return await _repository.getMaintenance(id);
|
|
}
|
|
|
|
// 장비 이력별 유지보수 조회
|
|
Future<List<MaintenanceDto>> getMaintenancesByEquipmentHistory(
|
|
int equipmentHistoryId) async {
|
|
return await _repository.getMaintenancesByEquipmentHistory(equipmentHistoryId);
|
|
}
|
|
|
|
// 유지보수 생성 (백엔드 스키마 기반)
|
|
Future<MaintenanceDto> createMaintenance(MaintenanceRequestDto request) async {
|
|
// 기본 검증만
|
|
_validateMaintenanceRequest(request);
|
|
|
|
return await _repository.createMaintenance(request);
|
|
}
|
|
|
|
// 유지보수 수정 (백엔드 스키마 기반)
|
|
Future<MaintenanceDto> updateMaintenance(
|
|
int id, MaintenanceUpdateRequestDto request) async {
|
|
// 기본 검증만
|
|
if (request.periodMonth != null && request.periodMonth! <= 0) {
|
|
throw Exception('유지보수 주기는 1개월 이상이어야 합니다.');
|
|
}
|
|
|
|
// 날짜 검증
|
|
if (request.startedAt != null && request.endedAt != null) {
|
|
if (request.endedAt!.isBefore(request.startedAt!)) {
|
|
throw Exception('종료일은 시작일 이후여야 합니다.');
|
|
}
|
|
}
|
|
|
|
return await _repository.updateMaintenance(id, request);
|
|
}
|
|
|
|
// 유지보수 삭제 (백엔드 soft delete)
|
|
Future<void> deleteMaintenance(int id) async {
|
|
// 기본 검증만
|
|
final maintenance = await _repository.getMaintenance(id);
|
|
|
|
// 진행 중인 유지보수는 삭제 불가 (선택적)
|
|
final now = DateTime.now();
|
|
|
|
if (now.isAfter(maintenance.startedAt) && now.isBefore(maintenance.endedAt)) {
|
|
throw Exception('진행 중인 유지보수는 삭제할 수 없습니다.');
|
|
}
|
|
|
|
await _repository.deleteMaintenance(id);
|
|
}
|
|
|
|
// 활성 유지보수만 조회 (백엔드 is_deleted = false)
|
|
Future<List<MaintenanceDto>> getActiveMaintenances({
|
|
int? equipmentHistoryId,
|
|
String? maintenanceType,
|
|
}) async {
|
|
final response = await getMaintenances(
|
|
pageSize: 1000, // 큰 사이즈로 모든 데이터 조회
|
|
equipmentHistoryId: equipmentHistoryId,
|
|
maintenanceType: maintenanceType,
|
|
);
|
|
|
|
// is_deleted = false인 것만 필터링
|
|
return response.items.where((m) => m.isActive).toList();
|
|
}
|
|
|
|
// 특정 기간 내 유지보수 조회 (백엔드 날짜 필터링)
|
|
Future<List<MaintenanceDto>> getMaintenancesByDateRange({
|
|
required DateTime startDate,
|
|
required DateTime endDate,
|
|
int? equipmentHistoryId,
|
|
}) async {
|
|
final response = await getMaintenances(
|
|
pageSize: 1000,
|
|
equipmentHistoryId: equipmentHistoryId,
|
|
);
|
|
|
|
// 날짜 범위 필터링 (클라이언트 사이드)
|
|
return response.items.where((maintenance) {
|
|
return maintenance.startedAt.isAfter(startDate) &&
|
|
maintenance.endedAt.isBefore(endDate);
|
|
}).toList();
|
|
}
|
|
|
|
// Private 헬퍼 메서드
|
|
void _validateMaintenanceRequest(MaintenanceRequestDto request) {
|
|
// 필수 필드 검증 (백엔드 스키마 기반)
|
|
if (request.periodMonth <= 0) {
|
|
throw Exception('유지보수 주기는 1개월 이상이어야 합니다.');
|
|
}
|
|
|
|
// 날짜 검증
|
|
if (request.endedAt.isBefore(request.startedAt)) {
|
|
throw Exception('종료일은 시작일 이후여야 합니다.');
|
|
}
|
|
|
|
// 유지보수 타입 검증 (백엔드 값)
|
|
if (request.maintenanceType != MaintenanceType.onsite &&
|
|
request.maintenanceType != MaintenanceType.remote) {
|
|
throw Exception('유지보수 타입은 방문(O) 또는 원격(R)이어야 합니다.');
|
|
}
|
|
}
|
|
|
|
// 통계 조회 (단순화)
|
|
Future<MaintenanceStatistics> getMaintenanceStatistics() async {
|
|
final response = await getMaintenances(pageSize: 1000);
|
|
final maintenances = response.items;
|
|
|
|
int totalCount = maintenances.length;
|
|
int activeCount = maintenances.where((m) => m.isActive).length;
|
|
int onsiteCount = maintenances.where((m) => m.maintenanceType == MaintenanceType.onsite).length;
|
|
int remoteCount = maintenances.where((m) => m.maintenanceType == MaintenanceType.remote).length;
|
|
|
|
return MaintenanceStatistics(
|
|
totalCount: totalCount,
|
|
activeCount: activeCount,
|
|
onsiteCount: onsiteCount,
|
|
remoteCount: remoteCount,
|
|
);
|
|
}
|
|
}
|
|
|
|
/// 유지보수 통계 모델 (단순화)
|
|
class MaintenanceStatistics {
|
|
final int totalCount;
|
|
final int activeCount;
|
|
final int onsiteCount;
|
|
final int remoteCount;
|
|
|
|
MaintenanceStatistics({
|
|
required this.totalCount,
|
|
required this.activeCount,
|
|
required this.onsiteCount,
|
|
required this.remoteCount,
|
|
});
|
|
} |