package com.sckw.transport.service.dashboard;

import com.sckw.core.common.enums.enums.DictEnum;
import com.sckw.transport.model.KwtWaybillOrderSubtask;
import org.junit.Assert;
import org.junit.Test;

import java.math.BigDecimal;

/**
 * 实时销量服务单元测试。
 */
public class RealtimeSalesVolumeServiceTest {

    /**
     * 按装货量计费时，应取子任务装货量作为净重。
     */
    @Test
    public void subtaskNetTonShouldUseLoadAmountWhenBillingByLoadAmount() {
        KwtWaybillOrderSubtask subtask = buildSubtask("10.50", "9.25");

        BigDecimal actual = RealtimeSalesVolumeService.subtaskNetTon(subtask, DictEnum.CHARGING_TYPE_1.getValue());

        Assert.assertEquals(new BigDecimal("10.50"), actual);
    }

    /**
     * 按卸货量计费时，应取子任务卸货量作为净重。
     */
    @Test
    public void subtaskNetTonShouldUseUnloadAmountWhenBillingByUnloadAmount() {
        KwtWaybillOrderSubtask subtask = buildSubtask("10.50", "9.25");

        BigDecimal actual = RealtimeSalesVolumeService.subtaskNetTon(subtask, DictEnum.CHARGING_TYPE_2.getValue());

        Assert.assertEquals(new BigDecimal("9.25"), actual);
    }

    /**
     * 计费方式缺失时保持默认卸货量口径，空值或非正数返回0。
     */
    @Test
    public void subtaskNetTonShouldDefaultToUnloadAmountAndIgnoreInvalidAmount() {
        KwtWaybillOrderSubtask subtask = buildSubtask("10.50", "-1.00");

        BigDecimal actual = RealtimeSalesVolumeService.subtaskNetTon(subtask, null);

        Assert.assertEquals(BigDecimal.ZERO, actual);
        Assert.assertEquals(BigDecimal.ZERO, RealtimeSalesVolumeService.subtaskNetTon(null, DictEnum.CHARGING_TYPE_1.getValue()));
    }

    /**
     * 构造子任务测试数据。
     *
     * @param loadAmount   装货量
     * @param unloadAmount 卸货量
     * @return 子任务对象
     */
    private KwtWaybillOrderSubtask buildSubtask(String loadAmount, String unloadAmount) {
        KwtWaybillOrderSubtask subtask = new KwtWaybillOrderSubtask();
        subtask.setLoadAmount(new BigDecimal(loadAmount));
        subtask.setUnloadAmount(new BigDecimal(unloadAmount));
        return subtask;
    }
}
