handle limit

This commit is contained in:
gowthaman 2024-08-15 16:27:21 +05:30
parent bb169d6cc8
commit 7b39e64ae5
2 changed files with 39 additions and 0 deletions

View File

@ -289,6 +289,10 @@ open class Plant : BaseModel() {
this.clear()
this.putAll(p.print ?: emptyMap())
}
this.limits?.apply {
this.clear()
this.putAll(p.limits ?: emptyMap())
}
this.save()
}

View File

@ -0,0 +1,35 @@
import com.restapi.domain.DataModel
import com.restapi.domain.EntityModel
import io.ebean.Database
import org.slf4j.Logger
import java.time.LocalDate
fun execute(d: Map<String, Any>, entityModel: EntityModel?, db: Database, logger: Logger, user: String, tenant: String): Map<String, Any> {
println("execute on $d")
//check if the limits have exceeded in the given month
//language=PostgreSQL
val q = db.sqlQuery(
"""
select sum(cast(data->>'amount' as float)) as amount from data_model
where entity_name = 'expense'
and cast(data->>'date' as timestamp) between :f and :t
and data->>'plantId' = :p
""".trimIndent()
)
q.setParameter("f", LocalDate.parse(d["fromDate"] as String))
q.setParameter("t", LocalDate.parse(d["toDate"] as String))
q.setParameter("p", d["plantId"] as String)
val sum = q.findOne()?.getLong("amount") ?: 0
return mapOf("sum" to sum)
}
fun preSave(d: DataModel, entityModel: EntityModel?, db: Database, logger: Logger, user: String, tenant: String): Boolean {
logger.warn("PreSave $d")
return true
}
fun postSave(d: DataModel, entityModel: EntityModel?, db: Database, logger: Logger, user: String, tenant: String) {
println("PostSave $d")
}