package com.fs.store.strategy.impl;

import com.fs.store.strategy.FeeCalculationStrategy;
import com.fs.store.strategy.Region;
import com.fs.store.strategy.ShippingOrder;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 按体积计算运费
 */
public class VolumeBasedStrategy implements FeeCalculationStrategy {
    @Override
    public BigDecimal calculate(ShippingOrder order, Region region) {
        // 总费用 = 首次计算数量*首次费用+剩下数量*续件费用
        BigDecimal volume = order.getVolume();
        BigDecimal firstVolumeFee = region.getFirstVolumeFee();
        BigDecimal additionalVolumeFee = region.getAdditionalVolumeFee();
        if (volume.compareTo(region.getFirstVolume()) <= 0) {
            return firstVolumeFee.multiply(volume);
        }
        BigDecimal firstFee = firstVolumeFee.multiply(region.getFirstVolume());
        BigDecimal remainingFee = additionalVolumeFee.multiply(volume.subtract(region.getFirstVolume()));
        return firstFee.add(remainingFee);
    }

    @Override
    public String getStrategyName() {
        return "按体积";
    }
}
