|
@@ -6,7 +6,7 @@ import 'package:sqflite/sqflite.dart';
|
|
import '../models/models.dart';
|
|
import '../models/models.dart';
|
|
|
|
|
|
class RepoService<T> {
|
|
class RepoService<T> {
|
|
- static int dbVersion = 16;
|
|
|
|
|
|
+ static int dbVersion = 17;
|
|
static String dbName = 'joshipos026.db';
|
|
static String dbName = 'joshipos026.db';
|
|
static const String id = Basico.identificadorWeb;
|
|
static const String id = Basico.identificadorWeb;
|
|
static const String idLocal = Basico.identificadorLocal;
|
|
static const String idLocal = Basico.identificadorLocal;
|
|
@@ -500,6 +500,62 @@ class RepoService<T> {
|
|
''');
|
|
''');
|
|
|
|
|
|
break;
|
|
break;
|
|
|
|
+
|
|
|
|
+ case 16:
|
|
|
|
+ await db.execute('''
|
|
|
|
+ CREATE TABLE Usuario (
|
|
|
|
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
+ nombre TEXT,
|
|
|
|
+ apellidos TEXT,
|
|
|
|
+ correo TEXT,
|
|
|
|
+ celular TEXT,
|
|
|
|
+ celularPersonal TEXT,
|
|
|
|
+ rol INTEGER,
|
|
|
|
+ genero BOOLEAN,
|
|
|
|
+ estatus INTEGER,
|
|
|
|
+ imagen TEXT,
|
|
|
|
+ rfc TEXT,
|
|
|
|
+ razonSocial TEXT,
|
|
|
|
+ calle TEXT,
|
|
|
|
+ numeroExterior TEXT,
|
|
|
|
+ colonia TEXT,
|
|
|
|
+ codigoPostal TEXT,
|
|
|
|
+ idCiudad INTEGER,
|
|
|
|
+ idEstado INTEGER,
|
|
|
|
+ idSucursal INTEGER,
|
|
|
|
+ turno TEXT,
|
|
|
|
+ eliminado TEXT,
|
|
|
|
+ creado TEXT,
|
|
|
|
+ modificado TEXT,
|
|
|
|
+ idLocal INTEGER
|
|
|
|
+ )
|
|
|
|
+ ''');
|
|
|
|
+
|
|
|
|
+ await db.execute('''
|
|
|
|
+ CREATE TABLE Permiso (
|
|
|
|
+ id TEXT PRIMARY KEY,
|
|
|
|
+ idModulo TEXT,
|
|
|
|
+ nombre TEXT,
|
|
|
|
+ descripcion TEXT,
|
|
|
|
+ eliminado TEXT,
|
|
|
|
+ creado TEXT,
|
|
|
|
+ modificado TEXT
|
|
|
|
+ )
|
|
|
|
+ ''');
|
|
|
|
+
|
|
|
|
+ await db.execute('''
|
|
|
|
+ CREATE TABLE UsuarioPermiso (
|
|
|
|
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
+ idUsuario INTEGER,
|
|
|
|
+ idPermiso TEXT,
|
|
|
|
+ asignado TEXT,
|
|
|
|
+ modificado TEXT,
|
|
|
|
+ eliminado TEXT,
|
|
|
|
+ idLocal INTEGER
|
|
|
|
+ )
|
|
|
|
+ ''');
|
|
|
|
+
|
|
|
|
+ break;
|
|
}
|
|
}
|
|
oldVersion++;
|
|
oldVersion++;
|
|
}
|
|
}
|
|
@@ -509,15 +565,6 @@ class RepoService<T> {
|
|
try {
|
|
try {
|
|
print("Guardando modelo en la base de datos: ${model.runtimeType}");
|
|
print("Guardando modelo en la base de datos: ${model.runtimeType}");
|
|
|
|
|
|
- // Comprobar si es un modelo que obtiene las fechas del servidor (Producto o CategoriaProducto)
|
|
|
|
- if (model is Producto || model is CategoriaProducto) {
|
|
|
|
- // No hacer nada, las fechas vienen del servidor
|
|
|
|
- print("Usando fechas del servidor para ${model.runtimeType}");
|
|
|
|
- } else if (model is Basico) {
|
|
|
|
- // Generar las fechas localmente para otros modelos
|
|
|
|
- asignarFechasLocalmente(model);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
// Convertir el modelo a JSON para la base de datos
|
|
// Convertir el modelo a JSON para la base de datos
|
|
String modelo = json.encode(model, toEncodable: toEncodable);
|
|
String modelo = json.encode(model, toEncodable: toEncodable);
|
|
print("Modelo convertido a JSON: $modelo");
|
|
print("Modelo convertido a JSON: $modelo");
|
|
@@ -526,30 +573,62 @@ class RepoService<T> {
|
|
var dbClient = await db;
|
|
var dbClient = await db;
|
|
String nombreTabla = model.runtimeType.toString();
|
|
String nombreTabla = model.runtimeType.toString();
|
|
|
|
|
|
- int? id = modelMap['id'];
|
|
|
|
-
|
|
|
|
- if (id == null || id == 0) {
|
|
|
|
- modelMap.remove('id');
|
|
|
|
- }
|
|
|
|
|
|
+ // Verificar si el modelo es de tipo Permiso (con id de tipo String)
|
|
|
|
+ if (model is Permiso) {
|
|
|
|
+ String? id = modelMap['id'];
|
|
|
|
|
|
- List<Map> existing = id != null && id > 0
|
|
|
|
- ? await dbClient!.query(nombreTabla, where: 'id = ?', whereArgs: [id])
|
|
|
|
- : [];
|
|
|
|
|
|
+ if (id == null || id.isEmpty) {
|
|
|
|
+ throw Exception('El ID del permiso no puede ser nulo o vacío');
|
|
|
|
+ }
|
|
|
|
|
|
- if (existing.isNotEmpty) {
|
|
|
|
- print("Actualizando registro existente con ID: $id");
|
|
|
|
- await dbClient!.update(
|
|
|
|
|
|
+ List<Map> existing = await dbClient!.query(
|
|
nombreTabla,
|
|
nombreTabla,
|
|
- modelMap,
|
|
|
|
where: 'id = ?',
|
|
where: 'id = ?',
|
|
whereArgs: [id],
|
|
whereArgs: [id],
|
|
);
|
|
);
|
|
|
|
+
|
|
|
|
+ if (existing.isNotEmpty) {
|
|
|
|
+ print("Actualizando registro existente con ID: $id");
|
|
|
|
+ await dbClient!.update(
|
|
|
|
+ nombreTabla,
|
|
|
|
+ modelMap,
|
|
|
|
+ where: 'id = ?',
|
|
|
|
+ whereArgs: [id],
|
|
|
|
+ );
|
|
|
|
+ } else {
|
|
|
|
+ print(
|
|
|
|
+ "Insertando nuevo registro en la tabla $nombreTabla con ID: $id");
|
|
|
|
+ await dbClient!.insert(nombreTabla, modelMap);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return 1;
|
|
} else {
|
|
} else {
|
|
- print("Insertando nuevo registro en la tabla $nombreTabla");
|
|
|
|
- id = await dbClient!.insert(nombreTabla, modelMap);
|
|
|
|
- }
|
|
|
|
|
|
+ int? id = modelMap['id'];
|
|
|
|
+
|
|
|
|
+ if (id == null || id == 0) {
|
|
|
|
+ modelMap.remove('id');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<Map> existing = id != null && id > 0
|
|
|
|
+ ? await dbClient!
|
|
|
|
+ .query(nombreTabla, where: 'id = ?', whereArgs: [id])
|
|
|
|
+ : [];
|
|
|
|
+
|
|
|
|
+ if (existing.isNotEmpty) {
|
|
|
|
+ print("Actualizando registro existente con ID: $id");
|
|
|
|
+ await dbClient!.update(
|
|
|
|
+ nombreTabla,
|
|
|
|
+ modelMap,
|
|
|
|
+ where: 'id = ?',
|
|
|
|
+ whereArgs: [id],
|
|
|
|
+ );
|
|
|
|
+ } else {
|
|
|
|
+ print("Insertando nuevo registro en la tabla $nombreTabla");
|
|
|
|
+ id = await dbClient!.insert(nombreTabla, modelMap);
|
|
|
|
+ }
|
|
|
|
|
|
- return id!;
|
|
|
|
|
|
+ return id!;
|
|
|
|
+ }
|
|
} catch (e) {
|
|
} catch (e) {
|
|
print('Error al guardar en dynamic: $e');
|
|
print('Error al guardar en dynamic: $e');
|
|
return 0;
|
|
return 0;
|
|
@@ -627,24 +706,7 @@ class RepoService<T> {
|
|
|
|
|
|
dynamic toEncodable(dynamic item) {
|
|
dynamic toEncodable(dynamic item) {
|
|
print("Serializando objeto: $item");
|
|
print("Serializando objeto: $item");
|
|
- if (item is Pedido) {
|
|
|
|
- return item.toJson();
|
|
|
|
- } else if (item is PedidoProducto) {
|
|
|
|
- return item.toJson();
|
|
|
|
- } else if (item is PedidoProductoTopping) {
|
|
|
|
- return item.toJson();
|
|
|
|
- } else if (item is Producto) {
|
|
|
|
- return item.toJson();
|
|
|
|
- } else if (item is CategoriaProducto) {
|
|
|
|
- return item.toJson();
|
|
|
|
- } else if (item is Sucursal) {
|
|
|
|
- print("Serializando Sucursal: ${item.nombre}, ${item.descripcion}");
|
|
|
|
- return item.toJson();
|
|
|
|
- } else if (item is Variable) {
|
|
|
|
- return item.toJson();
|
|
|
|
- }
|
|
|
|
- throw UnsupportedError(
|
|
|
|
- 'Type not supported for serialization: ${item.runtimeType}');
|
|
|
|
|
|
+ return item.toJson();
|
|
}
|
|
}
|
|
|
|
|
|
Future<List<T>> obtenerTodos({String orderBy = 'id DESC'}) async {
|
|
Future<List<T>> obtenerTodos({String orderBy = 'id DESC'}) async {
|
|
@@ -661,6 +723,8 @@ class RepoService<T> {
|
|
return Pedido.fromJson(map) as T;
|
|
return Pedido.fromJson(map) as T;
|
|
case Producto:
|
|
case Producto:
|
|
return Producto.fromJson(map) as T;
|
|
return Producto.fromJson(map) as T;
|
|
|
|
+ case Usuario:
|
|
|
|
+ return Usuario.fromJson(map) as T;
|
|
default:
|
|
default:
|
|
throw Exception('Tipo no soportado');
|
|
throw Exception('Tipo no soportado');
|
|
}
|
|
}
|
|
@@ -959,4 +1023,60 @@ class RepoService<T> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ Future<void> sincronizarPermisos(List<Permiso> permisosApi) async {
|
|
|
|
+ var db = await RepoService().db;
|
|
|
|
+ var permisosLocalesQuery = await db!.query('Permiso');
|
|
|
|
+ List<Permiso> permisosLocales =
|
|
|
|
+ permisosLocalesQuery.map((e) => Permiso.fromJson(e)).toList();
|
|
|
|
+
|
|
|
|
+ for (var permisoApi in permisosApi) {
|
|
|
|
+ var permisoLocal = permisosLocales.firstWhere(
|
|
|
|
+ (permiso) => permiso.id == permisoApi.id,
|
|
|
|
+ orElse: () => Permiso(),
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ if (permisoLocal.id != null && permisoApi.modificado != null) {
|
|
|
|
+ await RepoService().guardar(permisoApi);
|
|
|
|
+ } else if (permisoLocal.id == null) {
|
|
|
|
+ await RepoService().guardar(permisoApi);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Future<void> sincronizarUsuarios(List<Usuario> usuariosApi) async {
|
|
|
|
+ var db = await RepoService().db;
|
|
|
|
+
|
|
|
|
+ var usuariosLocalesQuery = await db!.query('Usuario');
|
|
|
|
+ List<Usuario> usuariosLocales =
|
|
|
|
+ usuariosLocalesQuery.map((e) => Usuario.fromJson(e)).toList();
|
|
|
|
+
|
|
|
|
+ for (var usuarioApi in usuariosApi) {
|
|
|
|
+ var usuarioLocal = usuariosLocales.firstWhere(
|
|
|
|
+ (usuario) => usuario.id == usuarioApi.id,
|
|
|
|
+ orElse: () => Usuario(),
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ if (usuarioLocal.id != 0) {
|
|
|
|
+ await RepoService().guardar(usuarioApi);
|
|
|
|
+ await _guardarPermisosUsuario(db, usuarioApi.id!, usuarioApi.permisos!);
|
|
|
|
+ } else {
|
|
|
|
+ await RepoService().guardar(usuarioApi);
|
|
|
|
+ await _guardarPermisosUsuario(db, usuarioApi.id!, usuarioApi.permisos!);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Future<void> _guardarPermisosUsuario(
|
|
|
|
+ Database db, int idUsuario, List<String> permisos) async {
|
|
|
|
+ await db.delete('UsuarioPermiso',
|
|
|
|
+ where: 'idUsuario = ?', whereArgs: [idUsuario]);
|
|
|
|
+
|
|
|
|
+ for (var idPermiso in permisos) {
|
|
|
|
+ await db.insert('UsuarioPermiso', {
|
|
|
|
+ 'idUsuario': idUsuario,
|
|
|
|
+ 'idPermiso': idPermiso,
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|