Files
rog_app/lib/utils/database_helper.dart

319 lines
9.6 KiB
Dart
Raw Normal View History

2022-07-09 22:51:34 +05:30
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:rogapp/model/destination.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
2022-11-14 22:22:41 +05:30
import '../model/rog.dart';
2023-10-06 16:25:21 +05:30
class DatabaseHelper {
2022-07-09 22:51:34 +05:30
DatabaseHelper._privateConstructor();
static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
static Database? _database;
Future<Database> get database async => _database ??= await _initDatabase();
Future<Database> _initDatabase() async {
Directory documentDirectory = await getApplicationDocumentsDirectory();
String path = join(documentDirectory.path, 'rog.db');
// return await openDatabase(
// path,
// version: 1,
// onCreate: _onCreate,
// );
2023-10-06 16:25:21 +05:30
return openDatabase(
join(
await getDatabasesPath(),
'rog.db',
),
version: 1,
onCreate: _onCreate);
2022-07-09 22:51:34 +05:30
}
Future _onCreate(Database db, int version) async {
await db.execute('''
CREATE TABLE destination(
location_id INTEGER PRIMARY KEY,
name TEXT,
address TEXT,
phone TEXT,
email TEXT,
webcontents TEXT,
videos TEXT,
category TEXT,
series INTEGER,
lat REAL,
lon REAL,
list_order INTEGER,
photos TEXT,
checkin_radious REAL,
2023-01-24 13:00:55 +05:30
sub_loc_id TEXT,
2022-07-14 23:10:24 +05:30
auto_checkin INTEGER,
selected INTEGER,
2022-09-01 19:14:18 +05:30
checkedin INTEGER,
cp REAL,
checkin_point REAL,
2022-10-30 21:43:29 +05:30
buy_point REAL,
2023-09-12 16:00:58 +05:30
hidden_location INTEGER,
2023-09-14 00:08:53 +05:30
checkin_image TEXT,
2023-09-14 22:53:51 +05:30
buypoint_image TEXT,
forced_checkin INTEGER,
recipt_times INTEGER
2022-07-09 22:51:34 +05:30
)
''');
2022-10-12 21:46:17 +05:30
2023-10-06 16:25:21 +05:30
await db.execute('''
2022-10-12 21:46:17 +05:30
CREATE TABLE rogaining(
rog_id INTEGER PRIMARY KEY AUTOINCREMENT,
course_id INTEGER,
location_id INTEGER,
user_id INTEGER,
lat REAL,
lon REAL,
2022-11-14 22:22:41 +05:30
time_stamp INTEGER,
image TEXT
2022-10-12 21:46:17 +05:30
)
''');
2023-10-06 16:25:21 +05:30
await db.execute('''
2022-11-14 22:22:41 +05:30
CREATE TABLE rog(
id INTEGER PRIMARY KEY AUTOINCREMENT,
team_name TEXT,
event_code TEXT,
user_id INTEGER,
cp_number INTEGER,
checkintime INTEGER,
image TEXT,
rog_action_type INTEGER
)
''');
2022-10-12 21:46:17 +05:30
}
2022-11-14 22:22:41 +05:30
Future<List<Rog>> allRogianing() async {
2022-10-12 21:46:17 +05:30
Database db = await instance.database;
2022-11-14 22:22:41 +05:30
var rog = await db.query('rog');
2023-10-06 16:25:21 +05:30
List<Rog> roglist =
rog.isNotEmpty ? rog.map((e) => Rog.fromMap(e)).toList() : [];
//print("--------- $rog");
2022-10-12 21:46:17 +05:30
return roglist;
2022-07-09 22:51:34 +05:30
}
2022-11-14 22:22:41 +05:30
Future<List<Rog>> getRogainingByLatLon(double lat, double lon) async {
2022-10-12 21:46:17 +05:30
Database db = await instance.database;
2023-08-16 14:53:32 +05:30
var rog = await db.query('rog', where: "lat = $lat and lon= $lon");
2023-10-06 16:25:21 +05:30
List<Rog> roglist =
rog.isNotEmpty ? rog.map((e) => Rog.fromMap(e)).toList() : [];
2022-10-12 21:46:17 +05:30
return roglist;
}
Future clearSelection() async {
Database db = await instance.database;
2023-10-06 16:25:21 +05:30
Map<String, dynamic> rowClear = {"selected": false};
return await db.update("destination", rowClear);
}
Future<int> toggleSelecttion(Destination dest) async {
Database db = await instance.database;
2023-10-06 16:25:21 +05:30
bool val = !dest.selected!;
2023-10-06 16:25:21 +05:30
Map<String, dynamic> rowTarget = {"selected": val};
await clearSelection();
2023-10-06 16:25:21 +05:30
return await db.update("destination", rowTarget,
where: 'location_id = ?', whereArgs: [dest.location_id!]);
}
2022-11-14 22:22:41 +05:30
Future<int> deleteRogaining(int id) async {
2022-10-30 21:43:29 +05:30
Database db = await instance.database;
2023-08-16 14:53:32 +05:30
var rog = await db.delete('rog', where: "id = $id");
2022-10-12 21:46:17 +05:30
int ret = rog > 0 ? rog : -1;
2023-10-06 16:25:21 +05:30
2022-10-12 21:46:17 +05:30
return ret;
}
Future<void> deleteAllRogaining() async {
Database db = await instance.database;
2022-11-14 22:22:41 +05:30
await db.delete('rog');
2022-10-12 21:46:17 +05:30
}
2023-10-06 16:25:21 +05:30
Future<bool> isRogAlreadyAvailable(int id) async {
2022-10-12 21:46:17 +05:30
Database db = await instance.database;
2023-08-16 14:53:32 +05:30
var rog = await db.query('rog', where: "id = $id");
return rog.isNotEmpty ? true : false;
2022-10-12 21:46:17 +05:30
}
2023-10-06 16:25:21 +05:30
Future<int?> latestGoal() async {
2023-01-31 10:14:23 +05:30
Database db = await instance.database;
2023-10-06 16:25:21 +05:30
return Sqflite.firstIntValue(
await db.rawQuery('SELECT MAX(checkintime) FROM rog'));
2023-01-31 10:14:23 +05:30
}
2022-11-14 22:22:41 +05:30
Future<int> insertRogaining(Rog rog) async {
2022-10-12 21:46:17 +05:30
Database db = await instance.database;
2023-10-06 16:25:21 +05:30
int? nextOrder =
Sqflite.firstIntValue(await db.rawQuery('SELECT MAX(id) FROM rog'));
2023-08-16 14:53:32 +05:30
nextOrder = nextOrder ?? 0;
nextOrder = nextOrder + 1;
rog.id = nextOrder;
2022-10-12 21:46:17 +05:30
int res = await db.insert(
2022-11-14 22:22:41 +05:30
'rog',
2022-10-12 21:46:17 +05:30
rog.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
2023-10-06 16:25:21 +05:30
//print("------ database helper insert $res-----------::::::::");
2022-10-12 21:46:17 +05:30
return res;
}
2022-07-09 22:51:34 +05:30
Future<List<Destination>> getDestinations() async {
Database db = await instance.database;
2022-09-01 19:14:18 +05:30
var dest = await db.query('destination', orderBy: 'list_order');
2023-10-06 16:25:21 +05:30
List<Destination> destList =
dest.isNotEmpty ? dest.map((e) => Destination.fromMap(e)).toList() : [];
//print("--------- $destList");
2022-07-09 22:51:34 +05:30
return destList;
}
2022-11-14 22:22:41 +05:30
Future<List<Destination>> getDestinationById(int id) async {
Database db = await instance.database;
2023-08-16 14:53:32 +05:30
var rog = await db.query('destination', where: "location_id = $id");
2023-10-06 16:25:21 +05:30
List<Destination> deslist =
rog.isNotEmpty ? rog.map((e) => Destination.fromMap(e)).toList() : [];
2022-11-14 22:22:41 +05:30
return deslist;
}
2023-10-06 16:25:21 +05:30
Future<List<Destination>> getDestinationByLatLon(
double lat, double lon) async {
2022-07-09 22:51:34 +05:30
Database db = await instance.database;
2023-10-06 16:25:21 +05:30
var dest = await db.query('destination',
where: "lat = $lat and lon= $lon", orderBy: 'list_order');
List<Destination> destList =
dest.isNotEmpty ? dest.map((e) => Destination.fromMap(e)).toList() : [];
2022-07-09 22:51:34 +05:30
return destList;
}
2023-08-16 14:53:32 +05:30
Future<int> deleteDestination(int locationId) async {
2022-07-09 22:51:34 +05:30
Database db = await instance.database;
2023-10-06 16:25:21 +05:30
var dest =
await db.delete('destination', where: "location_id = $locationId");
2022-07-09 22:51:34 +05:30
int ret = dest > 0 ? dest : -1;
2022-09-23 18:40:17 +05:30
//after deleting set correct order
await setOrder();
2023-10-06 16:25:21 +05:30
2022-07-09 22:51:34 +05:30
return ret;
}
2023-10-06 16:25:21 +05:30
Future<void> setOrder() async {
2022-09-23 18:40:17 +05:30
Database db = await instance.database;
var byOrder = await db.query('destination', orderBy: 'list_order');
2023-08-16 14:53:32 +05:30
List<Destination> desDb = byOrder.isNotEmpty
2023-10-06 16:25:21 +05:30
? byOrder.map((e) => Destination.fromMap(e)).toList()
: [];
2022-09-23 18:40:17 +05:30
int order = 1;
2023-10-06 16:25:21 +05:30
for (Destination d in desDb) {
Map<String, dynamic> rowTarget = {"list_order": order};
2022-09-23 18:40:17 +05:30
2023-10-06 16:25:21 +05:30
await db.update("destination", rowTarget,
where: 'location_id = ?', whereArgs: [d.location_id]);
2022-09-23 18:40:17 +05:30
order += 1;
}
}
2022-09-22 20:36:56 +05:30
Future<void> deleteAllDestinations() async {
Database db = await instance.database;
await db.delete('destination');
}
2023-10-06 16:25:21 +05:30
Future<bool> isAlreadyAvailable(int locationId) async {
2022-07-10 23:50:43 +05:30
Database db = await instance.database;
2023-10-06 16:25:21 +05:30
var dest =
await db.query('destination', where: "location_id = $locationId");
2023-08-16 14:53:32 +05:30
return dest.isNotEmpty ? true : false;
2022-07-10 23:50:43 +05:30
}
2022-07-09 22:51:34 +05:30
Future<int> insertDestination(Destination dest) async {
Database db = await instance.database;
2023-10-06 16:25:21 +05:30
int? nextOrder = Sqflite.firstIntValue(
await db.rawQuery('SELECT MAX(list_order) FROM destination'));
2023-08-16 14:53:32 +05:30
nextOrder = nextOrder ?? 0;
nextOrder = nextOrder + 1;
dest.list_order = nextOrder;
2022-07-09 22:51:34 +05:30
int res = await db.insert(
'destination',
dest.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
2022-11-01 23:47:35 +05:30
//print("------ database helper insert ${dest.toMap()}-----------::::::::");
2022-07-09 22:51:34 +05:30
return res;
}
2023-10-06 16:25:21 +05:30
Future<int> updateCancelBuyPoint(Destination destination) async {
//print("---- updating puypint image in db -----");
2023-09-15 17:19:26 +05:30
Database db = await instance.database;
2023-10-06 16:25:21 +05:30
Map<String, dynamic> row = {"buy_point": 0};
return await db.update("destination", row,
where: 'location_id = ?', whereArgs: [destination.location_id!]);
2023-09-15 17:19:26 +05:30
}
2023-10-06 16:25:21 +05:30
Future<int> updateBuyPoint(Destination destination, String imageUrl) async {
//print("---- updating puypint image in db -----");
2023-09-14 00:08:53 +05:30
Database db = await instance.database;
2023-10-06 16:25:21 +05:30
Map<String, dynamic> row = {"buypoint_image": imageUrl};
return await db.update("destination", row,
where: 'location_id = ?', whereArgs: [destination.location_id!]);
2023-09-14 00:08:53 +05:30
}
2023-10-06 16:25:21 +05:30
Future<int> updateAction(Destination destination, bool checkin) async {
2022-07-14 23:10:24 +05:30
Database db = await instance.database;
int act = checkin == false ? 0 : 1;
2023-10-06 16:25:21 +05:30
Map<String, dynamic> row = {"checkedin": act};
return await db.update("destination", row,
where: 'location_id = ?', whereArgs: [destination.location_id!]);
2022-07-14 23:10:24 +05:30
}
2023-10-06 16:25:21 +05:30
Future<void> updateOrder(Destination d, int dir) async {
2022-09-22 20:36:56 +05:30
Database db = await instance.database;
2023-10-06 16:25:21 +05:30
var target = await db.query('destination',
where: "list_order = ${d.list_order! + dir}");
var dest =
await db.query('destination', where: "location_id = ${d.location_id}");
2022-09-23 18:40:17 +05:30
2023-10-06 16:25:21 +05:30
// print("--- target in db is $target");
// print("--- destine in db is $dest");
2022-09-22 20:36:56 +05:30
2023-10-06 16:25:21 +05:30
if (target.isNotEmpty) {
2023-08-16 14:53:32 +05:30
List<Destination> targetIndb = target.isNotEmpty
2023-10-06 16:25:21 +05:30
? target.map((e) => Destination.fromMap(e)).toList()
: [];
2022-09-22 20:36:56 +05:30
2023-08-16 14:53:32 +05:30
List<Destination> destIndb = dest.isNotEmpty
2023-10-06 16:25:21 +05:30
? dest.map((e) => Destination.fromMap(e)).toList()
: [];
2022-09-22 20:36:56 +05:30
2023-10-06 16:25:21 +05:30
Map<String, dynamic> rowTarget = {"list_order": d.list_order};
2022-09-22 20:36:56 +05:30
2023-08-16 14:53:32 +05:30
Map<String, dynamic> rowDes = {
"list_order": destIndb[0].list_order! + dir
2022-09-22 20:36:56 +05:30
};
2022-09-23 18:40:17 +05:30
// print("--- target destination is ${target_indb[0].location_id}");
// print("--- destine destination is is ${dest_indb[0].location_id}");
2023-10-06 16:25:21 +05:30
await db.update("destination", rowTarget,
where: 'location_id = ?', whereArgs: [targetIndb[0].location_id]);
await db.update("destination", rowDes,
where: 'location_id = ?', whereArgs: [destIndb[0].location_id]);
2022-09-22 20:36:56 +05:30
}
}
2022-07-09 22:51:34 +05:30
// Future<int?> getPending() async{
// Database db = await instance.database;
// return await Sqflite.firstIntValue(await db.rawQuery("SELECT COUNT(*) FROM incidents"));
// }
}