fix: 修复活动报名接口ID类型精度丢失问题
- 将活动ID从Long改为String类型,避免前端JSON反序列化时的精度丢失 - 增强报名接口的参数校验和异常处理 - 优化错误提示信息,提供更详细的错误原因
This commit is contained in:
parent
4bc8961605
commit
e6bad39a56
@ -12,10 +12,10 @@ import lombok.Data;
|
||||
public class EventRegistrationVo {
|
||||
|
||||
/**
|
||||
* 活动ID
|
||||
* 活动ID(使用String避免前端JSON反序列化时的精度丢失)
|
||||
*/
|
||||
@NotNull(message = "活动ID不能为空")
|
||||
private Long eventId;
|
||||
private String eventId;
|
||||
|
||||
/**
|
||||
* 联系电话
|
||||
|
||||
@ -62,7 +62,7 @@ public class HrtEventController {
|
||||
@Operation(summary = "取消报名", description = "取消已报名的活动,活动开始后无法取消")
|
||||
@PostMapping("/cancel/{eventId}")
|
||||
public String cancelRegistration(
|
||||
@Parameter(description = "活动ID") @PathVariable Long eventId
|
||||
@Parameter(description = "活动ID") @PathVariable String eventId
|
||||
) {
|
||||
return hrtEventService.cancelRegistration(eventId);
|
||||
}
|
||||
|
||||
@ -31,7 +31,7 @@ public interface HrtEventService {
|
||||
* @description [取消报名]
|
||||
* @author Leocoder
|
||||
*/
|
||||
String cancelRegistration(Long eventId);
|
||||
String cancelRegistration(String eventId);
|
||||
|
||||
/**
|
||||
* @description [我报名的活动]
|
||||
|
||||
@ -158,9 +158,27 @@ public class HrtEventServiceImpl implements HrtEventService {
|
||||
public String registerEvent(EventRegistrationVo vo) {
|
||||
Long userId = HrtStpUtil.getLoginIdAsLong();
|
||||
|
||||
HrtEvent event = hrtEventMapper.selectById(vo.getEventId());
|
||||
if (event == null || event.getPublishStatus() != 1) {
|
||||
throw new BusinessException(404, "活动不存在或未发布");
|
||||
// 参数校验
|
||||
if (vo == null || vo.getEventId() == null || vo.getEventId().trim().isEmpty()) {
|
||||
throw new BusinessException(400, "活动ID不能为空");
|
||||
}
|
||||
|
||||
// 将String类型的eventId转换为Long(避免前端JSON精度丢失)
|
||||
Long eventId;
|
||||
try {
|
||||
eventId = Long.parseLong(vo.getEventId());
|
||||
} catch (NumberFormatException e) {
|
||||
throw new BusinessException(400, "活动ID格式不正确");
|
||||
}
|
||||
|
||||
// 查询活动
|
||||
HrtEvent event = hrtEventMapper.selectById(eventId);
|
||||
if (event == null) {
|
||||
throw new BusinessException(404, "活动不存在(ID:" + eventId + ")");
|
||||
}
|
||||
|
||||
if (event.getPublishStatus() != 1) {
|
||||
throw new BusinessException(400, "活动未发布,无法报名");
|
||||
}
|
||||
|
||||
if (!event.getStatus().equals("upcoming")) {
|
||||
@ -182,7 +200,7 @@ public class HrtEventServiceImpl implements HrtEventService {
|
||||
|
||||
LambdaQueryWrapper<HrtEventRegistration> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(HrtEventRegistration::getUserId, userId)
|
||||
.eq(HrtEventRegistration::getEventId, vo.getEventId())
|
||||
.eq(HrtEventRegistration::getEventId, eventId)
|
||||
.in(HrtEventRegistration::getStatus, List.of("pending", "approved"));
|
||||
Long count = hrtEventRegistrationMapper.selectCount(wrapper);
|
||||
if (count > 0) {
|
||||
@ -195,7 +213,7 @@ public class HrtEventServiceImpl implements HrtEventService {
|
||||
|
||||
HrtEventRegistration registration = new HrtEventRegistration();
|
||||
registration.setUserId(userId);
|
||||
registration.setEventId(vo.getEventId());
|
||||
registration.setEventId(eventId);
|
||||
registration.setStatus("approved");
|
||||
registration.setPhone(vo.getPhone());
|
||||
registration.setRemark(vo.getRemark());
|
||||
@ -208,7 +226,7 @@ public class HrtEventServiceImpl implements HrtEventService {
|
||||
hrtEventMapper.update(null,
|
||||
new LambdaUpdateWrapper<HrtEvent>()
|
||||
.setSql("current_participants = current_participants + 1")
|
||||
.eq(HrtEvent::getId, vo.getEventId())
|
||||
.eq(HrtEvent::getId, eventId)
|
||||
);
|
||||
|
||||
return "报名成功";
|
||||
@ -220,9 +238,22 @@ public class HrtEventServiceImpl implements HrtEventService {
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public String cancelRegistration(Long eventId) {
|
||||
public String cancelRegistration(String eventIdStr) {
|
||||
Long userId = HrtStpUtil.getLoginIdAsLong();
|
||||
|
||||
// 参数校验
|
||||
if (eventIdStr == null || eventIdStr.trim().isEmpty()) {
|
||||
throw new BusinessException(400, "活动ID不能为空");
|
||||
}
|
||||
|
||||
// 将String转换为Long(避免前端JSON精度丢失)
|
||||
Long eventId;
|
||||
try {
|
||||
eventId = Long.parseLong(eventIdStr);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new BusinessException(400, "活动ID格式不正确");
|
||||
}
|
||||
|
||||
LambdaQueryWrapper<HrtEventRegistration> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(HrtEventRegistration::getUserId, userId)
|
||||
.eq(HrtEventRegistration::getEventId, eventId)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user