超时4小时时间处理

This commit is contained in:
cb 2025-04-19 09:33:09 +08:00
parent 6f357a3bcb
commit c471f79d34
1 changed files with 27 additions and 5 deletions

View File

@ -51,10 +51,7 @@ import org.springframework.web.bind.annotation.RequestBody;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.LocalDate; import java.time.*;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -422,7 +419,8 @@ public class OrderServiceImpl implements OrderService {
// 未超时的单 // 未超时的单
else if (order.getOrderStatus().equals(OrderStatus.SERVER.code())) { else if (order.getOrderStatus().equals(OrderStatus.SERVER.code())) {
// 服务中状态要按预计上门时间计算4h超时 // 服务中状态要按预计上门时间计算4h超时
Date overTime = DateUtils.addMilliseconds(order.getUpdateTime(), 4 * 60 * 60 * 1000); // Date overTime = DateUtils.addMilliseconds(order.getUpdateTime(), 4 * 60 * 60 * 1000);
Date overTime=calculateOverTime(order.getUpdateTime());
if (overTime.before(now)) { if (overTime.before(now)) {
log.info("订单[{}]服务中状态超时4小时 扣款", order.getId()); log.info("订单[{}]服务中状态超时4小时 扣款", order.getId());
OrderTimeoutRecord record = new OrderTimeoutRecord(order.getId(), order.getWorkerId(), order.getDeptId(), order.getOrderStatus()); OrderTimeoutRecord record = new OrderTimeoutRecord(order.getId(), order.getWorkerId(), order.getDeptId(), order.getOrderStatus());
@ -1286,4 +1284,28 @@ public class OrderServiceImpl implements OrderService {
} }
return true; return true;
} }
/*判断超时4小时的时间 如果超过晚上八点则剩余的时间从第二天早上开始计时*/
public static Date calculateOverTime(Date updateTime) {
// 1. 增加4小时
LocalDateTime overTime = updateTime.toInstant()
.atZone(java.time.ZoneId.systemDefault())
.toLocalDateTime()
.plusHours(4);
// 2. 判断是否超过20:00
LocalTime cutoff = LocalTime.of(20, 0);
LocalTime timePart = overTime.toLocalTime();
if (timePart.isAfter(cutoff)) {
// 3. 计算超时部分时长
Duration excess = Duration.between(cutoff, timePart);
// 4. 重置到次日8:00并加上超时时长
LocalDateTime nextDay = overTime.toLocalDate().plusDays(1).atStartOfDay();
LocalDateTime adjustedTime = LocalDateTime.of(LocalDate.from(nextDay), LocalTime.of(8, 0)).plus(excess);
return Date.from(adjustedTime.atZone(java.time.ZoneId.systemDefault()).toInstant());
}
return Date.from(overTime.atZone(java.time.ZoneId.systemDefault()).toInstant());
}
} }