주요 변경사항: - 창고 관리 API 응답 구조와 DTO 불일치 수정 - WarehouseLocationDto에 code, manager_phone 필드 추가 - RemoteDataSource에서 API 응답을 DTO 구조에 맞게 변환 - 회사 관리 API 응답 파싱 오류 수정 - CompanyResponse의 필수 필드를 nullable로 변경 - PaginatedResponse 구조 매핑 로직 개선 - 에러 처리 및 로깅 개선 - Service Layer에 상세 에러 로깅 추가 - Controller에서 에러 타입별 처리 - 새로운 유틸리티 추가 - ResponseInterceptor: API 응답 정규화 - DebugLogger: 디버깅 도구 - HealthCheckService: 서버 상태 확인 - 문서화 - API 통합 테스트 가이드 - 에러 분석 보고서 - 리팩토링 계획서
195 lines
6.5 KiB
Dart
195 lines
6.5 KiB
Dart
import 'package:get_it/get_it.dart';
|
|
import 'package:injectable/injectable.dart';
|
|
import 'package:superport/core/errors/exceptions.dart';
|
|
import 'package:superport/core/errors/failures.dart';
|
|
import 'package:superport/data/datasources/remote/warehouse_remote_datasource.dart';
|
|
import 'package:superport/data/models/warehouse/warehouse_dto.dart';
|
|
import 'package:superport/models/address_model.dart';
|
|
import 'package:superport/models/warehouse_location_model.dart';
|
|
|
|
@lazySingleton
|
|
class WarehouseService {
|
|
final WarehouseRemoteDataSource _remoteDataSource = GetIt.instance<WarehouseRemoteDataSource>();
|
|
|
|
// 창고 위치 목록 조회
|
|
Future<List<WarehouseLocation>> getWarehouseLocations({
|
|
int page = 1,
|
|
int perPage = 20,
|
|
bool? isActive,
|
|
}) async {
|
|
try {
|
|
final response = await _remoteDataSource.getWarehouseLocations(
|
|
page: page,
|
|
perPage: perPage,
|
|
isActive: isActive,
|
|
);
|
|
|
|
return response.items.map((dto) => _convertDtoToWarehouseLocation(dto)).toList();
|
|
} on ApiException catch (e) {
|
|
print('[WarehouseService] ApiException: ${e.message}');
|
|
throw ServerFailure(message: e.message);
|
|
} catch (e, stackTrace) {
|
|
print('[WarehouseService] Error loading warehouse locations: $e');
|
|
print('[WarehouseService] Stack trace: $stackTrace');
|
|
throw ServerFailure(message: '창고 위치 목록을 불러오는 데 실패했습니다: $e');
|
|
}
|
|
}
|
|
|
|
// 창고 위치 상세 조회
|
|
Future<WarehouseLocation> getWarehouseLocationById(int id) async {
|
|
try {
|
|
final dto = await _remoteDataSource.getWarehouseLocationById(id);
|
|
return _convertDtoToWarehouseLocation(dto);
|
|
} on ApiException catch (e) {
|
|
throw ServerFailure(message: e.message);
|
|
} catch (e) {
|
|
throw ServerFailure(message: '창고 위치 정보를 불러오는 데 실패했습니다: $e');
|
|
}
|
|
}
|
|
|
|
// 창고 위치 생성
|
|
Future<WarehouseLocation> createWarehouseLocation(WarehouseLocation location) async {
|
|
try {
|
|
final request = CreateWarehouseLocationRequest(
|
|
name: location.name,
|
|
address: location.address.detailAddress,
|
|
city: location.address.region,
|
|
postalCode: location.address.zipCode,
|
|
country: 'KR', // 기본값
|
|
);
|
|
|
|
final dto = await _remoteDataSource.createWarehouseLocation(request);
|
|
return _convertDtoToWarehouseLocation(dto);
|
|
} on ApiException catch (e) {
|
|
throw ServerFailure(message: e.message);
|
|
} catch (e) {
|
|
throw ServerFailure(message: '창고 위치 생성에 실패했습니다: $e');
|
|
}
|
|
}
|
|
|
|
// 창고 위치 수정
|
|
Future<WarehouseLocation> updateWarehouseLocation(WarehouseLocation location) async {
|
|
try {
|
|
final request = UpdateWarehouseLocationRequest(
|
|
name: location.name,
|
|
address: location.address.detailAddress,
|
|
city: location.address.region,
|
|
postalCode: location.address.zipCode,
|
|
);
|
|
|
|
final dto = await _remoteDataSource.updateWarehouseLocation(location.id, request);
|
|
return _convertDtoToWarehouseLocation(dto);
|
|
} on ApiException catch (e) {
|
|
throw ServerFailure(message: e.message);
|
|
} catch (e) {
|
|
throw ServerFailure(message: '창고 위치 수정에 실패했습니다: $e');
|
|
}
|
|
}
|
|
|
|
// 창고 위치 삭제
|
|
Future<void> deleteWarehouseLocation(int id) async {
|
|
try {
|
|
await _remoteDataSource.deleteWarehouseLocation(id);
|
|
} on ApiException catch (e) {
|
|
throw ServerFailure(message: e.message);
|
|
} catch (e) {
|
|
throw ServerFailure(message: '창고 위치 삭제에 실패했습니다: $e');
|
|
}
|
|
}
|
|
|
|
// 창고별 장비 목록 조회
|
|
Future<List<Map<String, dynamic>>> getWarehouseEquipment(
|
|
int warehouseId, {
|
|
int page = 1,
|
|
int perPage = 20,
|
|
}) async {
|
|
try {
|
|
final response = await _remoteDataSource.getWarehouseEquipment(
|
|
warehouseId,
|
|
page: page,
|
|
perPage: perPage,
|
|
);
|
|
|
|
return response.items.map((dto) => {
|
|
'id': dto.id,
|
|
'equipmentNumber': dto.equipmentNumber,
|
|
'manufacturer': dto.manufacturer,
|
|
'equipmentName': dto.equipmentName,
|
|
'serialNumber': dto.serialNumber,
|
|
'quantity': dto.quantity,
|
|
'status': dto.status,
|
|
'storedAt': dto.storedAt,
|
|
}).toList();
|
|
} on ApiException catch (e) {
|
|
throw ServerFailure(message: e.message);
|
|
} catch (e) {
|
|
throw ServerFailure(message: '창고 장비 목록을 불러오는 데 실패했습니다: $e');
|
|
}
|
|
}
|
|
|
|
// 창고 용량 정보 조회
|
|
Future<WarehouseCapacityInfo> getWarehouseCapacity(int id) async {
|
|
try {
|
|
return await _remoteDataSource.getWarehouseCapacity(id);
|
|
} on ApiException catch (e) {
|
|
throw ServerFailure(message: e.message);
|
|
} catch (e) {
|
|
throw ServerFailure(message: '창고 용량 정보를 불러오는 데 실패했습니다: $e');
|
|
}
|
|
}
|
|
|
|
// 사용 중인 창고 위치 목록 조회
|
|
Future<List<WarehouseLocation>> getInUseWarehouseLocations() async {
|
|
try {
|
|
final dtos = await _remoteDataSource.getInUseWarehouseLocations();
|
|
return dtos.map((dto) => _convertDtoToWarehouseLocation(dto)).toList();
|
|
} on ApiException catch (e) {
|
|
throw ServerFailure(message: e.message);
|
|
} catch (e) {
|
|
throw ServerFailure(message: '사용 중인 창고 위치를 불러오는 데 실패했습니다: $e');
|
|
}
|
|
}
|
|
|
|
// DTO를 Flutter 모델로 변환
|
|
WarehouseLocation _convertDtoToWarehouseLocation(WarehouseLocationDto dto) {
|
|
// API에 주소 정보가 없으므로 기본값 사용
|
|
final address = Address(
|
|
zipCode: dto.postalCode ?? '',
|
|
region: dto.city ?? '',
|
|
detailAddress: dto.address ?? '주소 정보 없음',
|
|
);
|
|
|
|
// 담당자 정보 조합
|
|
final remarkParts = <String>[];
|
|
if (dto.code != null) {
|
|
remarkParts.add('코드: ${dto.code}');
|
|
}
|
|
if (dto.managerName != null) {
|
|
remarkParts.add('담당자: ${dto.managerName}');
|
|
}
|
|
if (dto.managerPhone != null) {
|
|
remarkParts.add('연락처: ${dto.managerPhone}');
|
|
}
|
|
|
|
return WarehouseLocation(
|
|
id: dto.id,
|
|
name: dto.name,
|
|
address: address,
|
|
remark: remarkParts.isNotEmpty ? remarkParts.join(', ') : null,
|
|
);
|
|
}
|
|
|
|
// 페이지네이션 정보
|
|
Future<int> getTotalWarehouseLocations({bool? isActive}) async {
|
|
try {
|
|
final response = await _remoteDataSource.getWarehouseLocations(
|
|
page: 1,
|
|
perPage: 1,
|
|
isActive: isActive,
|
|
);
|
|
return response.total;
|
|
} catch (e) {
|
|
return 0;
|
|
}
|
|
}
|
|
} |