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 CountBasedStrategy implements FeeCalculationStrategy {
    @Override
    public BigDecimal calculate(ShippingOrder order, Region region) {
        BigDecimal firstItemFee = region.getFirstItemFee();
        BigDecimal firstItem = region.getFirstItem();
        int firstItemInt = 0;
        if(firstItem != null) {
            firstItemInt = firstItem.intValue();
        }
        // 总数-首件数量 = 首次计算数量
        if (order.getItemCount() <= firstItemInt) {
            return firstItemFee.multiply(BigDecimal.valueOf(order.getItemCount()));
        }

        // 首次计算数量
        int firstItemCount = order.getItemCount() - firstItemInt;
        int remainingItemCount = order.getItemCount() - firstItemCount;
        // 总费用 = 首次计算数量*首次费用+剩下数量*续件费用
        BigDecimal firstFee = firstItemFee.multiply(BigDecimal.valueOf(firstItemCount));
        BigDecimal remainingFee = region.getAdditionalItemFee().multiply(BigDecimal.valueOf(remainingItemCount));

        return firstFee.add(remainingFee);
    }

    @Override
    public String getStrategyName() {
        return "按件数";
    }
}
