Compare commits
2 Commits
e227154587
...
925cd9ce85
| Author | SHA1 | Date | |
|---|---|---|---|
| 925cd9ce85 | |||
| dadc051087 |
@ -5,7 +5,10 @@ import com.cdzy.common.enums.Code;
|
||||
import com.cdzy.common.model.JsonResult;
|
||||
import com.cdzy.common.model.ResGPSDto;
|
||||
import com.cdzy.orders.component.EbikeCoreHandler;
|
||||
import com.cdzy.orders.enums.*;
|
||||
import com.cdzy.orders.enums.CmdCode;
|
||||
import com.cdzy.orders.enums.EbikeRegionInOperation;
|
||||
import com.cdzy.orders.enums.OrderStatus;
|
||||
import com.cdzy.orders.enums.OrderType;
|
||||
import com.cdzy.orders.mapper.UserOrdersMapper;
|
||||
import com.cdzy.orders.model.dto.req.ReqBikeDto;
|
||||
import com.cdzy.orders.model.dto.req.ReqOrderDto;
|
||||
@ -13,6 +16,7 @@ import com.cdzy.orders.model.dto.res.RedisPoint;
|
||||
import com.cdzy.orders.model.dto.res.RspBikeDto;
|
||||
import com.cdzy.orders.model.entity.UserOrders;
|
||||
import com.cdzy.orders.service.UserOrdersService;
|
||||
import com.cdzy.orders.uitls.NumberUtils;
|
||||
import com.cdzy.orders.uitls.RedisUtil;
|
||||
import com.cdzy.orders.uitls.TimeUtils;
|
||||
import com.ebike.feign.clients.MaintenanceFeignClient;
|
||||
@ -33,7 +37,8 @@ import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import static com.cdzy.orders.enums.TimeDivisionCharging.*;
|
||||
import static com.cdzy.orders.enums.TimeDivisionCharging.TIME_SLOT;
|
||||
import static com.cdzy.orders.enums.TimeDivisionCharging.WEEK;
|
||||
import static com.cdzy.orders.model.entity.table.UserOrdersTableDef.USER_ORDERS;
|
||||
|
||||
/**
|
||||
@ -310,7 +315,26 @@ public class UserOrdersServiceImpl extends ServiceImpl<UserOrdersMapper, UserOrd
|
||||
* @return 计费后总金额
|
||||
*/
|
||||
BigDecimal defaultCostCalculation(BigDecimal totalAmount,long minutes,ResFeignEbikeSysRcostsetDto feignEbikeSysRcostsetDto){
|
||||
|
||||
Integer startupDuration = feignEbikeSysRcostsetDto.getStartupDuration();
|
||||
BigDecimal startupCost = feignEbikeSysRcostsetDto.getStartupCost();
|
||||
totalAmount = totalAmount.add(startupCost);
|
||||
//超出起步时长计费
|
||||
if (minutes > startupDuration){
|
||||
BigDecimal minutesNew = BigDecimal.valueOf(minutes);
|
||||
BigDecimal startupDurationNew = BigDecimal.valueOf(startupDuration);
|
||||
//超出时长
|
||||
BigDecimal subtract = minutesNew.subtract(startupDurationNew);
|
||||
//时长计费
|
||||
BigDecimal durationCost = feignEbikeSysRcostsetDto.getDurationCost();
|
||||
//时长多久计费一次
|
||||
Integer duration = feignEbikeSysRcostsetDto.getDuration();
|
||||
//总计费几次(向上取整
|
||||
int ceil = NumberUtils.divideAndCeil(duration, subtract.intValue());
|
||||
BigDecimal ceilCost = BigDecimal.valueOf(ceil);
|
||||
//最终值
|
||||
BigDecimal multiply = durationCost.multiply(ceilCost);
|
||||
totalAmount = totalAmount.add(multiply);
|
||||
}
|
||||
return totalAmount;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,137 @@
|
||||
package com.cdzy.orders.uitls;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 数字计算工具类,提供安全的数学运算方法
|
||||
*/
|
||||
public final class NumberUtils {
|
||||
|
||||
private NumberUtils() {
|
||||
// 防止实例化
|
||||
}
|
||||
|
||||
/**
|
||||
* 除法向上取整
|
||||
* @param dividend 被除数
|
||||
* @param divisor 除数
|
||||
* @return 向上取整后的结果
|
||||
* @throws IllegalArgumentException 如果除数为0
|
||||
*/
|
||||
public static int divideAndCeil(int dividend, int divisor) {
|
||||
if (divisor == 0) throw new IllegalArgumentException("Divisor cannot be zero");
|
||||
return (int) Math.ceil((double) dividend / divisor);
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全转换字符串为整数
|
||||
* @param str 要转换的字符串
|
||||
* @param defaultValue 转换失败时的默认值
|
||||
* @return 转换结果或默认值
|
||||
*/
|
||||
public static int parseIntSafe(String str, int defaultValue) {
|
||||
if (str == null) return defaultValue;
|
||||
try {
|
||||
return Integer.parseInt(str.trim());
|
||||
} catch (NumberFormatException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算最大值
|
||||
* @param numbers 输入数字
|
||||
* @return 最大值
|
||||
* @throws IllegalArgumentException 如果输入为空或null
|
||||
*/
|
||||
public static int max(int... numbers) {
|
||||
validateArray(numbers);
|
||||
return Arrays.stream(numbers).max().getAsInt();
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算最小值
|
||||
* @param numbers 输入数字
|
||||
* @return 最小值
|
||||
* @throws IllegalArgumentException 如果输入为空或null
|
||||
*/
|
||||
public static int min(int... numbers) {
|
||||
validateArray(numbers);
|
||||
return Arrays.stream(numbers).min().getAsInt();
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算平均值(避免int溢出)
|
||||
* @param numbers 输入数字
|
||||
* @return 平均值
|
||||
* @throws IllegalArgumentException 如果输入为空或null
|
||||
*/
|
||||
public static double average(int... numbers) {
|
||||
validateArray(numbers);
|
||||
long sum = 0;
|
||||
for (int num : numbers) {
|
||||
sum += num;
|
||||
}
|
||||
return (double) sum / numbers.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全绝对值计算
|
||||
* @param value 输入值
|
||||
* @return 绝对值
|
||||
* @throws ArithmeticException 如果值为Integer.MIN_VALUE
|
||||
*/
|
||||
public static int absExact(int value) {
|
||||
if (value == Integer.MIN_VALUE) {
|
||||
throw new ArithmeticException("Absolute value of Integer.MIN_VALUE overflows");
|
||||
}
|
||||
return Math.abs(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全加法(溢出时返回边界值)
|
||||
* @param a 第一个操作数
|
||||
* @param b 第二个操作数
|
||||
* @return 计算结果或边界值
|
||||
*/
|
||||
public static int addExact(int a, int b) {
|
||||
long res = (long) a + b;
|
||||
return clampToInt(res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全减法(溢出时返回边界值)
|
||||
* @param a 被减数
|
||||
* @param b 减数
|
||||
* @return 计算结果或边界值
|
||||
*/
|
||||
public static int subtractExact(int a, int b) {
|
||||
long res = (long) a - b;
|
||||
return clampToInt(res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全乘法(溢出时返回边界值)
|
||||
* @param a 第一个因子
|
||||
* @param b 第二个因子
|
||||
* @return 计算结果或边界值
|
||||
*/
|
||||
public static int multiplyExact(int a, int b) {
|
||||
long res = (long) a * b;
|
||||
return clampToInt(res);
|
||||
}
|
||||
|
||||
// 验证数组有效性
|
||||
private static void validateArray(int[] array) {
|
||||
if (array == null || array.length == 0) {
|
||||
throw new IllegalArgumentException("Input array must not be null or empty");
|
||||
}
|
||||
}
|
||||
|
||||
// 将long值钳制到int范围
|
||||
private static int clampToInt(long value) {
|
||||
if (value > Integer.MAX_VALUE) return Integer.MAX_VALUE;
|
||||
if (value < Integer.MIN_VALUE) return Integer.MIN_VALUE;
|
||||
return (int) value;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user