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

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayaedu.cbs.openfeign.wrapper.music.CbsMusicSheetWrapper;
import com.yonge.cooleshow.biz.dal.dao.MusicSheetAccompanimentDao;
import com.yonge.cooleshow.biz.dal.entity.MusicSheetAccompaniment;
import com.yonge.cooleshow.biz.dal.service.MusicSheetAccompanimentService;
import com.yonge.toolset.base.exception.BizException;
import org.apache.commons.collections.CollectionUtils;
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 java.util.ArrayList;
import java.util.List;

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

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

    @Autowired
    private MusicSheetAccompanimentDao musicSheetAccompanimentDao;

    @Override
    public MusicSheetAccompanimentDao getDao() {
        return musicSheetAccompanimentDao;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<MusicSheetAccompaniment> initMusicSheetAccompaniment(String musicSheetJson,Long musicSheetId) {
        CbsMusicSheetWrapper.AddMusicSheet addMusicSheet = JSON.parseObject(musicSheetJson, CbsMusicSheetWrapper.AddMusicSheet.class);
        List<CbsMusicSheetWrapper.MusicSheetSound> soundList = addMusicSheet.getMusicSheetSoundList();
        if (CollectionUtils.isEmpty(soundList)) {
            throw new BizException("曲目原音不能为空");
        }
        List<MusicSheetAccompaniment> background = new ArrayList<>();
        for (CbsMusicSheetWrapper.MusicSheetSound sound : soundList) {
            MusicSheetAccompaniment accompaniment = new MusicSheetAccompaniment();
            accompaniment.setAudioFileUrl(sound.getAudioFileUrl());
            accompaniment.setSortNumber(sound.getSortNumber());
            accompaniment.setTrack(sound.getTrack());
            accompaniment.setMusicSheetId(musicSheetId);
            accompaniment.setMusicalInstrumentId(sound.getMusicalInstrumentId());
            background.add(accompaniment);
        }
        return background;
    }

    @Override
    public MusicSheetAccompaniment initSysMusicScoreAccompaniment(CbsMusicSheetWrapper.MusicSheetAccApplication musicSheetApplication) {
        MusicSheetAccompaniment record = new MusicSheetAccompaniment();
        record.setSpeed(musicSheetApplication.getPlaySpeed());
        record.setTrack(musicSheetApplication.getTrack());
        record.setAudioFileUrl(musicSheetApplication.getMp3Url());
        record.setMetronomeUrl(musicSheetApplication.getUrl());
        return record;
    }
}