null-safety-migration
suvaysov 2022-07-20 12:29:57 +06:00
parent 56aab17cec
commit d01366fced
12 changed files with 78 additions and 79 deletions

View File

@ -3,6 +3,7 @@ import 'package:satu/core/utils/utils_parse.dart';
const String goodTableName = 'goods'; const String goodTableName = 'goods';
const String GoodColumnId = 'id'; const String GoodColumnId = 'id';
const String GoodColumnCategoryId = 'category_id'; const String GoodColumnCategoryId = 'category_id';
const String GoodColumnCategoryName = 'categoryName';
const String GoodColumnName = 'name'; const String GoodColumnName = 'name';
const String GoodColumnEan = 'ean'; const String GoodColumnEan = 'ean';
const String GoodColumnArticul = 'articul'; const String GoodColumnArticul = 'articul';
@ -21,6 +22,7 @@ class Good {
name = (map[GoodColumnName] ?? '') as String ; name = (map[GoodColumnName] ?? '') as String ;
price = map[GoodColumnPrice] as num; price = map[GoodColumnPrice] as num;
categoryId = map[GoodColumnCategoryId] as int; categoryId = map[GoodColumnCategoryId] as int;
categoryName = map[GoodColumnCategoryName] as String?;
ean = map[GoodColumnEan] as String; ean = map[GoodColumnEan] as String;
appCompanyId = map[GoodColumnAppCompanyId] as int; appCompanyId = map[GoodColumnAppCompanyId] as int;
optPrice = map[GoodColumnOptPrice] as num; optPrice = map[GoodColumnOptPrice] as num;
@ -31,6 +33,7 @@ class Good {
int? id; int? id;
int? categoryId; int? categoryId;
String? categoryName;
String name = ''; String name = '';
String? ean; String? ean;
int? articul; int? articul;
@ -49,6 +52,7 @@ class Good {
GoodColumnName: name, GoodColumnName: name,
GoodColumnPrice: price, GoodColumnPrice: price,
GoodColumnCategoryId: categoryId, GoodColumnCategoryId: categoryId,
GoodColumnCategoryName: categoryName,
GoodColumnEan: ean, GoodColumnEan: ean,
GoodColumnAppCompanyId: appCompanyId, GoodColumnAppCompanyId: appCompanyId,
GoodColumnOptPrice: optPrice, GoodColumnOptPrice: optPrice,

View File

@ -19,6 +19,8 @@ class GoodResponseEntity {
double? price; double? price;
@JsonKey(name: 'opt_price') @JsonKey(name: 'opt_price')
double? optPrice; double? optPrice;
@JsonKey(name: 'app_company_id')
int? appCompanyId;
@JsonKey(name: 'base_price') @JsonKey(name: 'base_price')
double? basePrice; double? basePrice;
@JsonKey(name: 'auto_base_price') @JsonKey(name: 'auto_base_price')

View File

@ -15,6 +15,7 @@ GoodResponseEntity _$GoodResponseEntityFromJson(Map<String, dynamic> json) =>
..articul = json['articul'] as int? ..articul = json['articul'] as int?
..price = (json['price'] as num?)?.toDouble() ..price = (json['price'] as num?)?.toDouble()
..optPrice = (json['opt_price'] as num?)?.toDouble() ..optPrice = (json['opt_price'] as num?)?.toDouble()
..appCompanyId = json['app_company_id'] as int?
..basePrice = (json['base_price'] as num?)?.toDouble() ..basePrice = (json['base_price'] as num?)?.toDouble()
..autoBasePrice = (json['auto_base_price'] as num?)?.toDouble() ..autoBasePrice = (json['auto_base_price'] as num?)?.toDouble()
..divisible = json['divisible'] as int? ..divisible = json['divisible'] as int?
@ -30,6 +31,7 @@ Map<String, dynamic> _$GoodResponseEntityToJson(GoodResponseEntity instance) =>
'articul': instance.articul, 'articul': instance.articul,
'price': instance.price, 'price': instance.price,
'opt_price': instance.optPrice, 'opt_price': instance.optPrice,
'app_company_id': instance.appCompanyId,
'base_price': instance.basePrice, 'base_price': instance.basePrice,
'auto_base_price': instance.autoBasePrice, 'auto_base_price': instance.autoBasePrice,
'divisible': instance.divisible, 'divisible': instance.divisible,

View File

@ -5,7 +5,7 @@ class ProductDao {
ProductDao.fromMap(dynamic map) { ProductDao.fromMap(dynamic map) {
id = map['id'] as int; id = map['id'] as int;
categoryId = map['categoryId'] as int; categoryId = map['categoryId'] as int? ;
count = map['count'] as double; count = map['count'] as double;
price = map['price'] as double; price = map['price'] as double;
productName = cast<String>(map['productName']) ?? ''; productName = cast<String>(map['productName']) ?? '';

View File

@ -127,17 +127,10 @@ ThunkAction<AppState> addSellItem({required Good good, String? excise}) {
..count = 1.0 ..count = 1.0
..eanCode = good.ean ..eanCode = good.ean
..productName = good.name ..productName = good.name
..excise = excise; ..excise = excise
//category add logic ..categoryId = good.categoryId
if (good.categoryId != null) { ..categoryName = good.categoryName;
final List<Map<String, dynamic>> set = await _dbService
.queryRowsWithWhere(categoryTableName, 'id = ?', [good.categoryId]);
if (set.isNotEmpty) {
final Category category = Category.fromMap(set.first);
item.categoryId = category.id;
item.categoryName = category.name;
}
}
final TransactionRec rec = TransactionRec() final TransactionRec rec = TransactionRec()
..data = jsonEncode(item.toMap()) ..data = jsonEncode(item.toMap())
..transactionId = transaction.id ..transactionId = transaction.id

View File

@ -44,12 +44,12 @@ class ApiService extends BaseService {
headers.addAll(header); headers.addAll(header);
} }
final String url = '$endpoint$point'; final String url = '$endpoint$point';
// if (requestBody != null) { if (requestBody != null) {
// log.i(host); //log.i(host);
// log.i(url); //log.i(url);
// log.i(headers); //log.i(headers);
// log.i(jsonEncode(requestBody)); log.i(jsonEncode(requestBody));
// } }
final response = await http.post(Uri.https(host, url), final response = await http.post(Uri.https(host, url),
body: jsonEncode(requestBody), headers: headers); body: jsonEncode(requestBody), headers: headers);

View File

@ -50,30 +50,6 @@ class DbService extends BaseService {
log.i('create tables'); log.i('create tables');
//Goods table //Goods table
await db.execute('''
CREATE TABLE IF NOT EXISTS $goodTableName (
$GoodColumnId integer primary key unique,
$GoodColumnArticul integer not null,
$GoodColumnName text not null,
$GoodColumnPrice real not null,
$GoodColumnCategoryId integer not null,
$GoodColumnEan text,
$GoodColumnAppCompanyId integer,
$GoodColumnOptPrice real,
$GoodColumnBasePrice real,
$GoodColumnDivisible integer,
$GoodColumnUpdatedAt text not null
);
''');
await db.execute('''
CREATE TABLE IF NOT EXISTS $categoryTableName (
$categoryColumnId integer primary key unique,
$categoryColumnName text not null,
$categoryColumnParentIn integer,
$categoryColumnAppCompanyId integer,
$categoryColumnUpdatedAt text not null
);
''');
await db.execute(''' await db.execute('''
CREATE TABLE IF NOT EXISTS $transactionTableName ( CREATE TABLE IF NOT EXISTS $transactionTableName (
$transactionColumnId integer primary key autoincrement, $transactionColumnId integer primary key autoincrement,

View File

@ -15,21 +15,42 @@ class DictionaryService extends BaseService {
final ApiService _api = locator<ApiService>(); final ApiService _api = locator<ApiService>();
final DbService _db = locator<DbService>(); final DbService _db = locator<DbService>();
Good goodResponseToGood(GoodResponseEntity response) {
return Good()
..id = response.id
..name = response.name
..basePrice = response.basePrice
..optPrice = response.optPrice
..price = response.price
..articul = response.articul
..categoryId = response.categoryId
..categoryName = response.categoryName
..divisible = response.divisible
..ean = response.ean13
..appCompanyId = response.appCompanyId;
}
Category categoryResponseToCategory(CategoryResponse response) {
return Category()
..id = response.id
..parentId = response.parentId
..name = response.name;
}
Future<void> refreshFull() async {} Future<void> refreshFull() async {}
Future<List<Category>> getCategoryByParentId(int parentId) async { Future<List<Category>> getCategoryByParentId(int? parentId) async {
final List<Category> list = []; final List<Category> list = [];
try { try {
final int? appCompanyId = Redux.store?.state.userState?.auth?.companyId; dynamic filter = {'col': 'parent_id', 'action': 'equal', 'val': parentId};
final List<Map<String, dynamic>> elements = await _db.queryRowsWithWhere( List<CategoryResponse> responses =
categoryTableName, await getCategories(page: 1, perpage: 100, filter: filter);
'$categoryColumnAppCompanyId = ? and $categoryColumnParentIn = ?', for (final CategoryResponse response in responses) {
[appCompanyId, parentId]); final Category category = categoryResponseToCategory(response);
for (final Map<String, dynamic> element in elements) { list.add(category);
list.add(Category.fromMap(element));
} }
} catch (e, stack) { } catch (e, stack) {
log.e('getCategoryByParentId', e, stack); log.e('getCategories', e, stack);
} }
return list; return list;
} }
@ -52,19 +73,22 @@ class DictionaryService extends BaseService {
return result; return result;
} }
Future<List<Good>> getGoodsByCategoryId(int categoryId) async { Future<List<Good>> getGoodsByCategoryId(int? categoryId) async {
final List<Good> list = []; final List<Good> list = [];
try { try {
final int? appCompanyId = Redux.store?.state.userState?.auth?.companyId; dynamic filter = {
final List<Map<String, dynamic>> elements = await _db.queryRowsWithWhere( 'col': 'eacc_good_category_id',
goodTableName, 'action': 'equal',
'$GoodColumnAppCompanyId = ? and $GoodColumnCategoryId = ?', 'val': categoryId
[appCompanyId, categoryId]); };
for (final Map<String, dynamic> element in elements) { List<GoodResponseEntity> responses =
list.add(Good.fromMap(element)); await getGoods(page: 1, perpage: 100, filter: filter);
for (final GoodResponseEntity response in responses) {
final Good good = goodResponseToGood(response);
list.add(good);
} }
} catch (e, stack) { } catch (e, stack) {
log.e('getGoodsByCategoryId', e, stack); log.e('getGoods', e, stack);
} }
return list; return list;
} }
@ -76,7 +100,7 @@ class DictionaryService extends BaseService {
String where = String where =
'( $GoodColumnAppCompanyId = ? and $GoodColumnName like ? ) '; '( $GoodColumnAppCompanyId = ? and $GoodColumnName like ? ) ';
final List args = [appCompanyId, '%$query%']; final List args = [appCompanyId, '%$query%'];
if (_isNumericInt(query)) { if (_isNumericInt(query) && query.length >= 8) {
where += ' or $GoodColumnEan like ?'; where += ' or $GoodColumnEan like ?';
args.add('${int.parse(query).toString()}%'); args.add('${int.parse(query).toString()}%');
} }
@ -99,15 +123,13 @@ class DictionaryService extends BaseService {
} }
Future<List<CategoryResponse>> getCategories( Future<List<CategoryResponse>> getCategories(
{required int page, required int perpage, String? query}) async { {required int page, required int perpage, dynamic filter}) async {
List<CategoryResponse> list = []; List<CategoryResponse> list = [];
try { try {
final Map<String, dynamic> requestBody = <String, dynamic>{ final Map<String, dynamic> requestBody = <String, dynamic>{
'page': page, 'page': page,
'perpage': perpage, 'perpage': perpage,
'filter': [ 'filter': [filter]
{'col': 'name', 'action': 'like', 'val': query ?? ''}
]
}; };
DictResponseEntity categories = await _api DictResponseEntity categories = await _api
@ -148,15 +170,13 @@ class DictionaryService extends BaseService {
} }
Future<List<GoodResponseEntity>> getGoods( Future<List<GoodResponseEntity>> getGoods(
{required int page, required int perpage, String? query}) async { {required int page, required int perpage, dynamic filter}) async {
List<GoodResponseEntity> list = []; List<GoodResponseEntity> list = [];
try { try {
final Map<String, dynamic> requestBody = <String, dynamic>{ final Map<String, dynamic> requestBody = <String, dynamic>{
'page': page, 'page': page,
'perpage': perpage, 'perpage': perpage,
'filter': [ 'filter': [filter]
{'col': 'name', 'action': 'like', 'val': query ?? ''}
]
}; };
DictResponseEntity categories = DictResponseEntity categories =
@ -177,11 +197,9 @@ class DictionaryService extends BaseService {
Future<String?> saveGood(GoodResponseEntity good) async { Future<String?> saveGood(GoodResponseEntity good) async {
DictResponseEntity? status; DictResponseEntity? status;
if (good.id != null) { if (good.id != null) {
status = status = await _api.dictionarySave('/goods_goods_edit', good.toJson());
await _api.dictionarySave('/goods_goods_edit', good.toJson());
} else { } else {
status = status = await _api.dictionarySave('/goods_goods_add', good.toJson());
await _api.dictionarySave('/goods_goods_add', good.toJson());
} }
String? message = getErrorMsg(status); String? message = getErrorMsg(status);

View File

@ -132,8 +132,9 @@ class _CategorySelectViewState extends State<CategorySelectView> {
} }
Future<void> _fetchData(int pageKey, int perPage, String? query) async { Future<void> _fetchData(int pageKey, int perPage, String? query) async {
dynamic filter = {'col': 'name', 'action': 'like', 'val': query ?? ''};
final List<CategoryResponse> newItems = await _dictionaryService final List<CategoryResponse> newItems = await _dictionaryService
.getCategories(page: pageKey, query: query, perpage: perPage); .getCategories(page: pageKey, filter: filter, perpage: perPage);
final isLastPage = newItems.length < _pageSize; final isLastPage = newItems.length < _pageSize;
if (isLastPage) { if (isLastPage) {

View File

@ -137,8 +137,9 @@ class _CategoryDictionaryViewState extends State<CategoryDictionaryView> {
} }
Future<void> _fetchData(int pageKey, int perPage, String? query) async { Future<void> _fetchData(int pageKey, int perPage, String? query) async {
dynamic filter = {'col': 'name', 'action': 'like', 'val': query ?? ''};
final List<CategoryResponse> newItems = await _dictionaryService final List<CategoryResponse> newItems = await _dictionaryService
.getCategories(page: pageKey, query: query, perpage: perPage); .getCategories(page: pageKey, filter: filter, perpage: perPage);
final isLastPage = newItems.length < _pageSize; final isLastPage = newItems.length < _pageSize;
if (isLastPage) { if (isLastPage) {

View File

@ -152,7 +152,9 @@ class _GoodsDictionaryViewState extends State<GoodsDictionaryView> {
Future<void> _fetchData(int pageKey, int perPage, String? query) async { Future<void> _fetchData(int pageKey, int perPage, String? query) async {
final List<GoodResponseEntity> newItems = await _dictionaryService.getGoods( final List<GoodResponseEntity> newItems = await _dictionaryService.getGoods(
page: pageKey, query: query, perpage: perPage); page: pageKey,
filter: {'col': 'name', 'action': 'like', 'val': query ?? ''},
perpage: perPage);
final isLastPage = newItems.length < _pageSize; final isLastPage = newItems.length < _pageSize;
if (isLastPage) { if (isLastPage) {

View File

@ -40,11 +40,11 @@ class _AddProductViewState extends State<AddProductView> {
reset(); reset();
} }
}); });
_history = [Category()..id = 0]; _history = [Category()..id = null];
_categories = []; _categories = [];
_goods = []; _goods = [];
super.initState(); super.initState();
navigateCategory(0); navigateCategory(null);
} }
@override @override
@ -128,7 +128,7 @@ class _AddProductViewState extends State<AddProductView> {
_searchTextController.clear(); _searchTextController.clear();
} }
void navigateCategory(int categoryId) async { void navigateCategory(int? categoryId) async {
List<Category> categories = List<Category> categories =
await _dictionaryService.getCategoryByParentId(categoryId); await _dictionaryService.getCategoryByParentId(categoryId);
List<Good> goods = List<Good> goods =