package jnpf.cultivate.event.impl.file;

import cn.hutool.core.collection.CollUtil;
import jnpf.cultivate.event.JnpfApplicationEventService;
import jnpf.cultivate.service.impl.FtbCultivateFileService;
import jnpf.model.cultivate.event.dto.file.FileEventDTO;
import jnpf.model.cultivate.po.FtbCultivateFile;
import org.springframework.stereotype.Service;

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

/**
 * @Title:JnpfApplicationEventFileServiceImpl
 * @Author:peng.hao
 * @create: 2023/12/2618:01
 */
@Service
public class JnpfApplicationEventFileServiceImpl implements JnpfApplicationEventService {

    @Resource
    private FtbCultivateFileService ftbCultivateFileService;

    @Override
    public boolean isSupportedCourseEvent(Object event) {
        return event instanceof FileEventDTO;
    }

    @Override
    public void handlerCourseEvent(Object object) {
        FileEventDTO fileEventDTO = (FileEventDTO) object;
        // 附件文件
        if (CollUtil.isNotEmpty(fileEventDTO.getFiles())) {
            List<FtbCultivateFile> ftbCultivateFiles = fileEventDTO.getFiles()
                    .stream()
                    .map(ftbCultivateFileDTO -> {
                        FtbCultivateFile ftbCultivateFile = new FtbCultivateFile();
                        ftbCultivateFile.setFileName(ftbCultivateFileDTO.getName());
                        ftbCultivateFile.setUrl(ftbCultivateFileDTO.getUrl());
                        ftbCultivateFile.setExtension(ftbCultivateFileDTO.getFileExtension());
                        ftbCultivateFile.setSize(ftbCultivateFileDTO.getFileSize());
                        ftbCultivateFile.setType(fileEventDTO.getType().getType());
                        ftbCultivateFile.setBusinessId(fileEventDTO.getBusinessTypeID());
                        ftbCultivateFile.setVideoActionType(ftbCultivateFileDTO.getVideoActionType());
                        ftbCultivateFile.setDuration(ftbCultivateFileDTO.getDuration());
                        ftbCultivateFile.setEnabledMark(0);
                        return ftbCultivateFile;
                    }).collect(Collectors.toList());
            ftbCultivateFileService.saveBatch(ftbCultivateFiles);
        }
    }
}
