feat: 라이선스 및 창고 관리 API 연동 구현
- 라이선스 관리 API 연동 완료 - LicenseRemoteDataSource, LicenseService 구현 - LicenseListController, LicenseFormController API 연동 - 페이지네이션, 검색, 필터링 기능 추가 - 라이선스 할당/해제 기능 구현 - 창고 관리 API 연동 완료 - WarehouseRemoteDataSource, WarehouseService 구현 - WarehouseLocationListController, WarehouseLocationFormController API 연동 - 창고별 장비 조회 및 용량 관리 기능 추가 - DI 컨테이너에 새로운 서비스 등록 - API 통합 문서 업데이트 (전체 진행률 100% 달성)
This commit is contained in:
@@ -1,57 +1,186 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:superport/models/license_model.dart';
|
||||
import 'package:superport/services/license_service.dart';
|
||||
import 'package:superport/services/mock_data_service.dart';
|
||||
|
||||
// 라이센스 폼의 상태 및 비즈니스 로직을 담당하는 컨트롤러
|
||||
class LicenseFormController {
|
||||
final MockDataService dataService;
|
||||
class LicenseFormController extends ChangeNotifier {
|
||||
final bool useApi;
|
||||
final MockDataService? mockDataService;
|
||||
late final LicenseService _licenseService;
|
||||
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
|
||||
|
||||
bool isEditMode = false;
|
||||
int? licenseId;
|
||||
String name = '';
|
||||
int durationMonths = 12; // 기본값: 12개월
|
||||
String visitCycle = '미방문'; // 기본값: 미방문
|
||||
bool _isEditMode = false;
|
||||
int? _licenseId;
|
||||
License? _originalLicense;
|
||||
bool _isLoading = false;
|
||||
String? _error;
|
||||
bool _isSaving = false;
|
||||
|
||||
LicenseFormController({required this.dataService, this.licenseId});
|
||||
// 폼 필드 값
|
||||
String _name = '';
|
||||
int _companyId = 1;
|
||||
int _durationMonths = 12; // 기본값: 12개월
|
||||
String _visitCycle = '미방문'; // 기본값: 미방문
|
||||
|
||||
LicenseFormController({
|
||||
this.useApi = true,
|
||||
this.mockDataService,
|
||||
int? licenseId,
|
||||
}) {
|
||||
if (useApi && GetIt.instance.isRegistered<LicenseService>()) {
|
||||
_licenseService = GetIt.instance<LicenseService>();
|
||||
}
|
||||
|
||||
if (licenseId != null) {
|
||||
_licenseId = licenseId;
|
||||
_isEditMode = true;
|
||||
loadLicense();
|
||||
}
|
||||
}
|
||||
|
||||
// Getters
|
||||
bool get isEditMode => _isEditMode;
|
||||
int? get licenseId => _licenseId;
|
||||
License? get originalLicense => _originalLicense;
|
||||
bool get isLoading => _isLoading;
|
||||
String? get error => _error;
|
||||
bool get isSaving => _isSaving;
|
||||
String get name => _name;
|
||||
int get companyId => _companyId;
|
||||
int get durationMonths => _durationMonths;
|
||||
String get visitCycle => _visitCycle;
|
||||
|
||||
// Setters
|
||||
void setName(String value) {
|
||||
_name = value;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void setCompanyId(int value) {
|
||||
_companyId = value;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void setDurationMonths(int value) {
|
||||
_durationMonths = value;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void setVisitCycle(String value) {
|
||||
_visitCycle = value;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
// 라이센스 정보 로드 (수정 모드)
|
||||
void loadLicense() {
|
||||
if (licenseId == null) return;
|
||||
final license = dataService.getLicenseById(licenseId!);
|
||||
if (license != null) {
|
||||
name = license.name;
|
||||
durationMonths = license.durationMonths;
|
||||
visitCycle = license.visitCycle;
|
||||
Future<void> loadLicense() async {
|
||||
if (_licenseId == null) return;
|
||||
|
||||
_isLoading = true;
|
||||
_error = null;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
if (useApi && GetIt.instance.isRegistered<LicenseService>()) {
|
||||
_originalLicense = await _licenseService.getLicenseById(_licenseId!);
|
||||
} else {
|
||||
_originalLicense = mockDataService?.getLicenseById(_licenseId!);
|
||||
}
|
||||
|
||||
if (_originalLicense != null) {
|
||||
_name = _originalLicense!.name;
|
||||
_companyId = _originalLicense!.companyId;
|
||||
_durationMonths = _originalLicense!.durationMonths;
|
||||
_visitCycle = _originalLicense!.visitCycle;
|
||||
}
|
||||
} catch (e) {
|
||||
_error = e.toString();
|
||||
} finally {
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
// 라이센스 저장 (UI에서 호출)
|
||||
void saveLicense(Function() onSuccess) {
|
||||
if (formKey.currentState?.validate() != true) return;
|
||||
// 라이센스 저장
|
||||
Future<bool> saveLicense() async {
|
||||
if (formKey.currentState?.validate() != true) return false;
|
||||
|
||||
formKey.currentState?.save();
|
||||
if (isEditMode && licenseId != null) {
|
||||
final license = dataService.getLicenseById(licenseId!);
|
||||
if (license != null) {
|
||||
final updatedLicense = License(
|
||||
id: license.id,
|
||||
companyId: license.companyId,
|
||||
name: name,
|
||||
durationMonths: durationMonths,
|
||||
visitCycle: visitCycle,
|
||||
);
|
||||
dataService.updateLicense(updatedLicense);
|
||||
}
|
||||
} else {
|
||||
// 라이센스 추가 시 임시 회사 ID 사용 또는 나중에 설정하도록 변경
|
||||
final newLicense = License(
|
||||
companyId: 1, // 기본값 또는 필요에 따라 수정
|
||||
name: name,
|
||||
durationMonths: durationMonths,
|
||||
visitCycle: visitCycle,
|
||||
|
||||
_isSaving = true;
|
||||
_error = null;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
final license = License(
|
||||
id: _isEditMode ? _licenseId : null,
|
||||
companyId: _companyId,
|
||||
name: _name,
|
||||
durationMonths: _durationMonths,
|
||||
visitCycle: _visitCycle,
|
||||
);
|
||||
dataService.addLicense(newLicense);
|
||||
|
||||
if (useApi && GetIt.instance.isRegistered<LicenseService>()) {
|
||||
if (_isEditMode) {
|
||||
await _licenseService.updateLicense(license);
|
||||
} else {
|
||||
await _licenseService.createLicense(license);
|
||||
}
|
||||
} else {
|
||||
if (_isEditMode) {
|
||||
mockDataService?.updateLicense(license);
|
||||
} else {
|
||||
mockDataService?.addLicense(license);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (e) {
|
||||
_error = e.toString();
|
||||
notifyListeners();
|
||||
return false;
|
||||
} finally {
|
||||
_isSaving = false;
|
||||
notifyListeners();
|
||||
}
|
||||
onSuccess();
|
||||
}
|
||||
|
||||
// 폼 초기화
|
||||
void resetForm() {
|
||||
_name = '';
|
||||
_companyId = 1;
|
||||
_durationMonths = 12;
|
||||
_visitCycle = '미방문';
|
||||
_error = null;
|
||||
formKey.currentState?.reset();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
// 유효성 검사
|
||||
String? validateName(String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return '라이선스명을 입력해주세요';
|
||||
}
|
||||
if (value.length < 2) {
|
||||
return '라이선스명은 2자 이상이어야 합니다';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
String? validateDuration(String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return '계약 기간을 입력해주세요';
|
||||
}
|
||||
final duration = int.tryParse(value);
|
||||
if (duration == null || duration < 1) {
|
||||
return '유효한 계약 기간을 입력해주세요';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user