From 3714621173c606c4c58439ed8941100ce9ddea14 Mon Sep 17 00:00:00 2001
From: Codex Assistant <codex@example.com>
Date: 星期三, 05 十一月 2025 15:10:49 +0800
Subject: [PATCH] bug
---
backend/src/main/java/com/rongyichuang/activity/service/ActivityService.java | 301 +++++++++++++++++++++++++++++++++++++++----------
1 files changed, 236 insertions(+), 65 deletions(-)
diff --git a/backend/src/main/java/com/rongyichuang/activity/service/ActivityService.java b/backend/src/main/java/com/rongyichuang/activity/service/ActivityService.java
index 508e233..fb8b6e4 100644
--- a/backend/src/main/java/com/rongyichuang/activity/service/ActivityService.java
+++ b/backend/src/main/java/com/rongyichuang/activity/service/ActivityService.java
@@ -10,12 +10,17 @@
import com.rongyichuang.activity.repository.ActivityJudgeRepository;
import com.rongyichuang.activity.repository.ActivityRepository;
import com.rongyichuang.judge.entity.Judge;
+import com.rongyichuang.player.repository.ActivityPlayerRepository;
import com.rongyichuang.judge.repository.JudgeRepository;
import com.rongyichuang.common.dto.PageRequest;
import com.rongyichuang.common.dto.PageResponse;
import com.rongyichuang.rating.entity.RatingScheme;
import com.rongyichuang.rating.repository.RatingSchemeRepository;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Example;
+import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
@@ -23,14 +28,19 @@
import org.springframework.util.StringUtils;
import java.util.ArrayList;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.Optional;
+import java.util.Set;
import java.util.stream.Collectors;
@Service
@Transactional
public class ActivityService {
+
+ private static final Logger log = LoggerFactory.getLogger(ActivityService.class);
@Autowired
private ActivityRepository activityRepository;
@@ -44,22 +54,59 @@
@Autowired
private RatingSchemeRepository ratingSchemeRepository;
+ @Autowired
+ private ActivityPlayerRepository activityPlayerRepository;
+
/**
* 鍒嗛〉鏌ヨ姣旇禌鍒楄〃
*/
- public PageResponse<ActivityResponse> findActivities(PageRequest pageRequest, String name) {
+ public PageResponse<ActivityResponse> findActivities(PageRequest pageRequest, String name, Integer state) {
Pageable pageable = pageRequest.toPageable();
Page<Activity> page;
-
- if (StringUtils.hasText(name)) {
- page = activityRepository.findByPidAndStateAndNameContainingOrderByCreateTimeDesc(0L, 1, name, pageable);
+
+ boolean hasName = StringUtils.hasText(name);
+ if (state != null) {
+ if (hasName) {
+ page = activityRepository.findByPidAndStateAndNameContainingOrderByCreateTimeDesc(0L, state, name, pageable);
+ } else {
+ page = activityRepository.findByPidAndStateOrderByCreateTimeDesc(0L, state, pageable);
+ }
+ } else if (hasName) {
+ // 褰搒tate涓簄ull浣嗘湁鍚嶇О鎼滅储鏃讹紝闇�瑕佽繃婊ゆ帀宸插垹闄ょ殑姣旇禌锛坰tate != 0锛�
+ page = activityRepository.findByPidAndStateNotAndNameContainingOrderByCreateTimeDesc(0L, 0, name, pageable);
} else {
- page = activityRepository.findByPidAndStateOrderByCreateTimeDesc(0L, 1, pageable);
+ // 褰搒tate涓簄ull鏃讹紝鏌ヨ鎵�鏈夋湭鍒犻櫎鐨勪富娲诲姩锛坧id = 0 涓� state != 0锛�
+ page = activityRepository.findByPidAndStateNotOrderByCreateTimeDesc(0L, 0, pageable);
}
-
+
List<ActivityResponse> content = page.getContent().stream()
- .map(ActivityResponse::new)
+ .map(activity -> {
+ ActivityResponse response = new ActivityResponse(activity);
+ // 璁剧疆鍙傝禌浜烘暟锛堢粺璁$涓�闃舵鐨勫緟瀹℃牳鍜屽鏍搁�氳繃瀛﹀憳浜烘暟锛�
+ int playerCount = 0;
+ Activity firstStage = activityRepository.findFirstStageByActivityId(activity.getId());
+ if (firstStage != null) {
+ // 濡傛灉鏈夌涓�闃舵锛岀粺璁$涓�闃舵鐨勫緟瀹℃牳鍜屽鏍搁�氳繃浜烘暟
+ Long playerCountLong = activityPlayerRepository.countByStageId(firstStage.getId());
+ playerCount = playerCountLong != null ? playerCountLong.intValue() : 0;
+ } else {
+ // 濡傛灉娌℃湁闃舵锛岀粺璁℃椿鍔ㄦ湰韬殑寰呭鏍稿拰瀹℃牳閫氳繃浜烘暟
+ Long playerCountLong = activityPlayerRepository.countByActivityId(activity.getId());
+ playerCount = playerCountLong != null ? playerCountLong.intValue() : 0;
+ }
+ response.setPlayerCount(playerCount);
+ return response;
+ })
.collect(Collectors.toList());
+
+ // 璋冭瘯锛氭墦鍗拌繑鍥炵粰鍓嶇鐨勫搷搴斿璞′腑鐨勬姤鍚嶆埅姝�
+ try {
+ content.stream().limit(10).forEach(r ->
+ log.info("Response(id={}, name={}) signupDeadline={}, matchTime={}", r.getId(), r.getName(), r.getSignupDeadline(), r.getMatchTime())
+ );
+ } catch (Exception e) {
+ log.warn("鎵撳嵃ActivityResponse鏃ュ織澶辫触: {}", e.getMessage());
+ }
return new PageResponse<>(content, page.getTotalElements(), page.getNumber(), page.getSize());
}
@@ -73,11 +120,23 @@
Activity activity = activityOpt.get();
ActivityResponse response = new ActivityResponse(activity);
+ // 璁剧疆鍙傝禌浜烘暟锛堝鏍搁�氳繃鐨勬姤鍚嶆暟閲忥級
+ Long playerCountLong = activityPlayerRepository.countByActivityId(activity.getId());
+ int playerCount = playerCountLong != null ? playerCountLong.intValue() : 0;
+ response.setPlayerCount(playerCount);
+
// 濡傛灉鏄瘮璧涳紝鍔犺浇鍏堕樁娈�
if (activity.isMainActivity()) {
List<Activity> stages = activityRepository.findByPidAndStateOrderByCreateTimeAsc(id, 1);
List<ActivityResponse> stageResponses = stages.stream()
- .map(ActivityResponse::new)
+ .map(stage -> {
+ ActivityResponse stageResponse = new ActivityResponse(stage);
+ // 璁剧疆闃舵鐨勫弬璧涗汉鏁�
+ Long stagePlayerCountLong = activityPlayerRepository.countByActivityId(stage.getId());
+ int stagePlayerCount = stagePlayerCountLong != null ? stagePlayerCountLong.intValue() : 0;
+ stageResponse.setPlayerCount(stagePlayerCount);
+ return stageResponse;
+ })
.collect(Collectors.toList());
response.setStages(stageResponses);
}
@@ -129,8 +188,26 @@
}
}
+ // 濡傛灉鏄瘮璧涳紝楠岃瘉蹇呴』鑷冲皯鏈変竴涓樁娈�
+ if (input.isMainActivity()) {
+ if (input.getStages() == null || input.getStages().isEmpty()) {
+ throw new RuntimeException("姣旇禌蹇呴』鑷冲皯鏈変竴涓樁娈�");
+ }
+
+ // 楠岃瘉闃舵鏁伴噺涓嶈秴杩�5涓�
+ if (input.getStages().size() > 5) {
+ throw new RuntimeException("姣旇禌闃舵鏁伴噺涓嶈兘瓒呰繃5涓�");
+ }
+
+ // 楠岃瘉sort_order杩炵画鎬�
+ validateSortOrderContinuity(input.getStages());
+ }
+
// 淇濆瓨姣旇禌
activity = activityRepository.save(activity);
+
+ // 璁板綍鏃ュ織浠ヤ究璋冭瘯
+ log.info("淇濆瓨姣旇禌鎴愬姛锛屾瘮璧汭D: {}, 姣旇禌鍚嶇О: {}", activity.getId(), activity.getName());
// 濡傛灉鏄瘮璧涗笖鏈夐樁娈典俊鎭紝淇濆瓨闃舵
if (input.isMainActivity() && input.getStages() != null && !input.getStages().isEmpty()) {
@@ -198,6 +275,7 @@
stage.setMatchTime(stageInput.getMatchTime());
stage.setAddress(stageInput.getAddress());
stage.setPlayerMax(stageInput.getPlayerMax());
+ stage.setSortOrder(stageInput.getSortOrder());
stage.setState(stageInput.getState());
// 闃舵缁ф壙姣旇禌鐨勬姤鍚嶆埅姝㈡椂闂�
@@ -210,7 +288,11 @@
stage.setRatingSchemeId(activity.getRatingSchemeId());
}
- activityRepository.save(stage);
+ // 淇濆瓨闃舵骞惰幏鍙栬嚜澧濱D
+ stage = activityRepository.save(stage);
+
+ // 璁板綍鏃ュ織浠ヤ究璋冭瘯
+ log.info("淇濆瓨闃舵鎴愬姛锛岄樁娈礗D: {}, 闃舵鍚嶇О: {}", stage.getId(), stage.getName());
}
}
@@ -219,14 +301,20 @@
*/
private void saveActivityJudges(Long activityId, List<ActivityJudgeInput> judgeInputs) {
if (judgeInputs == null || judgeInputs.isEmpty()) {
+ log.info("娌℃湁璇勫闇�瑕佷繚瀛橈紝姣旇禌ID: {}", activityId);
return;
}
+
+ log.info("寮�濮嬩繚瀛樿瘎濮旓紝姣旇禌ID: {}, 璇勫鏁伴噺: {}", activityId, judgeInputs.size());
// 鑾峰彇姣旇禌鐨勬墍鏈夐樁娈碉紙濡傛灉鏈夌殑璇濓級
List<Activity> stages = activityRepository.findByPidAndStateOrderByCreateTimeAsc(activityId, 1);
// 淇濆瓨璇勫鍏宠仈
for (ActivityJudgeInput judgeInput : judgeInputs) {
+ log.info("澶勭悊璇勫: ID={}, 濮撳悕={}, 闃舵IDs={}",
+ judgeInput.getJudgeId(), judgeInput.getJudgeName(), judgeInput.getStageIds());
+
// 鍏堝垹闄よ璇勫鐨勭幇鏈夊叧鑱�
activityJudgeRepository.deleteByActivityIdAndJudgeId(activityId, judgeInput.getJudgeId());
@@ -235,19 +323,24 @@
if (stages.isEmpty()) {
// 姣旇禌娌℃湁闃舵锛岀洿鎺ュ叧鑱斿埌姣旇禌锛坰tage_id涓簄ull琛ㄧず鎵�鏈夐樁娈碉級
ActivityJudge activityJudge = new ActivityJudge(activityId, judgeInput.getJudgeId(), null);
- activityJudgeRepository.save(activityJudge);
+ activityJudge = activityJudgeRepository.save(activityJudge);
+ log.info("淇濆瓨璇勫鍏宠仈鎴愬姛: 姣旇禌ID={}, 璇勫ID={}, 闃舵ID=null", activityId, judgeInput.getJudgeId());
} else {
// 涓烘瘡涓樁娈靛垱寤哄叧鑱�
for (Activity stage : stages) {
ActivityJudge activityJudge = new ActivityJudge(activityId, judgeInput.getJudgeId(), stage.getId());
- activityJudgeRepository.save(activityJudge);
+ activityJudge = activityJudgeRepository.save(activityJudge);
+ log.info("淇濆瓨璇勫鍏宠仈鎴愬姛: 姣旇禌ID={}, 璇勫ID={}, 闃舵ID={}", activityId, judgeInput.getJudgeId(), stage.getId());
}
}
} else {
// 涓烘瘡涓寚瀹氱殑闃舵鍒涘缓鍏宠仈
for (Long stageId : judgeInput.getStageIds()) {
- ActivityJudge activityJudge = new ActivityJudge(activityId, judgeInput.getJudgeId(), stageId);
- activityJudgeRepository.save(activityJudge);
+ // 濡傛灉stageId绛変簬褰撳墠姣旇禌ID锛岃〃绀鸿瘎濮旇礋璐f暣涓瘮璧涳紝stage_id璁句负null
+ Long actualStageId = stageId.equals(activityId) ? null : stageId;
+ ActivityJudge activityJudge = new ActivityJudge(activityId, judgeInput.getJudgeId(), actualStageId);
+ activityJudge = activityJudgeRepository.save(activityJudge);
+ log.info("淇濆瓨璇勫鍏宠仈鎴愬姛: 姣旇禌ID={}, 璇勫ID={}, 闃舵ID={}", activityId, judgeInput.getJudgeId(), actualStageId);
}
}
}
@@ -277,63 +370,43 @@
}
return false;
}
+
+ public boolean updateActivityState(Long id, Integer state) {
+ Optional<Activity> activityOpt = activityRepository.findById(id);
+ if (activityOpt.isPresent()) {
+ Activity activity = activityOpt.get();
+ activity.setState(state);
+ activityRepository.save(activity);
+ return true;
+ }
+ return false;
+ }
/**
- * 鑾峰彇鎵�鏈夋湁鏁堟瘮璧涘拰闃舵锛堢敤浜庝笅鎷夐�夋嫨锛�
+ * 鑾峰彇鎵�鏈夋湁鏁堜富姣旇禌锛堢敤浜庝笅鎷夐�夋嫨锛�
*/
public List<ActivityResponse> findAllActivitiesForSelection() {
- // 鑾峰彇鎵�鏈夋椿鍔紙鍖呮嫭姣旇禌鍜岄樁娈碉級
- List<Activity> activities = activityRepository.findByStateOrderByPidAscNameAsc(1);
+ // 鑾峰彇鎵�鏈夌姸鎬佷负1鐨勬椿鍔�
+ List<Activity> allActivities = activityRepository.findByStateOrderByPidAscNameAsc(1);
- // 鍒涘缓姣旇禌ID鍒版瘮璧涘璞$殑鏄犲皠锛岀敤浜庡揩閫熸煡鎵剧埗姣旇禌
- Map<Long, Activity> mainActivityMap = activities.stream()
+ // 杩囨护锛氬彧淇濈暀涓绘瘮璧涳紙pid=0锛�
+ List<Activity> filteredActivities = allActivities.stream()
.filter(activity -> activity.getPid() == 0)
- .collect(Collectors.toMap(Activity::getId, activity -> activity));
-
- // 杞崲涓篈ctivityResponse骞跺~鍏卲arent淇℃伅
- List<ActivityResponse> result = activities.stream()
- .map(activity -> {
- ActivityResponse response = new ActivityResponse(activity);
- // 濡傛灉鏄樁娈碉紙pid > 0锛夛紝濉厖parent淇℃伅
- if (activity.getPid() > 0) {
- Activity parentActivity = mainActivityMap.get(activity.getPid());
- if (parentActivity != null) {
- response.setParent(new ActivityResponse(parentActivity));
- }
- }
- return response;
- })
.collect(Collectors.toList());
- // 鑷畾涔夋帓搴忥細姣旇禌鍜屽叾闃舵鏀惧湪涓�璧�
- result.sort((a, b) -> {
- // 濡傛灉閮芥槸姣旇禌锛坧id=0锛夛紝鎸夊悕绉版帓搴�
- if (a.getPid() == 0 && b.getPid() == 0) {
- return a.getName().compareTo(b.getName());
- }
- // 濡傛灉閮芥槸闃舵锛屽厛鎸夌埗姣旇禌鍚嶇О鎺掑簭锛屽啀鎸夐樁娈靛悕绉版帓搴�
- if (a.getPid() > 0 && b.getPid() > 0) {
- String aParentName = a.getParent() != null ? a.getParent().getName() : "";
- String bParentName = b.getParent() != null ? b.getParent().getName() : "";
- int parentCompare = aParentName.compareTo(bParentName);
- if (parentCompare != 0) {
- return parentCompare;
- }
- return a.getName().compareTo(b.getName());
- }
- // 濡傛灉涓�涓槸姣旇禌锛屼竴涓槸闃舵
- if (a.getPid() == 0 && b.getPid() > 0) {
- String bParentName = b.getParent() != null ? b.getParent().getName() : "";
- int compare = a.getName().compareTo(bParentName);
- return compare <= 0 ? -1 : 1; // 姣旇禌鎺掑湪鍏堕樁娈靛墠闈�
- }
- if (a.getPid() > 0 && b.getPid() == 0) {
- String aParentName = a.getParent() != null ? a.getParent().getName() : "";
- int compare = aParentName.compareTo(b.getName());
- return compare < 0 ? -1 : 1; // 闃舵鎺掑湪鍏舵瘮璧涘悗闈�
- }
- return 0;
- });
+ // 杞崲涓篈ctivityResponse
+ List<ActivityResponse> result = filteredActivities.stream()
+ .map(activity -> {
+ ActivityResponse response = new ActivityResponse(activity);
+ // 璁剧疆鍙傝禌浜烘暟锛堟墍鏈夐樁娈电殑鎶ュ悕鏁伴噺鎬诲拰锛�
+ Long playerCountLong = activityPlayerRepository.countByActivityId(activity.getId());
+ int playerCount = playerCountLong != null ? playerCountLong.intValue() : 0;
+ response.setPlayerCount(playerCount);
+
+ return response;
+ })
+ .sorted((a, b) -> a.getName().compareTo(b.getName()))
+ .collect(Collectors.toList());
return result;
}
@@ -344,8 +417,62 @@
public List<ActivityResponse> findStagesByActivityId(Long activityId) {
List<Activity> stages = activityRepository.findByPidAndStateOrderByCreateTimeAsc(activityId, 1);
return stages.stream()
- .map(ActivityResponse::new)
+ .map(activity -> {
+ ActivityResponse response = new ActivityResponse(activity);
+ // 璁剧疆鍙傝禌浜烘暟锛堝鏍搁�氳繃鐨勬姤鍚嶆暟閲忥級
+ Long playerCountLong = activityPlayerRepository.countByActivityId(activity.getId());
+ int playerCount = playerCountLong != null ? playerCountLong.intValue() : 0;
+ response.setPlayerCount(playerCount);
+ return response;
+ })
.collect(Collectors.toList());
+ }
+
+ /**
+ * 鑾峰彇鎵�鏈夋瘮璧涢樁娈碉紙鐢ㄤ簬璇勫椤甸潰涓嬫媺閫夋嫨锛�
+ * 杩斿洖鎵�鏈夌姸鎬佷负1涓攑id>0鐨勬瘮璧涢樁娈�
+ */
+ public List<ActivityResponse> findAllStagesForSelection() {
+ // 鑾峰彇鎵�鏈夌姸鎬佷负1鐨勬椿鍔�
+ List<Activity> allActivities = activityRepository.findByStateOrderByPidAscNameAsc(1);
+
+ // 鍒嗙涓绘瘮璧涘拰闃舵
+ Map<Long, Activity> parentActivitiesMap = allActivities.stream()
+ .filter(activity -> activity.getPid() == 0)
+ .collect(Collectors.toMap(Activity::getId, activity -> activity));
+
+ // 杩囨护锛氬彧淇濈暀姣旇禌闃舵锛坧id>0锛�
+ List<Activity> filteredStages = allActivities.stream()
+ .filter(activity -> activity.getPid() > 0)
+ .collect(Collectors.toList());
+
+ // 杞崲涓篈ctivityResponse
+ List<ActivityResponse> result = filteredStages.stream()
+ .map(activity -> {
+ ActivityResponse response = new ActivityResponse(activity);
+ // 璁剧疆鍙傝禌浜烘暟锛堝鏍搁�氳繃鐨勬姤鍚嶆暟閲忥級
+ Long playerCountLong = activityPlayerRepository.countByActivityId(activity.getId());
+ int playerCount = playerCountLong != null ? playerCountLong.intValue() : 0;
+ response.setPlayerCount(playerCount);
+
+ // 鎵嬪姩璁剧疆parent淇℃伅
+ Activity parentActivity = parentActivitiesMap.get(activity.getPid());
+ if (parentActivity != null) {
+ ActivityResponse parentResponse = new ActivityResponse(parentActivity);
+ response.setParent(parentResponse);
+ }
+
+ return response;
+ })
+ .sorted((a, b) -> {
+ // 鍏堟寜鐖舵瘮璧汭D鎺掑簭锛屽啀鎸夐樁娈靛悕绉版帓搴�
+ int pidCompare = Long.compare(a.getPid(), b.getPid());
+ if (pidCompare != 0) return pidCompare;
+ return a.getName().compareTo(b.getName());
+ })
+ .collect(Collectors.toList());
+
+ return result;
}
/**
@@ -361,7 +488,14 @@
public List<ActivityResponse> findOngoingActivities() {
List<Activity> activities = activityRepository.findOngoingActivities();
return activities.stream()
- .map(ActivityResponse::new)
+ .map(activity -> {
+ ActivityResponse response = new ActivityResponse(activity);
+ // 璁剧疆鍙傝禌浜烘暟锛堝鏍搁�氳繃鐨勬姤鍚嶆暟閲忥級
+ Long playerCountLong = activityPlayerRepository.countByActivityId(activity.getId());
+ int playerCount = playerCountLong != null ? playerCountLong.intValue() : 0;
+ response.setPlayerCount(playerCount);
+ return response;
+ })
.collect(Collectors.toList());
}
@@ -391,7 +525,10 @@
Map<Long, List<Long>> judgeStageMap = activityJudges.stream()
.collect(Collectors.groupingBy(
ActivityJudge::getJudgeId,
- Collectors.mapping(ActivityJudge::getStageId, Collectors.toList())
+ Collectors.mapping(
+ aj -> aj.getStageId() != null ? aj.getStageId() : activityId, // 濡傛灉stage_id涓簄ull锛岃〃绀鸿礋璐f暣涓瘮璧涳紝浣跨敤activityId
+ Collectors.toList()
+ )
));
// 鏌ヨ璇勫璇︾粏淇℃伅骞舵瀯寤哄搷搴�
@@ -416,4 +553,38 @@
return result;
}
+
+ /**
+ * 楠岃瘉闃舵鐨剆ort_order杩炵画鎬�
+ */
+ private void validateSortOrderContinuity(List<ActivityStageInput> stages) {
+ if (stages == null || stages.isEmpty()) {
+ return;
+ }
+
+ // 鏀堕泦鎵�鏈夌殑sortOrder鍊�
+ List<Integer> sortOrders = stages.stream()
+ .map(ActivityStageInput::getSortOrder)
+ .filter(Objects::nonNull)
+ .sorted()
+ .collect(Collectors.toList());
+
+ // 妫�鏌ユ槸鍚︿粠1寮�濮�
+ if (sortOrders.isEmpty() || sortOrders.get(0) != 1) {
+ throw new RuntimeException("闃舵鎺掑簭蹇呴』浠�1寮�濮�");
+ }
+
+ // 妫�鏌ヨ繛缁��
+ for (int i = 0; i < sortOrders.size(); i++) {
+ if (sortOrders.get(i) != i + 1) {
+ throw new RuntimeException("闃舵鎺掑簭蹇呴』杩炵画锛屼笉鑳借烦璺冿紙濡傦細1,2,3...锛�");
+ }
+ }
+
+ // 妫�鏌ユ槸鍚︽湁閲嶅鐨剆ortOrder
+ Set<Integer> uniqueSortOrders = new HashSet<>(sortOrders);
+ if (uniqueSortOrders.size() != sortOrders.size()) {
+ throw new RuntimeException("闃舵鎺掑簭涓嶈兘閲嶅");
+ }
+ }
}
\ No newline at end of file
--
Gitblit v1.8.0