transaction_rec_entity.dart added
parent
3be37e5057
commit
b30e9240e7
|
|
@ -0,0 +1,38 @@
|
||||||
|
const String transactionRecTableName = 'transactions_rec';
|
||||||
|
const String transactionRecColumnId = 'id';
|
||||||
|
const String transactionRecTransactionIdRef = 'transaction_id';
|
||||||
|
const String transactionRecColumnData = 'data';
|
||||||
|
const String transactionRecColumnCreatedAt = 'createdAt';
|
||||||
|
|
||||||
|
|
||||||
|
class TransactionRec {
|
||||||
|
|
||||||
|
TransactionRec();
|
||||||
|
|
||||||
|
TransactionRec.fromMap(Map<String, dynamic> map) {
|
||||||
|
id = map[transactionRecColumnId] as int;
|
||||||
|
transactionId = map[transactionRecTransactionIdRef] as int;
|
||||||
|
data = map[transactionRecColumnData] as String;
|
||||||
|
createdAt = map[transactionRecColumnCreatedAt] as String;
|
||||||
|
}
|
||||||
|
|
||||||
|
int? id;
|
||||||
|
int? transactionId;
|
||||||
|
String? data;
|
||||||
|
String? createdAt;
|
||||||
|
|
||||||
|
Map<String, dynamic> toMap() {
|
||||||
|
final Map<String, dynamic> map = <String, dynamic>{
|
||||||
|
transactionRecTransactionIdRef: transactionId,
|
||||||
|
transactionRecColumnData: data,
|
||||||
|
transactionRecColumnCreatedAt: createdAt,
|
||||||
|
};
|
||||||
|
if (id != null) {
|
||||||
|
map[transactionRecColumnId] = id;
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
import 'package:satu/core/utils/utils_parse.dart';
|
||||||
|
|
||||||
|
class TransactionData {
|
||||||
|
TransactionData();
|
||||||
|
|
||||||
|
TransactionData.fromMap(dynamic map) {
|
||||||
|
contragentName = map['contragentName'] as String;
|
||||||
|
card = map['card'] as double;
|
||||||
|
nal = map['nal'] as double;
|
||||||
|
}
|
||||||
|
String contragentName = '';
|
||||||
|
double card = 0;
|
||||||
|
double nal = 0;
|
||||||
|
|
||||||
|
Map<String, dynamic> toMap() {
|
||||||
|
final Map<String, dynamic> map = <String, dynamic>{
|
||||||
|
'contragentName': contragentName,
|
||||||
|
'card' : card,
|
||||||
|
'nal' : nal,
|
||||||
|
};
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -7,6 +7,8 @@ import 'package:redux_thunk/redux_thunk.dart';
|
||||||
import 'package:satu/core/entity/category_entity.dart';
|
import 'package:satu/core/entity/category_entity.dart';
|
||||||
import 'package:satu/core/entity/goods_entity.dart';
|
import 'package:satu/core/entity/goods_entity.dart';
|
||||||
import 'package:satu/core/entity/transaction_entity.dart';
|
import 'package:satu/core/entity/transaction_entity.dart';
|
||||||
|
import 'package:satu/core/entity/transaction_rec_entity.dart';
|
||||||
|
import 'package:satu/core/models/entity_data/transaction_data.dart';
|
||||||
import 'package:satu/core/models/flow/product_dao.dart';
|
import 'package:satu/core/models/flow/product_dao.dart';
|
||||||
import 'package:satu/core/models/flow/transaction_state.dart';
|
import 'package:satu/core/models/flow/transaction_state.dart';
|
||||||
import 'package:satu/core/redux/state/sell_state.dart';
|
import 'package:satu/core/redux/state/sell_state.dart';
|
||||||
|
|
@ -19,9 +21,8 @@ import '../store.dart';
|
||||||
|
|
||||||
@immutable
|
@immutable
|
||||||
class SetSellStateAction {
|
class SetSellStateAction {
|
||||||
|
const SetSellStateAction(this.sellState);
|
||||||
final SellState sellState;
|
final SellState sellState;
|
||||||
|
|
||||||
SetSellStateAction(this.sellState);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final Logger log = getLogger('SetSellStateAction');
|
final Logger log = getLogger('SetSellStateAction');
|
||||||
|
|
@ -31,33 +32,16 @@ final DbService _dbService = locator<DbService>();
|
||||||
ThunkAction<AppState> counterOrEditSellItem(
|
ThunkAction<AppState> counterOrEditSellItem(
|
||||||
{required int transactionId, required double counter, double? price}) {
|
{required int transactionId, required double counter, double? price}) {
|
||||||
return (Store<AppState> store) async {
|
return (Store<AppState> store) async {
|
||||||
log.i('counterSellItem');
|
log.i(
|
||||||
int? appCompanyId = store.state.userState!.auth!.companyId;
|
'counterSellItem id = $transactionId counter = $counter, price = $price',
|
||||||
String? uuid = store.state.sellState!.transactionState!.uuid;
|
);
|
||||||
|
final Map<String, dynamic>? map =
|
||||||
|
await _dbService.queryById(transactionRecTableName, transactionId);
|
||||||
|
|
||||||
Transaction? transaction;
|
if (map != null) {
|
||||||
|
final TransactionRec transactionRec = TransactionRec.fromMap(map);
|
||||||
if (uuid != null) {
|
final ProductDao item =
|
||||||
final List<Map<String, dynamic>> set =
|
ProductDao.fromMap(jsonDecode(transactionRec.data!));
|
||||||
await _dbService.queryRowsWithWhere(
|
|
||||||
transactionTableName,
|
|
||||||
'$transactionColumnAppCompanyId = ? '
|
|
||||||
' and $transactionColumnStatus = ? '
|
|
||||||
' and $transactionColumnType = ? '
|
|
||||||
' and $transactionColumnId = ?',
|
|
||||||
[
|
|
||||||
appCompanyId,
|
|
||||||
transactionStatusPrepare,
|
|
||||||
transactionTypeSell,
|
|
||||||
transactionId
|
|
||||||
],
|
|
||||||
orderBy: '$transactionColumnCreatedAt desc');
|
|
||||||
if (set.isNotEmpty) {
|
|
||||||
transaction = Transaction.fromMap(set.first);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (transaction != null) {
|
|
||||||
final ProductDao item = ProductDao.fromMap(jsonDecode(transaction.data!));
|
|
||||||
if (price != null) {
|
if (price != null) {
|
||||||
item.price = price;
|
item.price = price;
|
||||||
item.count = counter;
|
item.count = counter;
|
||||||
|
|
@ -65,8 +49,8 @@ ThunkAction<AppState> counterOrEditSellItem(
|
||||||
final String val = ((item.count ?? 0) + counter).toStringAsFixed(5);
|
final String val = ((item.count ?? 0) + counter).toStringAsFixed(5);
|
||||||
item.count = num.parse(val);
|
item.count = num.parse(val);
|
||||||
}
|
}
|
||||||
transaction.data = jsonEncode(item.toMap());
|
transactionRec.data = jsonEncode(item.toMap());
|
||||||
_dbService.update(transactionTableName, transaction.toMap());
|
_dbService.update(transactionRecTableName, transactionRec.toMap());
|
||||||
}
|
}
|
||||||
// refresh from db ? after save data
|
// refresh from db ? after save data
|
||||||
await loadSellData(store);
|
await loadSellData(store);
|
||||||
|
|
@ -77,39 +61,63 @@ ThunkAction<AppState> addSellItem({required Good good, String? excise}) {
|
||||||
return (Store<AppState> store) async {
|
return (Store<AppState> store) async {
|
||||||
log.i('addSellItem');
|
log.i('addSellItem');
|
||||||
final int? appCompanyId = store.state.userState!.auth!.companyId;
|
final int? appCompanyId = store.state.userState!.auth!.companyId;
|
||||||
String? uuid = store.state.sellState!.transactionState!.uuid;
|
|
||||||
|
|
||||||
Transaction? transaction;
|
Transaction? transaction;
|
||||||
|
|
||||||
if (uuid != null) {
|
final List<Map<String, dynamic>> transactions =
|
||||||
List<Map<String, dynamic>> set = await _dbService.queryRowsWithWhere(
|
await _dbService.queryRowsWithWhere(
|
||||||
transactionTableName,
|
transactionTableName,
|
||||||
'$transactionColumnAppCompanyId = ? '
|
'$transactionColumnAppCompanyId = ? '
|
||||||
' and $transactionColumnStatus = ? '
|
' and $transactionColumnStatus = ? '
|
||||||
' and $transactionColumnType = ?',
|
' and $transactionColumnType = ?',
|
||||||
[appCompanyId, transactionStatusPrepare, transactionTypeSell],
|
[appCompanyId, transactionStatusPrepare, transactionTypeSell],
|
||||||
orderBy: '$transactionColumnCreatedAt desc');
|
orderBy: '$transactionColumnCreatedAt desc');
|
||||||
if (set.isNotEmpty) {
|
if (transactions.isNotEmpty) {
|
||||||
for (Map<String, dynamic> map in set) {
|
for (final Map<String, dynamic> map in transactions) {
|
||||||
Transaction _transaction = Transaction.fromMap(map);
|
transaction = Transaction.fromMap(map);
|
||||||
ProductDao _product =
|
}
|
||||||
ProductDao.fromMap(jsonDecode(_transaction.data!));
|
}
|
||||||
if (_product.id == good.id && _product.excise == excise) {
|
|
||||||
transaction = _transaction;
|
if (transaction == null) {
|
||||||
|
const uuidTool = Uuid();
|
||||||
|
final String uuid = uuidTool.v4();
|
||||||
|
final TransactionData transactionData = TransactionData();
|
||||||
|
transaction = Transaction()
|
||||||
|
..uuid = uuid
|
||||||
|
..status = transactionStatusPrepare
|
||||||
|
..appCompanyId = appCompanyId
|
||||||
|
..type = transactionTypeSell
|
||||||
|
..createdAt = DateTime.now().toIso8601String()
|
||||||
|
..data = jsonEncode(transactionData.toMap());
|
||||||
|
final int returnKey =
|
||||||
|
await _dbService.insert(transactionTableName, transaction.toMap());
|
||||||
|
transaction.id = returnKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
final List<Map<String, dynamic>> recs = await _dbService.queryRowsWithWhere(
|
||||||
|
transactionRecTableName,
|
||||||
|
'$transactionRecTransactionIdRef = ? ',
|
||||||
|
[transaction.id]);
|
||||||
|
ProductDao? item;
|
||||||
|
if (recs.isNotEmpty) {
|
||||||
|
for (final Map<String, dynamic> map in recs) {
|
||||||
|
final TransactionRec transactionRec = TransactionRec.fromMap(map);
|
||||||
|
final ProductDao _item =
|
||||||
|
ProductDao.fromMap(jsonDecode(transactionRec.data!));
|
||||||
|
if (_item.id == good.id) {
|
||||||
|
log.i(map);
|
||||||
|
_item.count = _item.count! + 1;
|
||||||
|
transactionRec.data = jsonEncode(_item.toMap());
|
||||||
|
await _dbService.update(
|
||||||
|
transactionRecTableName, transactionRec.toMap());
|
||||||
|
item = _item;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (transaction != null) {
|
if (item == null) {
|
||||||
ProductDao item = ProductDao.fromMap(jsonDecode(transaction.data!));
|
item = ProductDao()
|
||||||
item..count = item.count! + 1;
|
|
||||||
transaction.data = jsonEncode(item.toMap());
|
|
||||||
transaction.createdAt = DateTime.now().toIso8601String();
|
|
||||||
_dbService.update(transactionTableName, transaction.toMap());
|
|
||||||
} else {
|
|
||||||
ProductDao item = new ProductDao()
|
|
||||||
..id = good.id
|
..id = good.id
|
||||||
..price = good.price
|
..price = good.price
|
||||||
..article = good.articul
|
..article = good.articul
|
||||||
|
|
@ -119,30 +127,22 @@ ThunkAction<AppState> addSellItem({required Good good, String? excise}) {
|
||||||
..excise = excise;
|
..excise = excise;
|
||||||
//category add logic
|
//category add logic
|
||||||
if (good.categoryId != null) {
|
if (good.categoryId != null) {
|
||||||
List<Map<String, dynamic>> set = await _dbService
|
final List<Map<String, dynamic>> set = await _dbService
|
||||||
.queryRowsWithWhere(categoryTableName, 'id = ?', [good.categoryId]);
|
.queryRowsWithWhere(categoryTableName, 'id = ?', [good.categoryId]);
|
||||||
if (set.isNotEmpty) {
|
if (set.isNotEmpty) {
|
||||||
Category category = Category.fromMap(set.first);
|
final Category category = Category.fromMap(set.first);
|
||||||
item.categoryId = category.id;
|
item.categoryId = category.id;
|
||||||
item.categoryName = category.name;
|
item.categoryName = category.name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
final TransactionRec rec = TransactionRec()
|
||||||
if (uuid == null) {
|
..data = jsonEncode(item.toMap())
|
||||||
var uuidTool = Uuid();
|
..transactionId = transaction.id
|
||||||
;
|
..createdAt = DateTime.now().toIso8601String();
|
||||||
uuid = uuidTool.v4();
|
log.i(rec.toMap());
|
||||||
|
await _dbService.insert(transactionRecTableName, rec.toMap());
|
||||||
}
|
}
|
||||||
|
|
||||||
transaction = new Transaction()
|
|
||||||
..uuid = uuid
|
|
||||||
..status = transactionStatusPrepare
|
|
||||||
..appCompanyId = appCompanyId
|
|
||||||
..type = transactionTypeSell
|
|
||||||
..createdAt = DateTime.now().toIso8601String()
|
|
||||||
..data = jsonEncode(item.toMap());
|
|
||||||
await _dbService.insert(transactionTableName, transaction.toMap());
|
|
||||||
}
|
|
||||||
// refresh from db ? after save data
|
// refresh from db ? after save data
|
||||||
await loadSellData(store);
|
await loadSellData(store);
|
||||||
};
|
};
|
||||||
|
|
@ -150,11 +150,21 @@ ThunkAction<AppState> addSellItem({required Good good, String? excise}) {
|
||||||
|
|
||||||
ThunkAction<AppState> removeSellItem({required int transactionId}) {
|
ThunkAction<AppState> removeSellItem({required int transactionId}) {
|
||||||
return (Store<AppState> store) async {
|
return (Store<AppState> store) async {
|
||||||
int? appCompanyId = store.state.userState!.auth!.companyId;
|
final List<Map<String, dynamic>> set = await _dbService
|
||||||
String? uuid = store.state.sellState!.transactionState!.uuid;
|
.queryRowsWithWhere(transactionRecTableName, 'id = ?', [transactionId]);
|
||||||
|
final TransactionRec rec = TransactionRec.fromMap(set.first);
|
||||||
|
|
||||||
int count = await _dbService.delete(transactionTableName, transactionId);
|
final int countdeleted =
|
||||||
log.i('removeSellItem ${count} by transactionId:${transactionId}');
|
await _dbService.delete(transactionRecTableName, transactionId);
|
||||||
|
|
||||||
|
final int? count = await _dbService.queryRowCount(transactionRecTableName,
|
||||||
|
where: '$transactionRecTransactionIdRef = ${rec.transactionId}');
|
||||||
|
if ((count ?? 0) < 1 && rec.transactionId != null) {
|
||||||
|
await _dbService.delete(transactionTableName, rec.transactionId!);
|
||||||
|
}
|
||||||
|
|
||||||
|
log.i('removeSellItem $countdeleted '
|
||||||
|
'last $count by transactionId:$transactionId');
|
||||||
|
|
||||||
// refresh from db ? after save data
|
// refresh from db ? after save data
|
||||||
await loadSellData(store);
|
await loadSellData(store);
|
||||||
|
|
@ -164,15 +174,13 @@ ThunkAction<AppState> removeSellItem({required int transactionId}) {
|
||||||
Future<void> removeAllSellData(Store<AppState> store) async {
|
Future<void> removeAllSellData(Store<AppState> store) async {
|
||||||
try {
|
try {
|
||||||
log.i('removeAllSellData');
|
log.i('removeAllSellData');
|
||||||
int? appCompanyId = store.state.userState!.auth!.companyId;
|
final int? appCompanyId = store.state.userState!.auth!.companyId;
|
||||||
String? uuid = store.state.sellState!.transactionState!.uuid;
|
|
||||||
await _dbService.deleteByWhere(
|
await _dbService.deleteByWhere(
|
||||||
transactionTableName,
|
transactionTableName,
|
||||||
'$transactionColumnAppCompanyId = ? '
|
'$transactionColumnAppCompanyId = ? '
|
||||||
' and $transactionColumnStatus = ? '
|
' and $transactionColumnStatus = ? '
|
||||||
' and ${transactionColumnType} = ?'
|
' and $transactionColumnType = ?',
|
||||||
' and ${transactionColumnUuid} = ?',
|
[appCompanyId, transactionStatusPrepare, transactionTypeSell]);
|
||||||
[appCompanyId, transactionStatusPrepare, transactionTypeSell, uuid]);
|
|
||||||
await loadSellData(store);
|
await loadSellData(store);
|
||||||
} catch (e, stack) {
|
} catch (e, stack) {
|
||||||
log.e('removeAllSellData', e, stack);
|
log.e('removeAllSellData', e, stack);
|
||||||
|
|
@ -182,21 +190,30 @@ Future<void> removeAllSellData(Store<AppState> store) async {
|
||||||
Future<void> loadSellData(Store<AppState> store) async {
|
Future<void> loadSellData(Store<AppState> store) async {
|
||||||
try {
|
try {
|
||||||
log.i('loadSellData');
|
log.i('loadSellData');
|
||||||
int? appCompanyId = store.state.userState!.auth!.companyId;
|
final int? appCompanyId = store.state.userState!.auth!.companyId;
|
||||||
List<Map<String, dynamic>> set = await _dbService.queryRowsWithWhere(
|
final List<Map<String, dynamic>> set = await _dbService.queryRowsWithWhere(
|
||||||
transactionTableName,
|
transactionTableName,
|
||||||
'$transactionColumnAppCompanyId = ? and $transactionColumnStatus = ? and ${transactionColumnType} = ?',
|
'$transactionColumnAppCompanyId = ?'
|
||||||
|
' and $transactionColumnStatus = ?'
|
||||||
|
' and $transactionColumnType = ?',
|
||||||
[appCompanyId, transactionStatusPrepare, transactionTypeSell],
|
[appCompanyId, transactionStatusPrepare, transactionTypeSell],
|
||||||
orderBy: '$transactionColumnCreatedAt desc');
|
orderBy: '$transactionColumnCreatedAt desc');
|
||||||
List<ProductDao> list = [];
|
final List<ProductDao> list = [];
|
||||||
String? uuid;
|
String? uuid;
|
||||||
for (Map<String, dynamic> map in set) {
|
if (set.isNotEmpty) {
|
||||||
Transaction transaction = Transaction.fromMap(map);
|
final Transaction transaction = Transaction.fromMap(set.first);
|
||||||
uuid = transaction.uuid;
|
uuid = transaction.uuid;
|
||||||
ProductDao productDao = ProductDao.fromMap(jsonDecode(transaction.data!));
|
final List<Map<String, dynamic>> recs =
|
||||||
productDao.transactionId = transaction.id;
|
await _dbService.queryRowsWithWhere(transactionRecTableName,
|
||||||
|
'$transactionRecTransactionIdRef = ? ', [transaction.id],
|
||||||
|
orderBy: '$transactionRecColumnCreatedAt desc');
|
||||||
|
for (final Map<String, dynamic> map in recs) {
|
||||||
|
final TransactionRec rec = TransactionRec.fromMap(map);
|
||||||
|
final ProductDao productDao = ProductDao.fromMap(jsonDecode(rec.data!));
|
||||||
|
productDao.transactionId = rec.id;
|
||||||
list.add(productDao);
|
list.add(productDao);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
store.dispatch(SetSellStateAction(SellState(
|
store.dispatch(SetSellStateAction(SellState(
|
||||||
items: list, transactionState: TransactionState()..uuid = uuid)));
|
items: list, transactionState: TransactionState()..uuid = uuid)));
|
||||||
} catch (e, stack) {
|
} catch (e, stack) {
|
||||||
|
|
|
||||||
|
|
@ -4,13 +4,14 @@ import 'package:satu/core/base/base_service.dart';
|
||||||
import 'package:satu/core/entity/category_entity.dart';
|
import 'package:satu/core/entity/category_entity.dart';
|
||||||
import 'package:satu/core/entity/goods_entity.dart';
|
import 'package:satu/core/entity/goods_entity.dart';
|
||||||
import 'package:satu/core/entity/transaction_entity.dart';
|
import 'package:satu/core/entity/transaction_entity.dart';
|
||||||
|
import 'package:satu/core/entity/transaction_rec_entity.dart';
|
||||||
import 'package:sqflite/sqflite.dart';
|
import 'package:sqflite/sqflite.dart';
|
||||||
import 'package:path/path.dart';
|
import 'package:path/path.dart';
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
|
|
||||||
class DbService extends BaseService {
|
class DbService extends BaseService {
|
||||||
static const _databaseName = "AmanSatuDb.db";
|
static const _databaseName = 'AmanSatuDb.db';
|
||||||
static const _databaseVersion = 1;
|
static const _databaseVersion = 2;
|
||||||
|
|
||||||
// make this a singleton class
|
// make this a singleton class
|
||||||
DbService._privateConstructor();
|
DbService._privateConstructor();
|
||||||
|
|
@ -39,7 +40,8 @@ class DbService extends BaseService {
|
||||||
//Goods table
|
//Goods table
|
||||||
await db.execute('DROP TABLE IF EXISTS $goodTableName;');
|
await db.execute('DROP TABLE IF EXISTS $goodTableName;');
|
||||||
await db.execute('DROP TABLE IF EXISTS $categoryTableName;');
|
await db.execute('DROP TABLE IF EXISTS $categoryTableName;');
|
||||||
//await db.execute('DROP TABLE IF EXISTS $Voucher_tableName;');
|
await db.execute('DROP TABLE IF EXISTS $transactionTableName;');
|
||||||
|
await db.execute('DROP TABLE IF EXISTS $transactionRecTableName;');
|
||||||
log.i('dropped tables');
|
log.i('dropped tables');
|
||||||
_onCreate(db, newVersion);
|
_onCreate(db, newVersion);
|
||||||
}
|
}
|
||||||
|
|
@ -47,6 +49,7 @@ class DbService extends BaseService {
|
||||||
Future _onCreate(Database db, int version) async {
|
Future _onCreate(Database db, int version) async {
|
||||||
log.i('create tables');
|
log.i('create tables');
|
||||||
//Goods table
|
//Goods table
|
||||||
|
|
||||||
await db.execute('''
|
await db.execute('''
|
||||||
CREATE TABLE IF NOT EXISTS $goodTableName (
|
CREATE TABLE IF NOT EXISTS $goodTableName (
|
||||||
$GoodColumnId integer primary key unique,
|
$GoodColumnId integer primary key unique,
|
||||||
|
|
@ -82,6 +85,17 @@ class DbService extends BaseService {
|
||||||
$transactionColumnCreatedAt text not null
|
$transactionColumnCreatedAt text not null
|
||||||
);
|
);
|
||||||
''');
|
''');
|
||||||
|
await db.execute('''
|
||||||
|
CREATE TABLE IF NOT EXISTS $transactionRecTableName (
|
||||||
|
$transactionRecColumnId integer primary key autoincrement,
|
||||||
|
$transactionRecTransactionIdRef integer not null,
|
||||||
|
$transactionRecColumnData text,
|
||||||
|
$transactionRecColumnCreatedAt text not null,
|
||||||
|
CONSTRAINT fk_$transactionTableName
|
||||||
|
FOREIGN KEY ($transactionRecTransactionIdRef)
|
||||||
|
REFERENCES $transactionTableName($transactionColumnId)
|
||||||
|
);
|
||||||
|
''');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inserts a row in the database where each key in the Map is a column name
|
// Inserts a row in the database where each key in the Map is a column name
|
||||||
|
|
@ -125,12 +139,21 @@ class DbService extends BaseService {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<Map<String, dynamic>?> queryById(String table, int id,
|
||||||
|
{String? idColumnName}) async {
|
||||||
|
final Database db = await instance.database;
|
||||||
|
final List<Map<String, dynamic>> result = await db
|
||||||
|
.query(table, where: '${idColumnName ?? 'id'} = ?', whereArgs: [id]);
|
||||||
|
|
||||||
|
return result.isNotEmpty ? result.first : null;
|
||||||
|
}
|
||||||
|
|
||||||
// All of the methods (insert, query, update, delete) can also be done using
|
// All of the methods (insert, query, update, delete) can also be done using
|
||||||
// raw SQL commands. This method uses a raw query to give the row count.
|
// raw SQL commands. This method uses a raw query to give the row count.
|
||||||
Future<int?> queryRowCount(String table) async {
|
Future<int?> queryRowCount(String table, {String? where}) async {
|
||||||
final Database db = await instance.database;
|
final Database db = await instance.database;
|
||||||
final int? result =
|
final int? result = Sqflite.firstIntValue(await db.rawQuery(
|
||||||
Sqflite.firstIntValue(await db.rawQuery('SELECT COUNT(*) FROM $table'));
|
'SELECT COUNT(*) FROM $table ${where != null ? 'where $where' : ''}'));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue