fix: save draft schedule with negative IDs from auto-arrange

- Handle negative groupId by creating new schedule groups
- Extract projectId from code format C{competitionId}-P{projectId}
- Save project type, participant count, and team count
- Query participants by participant_id instead of primary key
- Create new participant records if not exists

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
This commit is contained in:
2026-01-15 15:56:29 +08:00
co-authored by factory-droid[bot]
parent ba9b8609e9
commit 106e563f2e
@@ -409,16 +409,75 @@ public class MartialScheduleServiceImpl extends ServiceImpl<MartialScheduleMappe
for (CompetitionGroupDTO groupDTO : dto.getCompetitionGroups()) {
// 1. 更新或创建编排明细
Long groupId = groupDTO.getId();
Long realGroupId = groupId;
// Handle negative ID (draft data from auto-arrange)
if (groupId != null && groupId < 0) {
Long projectId = null;
if (groupDTO.getCode() != null && groupDTO.getCode().contains("-P")) {
try {
String suffix = groupDTO.getCode().substring(groupDTO.getCode().lastIndexOf("-P") + 2);
projectId = Long.parseLong(suffix);
} catch (Exception e) {
log.warn("Failed to parse projectId from code: {}", groupDTO.getCode());
}
}
MartialScheduleGroup existingGroup = null;
if (projectId != null) {
existingGroup = scheduleGroupMapper.selectOne(
new QueryWrapper<MartialScheduleGroup>()
.eq("competition_id", dto.getCompetitionId())
.eq("project_id", projectId)
.eq("is_deleted", 0)
.last("LIMIT 1")
);
}
// Determine project type from DTO
Integer projectType = null;
if ("集体".equals(groupDTO.getType())) {
projectType = 2;
} else if ("单人".equals(groupDTO.getType())) {
projectType = 1;
}
// Calculate participant/team counts
int participantCount = groupDTO.getParticipants() != null ? groupDTO.getParticipants().size() : 0;
if (existingGroup != null) {
realGroupId = existingGroup.getId();
existingGroup.setGroupName(groupDTO.getTitle());
existingGroup.setProjectType(projectType);
existingGroup.setTotalParticipants(participantCount);
existingGroup.setTotalTeams(participantCount);
existingGroup.setCategory(groupDTO.getCode());
scheduleGroupMapper.updateById(existingGroup);
} else {
MartialScheduleGroup newGroup = new MartialScheduleGroup();
newGroup.setCompetitionId(Long.valueOf(dto.getCompetitionId()));
newGroup.setProjectId(projectId);
newGroup.setGroupName(groupDTO.getTitle());
newGroup.setProjectType(projectType);
newGroup.setTotalParticipants(participantCount);
newGroup.setTotalTeams(participantCount);
newGroup.setCategory(groupDTO.getCode());
scheduleGroupMapper.insert(newGroup);
realGroupId = newGroup.getId();
}
}
MartialScheduleDetail detail = scheduleDetailMapper.selectOne(
new QueryWrapper<MartialScheduleDetail>()
.eq("schedule_group_id", groupDTO.getId())
.eq("schedule_group_id", realGroupId)
.eq("is_deleted", 0)
.last("LIMIT 1")
);
if (detail == null) {
detail = new MartialScheduleDetail();
detail.setScheduleGroupId(groupDTO.getId());
detail.setScheduleGroupId(realGroupId);
detail.setCompetitionId(dto.getCompetitionId());
}
@@ -460,7 +519,7 @@ public class MartialScheduleServiceImpl extends ServiceImpl<MartialScheduleMappe
// 1.5 同步更新项目的venue_id,保持数据一致性
if (groupDTO.getVenueId() != null) {
MartialScheduleGroup group = scheduleGroupMapper.selectById(groupDTO.getId());
MartialScheduleGroup group = scheduleGroupMapper.selectById(realGroupId);
if (group != null && group.getProjectId() != null) {
MartialProject project = projectService.getById(group.getProjectId());
if (project != null) {
@@ -473,12 +532,30 @@ public class MartialScheduleServiceImpl extends ServiceImpl<MartialScheduleMappe
// 2. 更新参赛者信息
if (groupDTO.getParticipants() != null) {
for (ParticipantDTO participantDTO : groupDTO.getParticipants()) {
MartialScheduleParticipant participant = scheduleParticipantMapper.selectById(participantDTO.getId());
// Query by participant_id (registration ID) and schedule_group_id, not by primary key
MartialScheduleParticipant participant = scheduleParticipantMapper.selectOne(
new QueryWrapper<MartialScheduleParticipant>()
.eq("participant_id", participantDTO.getId())
.eq("schedule_group_id", realGroupId)
.eq("is_deleted", 0)
.last("LIMIT 1")
);
if (participant != null) {
participant.setCheckInStatus(participantDTO.getStatus());
participant.setPerformanceOrder(participantDTO.getSortOrder());
participant.setScheduleStatus("draft");
scheduleParticipantMapper.updateById(participant);
} else {
// Create new participant record if not exists
MartialScheduleParticipant newParticipant = new MartialScheduleParticipant();
newParticipant.setScheduleGroupId(realGroupId);
newParticipant.setScheduleDetailId(detail.getId()); // Set schedule_detail_id
newParticipant.setParticipantId(Long.valueOf(participantDTO.getId()));
newParticipant.setOrganization(participantDTO.getSchoolUnit());
newParticipant.setCheckInStatus(participantDTO.getStatus());
newParticipant.setPerformanceOrder(participantDTO.getSortOrder());
newParticipant.setScheduleStatus("draft");
scheduleParticipantMapper.insert(newParticipant);
}
}
}