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.*;
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.*;
import java.util.stream.Collectors;

/**
 * 机构小组专辑配置表
 * 2024-01-24 13:32:00
 */
@Slf4j
@Service
public class TenantGroupAlbumServiceImpl extends ServiceImpl<TenantGroupAlbumMapper, TenantGroupAlbum> implements TenantGroupAlbumService {

    @Autowired
    private StudentService studentService;

    @Autowired
    private TenantStaffService tenantStaffService;

    @Autowired
    private UserOrderDao userOrderDao;

    @Autowired
    private TenantAlbumMusicMapper tenantAlbumMusicMapper;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private TenantAlbumService tenantAlbumService;

    @Autowired
    private UserTenantAlbumRecordService userTenantAlbumRecordService;

    @Autowired
    private UserTenantAlbumRecordMapper userTenantAlbumRecordMapper;

    @Autowired
    private CoursewareFeignService coursewareFeignService;

	/**
     * 查询详情
     * @param id 详情ID
     * @return TenantGroupAlbum
     */
	@Override
    public TenantGroupAlbum detail(Long id) {
        
        return baseMapper.selectById(id);
    }
    
    /**
     * 分页查询
     * @param page IPage<TenantGroupAlbum>
     * @param query TenantGroupAlbumWrapper.TenantGroupAlbumQuery
     * @return IPage<TenantGroupAlbum>
     */
    @Override
    public IPage<TenantGroupAlbumWrapper.TenantGroupAlbumVo> selectPage(IPage<TenantGroupAlbumWrapper.TenantGroupAlbumVo> page, TenantGroupAlbumWrapper.TenantGroupAlbumQuery query) {
        
        return page.setRecords(baseMapper.selectPage(page, query));
    }
	
    /**
     * 添加
     * @param tenantGroupAlbum TenantGroupAlbumWrapper.TenantGroupAlbum
     * @return Boolean
     */
    @Override
    public Boolean add(TenantGroupAlbumWrapper.TenantGroupAlbum tenantGroupAlbum) {    	
        
        return this.save(JSON.parseObject(tenantGroupAlbum.jsonString(), TenantGroupAlbum.class));
    }

    /**
     * 更新
     * @param tenantGroupAlbum TenantGroupAlbumWrapper.TenantGroupAlbum
     * @return Boolean
     */
    @Override
    public Boolean update(TenantGroupAlbumWrapper.TenantGroupAlbum tenantGroupAlbum){

        return this.updateById(JSON.parseObject(tenantGroupAlbum.jsonString(), TenantGroupAlbum.class));       
    }

    @Override
    public List<TenantGroupAlbumWrapper.BuyTenantAlbum> buyAlbumInfo(TenantGroupAlbumWrapper.BuyTenantAlbumQuery query) {
        // 学生端 查看小组专辑 获取专辑基本信息， 机构端查看机构专辑 获取专辑基本信息

        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 (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 (query.getClientType() == ClientEnum.TEACHER) {
            Teacher teacher = teacherService.getById(query.getUserId());
            if(teacher == null || teacher.getTenantId() == null) {
                throw new BizException("老师机构信息不存在");
            }
            query.setTenantId(teacher.getTenantId());
        } else {
            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());

        if (CollectionUtils.isNotEmpty(coursewareIds)) {
            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())));

        for (TenantGroupAlbumWrapper.BuyTenantAlbum buyTenantAlbum : buyAlbumInfo) {
            Map<SubjectTypeEnum, Long> subjectTypeEnumLongMap = map.get(buyTenantAlbum.getId());
            if (subjectTypeEnumLongMap != null) {
                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 (query.getClientType() == ClientEnum.STUDENT) {
            // 曲目ID集合
            List<Long> tenantGroupAlbumIds = buyAlbumInfo.stream().map(TenantGroupAlbumWrapper.BuyTenantAlbum::getTenantGroupAlbumId).collect(Collectors.toList());
            List<Long> studentTenantAlbumNum = userOrderDao.getStudentTenantAlbumNum(query.getUserId(), tenantGroupAlbumIds);

            // 判断是否还在生效中
            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());
                }
                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;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateStatus(TenantGroupAlbumWrapper.UpdateStatus updateStatus) {
        List<Long> ids = Arrays.stream(updateStatus.getIds().split(",")).map(Long::valueOf).collect(Collectors.toList());
        List<TenantGroupAlbum> tenantGroupAlbumList = this.lambdaQuery().in(TenantGroupAlbum::getId, ids).list();
        if (CollectionUtils.isEmpty(tenantGroupAlbumList)) {
            return;
        }
        List<Long> tenantAlbumIds = tenantGroupAlbumList.stream().map(TenantGroupAlbum::getTenantAlbumId).distinct().collect(Collectors.toList());
        List<TenantAlbum> list = tenantAlbumService.lambdaQuery()
                .in(TenantAlbum::getId, tenantAlbumIds)
                .eq(TenantAlbum::getDelFlag, false)
                .list();
        if (list.stream().anyMatch(next -> Boolean.FALSE.equals(next.getStatus()))) {
            throw new BizException("专辑已经停用");
        }
        this.lambdaUpdate()
                .set(TenantGroupAlbum::getStatus, updateStatus.getStatus())
                .in(TenantGroupAlbum::getId, ids)
                .update();
    }

}