gpt4 book ai didi

grails createCriteria 用于日期计算

转载 作者:行者123 更新时间:2023-12-02 15:19:32 27 4
gpt4 key购买 nike

我正在尝试使用 criteria 从数据库中获取数据
Domain class发票 -

class Invoice{
Date invoiceNo
Date invoiceDate
int interval

static constraints = { }
}
criteria来自 controller 的模块
if(!params.sort && !params.order) {
params.sort = "invoiceDate + interval" //invoiceDate+ interval
params.order = "asc"
}
def c = Invoice.createCriteria()
def results = c.list(params) {
between("invoiceDate+ interval", invoiceDate, invoiceDate+ interval)

}

我要计算 dueDate来自 invoiceDate & interval .

我想要这样的结果
invoiceNo | invoiceDate | interval| dueDate    |
001 | 2016-09-30 | 5 | 2016-10-05 |
002 | 2016-09-15 | 5 | 2016-09-20 |

最佳答案

我会坚持你修改你的Domain class添加derived field如下所示。

修改Domain class - 添加派生字段 dueDate

class Invoice{
Date invoiceNo
Date invoiceDate
int interval

//new field added
Date dueDate //Derived filed- will not persisted to the database

static mapping = {
//formula to calculate dueDate (invoiceDate + interval)
dueDate formula:"ADDDATE(invoice_date, interval)"
}

static constraints = { }
}
criteria代码片段 - 获取 dueDate 的发票介于 current Date 之间和 current Date + 7 .
def criteria = Invoice.createCriteria()

Date currentDate = new Date()

//fetching data based on the criteria
List invoiceList = criteria.list {
between('dueDate', currentDate, (currentDate + 7))
}

关于grails createCriteria 用于日期计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39783766/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com