From 91491239c5d4611b5b1404a7abeb2bb23d0edad5 Mon Sep 17 00:00:00 2001
From: liu <2021943741@qq.com>
Date: 星期三, 30 九月 2026 14:26:59 +0800
Subject: [PATCH] feat(wxcp): 活动封面改用附件中间表,报名姓名手机号改为选填

---
 yudao-module-wxcp/src/main/java/cn/iocoder/yudao/module/wxcp/service/activity/WxCpActivityServiceImpl.java |  120 ++++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 93 insertions(+), 27 deletions(-)

diff --git a/yudao-module-wxcp/src/main/java/cn/iocoder/yudao/module/wxcp/service/activity/WxCpActivityServiceImpl.java b/yudao-module-wxcp/src/main/java/cn/iocoder/yudao/module/wxcp/service/activity/WxCpActivityServiceImpl.java
index 70508df..ffc4d26 100644
--- a/yudao-module-wxcp/src/main/java/cn/iocoder/yudao/module/wxcp/service/activity/WxCpActivityServiceImpl.java
+++ b/yudao-module-wxcp/src/main/java/cn/iocoder/yudao/module/wxcp/service/activity/WxCpActivityServiceImpl.java
@@ -30,6 +30,7 @@
 
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
+import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -56,6 +57,16 @@
      */
     public static final String SIGNUP_RECORD_TYPE = "wxcp_activity_signup";
 
+    /**
+     * 娲诲姩灏侀潰鐨勫叧鑱旇褰曠被鍨嬶紝瀵瑰簲 system_storage_attachment.record_type
+     */
+    public static final String ACTIVITY_RECORD_TYPE = "wxcp_activity";
+
+    /**
+     * 闄勪欢鐢ㄩ�旓紝涓庡叾瀹冩ā鍧椾繚鎸佷竴鑷�
+     */
+    private static final String ATTACHMENT_APPLICATION = "file";
+
     @Resource
     private WxCpActivityMapper wxcpActivityMapper;
     @Resource
@@ -72,6 +83,7 @@
     // ==================== 娲诲姩绠$悊 ====================
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public Long createActivity(WxCpActivitySaveReqVO createReqVO) {
         WxCpActivityDO activity = BeanUtils.toBean(createReqVO, WxCpActivityDO.class);
         // 鏂板缓鐨勪竴寰嬫槸鑽夌锛岄伩鍏嶆湭閰嶇疆瀹屽氨琚壂鐮佹姤鍚�
@@ -83,10 +95,15 @@
             activity.setPushScope(WxCpPushScopeEnum.ALL.getScope());
         }
         wxcpActivityMapper.insert(activity);
+        // 灏侀潰璧� system 鐨勯檮浠朵腑闂磋〃锛屼笟鍔¤〃涓嶈惤鏂囦欢鍦板潃瀛楁
+        if (CollUtil.isNotEmpty(createReqVO.getBlobIds())) {
+            bindActivityAttachments(activity.getId(), createReqVO.getBlobIds());
+        }
         return activity.getId();
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public void updateActivity(WxCpActivitySaveReqVO updateReqVO) {
         WxCpActivityDO activity = validateActivityExists(updateReqVO.getId());
         // 宸茬粨鏉熸垨宸插彇娑堢殑娲诲姩涓嶅啀鍏佽淇敼
@@ -101,12 +118,19 @@
         updateObj.setPushStatus(null);
         updateObj.setPushTime(null);
         wxcpActivityMapper.updateById(updateObj);
+        // blobIds 涓� null 琛ㄧず璇锋眰閲屾病甯﹁繖涓瓧娈碉紝瑙嗕负涓嶆敼鍔ㄥ皝闈紱浼犵┖鏁扮粍鎵嶆槸鏄庣‘瑕佹竻绌哄皝闈�
+        if (updateReqVO.getBlobIds() != null) {
+            bindActivityAttachments(activity.getId(), updateReqVO.getBlobIds());
+        }
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public void deleteActivity(Long id) {
         validateActivityExists(id);
         wxcpActivityMapper.deleteById(id);
+        // 娲诲姩鍒犱簡锛屽皝闈㈤檮浠跺叧绯讳竴骞舵竻鎺夛紝閬垮厤娈嬬暀鎴愬鍎块檮浠�
+        storageAttachmentService.deleteAttachmentsByRecordTypeAndRecordId(ACTIVITY_RECORD_TYPE, id);
     }
 
     @Override
@@ -117,6 +141,53 @@
     @Override
     public PageResult<WxCpActivityDO> getActivityPage(WxCpActivityPageReqVO pageReqVO) {
         return wxcpActivityMapper.selectPage(pageReqVO);
+    }
+
+    @Override
+    public List<SystemStorageBlobRespVO> getActivityAttachments(Long activityId) {
+        return listAttachments(ACTIVITY_RECORD_TYPE, activityId);
+    }
+
+    /**
+     * 缁戝畾娲诲姩灏侀潰闄勪欢
+     *
+     * 璧� system 妯″潡鐨勯檮浠朵腑闂磋〃锛坮ecord_type = wxcp_activity锛夛紝
+     * 涓氬姟琛ㄤ笉鍔犳枃浠跺湴鍧�鍒椼�佷笉鑷缓 upload 鎺ュ彛锛岀鍚堥」鐩粺涓�鏂囦欢绠$悊瑙勮寖銆�
+     *
+     * 浼犵┖闆嗗悎浼氭竻鎺夊叧鑱旇褰曚絾淇濈暀鏂囦欢鏈韩锛坆indAttachments 鍙垹涓棿琛ㄨ褰曪級銆�
+     * 娉ㄦ剰涓嶈鏀圭敤 deleteAttachmentsByRecordTypeAndRecordId 鏉�"鍏堝垹鍚庡姞"锛�
+     * 閭d釜鏂规硶浼氳繛 blob 涓�璧峰垹鎺夛紝鍚屼竴涓枃浠堕噸鏂扮粦瀹氭椂浼氬洜涓烘枃浠跺凡涓嶅瓨鍦ㄨ�屾煡涓嶅嚭鏉ャ��
+     */
+    private void bindActivityAttachments(Long activityId, List<Long> blobIds) {
+        SystemStorageAttachmentSaveReqVO bindReqVO = new SystemStorageAttachmentSaveReqVO();
+        bindReqVO.setApplication(ATTACHMENT_APPLICATION);
+        bindReqVO.setRecordType(ACTIVITY_RECORD_TYPE);
+        bindReqVO.setRecordId(activityId);
+        bindReqVO.setBlobItems(toBlobItems(blobIds));
+        storageAttachmentService.bindAttachments(bindReqVO);
+    }
+
+    private List<SystemStorageAttachmentSaveReqVO.BlobItem> toBlobItems(List<Long> blobIds) {
+        if (CollUtil.isEmpty(blobIds)) {
+            return Collections.emptyList();
+        }
+        return blobIds.stream()
+                .filter(Objects::nonNull)
+                .distinct()
+                .map(blobId -> {
+                    SystemStorageAttachmentSaveReqVO.BlobItem item = new SystemStorageAttachmentSaveReqVO.BlobItem();
+                    item.setBlobId(blobId);
+                    return item;
+                })
+                .toList();
+    }
+
+    private List<SystemStorageBlobRespVO> listAttachments(String recordType, Long recordId) {
+        SystemStorageAttachmentListReqVO listReqVO = new SystemStorageAttachmentListReqVO();
+        listReqVO.setApplication(ATTACHMENT_APPLICATION);
+        listReqVO.setRecordType(recordType);
+        listReqVO.setRecordId(recordId);
+        return storageAttachmentService.listAttachments(listReqVO);
     }
 
     @Override
@@ -256,10 +327,16 @@
             throw exception(SIGNUP_FULL);
         }
 
-        // 4. 鍘婚噸锛氭湁浼佷笟寰俊璐﹀彿鎸夎处鍙锋煡锛屽惁鍒欐寜鎵嬫満鍙锋煡
-        WxCpActivitySignupDO exists = StrUtil.isNotBlank(reqVO.getWxUserId())
-                ? wxcpSignupMapper.selectByActivityIdAndWxUserId(activity.getId(), reqVO.getWxUserId().trim())
-                : wxcpSignupMapper.selectByActivityIdAndMobile(activity.getId(), reqVO.getMobile().trim());
+        // 4. 鍘婚噸锛氭湁浼佷笟寰俊璐﹀彿鎸夎处鍙锋煡锛涙病鏈夎处鍙蜂絾鏈夋墜鏈哄彿鍒欐寜鎵嬫満鍙锋煡锛�
+        //    涓よ�呴兘娌℃湁鏃舵棤娉曞垽瀹氳韩浠斤紝涓嶅仛鍘婚噸鈥斺�斿惁鍒欑┖鎵嬫満鍙蜂細浜掔浉璇垽鎴愬悓涓�浜�
+        String wxUserId = StrUtil.trimToEmpty(reqVO.getWxUserId());
+        String mobile = StrUtil.trimToEmpty(reqVO.getMobile());
+        WxCpActivitySignupDO exists = null;
+        if (StrUtil.isNotBlank(wxUserId)) {
+            exists = wxcpSignupMapper.selectByActivityIdAndWxUserId(activity.getId(), wxUserId);
+        } else if (StrUtil.isNotBlank(mobile)) {
+            exists = wxcpSignupMapper.selectByActivityIdAndMobile(activity.getId(), mobile);
+        }
         if (exists != null && !Objects.equals(exists.getStatus(), WxCpSignupStatusEnum.CANCELED.getStatus())) {
             throw exception(SIGNUP_DUPLICATED);
         }
@@ -267,8 +344,8 @@
         if (exists != null) {
             wxcpSignupMapper.updateById(WxCpActivitySignupDO.builder()
                     .id(exists.getId())
-                    .name(reqVO.getName())
-                    .mobile(reqVO.getMobile())
+                    .name(StrUtil.trimToEmpty(reqVO.getName()))
+                    .mobile(mobile)
                     .deptName(reqVO.getDeptName())
                     .content(reqVO.getContent())
                     .remark(reqVO.getRemark())
@@ -276,9 +353,7 @@
                     .signupTime(now)
                     .checkInTime(null)
                     .build());
-            // 閲嶆柊鎶ュ悕鏃舵棫闄勪欢宸插け鏁堬紝鍏堟竻鎺夊啀缁戞柊鐨�
-            storageAttachmentService.deleteAttachmentsByRecordTypeAndRecordId(
-                    SIGNUP_RECORD_TYPE, exists.getId());
+            // 閲嶆柊鎶ュ悕鏃舵棫闄勪欢浣滃簾锛岀洿鎺ラ噸鏂扮粦瀹氾紙bindAttachments 浼氬厛娓呮棫鍏宠仈锛屼笖涓嶄細鍒犳帀鏂囦欢鏈韩锛�
             bindSignupAttachments(exists.getId(), reqVO.getBlobIds());
             incrementSignupCount(activity.getId());
             return exists.getId();
@@ -286,7 +361,10 @@
 
         // 5. 鏂板鎶ュ悕璁板綍
         WxCpActivitySignupDO signup = BeanUtils.toBean(reqVO, WxCpActivitySignupDO.class);
-        signup.setWxUserId(StrUtil.trimToEmpty(reqVO.getWxUserId()));
+        signup.setWxUserId(wxUserId);
+        // 濮撳悕銆佹墜鏈哄彿鏀逛负閫夊~锛氱粺涓�瀛樼┖涓茶�屼笉鏄� null锛岄伩鍏嶅悗缁寜鎵嬫満鍙锋煡閲嶆椂绌哄�艰涔夋贩涔�
+        signup.setName(StrUtil.trimToEmpty(reqVO.getName()));
+        signup.setMobile(mobile);
         signup.setStatus(WxCpSignupStatusEnum.SIGNED_UP.getStatus());
         signup.setSignupTime(now);
         wxcpSignupMapper.insert(signup);
@@ -300,36 +378,24 @@
     }
 
     /**
-     * 缁戝畾鎶ュ悕闄勪欢
+     * 缁戝畾鎶ュ悕闄勪欢锛堝叏閲忚鐩栵細鍏堟竻鎺夎鎶ュ悕璁板綍涓嬬殑鏃у叧鑱旓紝鍐嶇粦鏂扮殑锛�
      *
      * 璧� system 妯″潡鐨勯檮浠朵腑闂磋〃锛坮ecord_type = wxcp_activity_signup锛夛紝
      * 涓嶆柊寤� upload 鎺ュ彛銆佷笉钀芥枃浠跺湴鍧�瀛楁锛岀鍚堥」鐩粺涓�鏂囦欢绠$悊瑙勮寖銆�
+     * 浼犵┖闆嗗悎鍙竻鍏宠仈銆佷笉鍒犳枃浠舵湰韬��
      */
     private void bindSignupAttachments(Long signupId, List<Long> blobIds) {
-        if (CollUtil.isEmpty(blobIds)) {
-            return;
-        }
         SystemStorageAttachmentSaveReqVO bindReqVO = new SystemStorageAttachmentSaveReqVO();
-        bindReqVO.setApplication("file");
+        bindReqVO.setApplication(ATTACHMENT_APPLICATION);
         bindReqVO.setRecordType(SIGNUP_RECORD_TYPE);
         bindReqVO.setRecordId(signupId);
-        bindReqVO.setBlobItems(blobIds.stream()
-                .filter(Objects::nonNull)
-                .map(blobId -> {
-                    SystemStorageAttachmentSaveReqVO.BlobItem item = new SystemStorageAttachmentSaveReqVO.BlobItem();
-                    item.setBlobId(blobId);
-                    return item;
-                })
-                .toList());
+        bindReqVO.setBlobItems(toBlobItems(blobIds));
         storageAttachmentService.bindAttachments(bindReqVO);
     }
 
     @Override
     public List<SystemStorageBlobRespVO> getSignupAttachments(Long signupId) {
-        SystemStorageAttachmentListReqVO listReqVO = new SystemStorageAttachmentListReqVO();
-        listReqVO.setRecordType(SIGNUP_RECORD_TYPE);
-        listReqVO.setRecordId(signupId);
-        return storageAttachmentService.listAttachments(listReqVO);
+        return listAttachments(SIGNUP_RECORD_TYPE, signupId);
     }
 
     private void incrementSignupCount(Long activityId) {

--
Gitblit v1.9.3