我需要获得每个商店每个订单所售出的所有商品的总和。我使用executeQuery()在表达式上运行sum()。它工作正常,如下图所示,但我想知道是否有更好的,更方便的方法来做到这一点。使用executeQuery()对算术表达式进行Grails投影?
StoreService {
static transactional = false
def getTotalOrders(def store) {
return Store.executeQuery("select sum(a.soldQuantity * a.soldPrice) as total
from OrderItem a inner join a.order b inner join b.store c
where c= :store", [store: store]).get(0)
}
}
Store {
transient storeService
def getTotalSales() {
storeService.getTotalSales()
}
static hasMany = [items: Item]
// no hasMany to Order
}
Item {
static belongsTo = [store: Store]
// no hasMany to OrderItem
}
Order {
static hasMany = [orderItems: OrderItem]
static belongsTo = [store: Store]
}
OrderItem {
BigDecimal soldPrice
Integer soldQuantity
static belongsTo = [order: Order, item: Item]
}
我认为withCriteria()会更容易阅读,但我无法弄清楚如何使用款项中的表达式做()不会采取明显的原因。
projections {
sum("soldPrice * soldQuantity")
}
感谢
谢谢您的建议。我们已经使用getTotalPrice()作为一个瞬态,并将totalPrice映射作为公式是一个很好的建议。 – Micor
你可以根据映射订购吗? –