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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.dayaedu.cbs.common.enums.music.EMusicSourceType;
import com.dayaedu.cbs.openfeign.service.CbsMusicSheetService;
import com.dayaedu.cbs.openfeign.wrapper.music.*;
import com.microsvc.toolkit.common.response.paging.PageInfo;
import com.microsvc.toolkit.common.response.paging.QueryInfo;
import com.yonge.cooleshow.biz.dal.entity.MusicSheet;
import com.yonge.cooleshow.biz.dal.entity.Subject;
import com.yonge.cooleshow.biz.dal.service.MusicSheetService;
import com.yonge.cooleshow.biz.dal.service.MusicTagService;
import com.yonge.cooleshow.biz.dal.service.SubjectService;
import com.yonge.toolset.base.exception.BizException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CbsMusicSheetServiceImpl implements CbsMusicSheetService {

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

    @Resource
    private MusicSheetService musicSheetService;
    @Resource
    private MusicTagService musicTagService;
    @Resource
    private SubjectService subjectService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveApplicationExtend(List<CbsMusicSheetApplicationExtendWrapper.MusicSheetApplicationExtend> addMusicSheet) {
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isEmpty(addMusicSheet)) {
            throw new BizException("请选择曲目信息");
        }
        List<Long> cbsMusicSheetIds = addMusicSheet.stream().map(e -> e.getMusicSheetId()).collect(Collectors.toList());
        for (CbsMusicSheetApplicationExtendWrapper.MusicSheetApplicationExtend extend : addMusicSheet) {
            CbsMusicSheetApiWrapper.MusicSheetUpdate musicSheetUpdate = extend.getMusicSheetUpdate();
            if(StringUtils.isNotBlank(musicSheetUpdate.getSubjectIds())) {
                List<Subject> subjectIds = subjectService.getDao().getByCbsSubjectIds(musicSheetUpdate.getSubjectIds());
                if (CollectionUtils.isNotEmpty(subjectIds)) {
                    musicSheetUpdate.setSubjectIds(subjectIds.stream().map(e -> e.getId().toString()).collect(Collectors.joining(",")));
                }
            }
        }
        //需要修改的曲目
        List<MusicSheet> musicSheets = musicSheetService.lambdaQuery()
                .in(MusicSheet::getCbsMusicSheetId, cbsMusicSheetIds)
                .list();
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(musicSheets)){
            List<Long> collect = musicSheets.stream().map(e -> e.getCbsMusicSheetId()).collect(Collectors.toList());
            //获取需要修改的曲目
            List<CbsMusicSheetApplicationExtendWrapper.MusicSheetApplicationExtend> updateMusicScore =
                    addMusicSheet.stream().filter(e -> collect.contains(e.getMusicSheetId())).collect(Collectors.toList());
            musicSheetService.getDao().updateBatchByCbsId(updateMusicScore);
            //排除已经存在的曲目
            addMusicSheet = addMusicSheet.stream().filter(e -> !collect.contains(e.getMusicSheetId())).collect(Collectors.toList());
        }
        if(CollectionUtils.isNotEmpty(addMusicSheet)){
            musicSheetService.getDao().batchInsert(addMusicSheet);
        }
        return true;
    }

    public Boolean updateApplicationExtendStatus(CbsMusicSheetApplicationExtendWrapper.MusicSheetApplicationExtendStatus updateMusicSheet){
        if (updateMusicSheet == null || org.apache.commons.lang3.StringUtils.isEmpty(updateMusicSheet.getIds())){
            throw new BizException("请选择曲目");
        }
        if (updateMusicSheet.getStatus() == null){
            throw new BizException("请选择状态");
        }
        musicSheetService.getDao().batchEnable(updateMusicSheet.getIds(),updateMusicSheet.getStatus());
        return true;
    }

    public Boolean delApplicationExtend(CbsMusicSheetApplicationExtendWrapper.MusicSheetApplicationExtendDel musicSheetApplicationExtendDel){
        if (musicSheetApplicationExtendDel == null || StringUtils.isEmpty(musicSheetApplicationExtendDel.getIds())){
            throw new BizException("请选择曲目");
        }
        musicSheetService.getDao().batchDel(musicSheetApplicationExtendDel.getIds());
        return true;
    }

    public List<CbsMusicSheetTagWrapper.MusicSheetTag> tagList(CbsMusicSheetTagWrapper.MusicSheetTagQuery queryInfo){
        return musicTagService.getBaseMapper().tagList(queryInfo);
    }

    public List<CbsMusicSheetCategoryWrapper.MusicSheetCategory> categoryList(CbsMusicSheetCategoryWrapper.MusicSheetCategoryQuery queryInfo){
        return musicTagService.getBaseMapper().categoryList(queryInfo);
    }

    public PageInfo<CbsMusicSheetExtendWrapper.MusicSheetExtend> ownerList(CbsMusicSheetExtendWrapper.MusicSheetExtendQuery queryInfo){
        IPage<CbsMusicSheetExtendWrapper.MusicSheetExtend> page = QueryInfo.getPage(queryInfo);
        if (queryInfo.getSourceType() == null){
            throw new BizException("请选择来源类型");
        }
        if(queryInfo.getSourceType() == EMusicSourceType.PERSON){
            return QueryInfo.pageInfo(musicSheetService.getDao().personOwnerList(page, queryInfo));
        }else {
            return QueryInfo.pageInfo(musicSheetService.getDao().orgOwnerList(page, queryInfo));
        }
    }

    public boolean updateMusicSheet(CbsMusicSheetApiWrapper.MusicSheetUpdate musicSheetUpdate) {
        musicSheetService.getDao().updateMusicSheet(musicSheetUpdate);
        return true;
    }
}