117 lines
3.8 KiB
Dart
117 lines
3.8 KiB
Dart
import '../database_helper.dart';
|
|
|
|
class IncidentesDao {
|
|
final _db = DatabaseHelper.instance;
|
|
|
|
Future<int> registrar({
|
|
int? alarmaId,
|
|
int? clienteId,
|
|
String? clienteNombre,
|
|
required String tipo,
|
|
required String operador,
|
|
String? notas,
|
|
}) async {
|
|
final db = await _db.db;
|
|
return db.insert('incidentes', {
|
|
'alarma_id': alarmaId,
|
|
'cliente_id': clienteId,
|
|
'cliente_nombre': clienteNombre,
|
|
'tipo': tipo,
|
|
'operador': operador,
|
|
'notas': notas,
|
|
'timestamp': DateTime.now().toIso8601String(),
|
|
'resuelto': 0,
|
|
'sincronizado': 0,
|
|
});
|
|
}
|
|
|
|
Future<List<Map<String, dynamic>>> getRecientes({int limit = 50}) async {
|
|
final db = await _db.db;
|
|
return db.query('incidentes', orderBy: 'timestamp DESC', limit: limit);
|
|
}
|
|
|
|
Future<List<Map<String, dynamic>>> getPendientes() async {
|
|
final db = await _db.db;
|
|
return db.query('incidentes', where: 'sincronizado = 0');
|
|
}
|
|
|
|
Future<int> marcarResuelto(int id, String notas) async {
|
|
final db = await _db.db;
|
|
return db.update(
|
|
'incidentes',
|
|
{'resuelto': 1, 'notas': notas, 'sincronizado': 0},
|
|
where: 'id = ?',
|
|
whereArgs: [id],
|
|
);
|
|
}
|
|
|
|
// Comentario explicativo: Marca un incidente como sincronizado (sincronizado=1) tras enviarse exitosamente a Centinel
|
|
Future<int> marcarSincronizado(int id) async {
|
|
final db = await _db.db;
|
|
return db.update(
|
|
'incidentes',
|
|
{'sincronizado': 1},
|
|
where: 'id = ?',
|
|
whereArgs: [id],
|
|
);
|
|
}
|
|
|
|
// Comentario explicativo: Consulta incidentes aplicando filtros opcionales de cliente, operador, resolución y antigüedad para el módulo de Reportes
|
|
Future<List<Map<String, dynamic>>> getFiltrados({
|
|
String? clienteNombre,
|
|
String? operador,
|
|
int? resuelto,
|
|
DateTime? desde,
|
|
}) async {
|
|
final db = await _db.db;
|
|
final whereClauses = <String>[];
|
|
final whereArgs = <dynamic>[];
|
|
|
|
if (clienteNombre != null && clienteNombre.isNotEmpty && clienteNombre != 'todos') {
|
|
whereClauses.add('cliente_nombre = ?');
|
|
whereArgs.add(clienteNombre);
|
|
}
|
|
if (operador != null && operador.isNotEmpty && operador != 'todos') {
|
|
whereClauses.add('operador = ?');
|
|
whereArgs.add(operador);
|
|
}
|
|
if (resuelto != null) {
|
|
whereClauses.add('resuelto = ?');
|
|
whereArgs.add(resuelto);
|
|
}
|
|
if (desde != null) {
|
|
whereClauses.add('timestamp >= ?');
|
|
whereArgs.add(desde.toIso8601String());
|
|
}
|
|
|
|
final where = whereClauses.isNotEmpty ? whereClauses.join(' AND ') : null;
|
|
return db.query(
|
|
'incidentes',
|
|
where: where,
|
|
whereArgs: whereArgs.isNotEmpty ? whereArgs : null,
|
|
orderBy: 'timestamp DESC',
|
|
);
|
|
}
|
|
|
|
// Comentario explicativo: Calcula métricas agregadas de incidentes (totales, resueltos, falsas alarmas) para las tarjetas KPI de reportes
|
|
Future<Map<String, int>> getEstadisticas() async {
|
|
final db = await _db.db;
|
|
final totalRes = await db.rawQuery('SELECT COUNT(*) as c FROM incidentes');
|
|
final resueltosRes = await db.rawQuery('SELECT COUNT(*) as c FROM incidentes WHERE resuelto = 1');
|
|
final pendientesRes = await db.rawQuery('SELECT COUNT(*) as c FROM incidentes WHERE resuelto = 0');
|
|
final falsasRes = await db.rawQuery("SELECT COUNT(*) as c FROM incidentes WHERE tipo LIKE '%falsa%'");
|
|
|
|
final total = (totalRes.isNotEmpty ? totalRes.first['c'] as int? : 0) ?? 0;
|
|
final resueltos = (resueltosRes.isNotEmpty ? resueltosRes.first['c'] as int? : 0) ?? 0;
|
|
final pendientes = (pendientesRes.isNotEmpty ? pendientesRes.first['c'] as int? : 0) ?? 0;
|
|
final falsas = (falsasRes.isNotEmpty ? falsasRes.first['c'] as int? : 0) ?? 0;
|
|
|
|
return {
|
|
'total': total,
|
|
'resueltos': resueltos,
|
|
'pendientes': pendientes,
|
|
'falsas': falsas,
|
|
};
|
|
}
|
|
}
|