import 'package:satu/core/base/base_service.dart'; import 'package:satu/core/entity/category_entity.dart'; import 'package:satu/core/entity/goods_entity.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(); final DbService _db = locator(); Future refreshFull() async { _db.deleteAll(categoryTableName); await refreshCategory(); _db.deleteAll(goodTableName); await refreshGood(); } Future refreshCategory() async { try { final int? appCompanyId = Redux.store!.state.userState!.auth!.companyId; final 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> getCategoryByParentId(int parentId) async { final List list = []; try { final int? appCompanyId = Redux.store?.state.userState?.auth?.companyId; final List> elements = await _db.queryRowsWithWhere( categoryTableName, '$categoryColumnAppCompanyId = ? and $categoryColumnParentIn = ?', [appCompanyId, parentId]); for (final Map element in elements) { list.add(Category.fromMap(element)); } } catch (e, stack) { log.e('getCategoryByParentId', e, stack); } return list; } Future getCategoryById(int id) async { Category? result; try { final int? appCompanyId = Redux.store?.state.userState?.auth?.companyId; final List> elements = await _db.queryRowsWithWhere( categoryTableName, '$categoryColumnAppCompanyId = ? and $categoryColumnId = ?', [appCompanyId, id]); for (final Map element in elements) { result = Category.fromMap(element); } } catch (e, stack) { log.e('getCategoryById', e, stack); } return result; } Future> getCategoriesAll() async { final List list = []; try { final int? appCompanyId = Redux.store?.state.userState?.auth?.companyId; final List> elements = await _db.queryRowsWithWhere( categoryTableName, '$categoryColumnAppCompanyId = ? ' ' order by $categoryColumnParentIn asc, $categoryColumnId asc ', [appCompanyId]); for (final Map element in elements) { list.add(Category.fromMap(element)); } } catch (e, stack) { log.e('getCategoriesAll', e, stack); } return list; } Future> getGoodsByCategoryId(int categoryId) async { final List list = []; try { final int? appCompanyId = Redux.store?.state.userState?.auth?.companyId; final List> elements = await _db.queryRowsWithWhere( goodTableName, '$GoodColumnAppCompanyId = ? and $GoodColumnCategoryId = ?', [appCompanyId, categoryId]); for (final Map element in elements) { list.add(Good.fromMap(element)); } } catch (e, stack) { log.e('getGoodsByCategoryId', e, stack); } return list; } Future> getGoodsByNameOrEan(String query) async { final List list = []; try { final int? appCompanyId = Redux.store?.state.userState?.auth?.companyId; String where = '( $GoodColumnAppCompanyId = ? and $GoodColumnName like ? ) '; final List args = [appCompanyId, '%$query%']; if (_isNumericInt(query)) { where += ' or $GoodColumnEan like ?'; args.add('${int.parse(query).toString()}%'); } final List> elements = await _db.queryRowsWithWhere(goodTableName, where, args); for (final Map element in elements) { list.add(Good.fromMap(element)); } } catch (e, stack) { log.e('getGoodsByCategoryId', e, stack); } return list; } Future> getGoodsByEan(String code) async { final List list = []; try { final int? appCompanyId = Redux.store?.state.userState?.auth?.companyId; final String where = '( $GoodColumnAppCompanyId = ? and $GoodColumnEan like ? ) '; final List args = [appCompanyId, '%$code%']; final List> elements = await _db.queryRowsWithWhere(goodTableName, where, args); for (final Map element in elements) { list.add(Good.fromMap(element)); } } catch (e, stack) { log.e('getGoodsByEan', e, stack); } return list; } bool _isNumericInt(String str) { if (str == null) { return false; } return int.tryParse(str) != null; } Future refreshGood() async { try { int? appCompanyId = Redux.store!.state.userState!.auth!.companyId; Response categories = await _api.dictionaries('/goods'); if (categories.operation! && categories.list!.isNotEmpty) { for (final dynamic map in categories.list!) { final GoodResponse good = GoodResponse.fromMap(map); final 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); } } }