充电桩预约功能

This commit is contained in:
Guoqs
2024-06-18 17:13:29 +08:00
parent 313392104a
commit f5b00e9696
6 changed files with 144 additions and 29 deletions

View File

@@ -1,12 +1,15 @@
package com.jsowell.pile.service.impl;
import com.jsowell.common.constant.Constants;
import com.jsowell.common.enums.ykc.ReturnCodeEnum;
import com.jsowell.common.exception.BusinessException;
import com.jsowell.common.util.DateUtils;
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.dto.PileReservedDTO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.sql.Time;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;
@@ -125,17 +128,17 @@ public class PileReservedInfoServiceImpl implements PileReservedInfoService {
* @param reservationId
* @return
*/
public boolean isTimeSlotAvailable(String memberId, String pileSn, Date startTime, Date endTime, Integer reservationId) {
public boolean isTimeSlotAvailable(String memberId, String pileSn, Time startTime, Time endTime, Integer reservationId) {
List<PileReservedInfo> reservations = pileReservedInfoMapper.findByMemberIdAndPileSnAndStatus(memberId, pileSn, "1");
LocalTime newStartTime = LocalTime.parse(DateUtils.formatDateTime(startTime));
LocalTime newEndTime = LocalTime.parse(DateUtils.formatDateTime(endTime));
LocalTime newStartTime = startTime.toLocalTime();
LocalTime newEndTime = endTime.toLocalTime();
for (PileReservedInfo res : reservations) {
if (res.getId().equals(reservationId)) {
continue; // Skip the current reservation if updating
}
LocalTime existingStartTime = LocalTime.parse(DateUtils.formatDateTime(res.getStartTime()));
LocalTime existingEndTime = LocalTime.parse(DateUtils.formatDateTime(res.getEndTime()));
LocalTime existingStartTime = res.getStartTime().toLocalTime();
LocalTime existingEndTime = res.getEndTime().toLocalTime();
if (newStartTime.isBefore(existingEndTime) && newEndTime.isAfter(existingStartTime)) {
return false; // Time slot overlaps
@@ -148,7 +151,7 @@ public class PileReservedInfoServiceImpl implements PileReservedInfoService {
if (isTimeSlotAvailable(reservation.getMemberId(), reservation.getPileSn(), reservation.getStartTime(), reservation.getEndTime(), reservation.getId())) {
this.updateByPrimaryKeySelective(reservation);
} else {
throw new RuntimeException("Time slot overlaps with an existing reservation.");
throw new BusinessException(ReturnCodeEnum.CODE_UPDATE_RESERVED_STATUS_REFUSED);
}
}
}