Añadir los 16.000 kms de margen para el cambio de aceite
This commit is contained in:
@@ -307,58 +307,61 @@ class MainActivity : AppCompatActivity() {
|
|||||||
Class.forName("com.mysql.jdbc.Driver")
|
Class.forName("com.mysql.jdbc.Driver")
|
||||||
val connection = java.sql.DriverManager.getConnection(dbUrl, dbUser, dbPass)
|
val connection = java.sql.DriverManager.getConnection(dbUrl, dbUser, dbPass)
|
||||||
|
|
||||||
|
// 1. Buscamos el último cambio de aceite (lógica n8n)
|
||||||
val rsOil = connection.createStatement().executeQuery(
|
val rsOil = connection.createStatement().executeQuery(
|
||||||
"SELECT kms FROM mantenimiento WHERE observaciones LIKE '%aceite%' ORDER BY fecha DESC LIMIT 1"
|
"SELECT kms, fecha FROM mantenimiento WHERE observaciones LIKE '%aceite%' ORDER BY fecha DESC LIMIT 1"
|
||||||
)
|
|
||||||
var ultimoCambio = 0
|
|
||||||
if (rsOil.next()) ultimoCambio = rsOil.getInt("kms")
|
|
||||||
|
|
||||||
val rsStats = connection.createStatement().executeQuery(
|
|
||||||
"SELECT MIN(fecha) as f_min, MIN(kms) as k_min, MAX(fecha) as f_max, MAX(kms) as k_max FROM mantenimiento"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var fechaEstimada: String? = null
|
var ultimoCambioKms = 0
|
||||||
var restan = 0
|
var fechaUltimoCambio: Date? = null
|
||||||
|
|
||||||
if (rsStats.next() && ultimoCambio > 0) {
|
if (rsOil.next()) {
|
||||||
val fMin = rsStats.getTimestamp("f_min")
|
ultimoCambioKms = rsOil.getInt("kms")
|
||||||
val kMin = rsStats.getInt("k_min")
|
fechaUltimoCambio = rsOil.getTimestamp("fecha")
|
||||||
val fMax = rsStats.getTimestamp("f_max")
|
}
|
||||||
val kMax = rsStats.getInt("k_max")
|
|
||||||
|
|
||||||
if (fMin != null && fMax != null && kMax > kMin) {
|
// 2. Kilometraje actual
|
||||||
val diffMs = fMax.time - fMin.time
|
val rsActual = connection.createStatement().executeQuery(
|
||||||
val diffDays = diffMs / (1000 * 60 * 60 * 24)
|
"SELECT kms, fecha FROM mantenimiento ORDER BY fecha DESC LIMIT 1"
|
||||||
|
)
|
||||||
|
|
||||||
if (diffDays > 0) {
|
var kmActual = 0
|
||||||
val kmPorDia = (kMax - kMin).toDouble() / diffDays
|
var fechaActual = Date()
|
||||||
val distanciaPuertollano = 320
|
|
||||||
restan = (ultimoCambio + 15000 - distanciaPuertollano) - kMax
|
|
||||||
|
|
||||||
if (restan > 0 && kmPorDia > 0) {
|
if (rsActual.next()) {
|
||||||
val diasFaltan = (restan / kmPorDia).toLong()
|
kmActual = rsActual.getInt("kms")
|
||||||
val calendar = Calendar.getInstance()
|
fechaActual = rsActual.getTimestamp("fecha")
|
||||||
calendar.add(Calendar.DAY_OF_YEAR, diasFaltan.toInt())
|
|
||||||
fechaEstimada = SimpleDateFormat("dd/MM/yyyy", Locale.getDefault()).format(calendar.time)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (restan <= 0) {
|
|
||||||
val distanciaPuertollano = 320
|
|
||||||
restan = (ultimoCambio + 15000 - distanciaPuertollano) - kMax
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
connection.close()
|
connection.close()
|
||||||
|
|
||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
if (ultimoCambio > 0) {
|
if (ultimoCambioKms > 0 && fechaUltimoCambio != null) {
|
||||||
|
val INTERVALO_CAMBIO = 16000 // Tu configuración de n8n
|
||||||
|
val distanciaPuertollano = 320
|
||||||
|
|
||||||
|
// Lógica de n8n: Calcular km diarios desde el último cambio hasta hoy
|
||||||
|
val diffMs = fechaActual.time - fechaUltimoCambio.time
|
||||||
|
val diasPasados = Math.max(1L, diffMs / (1000 * 60 * 60 * 24))
|
||||||
|
val kmsDesdeCambio = kmActual - ultimoCambioKms
|
||||||
|
val kmPorDia = Math.max(0.1, kmsDesdeCambio.toDouble() / diasPasados)
|
||||||
|
|
||||||
|
// Cálculo final restando el margen para el viaje a Puertollano
|
||||||
|
val restan = (ultimoCambioKms + INTERVALO_CAMBIO - distanciaPuertollano) - kmActual
|
||||||
|
|
||||||
|
var fechaEstimadaStr = "---"
|
||||||
|
if (restan > 0 && kmPorDia > 0) {
|
||||||
|
val diasParaCambio = (restan / kmPorDia).toLong()
|
||||||
|
val calendar = Calendar.getInstance()
|
||||||
|
calendar.add(Calendar.DAY_OF_YEAR, diasParaCambio.toInt())
|
||||||
|
fechaEstimadaStr = SimpleDateFormat("dd/MM/yyyy", Locale.getDefault()).format(calendar.time)
|
||||||
|
}
|
||||||
|
|
||||||
if (restan <= 0) {
|
if (restan <= 0) {
|
||||||
tvPrediction.text = getString(R.string.urgent_oil_change, Math.abs(restan))
|
tvPrediction.text = getString(R.string.urgent_oil_change, Math.abs(restan))
|
||||||
tvPrediction.setTextColor(android.graphics.Color.RED)
|
tvPrediction.setTextColor(android.graphics.Color.RED)
|
||||||
} else {
|
} else {
|
||||||
val fechaTexto = fechaEstimada ?: "---"
|
tvPrediction.text = getString(R.string.next_oil_change, restan, fechaEstimadaStr)
|
||||||
tvPrediction.text = getString(R.string.next_oil_change, restan, fechaTexto)
|
|
||||||
tvPrediction.setTextColor(android.graphics.Color.parseColor("#4CAF50"))
|
tvPrediction.setTextColor(android.graphics.Color.parseColor("#4CAF50"))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user