工作代码如下
fun List<MeasureLedgerModel>.sumUp(): List<MeasureLedgerModel> {
val pMap = this.filter { it.parentId != null }.groupBy { it.parentId!! }
//获取顶层节点
val top = this.filter { it.parentId == null }
//向下遍历
fun calculateChildren(node:MeasureLedgerModel):MeasureLedgerModel{
val childs = pMap[node.id]
if(childs.isNullOrEmpty()) return node
var launchQuantity = node.launchQuantity.orZero()
var listEditQuantity = node.listEditQuantity.orZero()
var listEditAmount = node.listEditAmount.orZero()
var periodAmounts = node.periodAmounts
childs.forEach {
val childs = calculateChildren(it)
launchQuantity += childs.launchQuantity.orZero()
listEditQuantity += childs.listEditQuantity.orZero()
listEditAmount += childs.listEditAmount.orZero()
childs.periodAmounts.forEachIndexed { index, value ->
periodAmounts[index] = periodAmounts[index].orZero().plus(value.orZero())
}
}
node.launchQuantity = launchQuantity
node.listEditQuantity = listEditQuantity
node.listEditAmount = listEditAmount
node.periodAmounts = periodAmounts
return node
}
top.forEach {
calculateChildren(it)
}
return this
}
标签:node,childs,递归,launchQuantity,汇总,listEditAmount,orZero,periodAmounts,向下
From: https://www.cnblogs.com/Acaak/p/17102836.html