计算优化

main
topsun 2 years ago
parent 7e5c29cc4d
commit 9a1355a379

@ -1,7 +0,0 @@
package com.internal;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}

@ -0,0 +1,51 @@
package com.internal.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
* @throws IllegalAccessException
*/
public static double divide(int v1,int v2) throws IllegalAccessException {
if(v2 <= 0){
throw new IllegalAccessException("除数异常");
}
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.divide(b2,2,BigDecimal.ROUND_HALF_UP).doubleValue();
}
public static double subtract(double v1,double v2) {
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.subtract(b2).doubleValue();
}
public static double multiply(double v1,double v2) {
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.multiply(b2).doubleValue();
}
}

@ -6,6 +6,7 @@ import com.internal.dto.ResponseResult;
import com.internal.request.ForecastPriceDTO;
import com.internal.response.DirectionResponse;
import com.internal.response.ForecastPriceResponse;
import com.internal.util.BigDecimalUtils;
import com.taxi.serviceprice.mapper.PriceRuleMapper;
import com.taxi.serviceprice.remote.ServiceMapClient;
import javafx.scene.effect.Light;
@ -30,6 +31,7 @@ public class ForecastPriceService {
/**
*
*
* @param depLongitude
* @param depLatitude
* @param destLongitude
@ -37,7 +39,7 @@ public class ForecastPriceService {
* @return
*/
public ResponseResult forecastPrice(String depLongitude, String depLatitude,
String destLongitude, String destLatitude){
String destLongitude, String destLatitude) {
log.info("出发经度," + depLongitude);
log.info("出发纬度," + depLatitude);
log.info("目的地经度," + destLongitude);
@ -52,19 +54,23 @@ public class ForecastPriceService {
forecastPriceDTO.setDestLongitude(destLongitude);
forecastPriceDTO.setDestLatitude(destLatitude);
ResponseResult<DirectionResponse> directionResponse = serviceMapClient.direction(forecastPriceDTO);
log.info("距离"+directionResponse.getData().getDistance());
log.info("时间"+directionResponse.getData().getDuration());
log.info("距离" + directionResponse.getData().getDistance());
log.info("时间" + directionResponse.getData().getDuration());
log.info("读取计价规则");
Map<String,Object> queryMap = new HashMap<>();
queryMap.put("city_code","11000");
queryMap.put("vehicle_type","1");
List<PriceRule> pricetRules = priceRuleMapper.selectByMap(queryMap);
log.info("读取计价规则count = "+pricetRules.size());
if(pricetRules.size() < 1){
Map<String, Object> queryMap = new HashMap<>();
queryMap.put("city_code", "11000");
queryMap.put("vehicle_type", "1");
List<PriceRule> pricetRules = priceRuleMapper.selectByMap(queryMap);
log.info("读取计价规则count = " + pricetRules.size());
if (pricetRules.size() < 1) {
ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EMPTY);
}else{
price = getPrice(directionResponse.getData().getDistance(),
directionResponse.getData().getDuration(),pricetRules.get(0));
} else {
try {
price = getPrice(directionResponse.getData().getDistance(),
directionResponse.getData().getDuration(), pricetRules.get(0));
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
log.info("根据距离、时长,计价规则;计算价格");
ForecastPriceResponse forecastPriceResponse = new ForecastPriceResponse();
@ -80,43 +86,35 @@ public class ForecastPriceService {
* @param priceRule
* @return
*/
private double getPrice(Integer distance,Integer duration,PriceRule priceRule){
BigDecimal price = new BigDecimal(0);
private double getPrice(Integer distance, Integer duration, PriceRule priceRule) throws IllegalAccessException {
double price = 0.0;
//BigDecimal //起步价
Double startFare = priceRule.getStartFare();
BigDecimal startFareDecimal = new BigDecimal(startFare);
price = price.add(startFareDecimal);
double startFare = priceRule.getStartFare();
price = BigDecimalUtils.add(price, startFare);
//里程碑 m
BigDecimal distanceBigDecimal = new BigDecimal(distance);
//总里程 km
BigDecimal distanceMileDecimal = distanceBigDecimal.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 distanceSubtract = distanceMileDecimal.subtract(startMileDecimal).doubleValue();
int startMile = priceRule.getStartMile();
double distanceSubtract = BigDecimalUtils.subtract(distanceMile, startMile);
//最终收费里程数 km
Double mile = distanceSubtract < 0 ? 0:distanceSubtract;
BigDecimal mileDecimal = new BigDecimal(mile);
double mile = distanceSubtract < 0 ? 0 : distanceSubtract;
//计程单价 元/km
Double unitPricePerMile = priceRule.getUnitPricePerMile();
BigDecimal unitPricePerMilDecimal = new BigDecimal(unitPricePerMile);
//里程价格
BigDecimal mileFare = mileDecimal.multiply(unitPricePerMilDecimal).setScale(2,BigDecimal.ROUND_HALF_UP);
price = price.add(mileFare);
double mileFare = BigDecimalUtils.multiply(mile, unitPricePerMile);
price = BigDecimalUtils.add(price, mileFare);
//时长费用
BigDecimal time = new BigDecimal(duration);
//时长的分钟数
BigDecimal timeDecimal = time.divide(new BigDecimal(60),2,BigDecimal.ROUND_HALF_UP);
double time = BigDecimalUtils.divide(duration,60);
//计时单价
Double unitPricePerMinute = priceRule.getUnitPricePerMinute();
BigDecimal unitPricePerMinuteDecimal = new BigDecimal(unitPricePerMinute);
BigDecimal timeFare = timeDecimal.multiply(unitPricePerMinuteDecimal);
price = price.add(timeFare).setScale(2,BigDecimal.ROUND_HALF_UP);
return price.doubleValue();
double timeFare = BigDecimalUtils.multiply(time,unitPricePerMinute);
price = BigDecimalUtils.add(price,timeFare);
return new BigDecimal(price).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}
// public static void main(String[] args) {
// public static void main(String[] args) throws IllegalAccessException {
// PriceRule priceRule = new PriceRule();
// priceRule.setUnitPricePerMile(1.8);
// priceRule.setUnitPricePerMinute(0.5);

Loading…
Cancel
Save