Procházet zdrojové kódy

复制直播间代码优化,简化流程

yuhongqi před 1 dnem
rodič
revize
469dfc7f94

+ 23 - 0
fs-admin/src/main/java/com/fs/hisStore/task/ExpressTask.java

@@ -0,0 +1,23 @@
+package com.fs.hisStore.task;
+
+
+import com.fs.hisStore.service.IFsStoreOrderScrmService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+/**
+ * 物流信息定时任务
+ */
+@Slf4j
+@Component("expressTask")
+public class expressTask {
+
+    @Autowired
+    private IFsStoreOrderScrmService fsStoreOrderScrmService;
+
+    public void syncExpressToWx(){
+        fsStoreOrderScrmService.syncExpressToWx();
+    }
+
+}

+ 1 - 1
fs-admin/src/main/java/com/fs/hisStore/task/expressTask.java

@@ -11,7 +11,7 @@ import org.springframework.stereotype.Component;
  */
 @Slf4j
 @Component("expressTask")
-public class expressTask {
+public class ExpressTask {
 
     @Autowired
     private IFsStoreOrderScrmService fsStoreOrderScrmService;

+ 292 - 149
fs-service/src/main/java/com/fs/live/service/impl/LiveServiceImpl.java

@@ -807,24 +807,71 @@ public class LiveServiceImpl implements ILiveService
         if (exist == null) {
             return R.error("直播间不存在");
         }
-        // 复制直播间, 创建 livedata
-        // 身份认证
+        
+        // 1. 复制直播间基础信息
+        Long newLiveId = copyLiveBasicInfo(exist, live, now);
+        
+        // 2. 创建直播数据
+        createLiveData(newLiveId);
+        
+        // 3. 复制直播视频
+        copyLiveVideos(existLiveId, newLiveId, now);
+        
+        // 4. 获取所有自动化任务并按类型分组
+        List<LiveAutoTask> allTasks = liveAutoTaskService.selectLiveAutoTaskByLiveId(existLiveId);
+        Map<Long, List<LiveAutoTask>> tasksByType = allTasks.stream()
+                .collect(Collectors.groupingBy(LiveAutoTask::getTaskType));
+        
+        // 5. 复制弹幕任务(taskType=3)
+        copyBarrageTasks(tasksByType.get(3L), newLiveId, now);
+        
+        // 6. 复制红包配置及任务(taskType=2)
+        Map<Long, Long> redIdMapping = copyRedConfs(existLiveId, newLiveId, tasksByType.get(2L), now);
+        
+        // 7. 复制抽奖配置及任务(taskType=4)
+        Map<Long, Long> lotteryIdMapping = copyLotteryConfs(existLiveId, newLiveId, tasksByType.get(4L), now);
+        
+        // 8. 复制商品、商品任务、上下架任务及优惠券关系
+        Map<Long, Long> goodsIdMapping = copyGoodsAndTasks(existLiveId, newLiveId, live, 
+                tasksByType.get(1L), tasksByType.get(6L), now);
+        
+        // 9. 复制优惠券关系
+        copyCouponRelations(existLiveId, newLiveId, goodsIdMapping);
+        
+        // 10. 复制优惠券自动化任务(taskType=5)
+        copyCouponTasks(tasksByType.get(5L), newLiveId, goodsIdMapping, now);
+
+        return R.ok("复制成功");
+    }
+    
+    /**
+     * 复制直播间基础信息
+     */
+    private Long copyLiveBasicInfo(Live exist, Live live, Date now) {
         Live liveEntity = new Live();
         BeanUtils.copyBeanProp(liveEntity, exist);
         liveEntity.setLiveId(null);
         liveEntity.setCompanyId(live.getCompanyId());
         liveEntity.setCompanyUserId(live.getCompanyUserId());
-        liveEntity.setRtmpUrl( null);
-        liveEntity.setFlvHlsUrl( null);
-        liveEntity.setFinishTime( null);
-        liveEntity.setIsAudit( 0);
-        liveEntity.setStatus( 1);
-        liveEntity.setCreateTime( now);
+        liveEntity.setRtmpUrl(null);
+        liveEntity.setFlvHlsUrl(null);
+        liveEntity.setFinishTime(null);
+        liveEntity.setIsAudit(0);
+        liveEntity.setStatus(1);
+        liveEntity.setCreateTime(now);
         baseMapper.insertLive(liveEntity);
-        if(liveEntity.getLiveId() == null) throw new RuntimeException("插入直播间异常");
-        Long newLiveId = liveEntity.getLiveId();
+        if (liveEntity.getLiveId() == null) {
+            throw new RuntimeException("插入直播间异常");
+        }
+        return liveEntity.getLiveId();
+    }
+    
+    /**
+     * 创建直播数据
+     */
+    private void createLiveData(Long liveId) {
         LiveData liveData = new LiveData();
-        liveData.setLiveId(newLiveId);
+        liveData.setLiveId(liveId);
         liveData.setPageViews(0L);
         liveData.setUniqueVisitors(0L);
         liveData.setTotalViews(0L);
@@ -834,172 +881,268 @@ public class LiveServiceImpl implements ILiveService
         liveData.setFavouriteNum(0L);
         liveData.setFollowNum(0L);
         liveDataService.insertLiveData(liveData);
-        // 优惠券
-        List<LiveCouponIssueRelation> liveCouponIssueRelations = liveCouponIssueMapper.selectRelationByLiveId(existLiveId);
-        Map<Long, LiveCouponIssueRelation> collect = liveCouponIssueRelations.stream().collect(Collectors.toMap(LiveCouponIssueRelation::getGoodsId, Function.identity(), (existing, replacement) -> existing));
-        // 直播视频
+    }
+    
+    /**
+     * 复制直播视频
+     */
+    private void copyLiveVideos(Long existLiveId, Long newLiveId, Date now) {
         List<LiveVideo> liveVideos = liveVideoService.selectLiveVideosByLiveId(existLiveId);
-        if (!liveVideos.isEmpty()) {
-            for (LiveVideo liveVideo : liveVideos) {
-                LiveVideo videoEntity = new LiveVideo();
-                BeanUtils.copyBeanProp(videoEntity, liveVideo);
-                videoEntity.setVideoId(null);
-                videoEntity.setLiveId(newLiveId);
-                videoEntity.setCreateTime(now);
-                liveVideoService.insertLiveVideo(videoEntity);
-            }
+        for (LiveVideo liveVideo : liveVideos) {
+            LiveVideo videoEntity = new LiveVideo();
+            BeanUtils.copyBeanProp(videoEntity, liveVideo);
+            videoEntity.setVideoId(null);
+            videoEntity.setLiveId(newLiveId);
+            videoEntity.setCreateTime(now);
+            liveVideoService.insertLiveVideo(videoEntity);
+        }
+    }
+    
+    /**
+     * 复制弹幕任务(taskType=3)
+     */
+    private void copyBarrageTasks(List<LiveAutoTask> tasks, Long newLiveId, Date now) {
+        if (tasks == null || tasks.isEmpty()) {
+            return;
         }
-
-        // 运营自动化
-        List<LiveAutoTask> liveAutoTasksList = liveAutoTaskService.selectLiveAutoTaskByLiveId(existLiveId);
-        List<LiveAutoTask> barrageTask = liveAutoTasksList.stream().filter(liveAutoTask -> liveAutoTask.getTaskType() == 3L).collect(Collectors.toList());
-        List<LiveAutoTask> goodsTaskList = liveAutoTasksList.stream().filter(liveAutoTask -> liveAutoTask.getTaskType() == 1L).collect(Collectors.toList());
-        List<LiveAutoTask> redTaskList = liveAutoTasksList.stream().filter(liveAutoTask -> liveAutoTask.getTaskType() == 2L).collect(Collectors.toList());
-        List<LiveAutoTask> lotteryTaskList = liveAutoTasksList.stream().filter(liveAutoTask -> liveAutoTask.getTaskType() == 4L).collect(Collectors.toList());
-        List<LiveAutoTask> shelfTaskList = liveAutoTasksList.stream().filter(liveAutoTask -> liveAutoTask.getTaskType() == 6L).collect(Collectors.toList());
         List<LiveAutoTask> addList = new ArrayList<>();
-        if (!barrageTask.isEmpty()) {
-            for (LiveAutoTask liveAutoTask : barrageTask) {
-                LiveAutoTask liveAutoTaskEntity = new LiveAutoTask();
-                BeanUtils.copyBeanProp(liveAutoTaskEntity, liveAutoTask);
-                liveAutoTaskEntity.setId(null);
-                liveAutoTaskEntity.setLiveId(newLiveId);
-                liveAutoTaskEntity.setCreateTime(now);
-                liveAutoTaskEntity.setUpdateTime(now);
-                liveAutoTaskEntity.setFinishStatus(0L);
-                addList.add(liveAutoTaskEntity);
-                if (addList.size() > 100) {
-                    liveAutoTaskService.batchInsertLiveAutoTask(addList);
-                    addList.clear();
-                }
+        for (LiveAutoTask task : tasks) {
+            LiveAutoTask newTask = createAutoTaskEntity(task, newLiveId, now, null);
+            addList.add(newTask);
+            if (addList.size() >= 100) {
+                liveAutoTaskService.batchInsertLiveAutoTask(addList);
+                addList.clear();
             }
         }
         if (!addList.isEmpty()) {
             liveAutoTaskService.batchInsertLiveAutoTask(addList);
-            addList.clear();
         }
-        //直播间红包配置
+    }
+    
+    /**
+     * 复制红包配置及任务
+     */
+    private Map<Long, Long> copyRedConfs(Long existLiveId, Long newLiveId, 
+                                         List<LiveAutoTask> redTasks, Date now) {
+        Map<Long, Long> redIdMapping = new HashMap<>();
+        if (redTasks == null) {
+            redTasks = Collections.emptyList();
+        }
+        Map<Long, LiveAutoTask> redTaskMap = redTasks.stream()
+                .collect(Collectors.toMap(task -> parseIdFromContent(task.getContent(), "redId"), 
+                        Function.identity(), (existing, replacement) -> existing));
+        
         List<LiveRedConf> liveRedConfs = liveRedConfService.selectByLiveId(existLiveId);
-        if (!liveRedConfs.isEmpty()) {
-
-            for (LiveRedConf liveRedConf : liveRedConfs) {
-                LiveRedConf liveRedConfEntity = new LiveRedConf();
-                BeanUtils.copyBeanProp(liveRedConfEntity, liveRedConf);
-                liveRedConfEntity.setRedId(null);
-                liveRedConfEntity.setLiveId(newLiveId);
-                liveRedConfEntity.setRedStatus(0L);
-                liveRedConfEntity.setCreateTime(now);
-                liveRedConfEntity.setTotalSend(0L);
-                liveRedConfService.insertLiveRedConf(liveRedConfEntity);
-                LiveAutoTask liveAutoTask = redTaskList.stream().filter(item -> parseIdFromContent(item.getContent(), "redId").equals(liveRedConf.getRedId())).findFirst().orElse(null);
-                if(liveAutoTask == null) continue;
-                LiveAutoTask liveAutoTaskEntity = new LiveAutoTask();
-                BeanUtils.copyBeanProp(liveAutoTaskEntity, liveAutoTask);
-                liveAutoTaskEntity.setId(null);
-                liveAutoTaskEntity.setLiveId(newLiveId);
-                liveAutoTaskEntity.setCreateTime(now);
-                liveAutoTaskEntity.setUpdateTime(now);
-                liveAutoTaskEntity.setFinishStatus(0L);
-                liveAutoTaskEntity.setContent(JSON.toJSONString(liveRedConfEntity));
-                liveAutoTaskService.directInsertLiveAutoTask(liveAutoTaskEntity);
+        for (LiveRedConf liveRedConf : liveRedConfs) {
+            LiveRedConf newRedConf = new LiveRedConf();
+            BeanUtils.copyBeanProp(newRedConf, liveRedConf);
+            newRedConf.setRedId(null);
+            newRedConf.setLiveId(newLiveId);
+            newRedConf.setRedStatus(0L);
+            newRedConf.setCreateTime(now);
+            newRedConf.setTotalSend(0L);
+            liveRedConfService.insertLiveRedConf(newRedConf);
+            
+            redIdMapping.put(liveRedConf.getRedId(), newRedConf.getRedId());
+            
+            LiveAutoTask task = redTaskMap.get(liveRedConf.getRedId());
+            if (task != null) {
+                LiveAutoTask newTask = createAutoTaskEntity(task, newLiveId, now, 
+                        JSON.toJSONString(newRedConf));
+                liveAutoTaskService.directInsertLiveAutoTask(newTask);
             }
         }
-        // 直播间礼物配置
+        return redIdMapping;
+    }
+    
+    /**
+     * 复制抽奖配置及任务
+     */
+    private Map<Long, Long> copyLotteryConfs(Long existLiveId, Long newLiveId, 
+                                             List<LiveAutoTask> lotteryTasks, Date now) {
+        Map<Long, Long> lotteryIdMapping = new HashMap<>();
+        if (lotteryTasks == null) {
+            lotteryTasks = Collections.emptyList();
+        }
+        Map<Long, LiveAutoTask> lotteryTaskMap = lotteryTasks.stream()
+                .collect(Collectors.toMap(task -> parseIdFromContent(task.getContent(), "lotteryId"), 
+                        Function.identity(), (existing, replacement) -> existing));
+        
         List<LiveLotteryConf> liveLotteryConfs = liveLotteryConfService.selectByLiveId(existLiveId);
         if (!liveLotteryConfs.isEmpty()) {
-
-            List<Long> lotteryIds = liveLotteryConfs.stream().map(LiveLotteryConf::getLotteryId).collect(Collectors.toList());
+            List<Long> lotteryIds = liveLotteryConfs.stream()
+                    .map(LiveLotteryConf::getLotteryId).collect(Collectors.toList());
             List<LiveLotteryProductConf> products = liveLotteryProductConfMapper.selectEntityByIds(lotteryIds);
+            Map<Long, List<LiveLotteryProductConf>> productsByLotteryId = products.stream()
+                    .collect(Collectors.groupingBy(LiveLotteryProductConf::getLotteryId));
+            
             for (LiveLotteryConf liveLotteryConf : liveLotteryConfs) {
-                LiveLotteryConf liveLotteryConfEntity = new LiveLotteryConf();
-                BeanUtils.copyBeanProp(liveLotteryConfEntity, liveLotteryConf);
-                liveLotteryConfEntity.setLotteryId(null);
-                liveLotteryConfEntity.setLiveId(newLiveId);
-                liveLotteryConfEntity.setLotteryStatus(String.valueOf(0));
-                liveLotteryConfEntity.setCreateTime(now);
-                liveLotteryConfEntity.setUpdateTime(now);
-                liveLotteryConfService.insertLiveLotteryConf(liveLotteryConfEntity);
-                products.stream().filter(product -> product.getLotteryId().equals(liveLotteryConf.getLotteryId()))
-                        .forEach(product -> {
-                            product.setId(null);
-                            product.setLotteryId(liveLotteryConfEntity.getLotteryId());
-                            liveLotteryProductConfMapper.insertLiveLotteryProductConf(product);
-                        });
-                LiveAutoTask liveAutoTask = lotteryTaskList.stream().filter(item -> parseIdFromContent(item.getContent(), "lotteryId").equals(liveLotteryConf.getLotteryId())).findFirst().orElse(null);
-                if(liveAutoTask == null) continue;
-                LiveAutoTask liveAutoTaskEntity = new LiveAutoTask();
-                BeanUtils.copyBeanProp(liveAutoTaskEntity, liveAutoTask);
-                liveAutoTaskEntity.setId(null);
-                liveAutoTaskEntity.setLiveId(newLiveId);
-                liveAutoTaskEntity.setCreateTime(now);
-                liveAutoTaskEntity.setUpdateTime(now);
-                liveAutoTaskEntity.setFinishStatus(0L);
-                liveAutoTaskEntity.setContent(JSON.toJSONString(liveLotteryConfEntity));
-                liveAutoTaskService.directInsertLiveAutoTask(liveAutoTaskEntity);
+                LiveLotteryConf newLotteryConf = new LiveLotteryConf();
+                BeanUtils.copyBeanProp(newLotteryConf, liveLotteryConf);
+                newLotteryConf.setLotteryId(null);
+                newLotteryConf.setLiveId(newLiveId);
+                newLotteryConf.setLotteryStatus(String.valueOf(0));
+                newLotteryConf.setCreateTime(now);
+                newLotteryConf.setUpdateTime(now);
+                liveLotteryConfService.insertLiveLotteryConf(newLotteryConf);
+                
+                lotteryIdMapping.put(liveLotteryConf.getLotteryId(), newLotteryConf.getLotteryId());
+                
+                // 复制奖品
+                List<LiveLotteryProductConf> lotteryProducts = productsByLotteryId.get(liveLotteryConf.getLotteryId());
+                if (lotteryProducts != null) {
+                    for (LiveLotteryProductConf product : lotteryProducts) {
+                        LiveLotteryProductConf newProduct = new LiveLotteryProductConf();
+                        BeanUtils.copyBeanProp(newProduct, product);
+                        newProduct.setId(null);
+                        newProduct.setLotteryId(newLotteryConf.getLotteryId());
+                        liveLotteryProductConfMapper.insertLiveLotteryProductConf(newProduct);
+                    }
+                }
+                
+                LiveAutoTask task = lotteryTaskMap.get(liveLotteryConf.getLotteryId());
+                if (task != null) {
+                    LiveAutoTask newTask = createAutoTaskEntity(task, newLiveId, now, 
+                            JSON.toJSONString(newLotteryConf));
+                    liveAutoTaskService.directInsertLiveAutoTask(newTask);
+                }
             }
         }
+        return lotteryIdMapping;
+    }
+    
+    /**
+     * 复制商品、商品任务、上下架任务
+     */
+    private Map<Long, Long> copyGoodsAndTasks(Long existLiveId, Long newLiveId, Live live,
+                                              List<LiveAutoTask> goodsTasks, List<LiveAutoTask> shelfTasks, Date now) {
+        Map<Long, Long> goodsIdMapping = new HashMap<>();
+        if (goodsTasks == null) {
+            goodsTasks = Collections.emptyList();
+        }
+        if (shelfTasks == null) {
+            shelfTasks = Collections.emptyList();
+        }
+        
+        Map<Long, LiveAutoTask> goodsTaskMap = goodsTasks.stream()
+                .collect(Collectors.toMap(task -> parseIdFromContent(task.getContent(), "goodsId"), 
+                        Function.identity(), (existing, replacement) -> existing));
+        Map<Long, LiveAutoTask> shelfTaskMap = shelfTasks.stream()
+                .collect(Collectors.toMap(task -> parseIdFromContent(task.getContent(), "goodsId"), 
+                        Function.identity(), (existing, replacement) -> existing));
+        
         LiveGoods queryParam = new LiveGoods();
         queryParam.setLiveId(existLiveId);
-        // 直播间商品
         List<LiveGoodsVo> goodsList = liveGoodsService.selectProductListByLiveId(queryParam);
+        
         if (!goodsList.isEmpty()) {
-
-            List<Long> goodsProductIds = goodsList.stream().map(item -> item.getProductId()).collect(Collectors.toList());
-            Map<Long, FsStoreProductScrm> goodsMap = fsStoreProductScrmMapper.selectFsStoreProductByProductIds(goodsProductIds).stream().collect(Collectors.toMap(FsStoreProductScrm::getProductId, item -> item));
-
+            List<Long> goodsProductIds = goodsList.stream()
+                    .map(LiveGoodsVo::getProductId).collect(Collectors.toList());
+            Map<Long, FsStoreProductScrm> goodsMap = fsStoreProductScrmMapper
+                    .selectFsStoreProductByProductIds(goodsProductIds).stream()
+                    .collect(Collectors.toMap(FsStoreProductScrm::getProductId, Function.identity()));
+            
             for (LiveGoodsVo liveGoods : goodsList) {
-                LiveGoods liveGoodsEntity = new LiveGoods();
-                BeanUtils.copyBeanProp(liveGoodsEntity, liveGoods);
-                liveGoodsEntity.setGoodsId(null);
-                liveGoodsEntity.setLiveId(newLiveId);
-                liveGoodsEntity.setCreateTime(now);
-                liveGoodsEntity.setIsShow(false);
-                liveGoodsEntity.setCompanyId(live.getCompanyId());
-                liveGoodsEntity.setCompanyUserId(live.getCompanyUserId());
-                liveGoodsService.insertLiveGoods(liveGoodsEntity);
-                // 优惠券
-                if (collect.containsKey(liveGoods.getGoodsId())) {
-                    liveCouponIssueRelations.stream().filter(relation -> relation.getGoodsId().equals(liveGoods.getGoodsId())).findFirst().ifPresent(liveCouponIssueRelation -> liveCouponIssueRelation.setGoodsId(liveGoodsEntity.getGoodsId()));
+                LiveGoods newGoods = new LiveGoods();
+                BeanUtils.copyBeanProp(newGoods, liveGoods);
+                newGoods.setGoodsId(null);
+                newGoods.setLiveId(newLiveId);
+                newGoods.setCreateTime(now);
+                newGoods.setIsShow(false);
+                newGoods.setCompanyId(live.getCompanyId());
+                newGoods.setCompanyUserId(live.getCompanyUserId());
+                newGoods.setStock(goodsMap.containsKey(liveGoods.getProductId()) 
+                        ? goodsMap.get(liveGoods.getProductId()).getStock() : 0);
+                liveGoodsService.insertLiveGoods(newGoods);
+                
+                goodsIdMapping.put(liveGoods.getGoodsId(), newGoods.getGoodsId());
+                
+                // 复制商品推送任务(taskType=1)
+                LiveAutoTask goodsTask = goodsTaskMap.get(liveGoods.getGoodsId());
+                if (goodsTask != null) {
+                    // 使用新的goodsId创建LiveGoodsVo对象
+                    LiveGoodsVo newGoodsVo = new LiveGoodsVo();
+                    BeanUtils.copyBeanProp(newGoodsVo, liveGoods);
+                    newGoodsVo.setGoodsId(newGoods.getGoodsId());
+                    LiveAutoTask newTask = createAutoTaskEntity(goodsTask, newLiveId, now, 
+                            JSON.toJSONString(newGoodsVo));
+                    liveAutoTaskService.directInsertLiveAutoTask(newTask);
                 }
-                LiveAutoTask liveAutoTask = goodsTaskList.stream().filter(item -> parseIdFromContent(item.getContent(), "goodsId").equals(liveGoods.getGoodsId())).findFirst().orElse(null);
-                if(liveAutoTask == null) continue;
-                liveGoods.setGoodsId(liveGoodsEntity.getGoodsId());
-                LiveAutoTask liveAutoTaskEntity = new LiveAutoTask();
-                BeanUtils.copyBeanProp(liveAutoTaskEntity, liveAutoTask);
-                liveAutoTaskEntity.setId(null);
-                liveAutoTaskEntity.setLiveId(newLiveId);
-                liveGoodsEntity.setStock(goodsMap.containsKey(liveGoods.getProductId()) ? goodsMap.get(liveGoods.getProductId()).getStock() : 0);
-                liveAutoTaskEntity.setCreateTime(now);
-                liveAutoTaskEntity.setUpdateTime(now);
-                liveAutoTaskEntity.setFinishStatus(0L);
-                liveAutoTaskEntity.setContent(JSON.toJSONString(liveGoods));
-                liveAutoTaskService.directInsertLiveAutoTask(liveAutoTaskEntity);
-
-                // 处理上下架任务 taskType=6
-                LiveAutoTask shelfTask = shelfTaskList.stream().filter(item -> parseIdFromContent(item.getContent(), "goodsId").equals(liveGoods.getGoodsId())).findFirst().orElse(null);
-                if(shelfTask != null) {
-                    LiveAutoTask shelfAutoTaskEntity = new LiveAutoTask();
-                    BeanUtils.copyBeanProp(shelfAutoTaskEntity, shelfTask);
-                    shelfAutoTaskEntity.setId(null);
-                    shelfAutoTaskEntity.setLiveId(newLiveId);
-                    shelfAutoTaskEntity.setCreateTime(now);
-                    shelfAutoTaskEntity.setUpdateTime(now);
-                    shelfAutoTaskEntity.setFinishStatus(0L);
-                    // 更新content中的goodsId为新的goodsId
+                
+                // 复制上下架任务(taskType=6)
+                LiveAutoTask shelfTask = shelfTaskMap.get(liveGoods.getGoodsId());
+                if (shelfTask != null) {
                     JSONObject contentJson = JSON.parseObject(shelfTask.getContent());
-                    contentJson.put("goodsId", liveGoodsEntity.getGoodsId());
-                    shelfAutoTaskEntity.setContent(contentJson.toJSONString());
-                    liveAutoTaskService.directInsertLiveAutoTask(shelfAutoTaskEntity);
+                    contentJson.put("goodsId", newGoods.getGoodsId());
+                    LiveAutoTask newTask = createAutoTaskEntity(shelfTask, newLiveId, now, 
+                            contentJson.toJSONString());
+                    liveAutoTaskService.directInsertLiveAutoTask(newTask);
                 }
             }
         }
-        for (LiveCouponIssueRelation liveCouponIssueRelation : liveCouponIssueRelations) {
-            liveCouponIssueRelation.setLiveId(newLiveId);
-            liveCouponIssueRelation.setIsShow(0);
-            liveCouponIssueMapper.insertLiveCouponIssueRelation(liveCouponIssueRelation);
+        return goodsIdMapping;
+    }
+    
+    /**
+     * 复制优惠券关系
+     */
+    private void copyCouponRelations(Long existLiveId, Long newLiveId, Map<Long, Long> goodsIdMapping) {
+        List<LiveCouponIssueRelation> relations = liveCouponIssueMapper.selectRelationByLiveId(existLiveId);
+        for (LiveCouponIssueRelation relation : relations) {
+            LiveCouponIssueRelation newRelation = new LiveCouponIssueRelation();
+            BeanUtils.copyBeanProp(newRelation, relation);
+            newRelation.setLiveId(newLiveId);
+            newRelation.setIsShow(0);
+            // 更新goodsId映射
+            if (relation.getGoodsId() != null && goodsIdMapping.containsKey(relation.getGoodsId())) {
+                newRelation.setGoodsId(goodsIdMapping.get(relation.getGoodsId()));
+            }
+            liveCouponIssueMapper.insertLiveCouponIssueRelation(newRelation);
         }
-
-        return R.ok("复制成功");
+    }
+    
+    /**
+     * 复制优惠券自动化任务(taskType=5)
+     */
+    private void copyCouponTasks(List<LiveAutoTask> couponTasks, Long newLiveId, 
+                                 Map<Long, Long> goodsIdMapping, Date now) {
+        if (couponTasks == null || couponTasks.isEmpty()) {
+            return;
+        }
+        for (LiveAutoTask task : couponTasks) {
+            try {
+                LiveCoupon liveCoupon = JSON.parseObject(task.getContent(), LiveCoupon.class);
+                if (liveCoupon != null && liveCoupon.getGoodsId() != null) {
+                    // 更新content中的goodsId为新的goodsId
+                    Long newGoodsId = goodsIdMapping.get(liveCoupon.getGoodsId());
+                    if (newGoodsId != null) {
+                        liveCoupon.setGoodsId(newGoodsId);
+                        LiveAutoTask newTask = createAutoTaskEntity(task, newLiveId, now, 
+                                JSON.toJSONString(liveCoupon));
+                        liveAutoTaskService.directInsertLiveAutoTask(newTask);
+                    }
+                }
+            } catch (Exception e) {
+                log.error("复制优惠券自动化任务失败,taskId: {}", task.getId(), e);
+            }
+        }
+    }
+    
+    /**
+     * 创建自动化任务实体
+     */
+    private LiveAutoTask createAutoTaskEntity(LiveAutoTask source, Long newLiveId, Date now, String content) {
+        LiveAutoTask newTask = new LiveAutoTask();
+        BeanUtils.copyBeanProp(newTask, source);
+        newTask.setId(null);
+        newTask.setLiveId(newLiveId);
+        newTask.setCreateTime(now);
+        newTask.setUpdateTime(now);
+        newTask.setFinishStatus(0L);
+        if (content != null) {
+            newTask.setContent(content);
+        }
+        return newTask;
     }
 
     private Long parseIdFromContent(String content, String key) {