From 4fa9591629721797386fc11836e3a9deb69cd58c Mon Sep 17 00:00:00 2001 From: lrj <owen.stl@gmail.com> Date: 星期三, 24 九月 2025 17:00:37 +0800 Subject: [PATCH] 修改评分逻辑,支持多个评委 --- backend/src/main/java/com/rongyichuang/judge/service/JudgeService.java | 67 +++++++++++++++++++++++++++------ 1 files changed, 54 insertions(+), 13 deletions(-) diff --git a/backend/src/main/java/com/rongyichuang/judge/service/JudgeService.java b/backend/src/main/java/com/rongyichuang/judge/service/JudgeService.java index 63eef94..6f8fd7c 100644 --- a/backend/src/main/java/com/rongyichuang/judge/service/JudgeService.java +++ b/backend/src/main/java/com/rongyichuang/judge/service/JudgeService.java @@ -3,22 +3,27 @@ import com.rongyichuang.judge.dto.request.JudgeInput; import com.rongyichuang.judge.dto.response.JudgeResponse; import com.rongyichuang.judge.entity.Judge; -import com.rongyichuang.judge.entity.Tag; +import com.rongyichuang.tag.entity.Tag; import com.rongyichuang.judge.repository.JudgeRepository; import com.rongyichuang.common.entity.Media; import com.rongyichuang.common.repository.MediaRepository; import com.rongyichuang.common.dto.request.MediaInput; import com.rongyichuang.common.dto.response.MediaResponse; import com.rongyichuang.common.service.MediaService; -import com.rongyichuang.judge.repository.TagRepository; +import com.rongyichuang.common.exception.BusinessException; +import com.rongyichuang.common.enums.MediaTargetType; +import com.rongyichuang.tag.repository.TagRepository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.util.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.stream.Collectors; @@ -31,6 +36,9 @@ private final TagRepository tagRepository; private final MediaRepository mediaRepository; private final MediaService mediaService; + + @Value("${app.media-url:${app.media.url:}}") + private String mediaBaseUrl; public JudgeService(JudgeRepository judgeRepository, TagRepository tagRepository, MediaRepository mediaRepository, MediaService mediaService) { @@ -63,12 +71,12 @@ Judge judge; if (input.getId() != null) { judge = judgeRepository.findById(input.getId()) - .orElseThrow(() -> new RuntimeException("璇勫涓嶅瓨鍦�")); + .orElseThrow(() -> new BusinessException("璇勫涓嶅瓨鍦�")); } else { judge = new Judge(); // 鏂板璇勫鏃舵鏌ユ墜鏈哄彿鏄惁宸插瓨鍦� if (judgeRepository.existsByPhone(input.getPhone())) { - throw new RuntimeException("鎵嬫満鍙风爜宸插瓨鍦紝璇蜂娇鐢ㄥ叾浠栨墜鏈哄彿鐮�"); + throw new BusinessException("PHONE_EXISTS", "鎵嬫満鍙风爜宸插瓨鍦紝璇蜂娇鐢ㄥ叾浠栨墜鏈哄彿鐮�"); } } @@ -76,16 +84,17 @@ judge.setPhone(input.getPhone()); judge.setGender(input.getGender()); judge.setDescription(input.getDescription()); - - // 澶村儚澶勭悊宸茬Щ闄わ紝鍥犱负鏁版嵁搴撹〃缁撴瀯涓病鏈塧vatar_media_id瀛楁 + judge.setTitle(input.getTitle()); + judge.setCompany(input.getCompany()); + judge.setIntroduction(input.getIntroduction()); // 澶村儚淇℃伅閫氳繃t_media琛ㄧ殑target_type鍜宼arget_id鍏宠仈 // 璁剧疆涓撲笟鏍囩 if (input.getMajorIds() != null && !input.getMajorIds().isEmpty()) { - List<Tag> specialties = tagRepository.findAllById(input.getMajorIds()); - judge.setSpecialties(specialties); + List<Tag> specialtiesList = tagRepository.findAllById(input.getMajorIds()); + judge.setSpecialties(new HashSet<>(specialtiesList)); } else { - judge.setSpecialties(Collections.emptyList()); + judge.setSpecialties(Collections.emptySet()); } Judge savedJudge = judgeRepository.save(judge); @@ -95,7 +104,7 @@ @Transactional public void delete(Long id) { Judge judge = judgeRepository.findById(id) - .orElseThrow(() -> new RuntimeException("璇勫涓嶅瓨鍦�")); + .orElseThrow(() -> new BusinessException("璇勫涓嶅瓨鍦�")); judgeRepository.delete(judge); } @@ -108,16 +117,21 @@ response.setPhone(judge.getPhone()); response.setGender(judge.getGender()); response.setDescription(judge.getDescription()); + response.setTitle(judge.getTitle()); + response.setCompany(judge.getCompany()); + response.setIntroduction(judge.getIntroduction()); - // 鏌ヨ澶村儚淇℃伅锛歵arget_type=1琛ㄧず璇勫锛宼arget_id涓鸿瘎濮擨D + // 鏌ヨ澶村儚淇℃伅锛氫娇鐢ㄦ灇涓惧父閲忚〃绀鸿瘎濮斿ご鍍忕被鍨� log.info("=== Querying media for judge ID: {}", judge.getId()); - List<Media> avatarMedias = mediaRepository.findByTargetTypeAndTargetIdAndState(1, judge.getId(), 1); + List<Media> avatarMedias = mediaRepository.findByTargetTypeAndTargetIdAndState( + MediaTargetType.JUDGE_AVATAR.getValue(), judge.getId(), 1); log.info("=== Found {} media records", avatarMedias.size()); if (!avatarMedias.isEmpty()) { // 鍙栫涓�涓獟浣撲綔涓哄ご鍍� Media avatarMedia = avatarMedias.get(0); - String avatarUrl = avatarMedia.getPath(); + String avatarPath = avatarMedia.getPath(); + String avatarUrl = buildFullMediaUrl(avatarPath); log.info("=== Setting avatarUrl: {}", avatarUrl); response.setAvatarUrl(avatarUrl); } else { @@ -138,6 +152,33 @@ return response; } + + /** + * 鏋勫缓瀹屾暣鐨勫獟浣揢RL + * @param path 濯掍綋璺緞 + * @return 瀹屾暣鐨刄RL + */ + private String buildFullMediaUrl(String path) { + if (!StringUtils.hasText(path)) { + return null; + } + + // 濡傛灉璺緞宸茬粡鏄畬鏁碪RL锛岀洿鎺ヨ繑鍥� + if (path.startsWith("http://") || path.startsWith("https://")) { + return path; + } + + // 鏋勫缓瀹屾暣URL + if (StringUtils.hasText(mediaBaseUrl)) { + // 纭繚baseUrl浠�/缁撳熬锛宲ath涓嶄互/寮�澶� + String baseUrl = mediaBaseUrl.endsWith("/") ? mediaBaseUrl : mediaBaseUrl + "/"; + String relativePath = path.startsWith("/") ? path.substring(1) : path; + return baseUrl + relativePath; + } + + // 濡傛灉娌℃湁閰嶇疆baseUrl锛岃繑鍥炲師璺緞 + return path; + } /** * 淇濆瓨濯掍綋淇℃伅 -- Gitblit v1.8.0