def shoppingCartService
def produceShoppingCartInfo(){
def shoppingItems = shoppingCartService.getItems()
def summary = new ShoppingCartSummary(items: [:], total: BigDecimal.ZERO)
shoppingItems.each { shoppingItem ->
def part = Shoppable.findByShoppingItem(shoppingItem)
def quantity = new BigDecimal(shoppingCartService.getQuantity(part))
BigDecimal lineTotal = part.price.multiply(quantity)
summary.items[part.id] = new Expando(quantity:quantity, item:part, lineTotal:lineTotal)
summary.total = summary.total.add(lineTotal)
}
summary
}
def updateCartQuantities(newQuantities) {
def summary = produceShoppingCartInfo()
newQuantities.each {
def partId = it.key
def newQuantity = it.value
if(summary.items[partId]) {
def currentQuantity = summary.items[partId].quantity
def delta = newQuantity - currentQuantity
if(delta > 0) {
shoppingCartService.addToShoppingCart(summary.items[partId].item, delta)
// shoppingCartService.addToShoppingCart(summary.items[partId].item)
} else if (delta < 0) {
shoppingCartService.removeFromShoppingCart(summary.items[partId].item, Math.abs(delta))
// shoppingCartService.removeFromShoppingCart(summary.items[partId].item)
}
}
}
produceShoppingCartInfo()
}
def getTotalItemsForCart(){
def summary = produceShoppingCartInfo()
def totalItems = 0
summary.items.each { partId, itemInfo ->
def currentItemQuantity = itemInfo.quantity
totalItems = totalItems+currentItemQuantity
}
totalItems
}
答案 0 :(得分:0)
这意味着您没有将正确的参数传递给addToShoppingCart
。它需要IShoppable
和Integer
。
def addToShoppingCart(IShoppable product, Integer qty = 1, ShoppingCart previousShoppingCart = null)
您的域类是否实现了IShoppable
?您是否将域实例作为第一个参数传递?
插件服务的来源here。
修改强>
您的问题可能与您的变量类型有关。在这种情况下,最好使用具体类型而不是Groovy def
。
def delta = newQuantity - currentQuantity
println delta.class.name //here you will see if this is really a Integer
您可能需要以下内容:
Integer delta = newQuantity.asInteger() - currentQuantity.asInteger()
仅当您不需要知道对象的类型时才选择使用def
。如果您知道这是Long
,Integer
,BigDecimal
等,则倾向于使用类型声明您的变量。