| package com.xzx.gc.system.service;  | 
|   | 
| import cn.hutool.core.bean.BeanUtil;  | 
| import cn.hutool.core.collection.CollUtil;  | 
| import cn.hutool.core.convert.Convert;  | 
| import cn.hutool.core.date.DateUtil;  | 
| import cn.hutool.core.lang.Dict;  | 
| import cn.hutool.core.util.ReflectUtil;  | 
| import cn.hutool.core.util.StrUtil;  | 
| import com.xzx.gc.common.constant.CommonEnum;  | 
| import com.xzx.gc.common.constant.Constants;  | 
| import com.xzx.gc.common.constant.OrderEnum;  | 
| import com.xzx.gc.common.utils.BusinessUtil;  | 
| import com.xzx.gc.common.utils.DateUtils;  | 
| import com.xzx.gc.common.utils.SpringUtil;  | 
| import com.xzx.gc.entity.*;  | 
| import com.xzx.gc.system.mapper.PlatformReportMapper;  | 
| import com.xzx.gc.util.CollUtils;  | 
| import lombok.extern.slf4j.Slf4j;  | 
| import org.springframework.beans.factory.annotation.Autowired;  | 
| import org.springframework.scheduling.annotation.Async;  | 
| import org.springframework.scheduling.annotation.Scheduled;  | 
| import org.springframework.stereotype.Service;  | 
| import org.springframework.transaction.annotation.Transactional;  | 
| import tk.mybatis.mapper.entity.Example;  | 
|   | 
| import java.math.BigDecimal;  | 
| import java.util.Date;  | 
| import java.util.List;  | 
| import java.util.stream.Collectors;  | 
|   | 
|   | 
| /**  | 
|  * 平台报表  | 
|  */  | 
| @Service  | 
| @Transactional  | 
| @Slf4j  | 
| public class PlatformReportService {  | 
|   | 
|     @Autowired  | 
|     private PlatformReportMapper platformReportMapper;  | 
|   | 
|     @Autowired  | 
|     private UserService userService;  | 
|   | 
|     @Autowired  | 
|     private OrderService orderService;  | 
|   | 
|     @Autowired  | 
|     private AccountService accountService;  | 
|   | 
|     @Autowired  | 
|     private OrderComplaintService orderComplaintService;  | 
|   | 
|     @Autowired  | 
|     private OrderStorageService orderStorageService;  | 
|   | 
|     @Autowired  | 
|     private CityPartnerService cityPartnerService;  | 
|   | 
|     @Autowired  | 
|     private BusinessUtil businessUtil;  | 
|   | 
|     @Autowired  | 
|     private OtherUserService otherUserService;  | 
|   | 
|     @Autowired  | 
|     private AccountLogService accountLogService;  | 
|   | 
|   | 
|   | 
|   | 
|   | 
|     /**  | 
|      * 按报表类型和指标类型查询  | 
|      * @param reportType  | 
|      * @param indicatorType  | 
|      * @return  | 
|      */  | 
|     public List<PlatformReport> findByType(String reportType,String indicatorType){  | 
|         Example example=new Example(PlatformReport.class);  | 
|         Example.Criteria criteria = example.createCriteria();  | 
|         criteria.andEqualTo("reportType",reportType);  | 
|         criteria.andEqualTo("indicatorType",indicatorType);  | 
|         return platformReportMapper.selectByExample(example);  | 
|     }  | 
|   | 
|   | 
|     /**  | 
|      * 根据报表类型 及小于入参时间查询  | 
|      * @param reportType  | 
|      * @param time  | 
|      * @return  | 
|      */  | 
|     public List<PlatformReport> findByReportTypeLessThanTime(String reportType,String time){  | 
|         Example example=new Example(PlatformReport.class);  | 
|         Example.Criteria criteria = example.createCriteria();  | 
|         criteria.andEqualTo("reportType",reportType);  | 
|         criteria.andLessThan("createTime",time);  | 
|         return platformReportMapper.selectByExample(example);  | 
|     }  | 
|   | 
|   | 
|     /**  | 
|      * 根据类型和时间筛选  | 
|      * @param reportType  | 
|      * @param indicatorType  | 
|      * @param timeType  | 
|      * @return  | 
|      */  | 
|     public List<PlatformReport> findByTypeAndTimeType(String reportType,String indicatorType,String timeType){  | 
|         Example example=new Example(PlatformReport.class);  | 
|         Example.Criteria criteria = example.createCriteria();  | 
|         criteria.andEqualTo("reportType",reportType);  | 
|         criteria.andEqualTo("indicatorType",indicatorType);  | 
|         //1:近3天 2 近1个星期 3:近一个月 4:近3个月 5:近6个月  | 
|         if("1".equals(timeType)){  | 
|             criteria.andCondition("DATE_FORMAT(create_time,'%Y-%m-%d')>=date_sub(CURRENT_DATE(), interval 3 day)");  | 
|         } else if ("2".equals(timeType)) {  | 
|             criteria.andCondition("DATE_FORMAT(create_time,'%Y-%m-%d')>=date_sub(CURRENT_DATE(), interval 7 day)");  | 
|         }else if ("3".equals(timeType)) {  | 
|             criteria.andCondition("DATE_FORMAT(create_time,'%Y-%m-%d')>=date_sub(CURRENT_DATE(), interval 1 month)");  | 
|         }else if ("4".equals(timeType)) {  | 
|             criteria.andCondition("DATE_FORMAT(create_time,'%Y-%m-%d')>=date_sub(CURRENT_DATE(), interval 3 month)");  | 
|         }else if ("5".equals(timeType)) {  | 
|             criteria.andCondition("DATE_FORMAT(create_time,'%Y-%m-%d')>=date_sub(CURRENT_DATE(), interval 6 month)");  | 
|         }  | 
|         example.setOrderByClause("create_time asc");  | 
|         return platformReportMapper.selectByExample(example);  | 
|     }  | 
|   | 
|     /**  | 
|      * 根据指标类型查找  | 
|      * @param indicatorType  | 
|      * @return  | 
|      */  | 
|     public List<PlatformReport> findByIndicatorType(String indicatorType){  | 
|         Example example=new Example(PlatformReport.class);  | 
|         Example.Criteria criteria = example.createCriteria();  | 
|         criteria.andEqualTo("indicatorType",indicatorType);  | 
|         example.setOrderByClause("create_time desc");  | 
|         return platformReportMapper.selectByExample(example);  | 
|     }  | 
|   | 
|     /**  | 
|      * 根据统计时间查找  | 
|      * @param time  | 
|      * @return  | 
|      */  | 
|     public int findByTime(String time){  | 
|         Example example=new Example(PlatformReport.class);  | 
|         Example.Criteria criteria = example.createCriteria();  | 
|         criteria.andEqualTo("createTime",time);  | 
|         return platformReportMapper.selectCountByExample(example);  | 
|     }  | 
|   | 
|     /**  | 
|      * 获取 日环比  | 
|      * @param moneys  | 
|      * @return  | 
|      */  | 
|     public String  getRatio(List<PlatformReport> moneys ){  | 
|         List<PlatformReport> collect = moneys.stream().filter(x -> DateUtil.offsetDay(new Date(),-2).toDateStr().equals(x.getCreateTime())).collect(Collectors.toList());  | 
|         double yesterday = collect.stream().mapToDouble(x -> Convert.toDouble(x.getIndicator())).sum();  | 
|         collect = moneys.stream().filter(x -> DateUtil.offsetDay(new Date(),-1).toDateStr().equals(x.getCreateTime())).collect(Collectors.toList());  | 
|         double today = collect.stream().mapToDouble(x -> Convert.toDouble(x.getIndicator())).sum();  | 
|         return businessUtil.getRatio(Convert.toBigDecimal(yesterday), Convert.toBigDecimal(today));  | 
|     }  | 
|   | 
|   | 
|     /**  | 
|      * 删除所有报表数据  | 
|      */  | 
|     public void deleteAll(){  | 
|         platformReportMapper.delete(new PlatformReport());  | 
|     }  | 
|   | 
|     /**  | 
|      * 批量添加  | 
|      * @param list  | 
|      */  | 
|     public void batchAdd(List<PlatformReport> list){  | 
|         platformReportMapper.insertList(list);  | 
|     }  | 
|   | 
|   | 
|     /**  | 
|      * 初始化所有数据生成报表  | 
|      */  | 
|     public void init(){  | 
|         List<UserInfo> userInfos = userService.findForBidden();  | 
|         List<OrderInfo> orderInfos =orderService.findPartnerId();  | 
|         List<OrderStorageInfo> storageInfos = orderStorageService.findByStorageType(Convert.toShort(OrderEnum.打包站.getValue()));  | 
|         List<OrderComplaint> complaints = orderComplaintService.findPartner();  | 
|         List<AccountInfo> accountInfos = accountService.findThanZero();  | 
|         List<AccountLog> withdraws = accountLogService.findByChannelType(Convert.toShort(CommonEnum.提现操作.getValue()));  | 
|         init(userInfos,orderInfos,storageInfos,complaints,accountInfos,withdraws,DateUtil.today());  | 
|     }  | 
|   | 
|     /**  | 
|      * 同步数据  | 
|      */  | 
|     @Scheduled(cron = "${order.report.cron}")  | 
|     @Async  | 
|     public void sysnc(){  | 
|         log.trace("********定时执行平台报表统计任务开始【凌晨12点半执行】*********");  | 
|         if(!SpringUtil.isCloud()) {  | 
|             List<UserInfo> userInfos = userService.findForBiddenByTime();  | 
|             List<OrderInfo> orderInfos = orderService.findPartnerIdByTime();  | 
|             List<OrderStorageInfo> storageInfos = orderStorageService.findByStorageTypeByTime(Convert.toShort(OrderEnum.打包站.getValue()));  | 
|             List<OrderComplaint> complaints = orderComplaintService.findPartnerByTime();  | 
|             List<AccountInfo> accountInfos = accountService.findThanZero();  | 
|             List<AccountLog> withdraws = accountLogService.findByChannelTypeAndTime(Convert.toShort(CommonEnum.提现操作.getValue()));  | 
|             init(userInfos, orderInfos, storageInfos, complaints, accountInfos,withdraws, DateUtil.yesterday().toDateStr());  | 
|         }  | 
|         log.trace("********定时执行平台报表统计任务结束【凌晨12点半执行】*********");  | 
|     }  | 
|   | 
|   | 
|     private void init(List<UserInfo> userInfos,List<OrderInfo> orderInfos,List<OrderStorageInfo> storageInfos,List<OrderComplaint> complaints,List<AccountInfo> accountInfos,List<AccountLog> withdraws,String time){  | 
|   | 
|         log.debug(businessUtil.repeatService("平台报表统计任务开始"));  | 
|   | 
|         if(findByTime(DateUtil.yesterday().toDateStr())>0||findByTime(DateUtil.today())>0){  | 
|             log.debug("统计已执行");  | 
|             return;  | 
|         }  | 
|   | 
|         List<PlatformReport> list=CollUtil.newArrayList();  | 
|         /**  | 
|          *总用户量  | 
|          */  | 
|         //按时间统计  | 
|         List<List<UserInfo>> userByTime = CollUtils.groupByField(userInfos, "registTime");  | 
|   | 
|         if(CollUtil.isNotEmpty(userByTime)) {  | 
|             for (List<UserInfo> userInfoList : userByTime) {  | 
|                 PlatformReport platformReport = new PlatformReport();  | 
|                 platformReport.setCreateTime(DateUtils.formatDate(userInfoList.get(0).getRegistTime()));  | 
|                 platformReport.setReportType(Convert.toShort(OrderEnum.用户量.getValue()));  | 
|                 platformReport.setIndicator(Convert.toStr(userInfoList.size()));  | 
|                 platformReport.setIndicatorType(Convert.toShort(OrderEnum.按时间统计.getValue()));  | 
|                 platformReport.setUserId(Constants.DEFAULT_ID);  | 
|                 list.add(platformReport);  | 
|             }  | 
|         }else {  | 
|             initForEmptyList(list,time,OrderEnum.用户量.getValue(),OrderEnum.按时间统计.getValue());  | 
|         }  | 
|         platformReportMapper.insertList(list);  | 
|         log.debug("已完成总用户量按时间统计,统计数目:{}",list.size());  | 
|   | 
|         //按合伙人统计  | 
|         list.clear();  | 
|   | 
|         //用户根据合伙人拆分成多个集合  | 
|         if(CollUtil.isNotEmpty(userInfos)){  | 
|             Dict dict =groupPartner(userInfos);  | 
|             for (String s : dict.keySet()) {  | 
|                 List<UserInfo> dictList = (List<UserInfo>) dict.get(s);  | 
|                 staticUser(dictList,list,time);  | 
|             }  | 
|         }else {  | 
|             initForEmptyList(list,time,OrderEnum.用户量.getValue(),OrderEnum.按合伙人统计.getValue());  | 
|         }  | 
|   | 
|         platformReportMapper.insertList(list);  | 
|         log.debug("已完成总用户量按合伙人统计,统计数目:{}",list.size());  | 
|   | 
|   | 
|         /*  | 
|          *总订单量  | 
|          */  | 
|         //按时间统计  | 
|         list.clear();  | 
|         List<List<OrderInfo>> orderByTime = CollUtils.groupByField(orderInfos, "createTime");  | 
|         if(CollUtil.isNotEmpty(orderByTime)) {  | 
|             for (List<OrderInfo> orderInfoList : orderByTime) {  | 
|                 PlatformReport platformReport = new PlatformReport();  | 
|                 platformReport.setCreateTime(DateUtils.formatDate(orderInfoList.get(0).getCreateTime()));  | 
|                 platformReport.setReportType(Convert.toShort(OrderEnum.总下单量.getValue()));  | 
|                 platformReport.setIndicator(Convert.toStr(orderInfoList.size()));  | 
|                 platformReport.setIndicatorType(Convert.toShort(OrderEnum.按时间统计.getValue()));  | 
|                 platformReport.setUserId(Constants.DEFAULT_ID);  | 
|                 list.add(platformReport);  | 
|             }  | 
|         }else {  | 
|             initForEmptyList(list,time,OrderEnum.总下单量.getValue(),OrderEnum.按时间统计.getValue());  | 
|         }  | 
|         platformReportMapper.insertList(list);  | 
|         log.debug("已完成总订单量按时间统计,统计数目:{}",list.size());  | 
|   | 
|         //按合伙人统计  | 
|         list.clear();  | 
|         List<List<OrderInfo>> orderByPartner = CollUtil.groupByField(orderInfos, "partnerId");  | 
|         if(CollUtil.isNotEmpty(orderByPartner)) {  | 
|             for (List<OrderInfo> orderInfoList : orderByPartner) {  | 
|                 PlatformReport platformReport = new PlatformReport();  | 
|                 platformReport.setCreateTime(time);  | 
|                 platformReport.setReportType(Convert.toShort(OrderEnum.总下单量.getValue()));  | 
|                 platformReport.setIndicator(Convert.toStr(orderInfoList.size()));  | 
|                 platformReport.setIndicatorType(Convert.toShort(OrderEnum.按合伙人统计.getValue()));  | 
|                 String partnterId = StrUtil.isBlank(orderInfoList.get(0).getPartnerId()) ? Constants.DEFAULT_ID : orderInfoList.get(0).getPartnerId();  | 
|                 platformReport.setUserId(partnterId);  | 
|                 list.add(platformReport);  | 
|             }  | 
|         }else {  | 
|             initForEmptyList(list,time,OrderEnum.总下单量.getValue(),OrderEnum.按合伙人统计.getValue());  | 
|         }  | 
|         platformReportMapper.insertList(list);  | 
|         log.debug("已完成总订单量按合伙人统计,统计数目:{}",list.size());  | 
|   | 
|   | 
|         /*  | 
|          *总交易金额  | 
|          */  | 
|         //按时间统计  | 
|         list.clear();  | 
|         //删除金额为0(订单状态为已取消等)  | 
|         orderInfos.removeIf(x->StrUtil.isBlank(x.getMoney())||Convert.toBigDecimal(x.getMoney()).compareTo(BigDecimal.ZERO)==0);  | 
|         orderByTime = CollUtils.groupByField(orderInfos, "createTime");  | 
|         if(CollUtil.isNotEmpty(orderByTime)) {  | 
|             for (List<OrderInfo> orderInfoList : orderByTime) {  | 
|                 PlatformReport platformReport = new PlatformReport();  | 
|                 platformReport.setCreateTime(DateUtils.formatDate(orderInfoList.get(0).getCreateTime()));  | 
|                 platformReport.setReportType(Convert.toShort(OrderEnum.总交易金额.getValue()));  | 
|                 platformReport.setIndicator(businessUtil.changeMoney(Convert.toStr(orderInfoList.stream().mapToDouble(x -> Convert.toDouble(x.getMoney())).sum())));  | 
|                 platformReport.setIndicatorType(Convert.toShort(OrderEnum.按时间统计.getValue()));  | 
|                 platformReport.setUserId(Constants.DEFAULT_ID);  | 
|                 list.add(platformReport);  | 
|             }  | 
|         }else {  | 
|             initForEmptyList(list,time,OrderEnum.总交易金额.getValue(),OrderEnum.按时间统计.getValue());  | 
|         }  | 
|         platformReportMapper.insertList(list);  | 
|         log.debug("已完成总交易额按时间统计,统计数目:{}",list.size());  | 
|   | 
|         //按合伙人统计  | 
|         list.clear();  | 
|         orderByPartner = CollUtil.groupByField(orderInfos, "partnerId");  | 
|         if(CollUtil.isNotEmpty(orderByPartner)) {  | 
|             for (List<OrderInfo> orderInfoList : orderByPartner) {  | 
|                 PlatformReport platformReport = new PlatformReport();  | 
|                 platformReport.setCreateTime(time);  | 
|                 platformReport.setReportType(Convert.toShort(OrderEnum.总交易金额.getValue()));  | 
|                 platformReport.setIndicator(businessUtil.changeMoney(Convert.toStr(orderInfoList.stream().mapToDouble(x -> Convert.toDouble(x.getMoney())).sum())));  | 
|                 platformReport.setIndicatorType(Convert.toShort(OrderEnum.按合伙人统计.getValue()));  | 
|                 String partnterId = StrUtil.isBlank(orderInfoList.get(0).getPartnerId()) ? Constants.DEFAULT_ID : orderInfoList.get(0).getPartnerId();  | 
|                 platformReport.setUserId(partnterId);  | 
|                 list.add(platformReport);  | 
|             }  | 
|         }else {  | 
|             initForEmptyList(list,time,OrderEnum.总交易金额.getValue(),OrderEnum.按合伙人统计.getValue());  | 
|         }  | 
|         platformReportMapper.insertList(list);  | 
|         log.debug("已完成总交易额按合伙人统计,统计数目:{}",list.size());  | 
|   | 
|   | 
|         /*  | 
|           打包站回收金额  | 
|          */  | 
|         //按时间统计  | 
|         list.clear();  | 
|         List<List<OrderStorageInfo>> storageByTime = CollUtils.groupByField(storageInfos, "storageTime");  | 
|         if(CollUtil.isNotEmpty(storageByTime)) {  | 
|             for (List<OrderStorageInfo> storageInfoList : storageByTime) {  | 
|                 PlatformReport platformReport = new PlatformReport();  | 
|                 platformReport.setCreateTime(DateUtils.formatDate(storageInfoList.get(0).getStorageTime()));  | 
|                 platformReport.setReportType(Convert.toShort(OrderEnum.打包站回收金额.getValue()));  | 
|                 platformReport.setIndicator(businessUtil.changeMoney(Convert.toStr(storageInfoList.stream().mapToDouble(x -> Convert.toDouble(x.getStorageMoney(),Convert.toDouble(BigDecimal.ZERO))).sum())));  | 
|                 platformReport.setIndicatorType(Convert.toShort(OrderEnum.按时间统计.getValue()));  | 
|                 platformReport.setUserId(Constants.DEFAULT_ID);  | 
|                 list.add(platformReport);  | 
|             }  | 
|         }else {  | 
|             initForEmptyList(list,time,OrderEnum.打包站回收金额.getValue(),OrderEnum.按时间统计.getValue());  | 
|         }  | 
|         platformReportMapper.insertList(list);  | 
|         log.debug("已完成打包站回收金额按时间统计,统计数目:{}",list.size());  | 
|   | 
|   | 
|         //按打包站统计  | 
|         list.clear();  | 
|         List<List<OrderStorageInfo>> storageByPartner = CollUtil.groupByField(storageInfos, "sysStorageId");  | 
|         if(CollUtil.isNotEmpty(storageByPartner)) {  | 
|             for (List<OrderStorageInfo> storageInfoList : storageByPartner) {  | 
|                 PlatformReport platformReport = new PlatformReport();  | 
|                 platformReport.setCreateTime(time);  | 
|                 platformReport.setReportType(Convert.toShort(OrderEnum.打包站回收金额.getValue()));  | 
|                 platformReport.setIndicator(businessUtil.changeMoney(Convert.toStr(storageInfoList.stream().mapToDouble(x -> Convert.toDouble(x.getStorageMoney(),Convert.toDouble(BigDecimal.ZERO))).sum())));  | 
|                 platformReport.setIndicatorType(Convert.toShort(OrderEnum.按打包站统计.getValue()));  | 
|                 platformReport.setUserId(Convert.toStr(storageInfoList.get(0).getSysStorageId()));  | 
|                 list.add(platformReport);  | 
|             }  | 
|         }else {  | 
|             initForEmptyList(list,time,OrderEnum.打包站回收金额.getValue(),OrderEnum.按打包站统计.getValue());  | 
|         }  | 
|         platformReportMapper.insertList(list);  | 
|         log.debug("已完成打包站回收金额按打包站统计,统计数目:{}",list.size());  | 
|   | 
|   | 
|         /*  | 
|          * 投诉总量  | 
|          */  | 
|         //按时间统计  | 
|         list.clear();  | 
|         List<List<OrderComplaint>> complaintByTime = CollUtils.groupByField(complaints, "createTime");  | 
|         if(CollUtil.isNotEmpty(complaintByTime)) {  | 
|             for (List<OrderComplaint> complaintList : complaintByTime) {  | 
|                 PlatformReport platformReport = new PlatformReport();  | 
|                 platformReport.setCreateTime(DateUtils.formatDate(complaintList.get(0).getCreateTime()));  | 
|                 platformReport.setReportType(Convert.toShort(OrderEnum.投诉总量.getValue()));  | 
|                 platformReport.setIndicator(Convert.toStr(complaintList.size()));  | 
|                 platformReport.setIndicatorType(Convert.toShort(OrderEnum.按时间统计.getValue()));  | 
|                 platformReport.setUserId(Constants.DEFAULT_ID);  | 
|                 list.add(platformReport);  | 
|             }  | 
|         }else {  | 
|             initForEmptyList(list,time,OrderEnum.投诉总量.getValue(),OrderEnum.按时间统计.getValue());  | 
|         }  | 
|         platformReportMapper.insertList(list);  | 
|         log.debug("已完成投诉总量按时间统计,统计数目:{}",list.size());  | 
|   | 
|         //按合伙人统计  | 
|         list.clear();  | 
|         List<List<OrderComplaint>> complaintByPartner = CollUtil.groupByField(complaints, "partnerId");  | 
|         if(CollUtil.isNotEmpty(complaintByPartner)) {  | 
|             for (List<OrderComplaint> complaintList : complaintByPartner) {  | 
|                 PlatformReport platformReport = new PlatformReport();  | 
|                 platformReport.setCreateTime(time);  | 
|                 platformReport.setReportType(Convert.toShort(OrderEnum.投诉总量.getValue()));  | 
|                 platformReport.setIndicator(Convert.toStr(complaintList.size()));  | 
|                 platformReport.setIndicatorType(Convert.toShort(OrderEnum.按合伙人统计.getValue()));  | 
|                 String partnterId = StrUtil.isBlank(complaintList.get(0).getPartnerId()) ? Constants.DEFAULT_ID : complaintList.get(0).getPartnerId();  | 
|                 platformReport.setUserId(partnterId);  | 
|                 list.add(platformReport);  | 
|             }  | 
|         }else {  | 
|             initForEmptyList(list,time,OrderEnum.投诉总量.getValue(),OrderEnum.按合伙人统计.getValue());  | 
|         }  | 
|         platformReportMapper.insertList(list);  | 
|         log.debug("已完成投诉总量按打合伙人统计,统计数目:{}",list.size());  | 
|   | 
|         /*  | 
|          * 未提现金额  | 
|          */  | 
|         //按时间统计  | 
|         if(CollUtil.isNotEmpty(accountInfos)) {  | 
|             PlatformReport platformReport = new PlatformReport();  | 
|             platformReport.setCreateTime(time);  | 
|             platformReport.setReportType(Convert.toShort(OrderEnum.未提现总额.getValue()));  | 
|             platformReport.setIndicator(businessUtil.changeMoney(Convert.toStr(accountInfos.stream().mapToDouble(x -> Convert.toDouble(x.getMoney())).sum())));  | 
|             platformReport.setIndicatorType(Convert.toShort(OrderEnum.按时间统计.getValue()));  | 
|             platformReport.setUserId(Constants.DEFAULT_ID);  | 
|             platformReportMapper.insertSelective(platformReport);  | 
|         }  | 
|         log.debug("已完成未提现金额按时间统计,统计数目:{}",CollUtil.isNotEmpty(accountInfos)?1:0);  | 
|         //按合伙人统计  | 
|         list.clear();  | 
|   | 
|         //查询当前账户所属合伙人 打包站的账户归为合伙人  | 
|         for (AccountInfo accountInfo : accountInfos) {  | 
|             String partnerId = getPartnerIdByUserId(accountInfo.getUserId());  | 
|             accountInfo.setPartnerId(partnerId);  | 
|         }  | 
|   | 
|   | 
|         //账户根据合伙人拆分成多个集合  | 
|         if(CollUtil.isNotEmpty(accountInfos)){  | 
|             Dict dict =groupPartner(accountInfos);  | 
|             for (String s : dict.keySet()) {  | 
|                 List<AccountInfo> dictList = (List<AccountInfo>) dict.get(s);  | 
|                 staticAccount(dictList,list,time);  | 
|             }  | 
|         }  | 
|   | 
|   | 
|         if(CollUtil.isNotEmpty(list)) {  | 
|             platformReportMapper.insertList(list);  | 
|         }  | 
|         log.debug("已完成未提现金额按合伙人统计,统计数目:{}",list.size());  | 
|   | 
|         /*  | 
|          *已提现金额  | 
|          */  | 
|         //按时间统计  | 
|         list.clear();  | 
|         List<List<AccountLog>> withdrawByTime = CollUtils.groupByField(withdraws, "createTime");  | 
|         if(CollUtil.isNotEmpty(withdrawByTime)) {  | 
|             for (List<AccountLog> withdrawList : withdrawByTime) {  | 
|                 PlatformReport platformReport = new PlatformReport();  | 
|                 platformReport.setCreateTime(DateUtils.formatDate(withdrawList.get(0).getCreateTime()));  | 
|                 platformReport.setReportType(Convert.toShort(OrderEnum.已提现总额.getValue()));  | 
|                 platformReport.setIndicator(businessUtil.changeMoney(Convert.toStr(withdrawList.stream().mapToDouble(x -> Convert.toDouble(x.getMoney())).sum())));  | 
|                 platformReport.setIndicatorType(Convert.toShort(OrderEnum.按时间统计.getValue()));  | 
|                 platformReport.setUserId(Constants.DEFAULT_ID);  | 
|                 list.add(platformReport);  | 
|             }  | 
|         }else {  | 
|             initForEmptyList(list,time,OrderEnum.已提现总额.getValue(),OrderEnum.按时间统计.getValue());  | 
|         }  | 
|         platformReportMapper.insertList(list);  | 
|         log.debug("已完成已提现总额按时间统计,统计数目:{}",list.size());  | 
|   | 
|         //按合伙人统计  | 
|         list.clear();  | 
|         //查询当前账户所属合伙人 打包站的账户归为合伙人  | 
|         for (AccountLog withdraw : withdraws) {  | 
|             String partnerId = getPartnerIdByUserId(withdraw.getUserId());  | 
|             withdraw.setPartnerId(partnerId);  | 
|         }  | 
|   | 
|   | 
|         //账户根据合伙人拆分成多个集合  | 
|         if(CollUtil.isNotEmpty(withdraws)){  | 
|             Dict dict =groupPartner(withdraws);  | 
|             for (String s : dict.keySet()) {  | 
|                 List<AccountLog> dictList = (List<AccountLog>) dict.get(s);  | 
|                 staticAccountLog(dictList,list,time);  | 
|             }  | 
|         }else {  | 
|             initForEmptyList(list,time,OrderEnum.已提现总额.getValue(),OrderEnum.按合伙人统计.getValue());  | 
|         }  | 
|   | 
|         platformReportMapper.insertList(list);  | 
|         log.debug("已完成已提现总额按合伙人统计,统计数目:{}",list.size());  | 
|   | 
|   | 
|         log.debug(businessUtil.repeatService("平台报表统计任务结束"));  | 
|     }  | 
|   | 
|   | 
|     /**  | 
|      * 根据用户ID查找合伙人ID  | 
|      * @param userId  | 
|      * @return  | 
|      */  | 
|     private String getPartnerIdByUserId(String userId) {  | 
|         String partnerId;  | 
|         OtherUserInfo byId = otherUserService.findById(userId);  | 
|         if(byId!=null){  | 
|             if(CommonEnum.打包员.getValue().equals(byId.getUserType())||CommonEnum.打包站运营员.getValue().equals(byId.getUserType())){  | 
|                 //打包站id转换成合伙人id  | 
|                 partnerId = cityPartnerService.queryById(Convert.toLong(byId.getPartnerId())).getPackingStation();  | 
|             }else {  | 
|                 partnerId=byId.getPartnerId();  | 
|             }  | 
|         }else {  | 
|             UserInfo byId1 = userService.findById(userId);  | 
|             if(byId1==null){  | 
|                 partnerId=Constants.DEFAULT_PARTNER_ID;  | 
|             }else {  | 
|                 partnerId = byId1.getPartnerId();  | 
|             }  | 
|         }  | 
|         return partnerId;  | 
|     }  | 
|   | 
|   | 
|     /**  | 
|      * 按合伙人分组  | 
|      */  | 
|     private <T> Dict groupPartner(List<T> list) {  | 
|         Dict dict = Dict.create();  | 
|         for (T t : list) {  | 
|             String partnerIds= (String) ReflectUtil.getFieldValue(t,"partnerId");  | 
|             if(StrUtil.isNotBlank(partnerIds)){  | 
|                 String[] split = partnerIds.split(",");  | 
|                 if(split.length>1) {  | 
|                     for (String partnerId : split) {  | 
|                         Object o = BeanUtil.copyProperties(t, t.getClass());  | 
|                         ReflectUtil.setFieldValue(o, "singlePartnerId", partnerId);  | 
|                         groupDict(dict, o, partnerId);  | 
|                     }  | 
|                 }else {  | 
|                     ReflectUtil.setFieldValue(t, "singlePartnerId", partnerIds);  | 
|                     groupDict(dict, t, partnerIds);  | 
|                 }  | 
|             }else {  | 
|                 String partnerId=Constants.DEFAULT_ID;  | 
|                 ReflectUtil.setFieldValue(t,"singlePartnerId",partnerId);  | 
|                 groupDict(dict,  t, partnerId);  | 
|             }  | 
|         }  | 
|         return dict;  | 
|     }  | 
|   | 
|     private <T> void groupDict(Dict dict, T t, String s) {  | 
|         if(dict.containsKey(s)){  | 
|             List<T> dictList = (List<T>) dict.get(s);  | 
|             dictList.add(t);  | 
|             dict.put(s,dictList);  | 
|         }else {  | 
|             dict.put(s,CollUtil.newArrayList(t));  | 
|         }  | 
|     }  | 
|   | 
|   | 
|     /**  | 
|      * 用户按合伙人统计  | 
|      * @param userInfos  | 
|      * @param list  | 
|      * @param time  | 
|      */  | 
|     private void staticUser(List<UserInfo> userInfos,List<PlatformReport> list,String time){  | 
|         List<List<UserInfo>> userByPartner = CollUtil.groupByField(userInfos, "singlePartnerId");  | 
|         for (List<UserInfo> userInfoList : userByPartner) {  | 
|             PlatformReport platformReport=new PlatformReport();  | 
|             platformReport.setCreateTime(time);  | 
|             platformReport.setReportType(Convert.toShort(OrderEnum.用户量.getValue()));  | 
|             platformReport.setIndicator(Convert.toStr(userInfoList.size()));  | 
|             platformReport.setIndicatorType(Convert.toShort(OrderEnum.按合伙人统计.getValue()));  | 
|             String partnterId = StrUtil.isBlank(userInfoList.get(0).getPartnerId()) ? Constants.DEFAULT_ID : userInfoList.get(0).getSinglePartnerId();  | 
|             platformReport.setUserId(partnterId);  | 
|             list.add(platformReport);  | 
|         }  | 
|     }  | 
|   | 
|   | 
|     /**  | 
|      * 账户按合伙人统计  | 
|      * @param list  | 
|      * @param time  | 
|      */  | 
|     private void staticAccount(List<AccountInfo> accountInfos,List<PlatformReport> list,String time){  | 
|         List<List<AccountInfo>> accountByPartner = CollUtil.groupByField(accountInfos, "singlePartnerId");  | 
|         for (List<AccountInfo> accountInfoList : accountByPartner) {  | 
|             PlatformReport platformReport=new PlatformReport();  | 
|             platformReport.setCreateTime(time);  | 
|             platformReport.setReportType(Convert.toShort(OrderEnum.未提现总额.getValue()));  | 
|             platformReport.setIndicator(businessUtil.changeMoney(Convert.toStr(accountInfoList.stream().mapToDouble(x->Convert.toDouble(x.getMoney())).sum())));  | 
|             platformReport.setIndicatorType(Convert.toShort(OrderEnum.按合伙人统计.getValue()));  | 
|             String partnterId = StrUtil.isBlank(accountInfoList.get(0).getPartnerId()) ? Constants.DEFAULT_ID : accountInfoList.get(0).getSinglePartnerId();  | 
|             platformReport.setUserId(partnterId);  | 
|             list.add(platformReport);  | 
|         }  | 
|     }  | 
|   | 
|   | 
|     /**  | 
|      * 已提现按合伙人统计  | 
|      * @param list  | 
|      * @param time  | 
|      */  | 
|     private void staticAccountLog(List<AccountLog> accountInfos,List<PlatformReport> list,String time){  | 
|         List<List<AccountLog>> accountByPartner = CollUtil.groupByField(accountInfos, "singlePartnerId");  | 
|         for (List<AccountLog> accountInfoList : accountByPartner) {  | 
|             PlatformReport platformReport=new PlatformReport();  | 
|             platformReport.setCreateTime(time);  | 
|             platformReport.setReportType(Convert.toShort(OrderEnum.已提现总额.getValue()));  | 
|             platformReport.setIndicator(businessUtil.changeMoney(Convert.toStr(accountInfoList.stream().mapToDouble(x->Convert.toDouble(x.getMoney())).sum())));  | 
|             platformReport.setIndicatorType(Convert.toShort(OrderEnum.按合伙人统计.getValue()));  | 
|             String partnterId = StrUtil.isBlank(accountInfoList.get(0).getPartnerId()) ? Constants.DEFAULT_ID : accountInfoList.get(0).getSinglePartnerId();  | 
|             platformReport.setUserId(partnterId);  | 
|             list.add(platformReport);  | 
|         }  | 
|     }  | 
|   | 
|     /**  | 
|      * 当查询数据为空的时候初始化  | 
|      */  | 
|     private void initForEmptyList( List<PlatformReport> list,String time,String reportType,String indicatorType){  | 
|         PlatformReport platformReport=new PlatformReport();  | 
|         platformReport.setCreateTime(time);  | 
|         platformReport.setReportType(Convert.toShort(reportType));  | 
|         platformReport.setIndicator(Convert.toStr(0));  | 
|         platformReport.setIndicatorType(Convert.toShort(indicatorType));  | 
|         platformReport.setUserId(Constants.DEFAULT_ID);  | 
|         list.add(platformReport);  | 
|     }  | 
| }  |