SOURCE

waterBudgetPrice = {
    waterPrice:0,
    sewagePrice:0,
    power:0,
    ladder:[],
}
userPrice = {
    ladderPower:"26/34/999999",
    waterPrice:"1.98/2.97/3.96",
    sewagePrice:"0.95/1.43/2.85"
}
currentItem = {
    DAYNUM:62,
    MRPOWER:176,
}

// 阶梯计费
let calculate = function(mrpower, index) {
    let power = mrpower
    if (power <= 0) {
        return 0
    }
    let $price = 0;
    if ($ladder.length == 1 && $ladder[0] != 0) return power * $prices[0]
    for (let i = index; i < $ladder.length; i++) {
        let ladder = $ladder[i]
        if(i > 0){
            ladder = ladder - $ladder[i - 1]
        }
        if (power > ladder) {
            if (ladder > 0) {
                $price += ladder * $prices[i] + calculate(power - ladder, i + 1)
            } else {
                $price += power * $prices[i]
            }
            break
        } else {
            $price += power * $prices[i]
            break
        }
        break
    }
    return $price;
}
// 用水天数
let DAYNUM = this.currentItem.DAYNUM
let $ladder = this.userPrice.ladderPower.split('/')
// 旧阶梯算费
// $ladder = $ladder.map(i => {
//     // Math.ceil
//     return (DAYNUM / 30.5 * i).toFixed(0)
// })
// 新阶梯上下限计算(先算二阶,再算一阶)
let ladderFormat = (array) => {
    if(array.length == 1){
        return [(DAYNUM / 30.5 * array[0]).toFixed(0)]
    }
    const ladder = []
    if(array.length > 1){
        ladder[1] = (DAYNUM / 30.5 * array[1]).toFixed(0)
        ladder[0] = (DAYNUM / 30.5 * (+array[0] + 1)).toFixed(0) - 1
        ladder[2] = (DAYNUM / 30.5 * array[2]).toFixed(0)
    }
    return ladder
}
$ladder = ladderFormat($ladder)
$ladder.forEach((i,j)=>{
    console.log(`${j+1}阶上限:${i}`)
})
console.log('——————————————')
// 水费价格
let $prices = this.userPrice.waterPrice.split('/')
let $waterPrices = this.userPrice.waterPrice.split('/')
this.waterBudgetPrice.waterPrice = (calculate(this.currentItem.MRPOWER,0) / 1).toFixed(2)


// 污水费价格
let $sewagePrices = this.userPrice.sewagePrice.split('/')
$prices = $sewagePrices
this.waterBudgetPrice.sewagePrice = (calculate(this.currentItem.MRPOWER,0) / 1).toFixed(2)
// 合计水量
this.waterBudgetPrice.power = this.currentItem.MRPOWER
console.log(this.waterBudgetPrice)
// 格式化显示的水量
let mpower = this.currentItem.MRPOWER
this.waterBudgetPrice.ladder = []
if($ladder.length > 1){
    $ladder.forEach((item,index)=>{
        let power = 0
        //
        if(index > 0){
            item = item - $ladder[index - 1]
        }
        if(mpower >= item){
            mpower-=item
            power = item
        }else{
            if(mpower > 0){
                power = mpower
                mpower-=item
            }else{
                power = 0
            }
        }                
        this.waterBudgetPrice.ladder[index] = {
            power:power,
            waterPrice:power ? Number($waterPrices[index] * power).toFixed(2) : 0,
            sewagePrice:power ? Number($sewagePrices[index] * power).toFixed(2) : 0,
        }
    })
}else if($ladder.length == 1){
    this.waterBudgetPrice.ladder[0] = {
        power:mpower,
        waterPrice:mpower ? Number($waterPrices[0] * mpower).toFixed(2) : 0,
        sewagePrice:mpower ? Number($sewagePrices[0] * mpower).toFixed(2) : 0,
    }
}
console.log('————————————')
this.waterBudgetPrice.ladder.forEach(item=>{
    console.log(item)
})
console 命令行工具 X clear

                    
>
console