飞滴出行网约车2022-service-price 实现传入时长里程计算规则计算总费用

master
yh 3 years ago
parent 22b6d9682f
commit a96fb4e518

@ -12,6 +12,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -67,9 +68,67 @@ public class ForecastPriceService {
// 根据 距离时长 进行价格预估 // 根据 距离时长 进行价格预估
ForecastPriceResponse priceResponse = new ForecastPriceResponse(); ForecastPriceResponse priceResponse = new ForecastPriceResponse();
priceResponse.setPrice(12.34); Double price = getPriceByRule(distance, duration, priceRule);
priceResponse.setPrice(price);
return ResponseResult.success(priceResponse); return ResponseResult.success(priceResponse);
} }
/**
*@param distance
* @param duration
* @param priceRule
*@return
*/
private Double getPriceByRule (Integer distance,Integer duration,PriceRule priceRule) {
// BigDecimal
BigDecimal sumPriceDecimal = new BigDecimal(0);
// 起步价
Double startFare = priceRule.getStartFare();
BigDecimal startFareDecimal = new BigDecimal(startFare);
sumPriceDecimal = sumPriceDecimal.add(startFareDecimal);
//里程费
// 总里程 m => km
BigDecimal distanceDecimal = new BigDecimal(distance);
BigDecimal distanceMileDecimal = distanceDecimal.divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP);
// 起步里程
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 unitPricePerMile = priceRule.getUnitPricePerMile();
BigDecimal unitPricePerMileDecimal = new BigDecimal(unitPricePerMile);
// 计算最终里程费用
BigDecimal finalMileFareDecimal = subMileDecimal.multiply(unitPricePerMileDecimal).setScale(2, BigDecimal.ROUND_HALF_UP);
sumPriceDecimal = sumPriceDecimal.add(finalMileFareDecimal);
//时长费
// 时长的分钟数 s => min
BigDecimal durationDecimal = new BigDecimal(duration);
BigDecimal durationMinDecimal = durationDecimal.divide(new BigDecimal(60), 2, BigDecimal.ROUND_HALF_UP);
// 获取计费规则-每分钟收费
Double unitPricePerMinute = priceRule.getUnitPricePerMinute();
BigDecimal unitPricePerMinuteDecimal = new BigDecimal(unitPricePerMinute);
// 计算最终时长费用
BigDecimal finalTimeFareDecimal = durationMinDecimal.multiply(unitPricePerMinuteDecimal.setScale(2, BigDecimal.ROUND_HALF_UP));
sumPriceDecimal = sumPriceDecimal.add(finalTimeFareDecimal);
return sumPriceDecimal.doubleValue();
}
// public static void main(String[] args) {
// PriceRule priceRule = new PriceRule();
// priceRule.setUnitPricePerMile(1.8);
// priceRule.setUnitPricePerMinute(0.5);
// priceRule.setStartFare(10.0);
// priceRule.setStartMile(3);
// System.out.println( getPriceByRule(6500 , 1800 , priceRule) );
// }
} }
Loading…
Cancel
Save