320 lines
8.2 KiB
Dart
320 lines
8.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:injectable/injectable.dart';
|
|
|
|
import '../../../data/models/rent_dto.dart';
|
|
import '../../../domain/usecases/rent_usecase.dart';
|
|
|
|
@injectable
|
|
class RentController with ChangeNotifier {
|
|
final RentUseCase _rentUseCase;
|
|
|
|
// 상태 관리 (단순화)
|
|
bool _isLoading = false;
|
|
String? _error;
|
|
List<RentDto> _rents = []; // 단순한 List 구조
|
|
RentDto? _selectedRent;
|
|
|
|
// 필터링 상태
|
|
String? _selectedStatus;
|
|
int? _selectedEquipmentHistoryId;
|
|
|
|
RentController(this._rentUseCase);
|
|
|
|
// Getters (단순화)
|
|
bool get isLoading => _isLoading;
|
|
String? get error => _error;
|
|
bool get hasError => _error != null;
|
|
RentDto? get selectedRent => _selectedRent;
|
|
String? get selectedStatus => _selectedStatus;
|
|
int? get selectedEquipmentHistoryId => _selectedEquipmentHistoryId;
|
|
|
|
// 편의 메서드 (백엔드 실제 구조)
|
|
List<RentDto> get rents => _rents;
|
|
int get totalRents => _rents.length;
|
|
|
|
// 페이징 관련 getter (UI 호환성)
|
|
int get currentPage => 1; // 단순화된 페이징 구조
|
|
int get totalPages => (_rents.length / 10).ceil();
|
|
int get totalItems => _rents.length;
|
|
|
|
// Dashboard 관련 getter
|
|
Map<String, int> get rentStats => getRentStats();
|
|
List<RentDto> get activeRents => getActiveRents();
|
|
List<RentDto> get overdueRents => getOverdueRents();
|
|
|
|
void _setLoading(bool loading) {
|
|
_isLoading = loading;
|
|
notifyListeners();
|
|
}
|
|
|
|
void _setError(String? error) {
|
|
_error = error;
|
|
notifyListeners();
|
|
}
|
|
|
|
void clearError() {
|
|
_error = null;
|
|
notifyListeners();
|
|
}
|
|
|
|
/// 임대 목록 조회
|
|
Future<void> loadRents({
|
|
int page = 1,
|
|
int pageSize = 10,
|
|
String? search,
|
|
bool refresh = false,
|
|
}) async {
|
|
try {
|
|
if (refresh) {
|
|
_rents.clear();
|
|
notifyListeners();
|
|
}
|
|
|
|
_setLoading(true);
|
|
|
|
final response = await _rentUseCase.getRents(
|
|
page: page,
|
|
pageSize: pageSize,
|
|
search: search,
|
|
// status: _selectedStatus, // 삭제된 파라미터
|
|
equipmentHistoryId: _selectedEquipmentHistoryId,
|
|
);
|
|
|
|
// response를 List<RentDto>로 캐스팅
|
|
_rents = response as List<RentDto>;
|
|
|
|
clearError();
|
|
} catch (e) {
|
|
_setError('임대 목록을 불러오는데 실패했습니다: $e');
|
|
} finally {
|
|
_setLoading(false);
|
|
}
|
|
}
|
|
|
|
/// 임대 상세 조회
|
|
Future<void> loadRent(int id) async {
|
|
try {
|
|
_setLoading(true);
|
|
|
|
final response = await _rentUseCase.getRent(id);
|
|
_selectedRent = response; // response가 직접 RentDto
|
|
|
|
clearError();
|
|
} catch (e) {
|
|
_setError('임대 상세 정보를 불러오는데 실패했습니다: $e');
|
|
} finally {
|
|
_setLoading(false);
|
|
}
|
|
}
|
|
|
|
/// 임대 생성 (백엔드 실제 스키마)
|
|
Future<bool> createRent({
|
|
required int equipmentHistoryId,
|
|
required DateTime startedAt,
|
|
required DateTime endedAt,
|
|
}) async {
|
|
try {
|
|
_setLoading(true);
|
|
|
|
await _rentUseCase.createRent(
|
|
RentRequestDto(
|
|
equipmentHistoryId: equipmentHistoryId,
|
|
startedAt: startedAt,
|
|
endedAt: endedAt,
|
|
),
|
|
);
|
|
|
|
// 목록 새로고침
|
|
await loadRents(refresh: true);
|
|
|
|
clearError();
|
|
return true;
|
|
} catch (e) {
|
|
_setError('임대 생성에 실패했습니다: $e');
|
|
return false;
|
|
} finally {
|
|
_setLoading(false);
|
|
}
|
|
}
|
|
|
|
/// 임대 수정 (백엔드 실제 스키마)
|
|
Future<bool> updateRent({
|
|
required int id,
|
|
DateTime? startedAt,
|
|
DateTime? endedAt,
|
|
}) async {
|
|
try {
|
|
_setLoading(true);
|
|
|
|
await _rentUseCase.updateRent(
|
|
id,
|
|
RentUpdateRequestDto(
|
|
startedAt: startedAt,
|
|
endedAt: endedAt,
|
|
),
|
|
);
|
|
|
|
// 목록 새로고침
|
|
await loadRents(refresh: true);
|
|
|
|
clearError();
|
|
return true;
|
|
} catch (e) {
|
|
_setError('임대 수정에 실패했습니다: $e');
|
|
return false;
|
|
} finally {
|
|
_setLoading(false);
|
|
}
|
|
}
|
|
|
|
/// 임대 삭제
|
|
Future<bool> deleteRent(int id) async {
|
|
try {
|
|
_setLoading(true);
|
|
|
|
await _rentUseCase.deleteRent(id);
|
|
|
|
// 목록 새로고침
|
|
await loadRents(refresh: true);
|
|
|
|
clearError();
|
|
return true;
|
|
} catch (e) {
|
|
_setError('임대 삭제에 실패했습니다: $e');
|
|
return false;
|
|
} finally {
|
|
_setLoading(false);
|
|
}
|
|
}
|
|
|
|
// 진행 중인 임대 간단 통계 (백엔드 데이터 기반)
|
|
List<RentDto> getActiveRents() {
|
|
final now = DateTime.now();
|
|
return _rents.where((rent) =>
|
|
rent.startedAt.isBefore(now) && rent.endedAt.isAfter(now)
|
|
).toList();
|
|
}
|
|
|
|
// 연체된 임대 간단 통계 (백엔드 데이터 기반)
|
|
List<RentDto> getOverdueRents() {
|
|
final now = DateTime.now();
|
|
return _rents.where((rent) => rent.endedAt.isBefore(now)).toList();
|
|
}
|
|
|
|
// 간단한 통계 (백엔드 데이터 기반)
|
|
Map<String, int> getRentStats() {
|
|
final now = DateTime.now();
|
|
return {
|
|
'total': _rents.length,
|
|
'active': _rents.where((r) => r.startedAt.isBefore(now) && r.endedAt.isAfter(now)).length,
|
|
'overdue': _rents.where((r) => r.endedAt.isBefore(now)).length,
|
|
'upcoming': _rents.where((r) => r.startedAt.isAfter(now)).length,
|
|
};
|
|
}
|
|
|
|
// 백엔드에서 반납/연장 처리는 endedAt 수정으로 처리
|
|
Future<bool> updateRentEndDate(int id, DateTime newEndDate) async {
|
|
return await updateRent(
|
|
id: id,
|
|
endedAt: newEndDate,
|
|
);
|
|
}
|
|
|
|
/// 상태 필터 설정
|
|
void setStatusFilter(String? status) {
|
|
_selectedStatus = status;
|
|
notifyListeners();
|
|
}
|
|
|
|
/// 장비 이력 필터 설정
|
|
void setEquipmentHistoryFilter(int? equipmentHistoryId) {
|
|
_selectedEquipmentHistoryId = equipmentHistoryId;
|
|
notifyListeners();
|
|
}
|
|
|
|
/// 필터 초기화
|
|
void clearFilters() {
|
|
_selectedStatus = null;
|
|
_selectedEquipmentHistoryId = null;
|
|
notifyListeners();
|
|
}
|
|
|
|
/// 선택된 임대 초기화
|
|
void clearSelectedRent() {
|
|
_selectedRent = null;
|
|
notifyListeners();
|
|
}
|
|
|
|
// 간단한 기간 계산 (클라이언트 사이드)
|
|
int calculateRentDays(DateTime startDate, DateTime endDate) {
|
|
return endDate.difference(startDate).inDays;
|
|
}
|
|
|
|
// 임대 상태 간단 판단
|
|
String getRentStatus(RentDto rent) {
|
|
final now = DateTime.now();
|
|
if (rent.startedAt.isAfter(now)) return '예약';
|
|
if (rent.endedAt.isBefore(now)) return '종료';
|
|
return '진행중';
|
|
}
|
|
|
|
// UI 호환성을 위한 상태 표시명
|
|
String getRentStatusDisplayName(String status) {
|
|
switch (status.toLowerCase()) {
|
|
case '예약':
|
|
case 'reserved':
|
|
return '예약';
|
|
case '진행중':
|
|
case 'active':
|
|
return '진행중';
|
|
case '종료':
|
|
case 'completed':
|
|
return '종료';
|
|
default:
|
|
return status;
|
|
}
|
|
}
|
|
|
|
/// 새로고침
|
|
Future<void> refresh() async {
|
|
await loadRents(refresh: true);
|
|
}
|
|
|
|
/// 임대 반납 처리 (endedAt를 현재 시간으로 수정)
|
|
Future<bool> returnRent(int id) async {
|
|
return await updateRent(
|
|
id: id,
|
|
endedAt: DateTime.now(),
|
|
);
|
|
}
|
|
|
|
/// 임대 총 비용 계산 (문자열 날짜 기반)
|
|
double calculateTotalRent(String startDate, String endDate, double dailyRate) {
|
|
try {
|
|
final start = DateTime.parse(startDate);
|
|
final end = DateTime.parse(endDate);
|
|
final days = end.difference(start).inDays;
|
|
return days * dailyRate;
|
|
} catch (e) {
|
|
return 0.0;
|
|
}
|
|
}
|
|
|
|
/// Dashboard용 통계 로드 (기존 데이터 기반)
|
|
Future<void> loadRentStats() async {
|
|
// 현재 로드된 데이터 기반으로 통계 계산 (별도 API 호출 없음)
|
|
notifyListeners();
|
|
}
|
|
|
|
/// Dashboard용 활성 임대 로드 (기존 데이터 기반)
|
|
Future<void> loadActiveRents() async {
|
|
// 현재 로드된 데이터 기반으로 활성 임대 필터링 (별도 API 호출 없음)
|
|
notifyListeners();
|
|
}
|
|
|
|
/// Dashboard용 연체 임대 로드 (기존 데이터 기반)
|
|
Future<void> loadOverdueRents() async {
|
|
// 현재 로드된 데이터 기반으로 연체 임대 필터링 (별도 API 호출 없음)
|
|
notifyListeners();
|
|
}
|
|
} |