package com.ruoyi.system.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.domain.entity.CategoryInfo;
import com.ruoyi.common.core.domain.entity.MusicInfo;
import com.ruoyi.common.enums.Audio;
import com.ruoyi.common.enums.MusicType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.config.AliConfig;
import com.ruoyi.system.mapper.MusicInfoMapper;
import com.ruoyi.system.service.IMusicInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 音乐信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-15
 */
@Service
public class MusicInfoServiceImpl implements IMusicInfoService 
{
    @Autowired
    private MusicInfoMapper musicInfoMapper;

    /**
     * 查询音乐信息
     * 
     * @param id 音乐信息主键
     * @return 音乐信息
     */
    @Override
    public MusicInfo selectMusicInfoById(String id)
    {
        return musicInfoMapper.selectMusicInfoById(id);
    }

    /**
     * 查询音乐信息列表
     * 
     * @param musicInfo 音乐信息
     * @return 音乐信息
     */
    @Override
    public List<MusicInfo> selectMusicInfoList(MusicInfo musicInfo)
    {
        return musicInfoMapper.selectMusicInfoList(musicInfo);
    }

    /**
     * 新增音乐信息
     * 
     * @param musicInfo 音乐信息
     * @return 结果
     */
    @Override
    public int insertMusicInfo(MusicInfo musicInfo, MultipartFile img, MultipartFile music)
    {
        musicInfo.setCreateTime(DateUtils.getNowDate());
        musicInfo.setMusicId(IdUtil.getSnowflakeNextId());
        try {
            String imgAddr = AliConfig.ossUp("musicImg/" + img.getOriginalFilename(), img.getInputStream());
            String musicAddr = AliConfig.ossUp("musicFile/" + music.getOriginalFilename(), music.getInputStream());
            musicInfo.setMusicAddr(musicAddr);
            musicInfo.setImgAddr(imgAddr);
            musicInfo.setMusicType(MusicType.ORDINARY.getMusicType());
            musicInfo.setCreator(SecurityUtils.getUsername());
            musicInfo.setShelf(2L);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return musicInfoMapper.insertMusicInfo(musicInfo);
    }

    /**
     * 修改音乐信息
     * 
     * @param musicInfo 音乐信息
     * @return 结果
     */
    @Override
    public int updateMusicInfo(MusicInfo musicInfo)
    {
        musicInfo.setUpdateTime(DateUtils.getNowDate());
        return musicInfoMapper.updateMusicInfo(musicInfo);
    }

    /**
     * 批量删除音乐信息
     * 
     * @param ids 需要删除的音乐信息主键
     * @return 结果
     */
    @Override
    public int deleteMusicInfoByIds(String[] ids)
    {
        return musicInfoMapper.deleteMusicInfoByIds(ids);
    }

    /**
     * 删除音乐信息信息
     * 
     * @param id 音乐信息主键
     * @return 结果
     */
    @Override
    public int deleteMusicInfoById(String id)
    {
        return musicInfoMapper.deleteMusicInfoById(id);
    }

    @Override
    public List<MusicInfo> findMusicByCate(CategoryInfo categoryInfo) {
        musicInfoMapper.selectByCate(categoryInfo);
        return null;
    }

    @Override
    public List<MusicInfo> selectMusicInfoByCid(String categoryId) {
        return  musicInfoMapper.selectMusicInfoByCid(categoryId);
    }


}
