package jnpf.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jnpf.base.service.SuperServiceImpl;
import jnpf.cultivate.mapper.CultivateFileMapper;
import jnpf.cultivate.service.CultivateFileService;
import jnpf.model.cultivate.po.teaching.CultivateFile;
import jnpf.model.cultivate.v2.teaching.model.V2Attachment;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 文件服务实现
 *
 * @author yanwenfu
 * @create 2026-03-02
 */
@Service
public class CultivateFileServiceImpl extends SuperServiceImpl<CultivateFileMapper, CultivateFile> implements CultivateFileService {

    @Override
    public Map<String, List<V2Attachment>> getFileListByBizIds(List<String> bizIds) {

        LambdaQueryWrapper<CultivateFile> fileQuery = new LambdaQueryWrapper<CultivateFile>()
                .in(CultivateFile::getBusinessId, bizIds);
        List<CultivateFile> list = this.list(fileQuery);
        if (list == null || list.isEmpty()) {
            return Collections.emptyMap();
        }
        return list.stream()
                .map(this::convertToV2Attachment)
                .collect(Collectors.groupingBy(V2Attachment::getBizId));
    }

    @Override
    public V2Attachment convertToV2Attachment(CultivateFile file) {

        V2Attachment attachment = new V2Attachment();
        attachment.setBizId(file.getBusinessId());
        attachment.setFileName(file.getFileName());
        attachment.setUrl(file.getUrl());
        attachment.setSize(file.getSize());
        attachment.setVideoActionType(file.getVideoActionType());
        attachment.setExtension(file.getExtension());
        return attachment;
    }
}
