diff --git a/internal-common/src/main/java/com/mashibing/internalcommon/util/BigDecimalUtils.java b/internal-common/src/main/java/com/mashibing/internalcommon/util/BigDecimalUtils.java new file mode 100644 index 0000000..957c4e4 --- /dev/null +++ b/internal-common/src/main/java/com/mashibing/internalcommon/util/BigDecimalUtils.java @@ -0,0 +1,59 @@ +package com.mashibing.internalcommon.util; + +import java.math.BigDecimal; + +public class BigDecimalUtils { + + /** + * 加法 + * @param v1 + * @param v2 + * @return + */ + public static double add(double v1 , double v2){ + BigDecimal b1 = BigDecimal.valueOf(v1); + BigDecimal b2 = BigDecimal.valueOf(v2); + + return b1.add(b2).doubleValue(); + } + + /** + * 减法 + * @param v1 + * @param v2 + * @return + */ + public static double substract(double v1,double v2){ + BigDecimal b1 = BigDecimal.valueOf(v1); + BigDecimal b2 = BigDecimal.valueOf(v2); + return b1.subtract(b2).doubleValue(); + } + + /** + * 乘法 + * @param v1 + * @param v2 + * @return + */ + public static double multiply(double v1,double v2){ + BigDecimal b1 = BigDecimal.valueOf(v1); + BigDecimal b2 = BigDecimal.valueOf(v2); + return b1.multiply(b2).doubleValue(); + + } + /** + * 除法 + * @param v1 + * @param v2 + * @return + */ + public static double divide(int v1, int v2){ + if (v2 <= 0){ + throw new IllegalArgumentException("除数非法"); + } + + BigDecimal b1 = BigDecimal.valueOf(v1); + BigDecimal b2 = BigDecimal.valueOf(v2); + return b1.divide(b2,2,BigDecimal.ROUND_HALF_UP).doubleValue(); + } +} \ No newline at end of file diff --git a/service-price/src/main/java/com/mashibing/serviceprice/service/ForecastPriceService.java b/service-price/src/main/java/com/mashibing/serviceprice/service/ForecastPriceService.java index 4c1550b..afa25c7 100644 --- a/service-price/src/main/java/com/mashibing/serviceprice/service/ForecastPriceService.java +++ b/service-price/src/main/java/com/mashibing/serviceprice/service/ForecastPriceService.java @@ -6,6 +6,7 @@ import com.mashibing.internalcommon.dto.ResponseResult; import com.mashibing.internalcommon.request.ForecastPriceDTO; import com.mashibing.internalcommon.response.DirectionResponse; import com.mashibing.internalcommon.response.ForecastPriceResponse; +import com.mashibing.internalcommon.util.BigDecimalUtils; import com.mashibing.serviceprice.mapper.PriceRuleMapper; import com.mashibing.serviceprice.remote.ServiceMapClient; import lombok.extern.slf4j.Slf4j; @@ -81,44 +82,40 @@ public class ForecastPriceService { * @param priceRule 计价规则 *@return */ - private Double getPriceByRule (Integer distance,Integer duration,PriceRule priceRule) { + private static Double getPriceByRule (Integer distance,Integer duration,PriceRule priceRule) { // BigDecimal - BigDecimal sumPriceDecimal = new BigDecimal(0); + double sumPrice = 0; + // 起步价 Double startFare = priceRule.getStartFare(); - BigDecimal startFareDecimal = new BigDecimal(startFare); - sumPriceDecimal = sumPriceDecimal.add(startFareDecimal); + sumPrice = BigDecimalUtils.add(sumPrice,startFare); //里程费 // 总里程 m => km - BigDecimal distanceDecimal = new BigDecimal(distance); - BigDecimal distanceMileDecimal = distanceDecimal.divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP); + double distanceMile = BigDecimalUtils.divide(distance,1000); // 起步里程 Integer startMile = priceRule.getStartMile(); - BigDecimal startMileDecimal = new BigDecimal(startMile); // 需收费的里程数 - double mileTmp = distanceMileDecimal.subtract(startMileDecimal).doubleValue(); - if(mileTmp < 0){ - mileTmp = 0; - } - BigDecimal subMileDecimal = new BigDecimal(mileTmp); + double subMile = BigDecimalUtils.substract(distanceMile, startMile); + subMile = subMile<0 ? 0:subMile; // 获取计费规则-每公里收费 Double unitPricePerMile = priceRule.getUnitPricePerMile(); - BigDecimal unitPricePerMileDecimal = new BigDecimal(unitPricePerMile); // 计算最终里程费用 - BigDecimal finalMileFareDecimal = subMileDecimal.multiply(unitPricePerMileDecimal).setScale(2, BigDecimal.ROUND_HALF_UP); - sumPriceDecimal = sumPriceDecimal.add(finalMileFareDecimal); + double mileFare = BigDecimalUtils.multiply(subMile,unitPricePerMile); + sumPrice = BigDecimalUtils.add(sumPrice,mileFare); //时长费 // 时长的分钟数 s => min - BigDecimal durationDecimal = new BigDecimal(duration); - BigDecimal durationMinDecimal = durationDecimal.divide(new BigDecimal(60), 2, BigDecimal.ROUND_HALF_UP); + double timeMinute = BigDecimalUtils.divide(duration, 60); // 获取计费规则-每分钟收费 Double unitPricePerMinute = priceRule.getUnitPricePerMinute(); - BigDecimal unitPricePerMinuteDecimal = new BigDecimal(unitPricePerMinute); // 计算最终时长费用 - BigDecimal finalTimeFareDecimal = durationMinDecimal.multiply(unitPricePerMinuteDecimal.setScale(2, BigDecimal.ROUND_HALF_UP)); - sumPriceDecimal = sumPriceDecimal.add(finalTimeFareDecimal); + double timeFare = BigDecimalUtils.multiply(timeMinute, unitPricePerMinute); + sumPrice = BigDecimalUtils.add(sumPrice,timeFare); + + // 过滤 最终计算值 + BigDecimal sumPriceDecimal = new BigDecimal(sumPrice); + sumPriceDecimal = sumPriceDecimal.setScale(2, BigDecimal.ROUND_HALF_UP); return sumPriceDecimal.doubleValue(); }