| 
					
				 | 
			
			
				@@ -1,13 +1,16 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 package com.fs.course.service.impl; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import java.math.BigDecimal; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import java.util.Date; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import java.util.List; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import java.util.Map; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.time.ZoneId; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.time.ZonedDateTime; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.*; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import java.util.function.Function; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import java.util.stream.Collectors; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.fs.company.domain.Company; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.fs.company.mapper.CompanyMapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.fs.course.domain.FsUserCoursePeriod; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.fs.course.mapper.FsUserCoursePeriodMapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.fs.store.mapper.FsUserMapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.google.common.collect.Lists; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -15,6 +18,7 @@ import lombok.AllArgsConstructor; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.apache.ibatis.session.ExecutorType; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.apache.ibatis.session.SqlSession; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.apache.ibatis.session.SqlSessionFactory; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.beans.BeanUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.springframework.beans.factory.annotation.Autowired; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.springframework.stereotype.Service; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.fs.course.mapper.FsUserWatchStatisticsMapper; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -39,6 +43,8 @@ public class FsUserWatchStatisticsServiceImpl extends ServiceImpl<FsUserWatchSta 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     private SqlSessionFactory sqlSessionFactory; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private final CompanyMapper companyMapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				      * 查询会员看课统计-按营期统计 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				      * 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -115,33 +121,68 @@ public class FsUserWatchStatisticsServiceImpl extends ServiceImpl<FsUserWatchSta 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     @Transactional 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     public void insertStatistics() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         // 1、获取统计结果 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        //获取会员数量和新增会员数量 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        List<FsUserWatchStatistics> userTotal = fsUserMapper.selectFsUserTotal(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        FsUserWatchStatistics userTotalStatistics = new FsUserWatchStatistics(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if(!userTotal.isEmpty()){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            userTotalStatistics = userTotal.get(0); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // 获取看课统计 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 获取所有的营期,并拆分公司id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        FsUserCoursePeriod periodParam = new FsUserCoursePeriod(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<FsUserCoursePeriod> fsUserCoursePeriods = fsUserCoursePeriodMapper.selectFsUserCoursePeriodList(periodParam); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Set<Long> companyIds = fsUserCoursePeriods.stream() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .flatMap(item -> Arrays.stream(item.getCompanyId().split(","))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .map(Long::valueOf) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .collect(Collectors.toSet()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 获取公司信息 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<Company> companies = companyMapper.selectCompanyByIds(companyIds); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Map<Long, Company> companyMap = companies.stream().collect(Collectors.toMap(Company::getCompanyId, Function.identity())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 获取看课统计(按营期按公司) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         List<FsUserWatchStatistics> courseWatchStatistics = baseMapper.getCourseWatchStatistics(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Map<String, FsUserWatchStatistics> courseWatchStatisticsMap = courseWatchStatistics.stream().collect(Collectors.toMap(k -> String.format("%s-%s", k.getPeriodId(), k.getCompanyId()), v -> v)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-//        // 转化map 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-//        Map<Long, FsUserWatchStatistics> watchStatisticsMap = courseWatchStatistics.stream() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-//                .collect(Collectors.toMap( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-//                        FsUserWatchStatistics::getPeriodId, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-//                        Function.identity() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-//                )); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // 组装数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        for (FsUserWatchStatistics courseWatchStatistic : courseWatchStatistics) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            // 单独set,不用copy,避免copy出来的结果被前面的覆盖 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            courseWatchStatistic.setUserNum(userTotalStatistics.getUserNum() != null ? userTotalStatistics.getUserNum():0); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            courseWatchStatistic.setNewUserNum(userTotalStatistics.getNewUserNum() != null ? userTotalStatistics.getNewUserNum():0); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            courseWatchStatistic.setCreateTime(new Date()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            courseWatchStatistic.setUpdateTime(new Date()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //获取公司的会员数量和今日新增会员数量 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<FsUserWatchStatistics> userTotal = fsUserMapper.selectFsUserTotal(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Map<String, FsUserWatchStatistics> userTotalMap = userTotal.stream().collect(Collectors.toMap(FsUserWatchStatistics::getCompanyId, Function.identity())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<FsUserWatchStatistics> list = fsUserCoursePeriods.stream() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .flatMap(item -> Arrays.stream(item.getCompanyId().split(",")) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        .map(companyIdStr -> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            Long companyId = Long.valueOf(companyIdStr.trim()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            Company company = companyMap.get(companyId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            // 赋值 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            FsUserWatchStatistics fsUserWatchStatistics = new FsUserWatchStatistics(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            BeanUtils.copyProperties(item, fsUserWatchStatistics); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            ZonedDateTime zonedDateTime = item.getPeriodStartingTime().atStartOfDay(ZoneId.systemDefault()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            fsUserWatchStatistics.setPeriodStartingTime(Date.from(zonedDateTime.toInstant())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            fsUserWatchStatistics.setCompanyId(companyIdStr.trim()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            fsUserWatchStatistics.setCompanyName(company != null ? company.getCompanyName() : null); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            FsUserWatchStatistics userTotalData = userTotalMap.get(fsUserWatchStatistics.getCompanyId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            String key = String.format("%s-%s", fsUserWatchStatistics.getPeriodId(), fsUserWatchStatistics.getCompanyId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            FsUserWatchStatistics watchData = courseWatchStatisticsMap.get(key); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            if(userTotalData != null){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                fsUserWatchStatistics.setUserNum(userTotalData.getUserNum()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                fsUserWatchStatistics.setNewUserNum(userTotalData.getNewUserNum()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                fsUserWatchStatistics.setUserNum(0); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                fsUserWatchStatistics.setNewUserNum(0); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            if(watchData != null){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                fsUserWatchStatistics.setWatchNum(watchData.getWatchNum()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                fsUserWatchStatistics.setCompleteWatchNum(watchData.getCompleteWatchNum()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                fsUserWatchStatistics.setCompleteWatchRate(watchData.getCompleteWatchRate()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                fsUserWatchStatistics.setWatchNum(0); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                fsUserWatchStatistics.setCompleteWatchNum(0); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                fsUserWatchStatistics.setCompleteWatchRate(BigDecimal.ZERO); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            return fsUserWatchStatistics; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        })).collect(Collectors.toList()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         //2、分批次插入数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        this.batchInsert(courseWatchStatistics); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        this.batchInsert(list); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     private void batchInsert(List<FsUserWatchStatistics> list) { 
			 |