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/category_response.dart'; import 'package:satu/core/models/dictionary/contragent/contragent_response_entity.dart'; import 'package:satu/core/models/dictionary/good/good_response_entity.dart'; import 'package:satu/core/models/dictionary/response/dict_response_entity.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 {} 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> 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; } bool _isNumericInt(String? str) { if (str == null) { return false; } return int.tryParse(str) != null; } Future> getCategories( {required int page, required int perpage, String? query}) async { List list = []; try { final Map requestBody = { 'page': page, 'perpage': perpage, 'filter': [ {'col': 'name', 'action': 'like', 'val': query ?? ''} ] }; DictResponseEntity categories = await _api .dictionaries('/goods_categories_get', requestBody: requestBody); if (categories.original.data != null && categories.original.data!.isNotEmpty) { for (final dynamic map in categories.original.data!) { final CategoryResponse good = CategoryResponse.fromJson(map); list.add(good); } } } catch (e, stack) { log.e('getCategories', e, stack); } return list; } Future saveCategory(CategoryResponse category) async { DictResponseEntity? status; log.i(category.toJson()); if (category.id != null) { status = await _api.dictionarySave( '/goods_categories_edit', category.toJson()); } else { status = await _api.dictionarySave('/goods_categories_add', category.toJson()); } String? message = getErrorMsg(status); return message; } Future deleteCategory(CategoryResponse category) async { DictResponseEntity status = await _api.dictionarySave( '/goods_categories_delete', category.toJson()); String? message = getErrorMsg(status); return message; } Future> getGoods( {required int page, required int perpage, String? query}) async { List list = []; try { final Map requestBody = { 'page': page, 'perpage': perpage, 'filter': [ {'col': 'name', 'action': 'like', 'val': query ?? ''} ] }; DictResponseEntity categories = await _api.dictionaries('/goods_goods_get', requestBody: requestBody); if (categories.original.data != null && categories.original.data!.isNotEmpty) { for (final dynamic map in categories.original.data!) { final GoodResponseEntity good = GoodResponseEntity.fromJson(map); list.add(good); } } } catch (e, stack) { log.e('getGoods', e, stack); } return list; } Future saveGood(GoodResponseEntity good) async { DictResponseEntity? status; if (good.id != null) { status = await _api.dictionarySave('/goods_goods_edit', good.toJson()); } else { status = await _api.dictionarySave('/goods_goods_add', good.toJson()); } String? message = getErrorMsg(status); return message; } Future deleteGood(GoodResponseEntity good) async { DictResponseEntity status = await _api.dictionarySave('/goods_goods_delete', good.toJson()); String? message = getErrorMsg(status); return message; } Future> getContragents( {int? page, int? perpage, String? query}) async { final List list = []; try { final Map requestBody = { 'page': page, 'perpage': perpage, 'filter': [ {'col': 'name', 'action': 'like', 'val': query ?? ''} ] }; final DictResponseEntity contragents = await _api .dictionaries('/general_contragents_get', requestBody: requestBody); if (contragents.original.data != null) { if (contragents.original.data!.isNotEmpty) { for (dynamic map in contragents.original.data!) { list.add(ContragentResponseEntity.fromJson(map)); } } } } catch (e, stack) { log.e('getContragents', e, stack); } return list; } Future saveContragent(ContragentResponseEntity contragent) async { DictResponseEntity? status; if (contragent.id != null) { status = await _api.dictionarySave( '/general_contragents_edit', contragent.toJson()); } else { status = await _api.dictionarySave( '/general_contragents_add', contragent.toJson()); } String? message = getErrorMsg(status); return message; } Future deleteContragent(ContragentResponseEntity contragent) async { DictResponseEntity status = await _api.dictionarySave( '/general_contragents_delete', contragent.toJson()); String? message = getErrorMsg(status); return message; } String? getErrorMsg(DictResponseEntity status) { String? message; if (status.exception != null) { message = status.exception; } else if (status.original.message != null) { message = status.original.message; } else if (status.original.errors != null) { Map> errors = status.original.errors!; errors.values.forEach((element) { message = '${message ?? ''} \n ${element.join(',')}'; }); } return message; } }