| 
					
				 | 
			
			
				@@ -1,15 +1,24 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 package com.fs.course.service.impl; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import java.util.Collections; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import java.util.List; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import java.util.ArrayList; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import java.util.Set; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import java.util.HashSet; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.*; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.function.Function; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.stream.Collectors; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.fs.course.domain.FsUserCoursePeriod; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.fs.course.domain.FsUserCoursePeriodDays; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.fs.course.mapper.FsUserCoursePeriodDaysMapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.fs.course.mapper.FsUserCoursePeriodMapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.fs.course.param.FsBatchPeriodRedPackageParam; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.google.common.collect.Lists; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.apache.ibatis.session.ExecutorType; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.apache.ibatis.session.SqlSession; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.apache.ibatis.session.SqlSessionFactory; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.springframework.beans.factory.annotation.Autowired; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.springframework.stereotype.Service; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.fs.course.mapper.FsUserCourseVideoRedPackageMapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.fs.course.domain.FsUserCourseVideoRedPackage; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.fs.course.service.IFsUserCourseVideoRedPackageService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.test.annotation.Rollback; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.springframework.transaction.annotation.Transactional; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 /** 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -24,6 +33,15 @@ public class FsUserCourseVideoRedPackageServiceImpl implements IFsUserCourseVide 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     private FsUserCourseVideoRedPackageMapper fsUserCourseVideoRedPackageMapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private SqlSessionFactory sqlSessionFactory; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private FsUserCoursePeriodDaysMapper userCoursePeriodDaysMapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private FsUserCoursePeriodMapper userCoursePeriodMapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				      * 查询课程公司红包 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				      * 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -108,12 +126,11 @@ public class FsUserCourseVideoRedPackageServiceImpl implements IFsUserCourseVide 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     @Transactional 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    public int batchSaveCompanyRedPackage(List<FsUserCourseVideoRedPackage> fsUserCourseVideoRedPackageList) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void batchSaveCompanyRedPackage(List<FsUserCourseVideoRedPackage> fsUserCourseVideoRedPackageList) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         if (fsUserCourseVideoRedPackageList == null || fsUserCourseVideoRedPackageList.isEmpty()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            return 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // 一次性查询所有匹配的数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         List<FsUserCourseVideoRedPackage> existingData = fsUserCourseVideoRedPackageMapper 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 .selectByParamsList(fsUserCourseVideoRedPackageList); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -124,7 +141,7 @@ public class FsUserCourseVideoRedPackageServiceImpl implements IFsUserCourseVide 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             existingKeys.add(key); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // 分离需要更新和需要新增的数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //需要修改和新增的数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         List<FsUserCourseVideoRedPackage> updateList = new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         List<FsUserCourseVideoRedPackage> insertList = new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -140,14 +157,13 @@ public class FsUserCourseVideoRedPackageServiceImpl implements IFsUserCourseVide 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        int result = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         if (!updateList.isEmpty()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            result += fsUserCourseVideoRedPackageMapper.batchUpdateFsUserCourseVideoRedPackage(updateList); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//            result += fsUserCourseVideoRedPackageMapper.batchUpdateFsUserCourseVideoRedPackage(updateList); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            this.batchUpdateRedPackage(updateList); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         if (!insertList.isEmpty()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            result += fsUserCourseVideoRedPackageMapper.batchSaveFsUserCourseVideoRedPackage(insertList); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            fsUserCourseVideoRedPackageMapper.batchSaveFsUserCourseVideoRedPackage(insertList); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     private String generateKey(Long periodId, Long videoId, Long companyId) { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -163,4 +179,65 @@ public class FsUserCourseVideoRedPackageServiceImpl implements IFsUserCourseVide 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     public List<FsUserCourseVideoRedPackage> listByRuleIds(List<Long> ids) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         return fsUserCourseVideoRedPackageMapper.listByRuleIds(ids); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private void batchUpdateRedPackage(List<FsUserCourseVideoRedPackage> list) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 分批次处理(避免一次性提交太多) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<List<FsUserCourseVideoRedPackage>> batches = Lists.partition(list, 500); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        batches.forEach(batch -> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                FsUserCourseVideoRedPackageMapper mapper = sqlSession.getMapper(FsUserCourseVideoRedPackageMapper.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                batch.forEach(mapper::updateRedPackageByParams); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                sqlSession.commit(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } finally { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                sqlSession.close(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Transactional(rollbackFor = Exception.class) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void batchRedPacketByPeriod(List<FsBatchPeriodRedPackageParam> periodRedPackageList) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 1、提取所有传入的营期id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Set<Long> periodIds = periodRedPackageList.stream() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .map(FsBatchPeriodRedPackageParam::getPeriodId) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .collect(Collectors.toSet()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 2、查询传入的营期信息,主要作用是获取营期的公司ids 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<FsUserCoursePeriod> fsUserCoursePeriods = userCoursePeriodMapper.selectFsUserCoursePeriodsByIds(periodIds); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Map<Long, FsUserCoursePeriod> periodMap = fsUserCoursePeriods.stream() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .collect(Collectors.toMap(FsUserCoursePeriod::getPeriodId, Function.identity())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 3、批量查询营期课程信息 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<FsUserCoursePeriodDays> fsUserCoursePeriodDays = userCoursePeriodDaysMapper.selectCourseVideoList(periodIds); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Map<Long, List<FsUserCoursePeriodDays>> fsUserCoursePeriodDaysMap = fsUserCoursePeriodDays.stream() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .collect(Collectors.groupingBy(FsUserCoursePeriodDays::getPeriodId)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 4、组装数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<FsUserCourseVideoRedPackage> fsRedPackageList = new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        periodRedPackageList.forEach(v -> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Long periodId = v.getPeriodId(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            String[] companyIds = periodMap.get(periodId).getCompanyId().split(","); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            List<FsUserCoursePeriodDays> periodVideos = fsUserCoursePeriodDaysMap.getOrDefault(periodId, Collections.emptyList()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // 将营期id、视频id、公司id组装成一条数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            for (String companyId : companyIds) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                periodVideos.forEach(c -> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    FsUserCourseVideoRedPackage fsUserCourseVideoRedPackage = new FsUserCourseVideoRedPackage(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    fsUserCourseVideoRedPackage.setCompanyId(Long.parseLong(companyId)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    fsUserCourseVideoRedPackage.setVideoId(c.getVideoId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    fsUserCourseVideoRedPackage.setRedPacketMoney(v.getRedPacketMoney()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    fsUserCourseVideoRedPackage.setPeriodId(periodId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    fsUserCourseVideoRedPackage.setDataType(2); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    fsRedPackageList.add(fsUserCourseVideoRedPackage); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 5、 新增或修改 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        this.batchSaveCompanyRedPackage(fsRedPackageList); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 |