package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.MusicInfo;
import com.ruoyi.common.core.domain.entity.MusicRecommend;
import com.ruoyi.common.core.domain.entity.RecommendInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.MusicInfoMapper;
import com.ruoyi.system.mapper.MusicRecommendMapper;
import com.ruoyi.system.mapper.RecommendInfoMapper;
import com.ruoyi.system.service.IRecommendInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 推荐Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-15
 */
@Service
public class RecommendInfoServiceImpl implements IRecommendInfoService 
{
    @Autowired
    private RecommendInfoMapper recommendInfoMapper;
    @Autowired
    private MusicRecommendMapper musicRecommendMapper;
    @Autowired
    private MusicInfoMapper musicInfoMapper;

    /**
     * 查询推荐
     * 
     * @param id 推荐主键
     * @return 推荐
     */
    @Override
    public RecommendInfo selectRecommendInfoById(Long id)
    {
        return recommendInfoMapper.selectRecommendInfoById(id);
    }

    /**
     * 查询推荐列表
     * 
     * @param recommendInfo 推荐
     * @return 推荐
     */
    @Override
    public List<RecommendInfo> selectRecommendInfoList(RecommendInfo recommendInfo)
    {
        return recommendInfoMapper.selectRecommendInfoList(recommendInfo);
    }

    /**
     * 新增推荐
     * 
     * @param recommendInfo 推荐
     * @return 结果
     */
    @Override
    public int insertRecommendInfo(RecommendInfo recommendInfo)
    {
        recommendInfo.setCreator(SecurityUtils.getUsername());
        recommendInfo.setCreateTime(DateUtils.getNowDate());
        return recommendInfoMapper.insertRecommendInfo(recommendInfo);
    }

    /**
     * 修改推荐
     * 
     * @param recommendInfo 推荐
     * @return 结果
     */
    @Override
    public int updateRecommendInfo(RecommendInfo recommendInfo)
    {
        recommendInfo.setUpdateTime(DateUtils.getNowDate());
        return recommendInfoMapper.updateRecommendInfo(recommendInfo);
    }

    /**
     * 批量删除推荐
     * 
     * @param ids 需要删除的推荐主键
     * @return 结果
     */
    @Override
    public int deleteRecommendInfoByIds(Long[] ids)
    {
        musicRecommendMapper.deleteMusicRecommendByIds(ids);
        return recommendInfoMapper.deleteRecommendInfoByIds(ids);
    }

    /**
     * 删除推荐信息
     * 
     * @param id 推荐主键
     * @return 结果
     */
    @Override
    public int deleteRecommendInfoById(Long id)
    {
        return recommendInfoMapper.deleteRecommendInfoById(id);
    }

    @Override
    public int bindMusic(String reId, String[] musicIds) {
        recommendInfoMapper.deleteRecommendInfoByReId(reId);
        return recommendInfoMapper.bindMusic(reId,musicIds);
    }

    @Override
    public  List<MusicInfo> findRecommendMusic() {
       return musicInfoMapper.selectRecommendMusic();
    }

    @Override
    public List<RecommendInfo> findRecommendBindMusic() {
        List<MusicRecommend> musicRecommends = musicRecommendMapper.selectMusicRecommendList(new MusicRecommend());

        Map<Long, List<MusicRecommend>> reMusicMap = musicRecommends.stream().collect(Collectors.groupingBy(MusicRecommend::getRecommendId));

        List<RecommendInfo> result = new ArrayList<>();

        reMusicMap.forEach((reId,rmcs) -> {
            RecommendInfo recommendInfo = recommendInfoMapper.selectRecommendInfoById(reId);
            List<Long> musicIdList = rmcs.stream().map(MusicRecommend::getMusicId).collect(Collectors.toList());
            musicIdList = musicIdList.stream().limit(10).collect(Collectors.toList());
            List<MusicInfo> musicInfos = musicInfoMapper.selectMusicInfoByIds(musicIdList);
            recommendInfo.setMusics(musicInfos);
            result.add(recommendInfo);
        });
        return result;
    }

    @Override
    public List<MusicInfo> findRecommendMusicByReId(String reId) {
        return musicInfoMapper.selectRecommendMusicByReId(reId);
    }
}
