From 9f8395fab13ca4b230a0f7d62636e209745c91d4 Mon Sep 17 00:00:00 2001
From: lrj <owen.stl@gmail.com>
Date: 星期日, 28 九月 2025 14:16:18 +0800
Subject: [PATCH] feat: 完善注册流程的文件上传功能

---
 backend/src/main/java/com/rongyichuang/judge/service/JudgeService.java |  123 +++++++++++++++++++++++++++++++++-------
 1 files changed, 100 insertions(+), 23 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..a0f5dc2 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,29 @@
 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 com.rongyichuang.user.service.UserService;
+import com.rongyichuang.user.entity.User;
 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,13 +38,19 @@
     private final TagRepository tagRepository;
     private final MediaRepository mediaRepository;
     private final MediaService mediaService;
+    private final UserService userService;
+    
+    @Value("${app.media-url}")
+    private String mediaBaseUrl;
 
     public JudgeService(JudgeRepository judgeRepository, TagRepository tagRepository, 
-                       MediaRepository mediaRepository, MediaService mediaService) {
+                       MediaRepository mediaRepository, MediaService mediaService,
+                       UserService userService) {
         this.judgeRepository = judgeRepository;
         this.tagRepository = tagRepository;
         this.mediaRepository = mediaRepository;
         this.mediaService = mediaService;
+        this.userService = userService;
     }
 
     public List<JudgeResponse> findAll() {
@@ -61,14 +74,25 @@
     @Transactional
     public JudgeResponse save(JudgeInput input) {
         Judge judge;
+        User user;
+        
+        // 澶勭悊User琛ㄩ�昏緫
+        if (input.getPassword() != null && !input.getPassword().trim().isEmpty() && !"鈥⑩�⑩�⑩�⑩�⑩�⑩�⑩��".equals(input.getPassword())) {
+            // 鏈夊瘑鐮佷笖涓嶆槸鍗犱綅绗︽椂锛屽垱寤烘垨鏇存柊鐢ㄦ埛锛堝寘鍚瘑鐮侊級
+            user = userService.findOrCreateUserByPhone(input.getPhone(), input.getName(), input.getPassword());
+        } else {
+            // 鏃犲瘑鐮佹垨鏄崰浣嶇鏃讹紝鍙洿鏂扮敤鎴峰熀鏈俊鎭紙涓嶆洿鏂板瘑鐮侊級
+            user = userService.findOrCreateUserByPhone(input.getPhone(), input.getName(), null);
+        }
+
         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 +100,18 @@
         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());
+        judge.setUserId(user.getId()); // 璁剧疆鍏宠仈鐨勭敤鎴稩D
         // 澶村儚淇℃伅閫氳繃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 +121,7 @@
     @Transactional
     public void delete(Long id) {
         Judge judge = judgeRepository.findById(id)
-                .orElseThrow(() -> new RuntimeException("璇勫涓嶅瓨鍦�"));
+                .orElseThrow(() -> new BusinessException("璇勫涓嶅瓨鍦�"));
         judgeRepository.delete(judge);
     }
 
@@ -108,16 +134,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,20 +169,63 @@
 
         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;
+    }
 
     /**
      * 淇濆瓨濯掍綋淇℃伅
      */
     public MediaResponse saveMediaInfo(MediaInput input) {
-        Media media = mediaService.saveMedia(
-            input.getName(),
-            input.getPath(),
-            input.getFileSize(),
-            input.getFileExt(),
-            input.getMediaType(),
-            input.getTargetType(),
-            input.getTargetId()
-        );
+        Media media;
+        
+        // 濡傛灉鏈夌缉鐣ュ浘璺緞锛屼娇鐢ㄦ敮鎸佺缉鐣ュ浘鐨勬柟娉�
+        if (input.getThumbPath() != null && !input.getThumbPath().trim().isEmpty()) {
+            media = mediaService.saveMedia(
+                input.getName(),
+                input.getPath(),
+                input.getFileSize(),
+                input.getFileExt(),
+                input.getMediaType(),
+                input.getTargetType(),
+                input.getTargetId(),
+                input.getThumbPath()
+            );
+        } else {
+            media = mediaService.saveMedia(
+                input.getName(),
+                input.getPath(),
+                input.getFileSize(),
+                input.getFileExt(),
+                input.getMediaType(),
+                input.getTargetType(),
+                input.getTargetId()
+            );
+        }
         
         MediaResponse response = new MediaResponse();
         response.setId(media.getId());
@@ -162,6 +236,9 @@
         response.setMediaType(media.getMediaType());
         response.setTargetType(media.getTargetType());
         response.setTargetId(media.getTargetId());
+        response.setThumbPath(media.getThumbPath());
+        response.setDuration(media.getDuration());
+        response.setDescription(media.getDescription());
         
         return response;
     }

--
Gitblit v1.8.0