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 WeightBasedStrategy implements FeeCalculationStrategy {
    @Override
    public BigDecimal calculate(ShippingOrder order, Region region) {
        // 总费用 = 首次计算数量*首次费用+剩下数量*续件费用

        BigDecimal weight = order.getWeight();
        BigDecimal firstWeightFee = region.getFirstWeightFee();
        BigDecimal additionalWeightFee = region.getAdditionalWeightFee();
        if (weight.compareTo(region.getFirstWeight()) <= 0) {
            return firstWeightFee.multiply(weight);
        }
        BigDecimal firstFee = firstWeightFee.multiply(region.getFirstWeight());
        BigDecimal remainingFee = additionalWeightFee.multiply(weight.subtract(region.getFirstWeight()));
        return firstFee.add(remainingFee);
    }

    @Override
    public String getStrategyName() {
        return "按重量";
    }
}
