package com.ylx.massage.utils;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ylx.massage.domain.TFareSetting;
import com.ylx.massage.service.TFareSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author jianlong
 * @date 2024-04-15 14:38
 */
@Slf4j
@Component
public class MassageUtil {

    @Value("${fare.base-fare}")
    private BigDecimal baseFare;

    @Value("${fare.additional-fare-per-km}")
    private BigDecimal additionalFarePerKm;

    @Value("${fare.base-distance-km}")
    private BigDecimal baseDistanceKm;

    @Resource
    private TFareSettingService fareSettingService;

    /**
     * 计算出租车费用
     *
     * @param distance 全程距离（公里）
     * @param deptId 部门id
     * @return BigDecimal 费用总额（元）
     */
    public BigDecimal calculateTaxiFare(BigDecimal distance, String deptId) {
        BigDecimal BASE_FARE = baseFare; // 起步价
        BigDecimal ADDITIONAL_FARE_PER_KM = additionalFarePerKm; // 超出起步价后每公里费用
        BigDecimal BASE_DISTANCE_KM = baseDistanceKm; // 起步距离（公里）
        LambdaQueryWrapper<TFareSetting> tFareSettingLambdaQueryWrapper = new LambdaQueryWrapper<>();
        log.info("部门{}", deptId);
//        tFareSettingLambdaQueryWrapper.eq(TFareSetting::getDeptId, deptId).eq(TFareSetting::getEnable, 1);
//        TFareSetting fareSetting = fareSettingService.getOne(tFareSettingLambdaQueryWrapper);
//        log.info("配置{}", fareSetting);
//        if (null != fareSetting) {
//            BASE_FARE = fareSetting.getBaseFare();
//            ADDITIONAL_FARE_PER_KM = fareSetting.getAdditionalFarePer();
//            BASE_DISTANCE_KM = fareSetting.getBaseDistance();
//        }
        log.info("起步价:{},超出起步价后每公里费用:{},起步距离{}", BASE_FARE, ADDITIONAL_FARE_PER_KM, BASE_DISTANCE_KM);

        // 计算超出起步价的公里数，注意应该向上取整，因为不足1公里也要按1公里计费
        BigDecimal extraKms = distance.subtract(BASE_DISTANCE_KM).setScale(0, RoundingMode.UP);

        // 计算总费用

        BigDecimal totalFare = BASE_FARE;
        if (extraKms.compareTo(BigDecimal.ZERO) > 0) {
            totalFare = totalFare.add(extraKms.multiply(ADDITIONAL_FARE_PER_KM));
        }
        return totalFare;
    }
}
