22f07ed805
- product_option.dart: 新增 4 个字典 model(Origin/ShelfLife/Storage/DescriptionDoc) - product_option_repository.dart: 新增 4 个 list 方法(listOrigins/ShelfLives/Storages/DescriptionDocs) - product_option_provider.dart: 新增 4 个 FutureProvider 供表单使用 - product_repository.dart: findOrCreate 加 originId/shelfLifeId/storageId/descriptionDocId 可选参数 - stock_in_form_screen.dart: _ItemRow 加 4 个可选 ID 字段;新增 4 个 SearchableOptionField 方法;移动端卡片视图增加 4 个可选字段;提交时传入属性 ID Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
164 lines
5.1 KiB
Dart
164 lines
5.1 KiB
Dart
import 'dart:typed_data';
|
|
import 'package:dio/dio.dart';
|
|
import '../core/api/api_client.dart';
|
|
import '../core/exceptions.dart';
|
|
import '../core/models/page_result.dart';
|
|
import '../models/product.dart';
|
|
import '../models/product_image.dart';
|
|
|
|
class ProductRepository {
|
|
final ApiClient _client;
|
|
|
|
const ProductRepository(this._client);
|
|
|
|
Future<PageResult<Product>> list({
|
|
int page = 1,
|
|
int pageSize = 20,
|
|
String? keyword,
|
|
int? categoryId,
|
|
}) async {
|
|
try {
|
|
final params = <String, dynamic>{
|
|
'page': page,
|
|
'page_size': pageSize,
|
|
if (keyword != null && keyword.isNotEmpty) 'keyword': keyword,
|
|
if (categoryId != null) 'category_id': categoryId,
|
|
};
|
|
final resp = await _client.get('/products', params: params);
|
|
return PageResult.fromJson(
|
|
resp.data as Map<String, dynamic>,
|
|
Product.fromJson,
|
|
);
|
|
} on DioException catch (e) {
|
|
throw AppException(
|
|
e.response?.data?['error'] as String? ?? '获取商品列表失败',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<Product> create(Map<String, dynamic> data) async {
|
|
try {
|
|
final resp = await _client.post('/products', data: data);
|
|
return Product.fromJson(
|
|
(resp.data as Map<String, dynamic>)['data'] as Map<String, dynamic>);
|
|
} on DioException catch (e) {
|
|
throw AppException(
|
|
e.response?.data?['error'] as String? ?? '创建商品失败',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<Product> update(int id, Map<String, dynamic> data) async {
|
|
try {
|
|
final resp = await _client.put('/products/$id', data: data);
|
|
return Product.fromJson(
|
|
(resp.data as Map<String, dynamic>)['data'] as Map<String, dynamic>);
|
|
} on DioException catch (e) {
|
|
throw AppException(
|
|
e.response?.data?['error'] as String? ?? '更新商品失败',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> delete(int id) async {
|
|
try {
|
|
await _client.delete('/products/$id');
|
|
} on DioException catch (e) {
|
|
throw AppException(
|
|
e.response?.data?['error'] as String? ?? '删除商品失败',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<Product> getDetail(int id) async {
|
|
try {
|
|
final resp = await _client.get('/products/$id/detail');
|
|
return Product.fromJson(
|
|
(resp.data as Map<String, dynamic>)['data'] as Map<String, dynamic>);
|
|
} on DioException catch (e) {
|
|
throw AppException(
|
|
e.response?.data?['error'] as String? ?? '获取商品详情失败',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<ProductImage> uploadImage(int productId, String? filePath,
|
|
{Uint8List? bytes, String? fileName}) async {
|
|
try {
|
|
final MultipartFile file;
|
|
if (bytes != null) {
|
|
file = MultipartFile.fromBytes(bytes, filename: fileName ?? 'image.jpg');
|
|
} else {
|
|
file = await MultipartFile.fromFile(filePath!);
|
|
}
|
|
final formData = FormData.fromMap({'file': file});
|
|
final resp = await _client.post('/products/$productId/images', data: formData);
|
|
return ProductImage.fromJson(
|
|
(resp.data as Map<String, dynamic>)['data'] as Map<String, dynamic>);
|
|
} on DioException catch (e) {
|
|
throw AppException(
|
|
e.response?.data?['error'] as String? ?? '上传图片失败',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<Uint8List> getQRCodeBytes(int productId) async {
|
|
try {
|
|
final bytes = await _client.getBytes('/products/$productId/qrcode');
|
|
return Uint8List.fromList(bytes);
|
|
} on DioException catch (e) {
|
|
throw AppException(
|
|
e.response?.data?['error'] as String? ?? '获取二维码失败',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> deleteImage(int productId, int imageId) async {
|
|
try {
|
|
await _client.delete('/products/$productId/images/$imageId');
|
|
} on DioException catch (e) {
|
|
throw AppException(
|
|
e.response?.data?['error'] as String? ?? '删除图片失败',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<Product> findOrCreate({
|
|
required String name,
|
|
String series = '',
|
|
String spec = '',
|
|
int? originId,
|
|
int? shelfLifeId,
|
|
int? storageId,
|
|
int? descriptionDocId,
|
|
}) async {
|
|
try {
|
|
final body = <String, dynamic>{
|
|
'name': name,
|
|
'series': series,
|
|
'spec': spec,
|
|
};
|
|
if (originId != null) body['origin_id'] = originId;
|
|
if (shelfLifeId != null) body['shelf_life_id'] = shelfLifeId;
|
|
if (storageId != null) body['storage_id'] = storageId;
|
|
if (descriptionDocId != null) body['description_doc_id'] = descriptionDocId;
|
|
final resp = await _client.post('/products/find-or-create', data: body);
|
|
return Product.fromJson(
|
|
(resp.data as Map<String, dynamic>)['data'] as Map<String, dynamic>);
|
|
} on DioException catch (e) {
|
|
throw AppException(
|
|
e.response?.data?['error'] as String? ?? '查找/创建商品失败',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
}
|