128 lines
4.3 KiB
Dart
128 lines
4.3 KiB
Dart
|
|
import 'package:satu/core/base/base_service.dart';
|
|
import 'package:satu/core/entity/Category.dart';
|
|
import 'package:satu/core/entity/Goods.dart';
|
|
import 'package:satu/core/models/dictionary/category_response.dart';
|
|
import 'package:satu/core/models/dictionary/good_response.dart';
|
|
import 'package:satu/core/models/response.dart';
|
|
import 'package:satu/core/redux/store.dart';
|
|
import 'package:satu/core/utils/locator.dart';
|
|
|
|
import 'api_service.dart';
|
|
import 'db_service.dart';
|
|
|
|
class DictionaryService extends BaseService {
|
|
final ApiService _api = locator<ApiService>();
|
|
final DbService _db = locator<DbService>();
|
|
|
|
Future<void> refreshFull() async {
|
|
_db.deleteAll(CategoryTableName);
|
|
await refreshCategory();
|
|
_db.deleteAll(GoodTableName);
|
|
await refreshGood();
|
|
}
|
|
|
|
Future<void> refreshCategory() async {
|
|
try {
|
|
int? appCompanyId = Redux.store!.state.userState!.auth!.companyId;
|
|
Response categories = await _api.dictionaries('/categories');
|
|
if (categories.operation! && categories.list!.isNotEmpty) {
|
|
for (dynamic map in categories.list!) {
|
|
CategoryResponse cat = CategoryResponse.fromMap(map)!;
|
|
Category entity = new Category()
|
|
..id = cat.id
|
|
..name = cat.name
|
|
..updatedAt = cat.updatedAt
|
|
..parentId = cat.parentId
|
|
..appCompanyId = appCompanyId;
|
|
_db.insert(CategoryTableName, entity.toMap());
|
|
}
|
|
}
|
|
} catch (e, stack) {
|
|
log.e("categories", e, stack);
|
|
}
|
|
}
|
|
|
|
Future<List<Category>> getCategoryByParentId(int parentId ) async {
|
|
List<Category> list = [];
|
|
try {
|
|
int? appCompanyId = Redux.store!.state.userState!.auth!.companyId;
|
|
List<Map<String, dynamic>> elements = await _db.queryRowsWithWhere(CategoryTableName, '$CategoryColumnAppCompanyId = ? and $CategoryColumnParentIn = ?', [appCompanyId, parentId]);
|
|
elements.forEach((element) {
|
|
list.add(Category.fromMap(element));
|
|
});
|
|
} catch (e, stack) {
|
|
log.e("getCategoryByParentId", e, stack);
|
|
}
|
|
return list;
|
|
}
|
|
|
|
Future<List<Good>> getGoodsByCategoryId(int categoryId ) async {
|
|
List<Good> list = [];
|
|
try {
|
|
int? appCompanyId = Redux.store!.state.userState!.auth!.companyId;
|
|
List<Map<String, dynamic>> elements = await _db.queryRowsWithWhere(GoodTableName, '$GoodColumnAppCompanyId = ? and $GoodColumnCategoryId = ?', [appCompanyId, categoryId]);
|
|
elements.forEach((element) {
|
|
list.add(Good.fromMap(element));
|
|
});
|
|
} catch (e, stack) {
|
|
log.e("getGoodsByCategoryId", e, stack);
|
|
}
|
|
return list;
|
|
}
|
|
|
|
Future<List<Good>> getGoodsByNameOrEan( String query ) async {
|
|
List<Good> list = [];
|
|
try {
|
|
int? appCompanyId = Redux.store!.state.userState!.auth!.companyId;
|
|
String where = '( $GoodColumnAppCompanyId = ? and $GoodColumnName like ? ) ';
|
|
List args = [appCompanyId, '%$query%'];
|
|
if(_isNumericInt(query)){
|
|
where += ' or $GoodColumnEan like ?';
|
|
args.add('${int.parse(query).toString()}%');
|
|
}
|
|
List<Map<String, dynamic>> elements = await _db.queryRowsWithWhere(GoodTableName, where, args);
|
|
elements.forEach((element) {
|
|
list.add(Good.fromMap(element));
|
|
});
|
|
} catch (e, stack) {
|
|
log.e("getGoodsByCategoryId", e, stack);
|
|
}
|
|
return list;
|
|
}
|
|
|
|
bool _isNumericInt(String str) {
|
|
if(str == null) {
|
|
return false;
|
|
}
|
|
return int.tryParse(str) != null;
|
|
}
|
|
|
|
Future<void> refreshGood() async {
|
|
try {
|
|
int? appCompanyId = Redux.store!.state.userState!.auth!.companyId;
|
|
Response categories = await _api.dictionaries('/goods');
|
|
if (categories.operation! && categories.list!.isNotEmpty) {
|
|
for (dynamic map in categories.list!) {
|
|
GoodResponse good = GoodResponse.fromMap(map)!;
|
|
Good entity = new Good()
|
|
..id = good.id
|
|
..name = good.name
|
|
..categoryId = good.categoryId
|
|
..ean = good.ean
|
|
..articul = good.articul
|
|
..price = good.price
|
|
..optPrice = good.optPrice
|
|
..basePrice = good.basePrice
|
|
..divisible = good.divisible
|
|
..updatedAt = good.updatedAt
|
|
..appCompanyId = appCompanyId;
|
|
_db.insert(GoodTableName, entity.toMap());
|
|
}
|
|
}
|
|
} catch (e, stack) {
|
|
log.e("goods", e, stack);
|
|
}
|
|
}
|
|
}
|