|  | @@ -3,29 +3,27 @@ package com.yonge.cooleshow.biz.dal.service.impl;
 | 
	
		
			
				|  |  |  import com.alibaba.fastjson.JSON;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.core.metadata.IPage;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 | 
	
		
			
				|  |  | +import com.beust.jcommander.internal.Lists;
 | 
	
		
			
				|  |  | +import com.dayaedu.cbs.openfeign.client.CoursewareFeignService;
 | 
	
		
			
				|  |  | +import com.dayaedu.cbs.openfeign.wrapper.courseware.CbsLessonCoursewareWrapper;
 | 
	
		
			
				|  |  |  import com.yonge.cooleshow.biz.dal.dao.UserOrderDao;
 | 
	
		
			
				|  |  | -import com.yonge.cooleshow.biz.dal.entity.Student;
 | 
	
		
			
				|  |  | -import com.yonge.cooleshow.biz.dal.entity.Teacher;
 | 
	
		
			
				|  |  | -import com.yonge.cooleshow.biz.dal.entity.TenantAlbum;
 | 
	
		
			
				|  |  | -import com.yonge.cooleshow.biz.dal.entity.TenantAlbumMusic;
 | 
	
		
			
				|  |  | -import com.yonge.cooleshow.biz.dal.entity.TenantGroupAlbum;
 | 
	
		
			
				|  |  | -import com.yonge.cooleshow.biz.dal.entity.TenantStaff;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.biz.dal.entity.*;
 | 
	
		
			
				|  |  |  import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
 | 
	
		
			
				|  |  |  import com.yonge.cooleshow.biz.dal.enums.SubjectTypeEnum;
 | 
	
		
			
				|  |  |  import com.yonge.cooleshow.biz.dal.mapper.TenantAlbumMusicMapper;
 | 
	
		
			
				|  |  |  import com.yonge.cooleshow.biz.dal.mapper.TenantGroupAlbumMapper;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.biz.dal.mapper.UserTenantAlbumRecordMapper;
 | 
	
		
			
				|  |  |  import com.yonge.cooleshow.biz.dal.service.*;
 | 
	
		
			
				|  |  |  import com.yonge.cooleshow.biz.dal.wrapper.TenantGroupAlbumWrapper;
 | 
	
		
			
				|  |  |  import com.yonge.toolset.base.exception.BizException;
 | 
	
		
			
				|  |  |  import lombok.extern.slf4j.Slf4j;
 | 
	
		
			
				|  |  |  import org.apache.commons.collections.CollectionUtils;
 | 
	
		
			
				|  |  | +import org.jetbrains.annotations.Nullable;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  |  import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -import java.util.Arrays;
 | 
	
		
			
				|  |  | -import java.util.List;
 | 
	
		
			
				|  |  | -import java.util.Map;
 | 
	
		
			
				|  |  | +import java.util.*;
 | 
	
		
			
				|  |  |  import java.util.stream.Collectors;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /**
 | 
	
	
		
			
				|  | @@ -57,6 +55,12 @@ public class TenantGroupAlbumServiceImpl extends ServiceImpl<TenantGroupAlbumMap
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private UserTenantAlbumRecordService userTenantAlbumRecordService;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private UserTenantAlbumRecordMapper userTenantAlbumRecordMapper;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private CoursewareFeignService coursewareFeignService;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  	/**
 | 
	
		
			
				|  |  |       * 查询详情
 | 
	
		
			
				|  |  |       * @param id 详情ID
 | 
	
	
		
			
				|  | @@ -103,26 +107,24 @@ public class TenantGroupAlbumServiceImpl extends ServiceImpl<TenantGroupAlbumMap
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  | -    public List<TenantGroupAlbumWrapper.BuyTenantAlbum> buyAlbumInfo(Long tenantGroupAlbumId, Long userId, ClientEnum clientType) {
 | 
	
		
			
				|  |  | +    public List<TenantGroupAlbumWrapper.BuyTenantAlbum> buyAlbumInfo(TenantGroupAlbumWrapper.BuyTenantAlbumQuery query) {
 | 
	
		
			
				|  |  |          // 学生端 查看小组专辑 获取专辑基本信息, 机构端查看机构专辑 获取专辑基本信息
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        TenantGroupAlbumWrapper.BuyTenantAlbumQuery query = new TenantGroupAlbumWrapper.BuyTenantAlbumQuery();
 | 
	
		
			
				|  |  | -        query.setTenantGroupAlbumId(tenantGroupAlbumId);
 | 
	
		
			
				|  |  | -        if (tenantGroupAlbumId != null) {} else if (clientType == ClientEnum.STUDENT) {
 | 
	
		
			
				|  |  | -            Student student = studentService.getById(userId);
 | 
	
		
			
				|  |  | +        if (query.getTenantGroupAlbumId() != null) {} else if (query.getClientType() == ClientEnum.STUDENT) {
 | 
	
		
			
				|  |  | +            Student student = studentService.getById(query.getUserId());
 | 
	
		
			
				|  |  |              if (student.getTenantId() == null || student.getTenantId() <=0 || student.getTenantGroupId() == null) {
 | 
	
		
			
				|  |  |                  throw new BizException("学生未加入机构或者小组");
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              query.setTenantId(student.getTenantId());
 | 
	
		
			
				|  |  |              query.setTenantGroupId(student.getTenantGroupId());
 | 
	
		
			
				|  |  | -        } else if (clientType == ClientEnum.TENANT) {
 | 
	
		
			
				|  |  | -            TenantStaff tenantStaff = tenantStaffService.getByUserId(userId);
 | 
	
		
			
				|  |  | +        } else if (query.getClientType() == ClientEnum.TENANT) {
 | 
	
		
			
				|  |  | +            TenantStaff tenantStaff = tenantStaffService.getByUserId(query.getUserId());
 | 
	
		
			
				|  |  |              if(tenantStaff == null || tenantStaff.getTenantId() == null) {
 | 
	
		
			
				|  |  |                  throw new BizException("机构员工不存在");
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              query.setTenantId(tenantStaff.getTenantId());
 | 
	
		
			
				|  |  | -        }else if (clientType == ClientEnum.TEACHER) {
 | 
	
		
			
				|  |  | -            Teacher teacher = teacherService.getById(userId);
 | 
	
		
			
				|  |  | +        }else if (query.getClientType() == ClientEnum.TEACHER) {
 | 
	
		
			
				|  |  | +            Teacher teacher = teacherService.getById(query.getUserId());
 | 
	
		
			
				|  |  |              if(teacher == null || teacher.getTenantId() == null) {
 | 
	
		
			
				|  |  |                  throw new BizException("老师机构信息不存在");
 | 
	
		
			
				|  |  |              }
 | 
	
	
		
			
				|  | @@ -131,15 +133,39 @@ public class TenantGroupAlbumServiceImpl extends ServiceImpl<TenantGroupAlbumMap
 | 
	
		
			
				|  |  |              throw new BizException("客户端类型错误");
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +        return tenantAlbumsList(query);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Nullable
 | 
	
		
			
				|  |  | +    private List<TenantGroupAlbumWrapper.BuyTenantAlbum> tenantAlbumsList(TenantGroupAlbumWrapper.BuyTenantAlbumQuery query) {
 | 
	
		
			
				|  |  |          List<TenantGroupAlbumWrapper.BuyTenantAlbum> buyAlbumInfo = baseMapper.getBuyAlbumInfo(query);
 | 
	
		
			
				|  |  |          if (CollectionUtils.isEmpty(buyAlbumInfo)) {
 | 
	
		
			
				|  |  |              return buyAlbumInfo;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | +        if (query.getTenantGroupId() !=null || query.getTenantGroupAlbumId() != null) {
 | 
	
		
			
				|  |  | +            buyAlbumInfo.sort(Comparator.comparing(TenantGroupAlbumWrapper.BuyTenantAlbum::getTenantGroupAlbumId));
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |          // 查询专辑可用的分类的曲目数量
 | 
	
		
			
				|  |  |          // 机构专辑ID集合
 | 
	
		
			
				|  |  |          List<Long> tenantAlbumIds = buyAlbumInfo.stream().map(TenantGroupAlbumWrapper.BuyTenantAlbum::getId).collect(Collectors.toList());
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          List<TenantAlbumMusic> list = tenantAlbumMusicMapper.getByAlbumAndEnable(tenantAlbumIds);
 | 
	
		
			
				|  |  | +        // 过滤出课件
 | 
	
		
			
				|  |  | +        List<Long> coursewareIds = list.stream().filter(x -> x.getSubjectType() == SubjectTypeEnum.COURSEWARE).map(TenantAlbumMusic::getMusicSheetId).collect(Collectors.toList());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        CbsLessonCoursewareWrapper.LambdaQuery lambdaQuery = CbsLessonCoursewareWrapper.LambdaQuery.builder().ids(coursewareIds)
 | 
	
		
			
				|  |  | +            .delFlag(false).enableFlag(true).build();
 | 
	
		
			
				|  |  | +        List<CbsLessonCoursewareWrapper.Entity> entities = coursewareFeignService.lessonCoursewareLambdaQuery(lambdaQuery).feignData();
 | 
	
		
			
				|  |  | +        if (CollectionUtils.isNotEmpty(entities)) {
 | 
	
		
			
				|  |  | +            // id 集合
 | 
	
		
			
				|  |  | +            List<Long> courseIds = entities.stream().map(CbsLessonCoursewareWrapper.Entity::getId).collect(Collectors.toList());
 | 
	
		
			
				|  |  | +            // list 去除类型为COURSEWARE,id 不在courseIds
 | 
	
		
			
				|  |  | +            list = list.stream().filter(x -> x.getSubjectType() != SubjectTypeEnum.COURSEWARE || courseIds.contains(x.getMusicSheetId())).collect(Collectors.toList());
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            list = list.stream().filter(x -> x.getSubjectType() != SubjectTypeEnum.COURSEWARE ).collect(Collectors.toList());
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          // 根据专辑ID分组 在根据声部分类 分组
 | 
	
		
			
				|  |  |          Map<Long, Map<SubjectTypeEnum, Long>> map = list.stream()
 | 
	
		
			
				|  |  |              .collect(Collectors.groupingBy(TenantAlbumMusic::getTenantAlbumId, Collectors.groupingBy(TenantAlbumMusic::getSubjectType,Collectors.counting())));
 | 
	
	
		
			
				|  | @@ -150,36 +176,52 @@ public class TenantGroupAlbumServiceImpl extends ServiceImpl<TenantGroupAlbumMap
 | 
	
		
			
				|  |  |                  buyTenantAlbum.setMusicCounts(subjectTypeEnumLongMap.getOrDefault(SubjectTypeEnum.MUSIC,0L).intValue());
 | 
	
		
			
				|  |  |                  buyTenantAlbum.setEnsembleCounts(subjectTypeEnumLongMap.getOrDefault(SubjectTypeEnum.ENSEMBLE,0L).intValue());
 | 
	
		
			
				|  |  |                  buyTenantAlbum.setSubjectCounts(subjectTypeEnumLongMap.getOrDefault(SubjectTypeEnum.SUBJECT,0L).intValue());
 | 
	
		
			
				|  |  | +                buyTenantAlbum.setCoursewareCounts(subjectTypeEnumLongMap.getOrDefault(SubjectTypeEnum.COURSEWARE,0L).intValue());
 | 
	
		
			
				|  |  |              } else {
 | 
	
		
			
				|  |  |                  buyTenantAlbum.setMusicCounts(0);
 | 
	
		
			
				|  |  |                  buyTenantAlbum.setEnsembleCounts(0);
 | 
	
		
			
				|  |  |                  buyTenantAlbum.setSubjectCounts(0);
 | 
	
		
			
				|  |  | +                buyTenantAlbum.setCoursewareCounts(0);
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              buyTenantAlbum.setMusicNum(buyTenantAlbum.getMusicCounts() + buyTenantAlbum.getEnsembleCounts()+buyTenantAlbum.getSubjectCounts());
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          // 如果学生购买 判断购买次数
 | 
	
		
			
				|  |  | -        if (clientType == ClientEnum.STUDENT) {
 | 
	
		
			
				|  |  | +        if (query.getClientType() == ClientEnum.STUDENT) {
 | 
	
		
			
				|  |  |              // 曲目ID集合
 | 
	
		
			
				|  |  |              List<Long> tenantGroupAlbumIds = buyAlbumInfo.stream().map(TenantGroupAlbumWrapper.BuyTenantAlbum::getTenantGroupAlbumId).collect(Collectors.toList());
 | 
	
		
			
				|  |  | -            List<Long> studentTenantAlbumNum = userOrderDao.getStudentTenantAlbumNum(userId, tenantGroupAlbumIds);
 | 
	
		
			
				|  |  | +            List<Long> studentTenantAlbumNum = userOrderDao.getStudentTenantAlbumNum(query.getUserId(), tenantGroupAlbumIds);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              // 判断是否还在生效中
 | 
	
		
			
				|  |  | -            List<Long> useAlbumIds = userTenantAlbumRecordService.getUseAlbumIdsByUserId(userId, ClientEnum.STUDENT);
 | 
	
		
			
				|  |  | +            List<UserTenantAlbumRecord> albumRecordList = userTenantAlbumRecordService.getUseAlbumByUserId(query.getUserId(), ClientEnum.STUDENT);
 | 
	
		
			
				|  |  | +            Map<Long,UserTenantAlbumRecord> albumRecordMap = new HashMap<>();
 | 
	
		
			
				|  |  | +            // 转map
 | 
	
		
			
				|  |  | +            if (CollectionUtils.isNotEmpty(albumRecordList)) {
 | 
	
		
			
				|  |  | +                albumRecordMap = albumRecordList.stream().collect(Collectors.toMap(UserTenantAlbumRecord::getTenantAlbumId, x -> x,(o1,o2)->o1));
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            // 转map
 | 
	
		
			
				|  |  | +            Map<Long, Long> sortMap = new HashMap<>();
 | 
	
		
			
				|  |  | +            if (query.isSortFlag()) {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                // 获取专辑的最新激活时间排序
 | 
	
		
			
				|  |  | +                List<TenantGroupAlbumWrapper.TenantAlbumSort> sorts = userTenantAlbumRecordMapper
 | 
	
		
			
				|  |  | +                    .getTenantAlbumMaxCreateTime(query.getUserId(),query.getClientType(),tenantAlbumIds);
 | 
	
		
			
				|  |  | +                sortMap = sorts.stream()
 | 
	
		
			
				|  |  | +                    .collect(Collectors.toMap(TenantGroupAlbumWrapper.TenantAlbumSort::getId, TenantGroupAlbumWrapper.TenantAlbumSort::getSort));
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              for (TenantGroupAlbumWrapper.BuyTenantAlbum item : buyAlbumInfo) {
 | 
	
		
			
				|  |  |                  item.setBuyedTimes((int) studentTenantAlbumNum.stream().filter(x -> x.equals(item.getTenantGroupAlbumId())).count());
 | 
	
		
			
				|  |  |                  if (item.getBuyedTimes() > item.getBuyTimes()) {
 | 
	
		
			
				|  |  |                      item.setBuyedTimes(item.getBuyTimes());
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  | -                if (CollectionUtils.isNotEmpty(useAlbumIds)) {
 | 
	
		
			
				|  |  | -                    item.setStatus(useAlbumIds.contains(item.getTenantGroupAlbumId()));
 | 
	
		
			
				|  |  | -                } else {
 | 
	
		
			
				|  |  | -                    item.setStatus(false);
 | 
	
		
			
				|  |  | +                item.setStatus(albumRecordMap.containsKey(item.getId()));
 | 
	
		
			
				|  |  | +                UserTenantAlbumRecord userTenantAlbumRecord = albumRecordMap.get(item.getId());
 | 
	
		
			
				|  |  | +                if (userTenantAlbumRecord != null) {
 | 
	
		
			
				|  |  | +                    item.setExpireTime(userTenantAlbumRecord.getEndTime());
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  | +                item.setSort(sortMap.getOrDefault(item.getId(),0L));
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          return buyAlbumInfo;
 | 
	
	
		
			
				|  | @@ -206,4 +248,5 @@ public class TenantGroupAlbumServiceImpl extends ServiceImpl<TenantGroupAlbumMap
 | 
	
		
			
				|  |  |                  .in(TenantGroupAlbum::getId, ids)
 | 
	
		
			
				|  |  |                  .update();
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  }
 |