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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.biz.dal.dao.MusicSheetAuthRecordDao;
import com.yonge.cooleshow.biz.dal.dto.req.TeacherMusicSheetAuditReq;
import com.yonge.cooleshow.biz.dal.entity.MusicSheetAuthRecord;
import com.yonge.cooleshow.biz.dal.enums.AuthStatusEnum;
import com.yonge.cooleshow.biz.dal.service.MusicSheetAuthRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;


@Service
public class MusicSheetAuthRecordServiceImpl extends ServiceImpl<MusicSheetAuthRecordDao, MusicSheetAuthRecord> implements MusicSheetAuthRecordService {


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long audit(TeacherMusicSheetAuditReq param, Long userId) {

        // 查询到审核记录
        List<MusicSheetAuthRecord> list = this.lambdaQuery()
                                              .eq(MusicSheetAuthRecord::getMusicSheetId, param.getMusicSheetId())
                                              .eq(MusicSheetAuthRecord::getAuditState, AuthStatusEnum.DOING.getCode())
                                              .list();

        MusicSheetAuthRecord musicSheetAuthRecord = new MusicSheetAuthRecord();
        if (!CollectionUtils.isEmpty(list)) {
            musicSheetAuthRecord = list.get(0);
        } else {
            musicSheetAuthRecord.setCreateTime(new Date());
        }

        // 替换审核id
        replaceAuthRecord(param, musicSheetAuthRecord);
        musicSheetAuthRecord.setMusicSheetId(param.getMusicSheetId());
        if (param.getAuthStatus().equals(AuthStatusEnum.PASS)) {
            musicSheetAuthRecord.setMusicSheetId(musicSheetAuthRecord.getOriginalMusicSheetId());
        }
        musicSheetAuthRecord.setAuditState(param.getAuthStatus());
        musicSheetAuthRecord.setReason(param.getRemark());
        musicSheetAuthRecord.setVerifyUserId(userId);
        musicSheetAuthRecord.setUpdateTime(new Date());
        this.saveOrUpdate(musicSheetAuthRecord);
        return musicSheetAuthRecord.getOriginalMusicSheetId();
    }

    private void replaceAuthRecord(TeacherMusicSheetAuditReq param, MusicSheetAuthRecord musicSheetAuthRecord) {
        if (param.getAuthStatus().equals(AuthStatusEnum.PASS) && !musicSheetAuthRecord.getOriginalMusicSheetId().equals(
                musicSheetAuthRecord.getMusicSheetId())) {
            List<MusicSheetAuthRecord> musicSheetAuthRecords = this.lambdaQuery()
                                                   .eq(MusicSheetAuthRecord::getMusicSheetId,
                                                       musicSheetAuthRecord.getOriginalMusicSheetId())
                                                   .list();
            if (!CollectionUtils.isEmpty(musicSheetAuthRecords)) {
                MusicSheetAuthRecord record = musicSheetAuthRecords.get(0);
                record.setMusicSheetId(param.getMusicSheetId());
                this.saveOrUpdate(record);
            }

        }
    }

    @Override
    public void setAuditDoing(Long musicSheetId, Long originalMusicSheetId, Long lastMusicSheetId) {
        List<MusicSheetAuthRecord> list = this.lambdaQuery()
                                              .eq(MusicSheetAuthRecord::getMusicSheetId, musicSheetId)
                                              .eq(MusicSheetAuthRecord::getAuditState, AuthStatusEnum.DOING.getCode())
                                              .list();
        if (!CollectionUtils.isEmpty(list)) {
            return;
        }
        MusicSheetAuthRecord musicSheetAuthRecord = new MusicSheetAuthRecord();
        musicSheetAuthRecord.setMusicSheetId(musicSheetId);
        musicSheetAuthRecord.setAuditState(AuthStatusEnum.DOING);
        if (originalMusicSheetId == null) {
            originalMusicSheetId = musicSheetId;
            musicSheetAuthRecord.setFirstAuth(0);
        } else {
            musicSheetAuthRecord.setFirstAuth(1);
        }
        musicSheetAuthRecord.setCreateTime(new Date());
        musicSheetAuthRecord.setOriginalMusicSheetId(originalMusicSheetId);
        this.saveOrUpdate(musicSheetAuthRecord);
    }

    @Override
    public String selectAuditReason(Long musicSheetId) {
        MusicSheetAuthRecord one = this.lambdaQuery()
                                       .eq(MusicSheetAuthRecord::getMusicSheetId, musicSheetId)
                                       .orderByDesc(MusicSheetAuthRecord::getId)
                                       .last("limit 1")
                                       .one();
        if (one == null) {
            return "";
        }
        return one.getReason();

    }
}