package com.yonge.cooleshow.biz.dal.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.biz.dal.dao.AlbumMusicRelateDao;
import com.yonge.cooleshow.biz.dal.dto.MusicAlbumSheetDto;
import com.yonge.cooleshow.biz.dal.entity.AlbumMusicRelate;
import com.yonge.cooleshow.biz.dal.service.AlbumMusicRelateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * AlbumMusicRelateService服务实现类
 * @author yzp
 * @date 2022-03-26 00:01:37
 * @version v1.0
 **/
@Service
public class AlbumMusicRelateServiceImpl extends ServiceImpl<AlbumMusicRelateDao,AlbumMusicRelate> implements AlbumMusicRelateService {

    private final static Logger log = LoggerFactory.getLogger(AlbumMusicRelateServiceImpl.class);

    @Autowired
    private AlbumMusicRelateDao albumMusicRelateDao;

    public AlbumMusicRelateDao getDao() {
        return albumMusicRelateDao;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delMusicSheet(Long albumId, List<Long> musicSheetIdList) {
        List<AlbumMusicRelate> list = this.lambdaQuery()
                                          .eq(AlbumMusicRelate::getAlbumId, albumId)
                                          .in(AlbumMusicRelate::getMusicSheetId, musicSheetIdList)
                                          .list();
        if (!CollectionUtils.isEmpty(list)) {
            List<Long> albumMusicRelateIdList = list.stream().map(AlbumMusicRelate::getId).collect(Collectors.toList());
            return this.removeByIds(albumMusicRelateIdList);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addMusicSheet(Long albumId, List<MusicAlbumSheetDto.AlbumMusic> albumMusicList, Long userId) {
        List<Long> musicSheetIdList = albumMusicList.stream()
                                           .map(MusicAlbumSheetDto.AlbumMusic::getMusicSheetId)
                                           .collect(Collectors.toList());
        List<AlbumMusicRelate> list = this.lambdaQuery()
                                          .eq(AlbumMusicRelate::getAlbumId, albumId)
                                          .in(AlbumMusicRelate::getMusicSheetId, musicSheetIdList)
                                          .list();
        List<Long> musicSheetIds = list.stream().map(AlbumMusicRelate::getMusicSheetId).collect(Collectors.toList());

        // 去除已有的曲目
        List<AlbumMusicRelate> albumMusicRelateList = albumMusicList.stream()
                                                                      .filter(albumMusic -> !musicSheetIds.contains(albumMusic.getMusicSheetId()))
                                                                      .map(albumMusic -> {
            AlbumMusicRelate albumMusicRelate = new AlbumMusicRelate();
            albumMusicRelate.setAlbumId(albumId);
            albumMusicRelate.setMusicSheetId(albumMusic.getMusicSheetId());
            albumMusicRelate.setSortNumber(albumMusic.getSortNumber());
            albumMusicRelate.setCreateTime(new Date());
            albumMusicRelate.setCreateBy(userId);
            return albumMusicRelate;
        }).collect(Collectors.toList());

        return this.saveBatch(albumMusicRelateList);
    }

    @Override
    public void delByMusicSheet(Long musicSheetId) {
        this.lambdaUpdate()
                .eq(AlbumMusicRelate::getMusicSheetId,musicSheetId)
                .remove();
    }
}