package cn.reghao.tnb.oss.store.db.repository;

import cn.reghao.tnb.oss.store.util.AuthContext;
import cn.reghao.tnb.oss.store.db.mapper.FilePartMapper;
import cn.reghao.tnb.oss.store.db.mapper.FileMultipartMapper;
import cn.reghao.tnb.oss.store.model.po.FilePart;
import cn.reghao.tnb.oss.store.model.po.FileMultipart;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author reghao
 * @date 2024-10-24 17:05:20
 */
@Service
public class FilePartRepository {
    private final FileMultipartMapper fileMultipartMapper;
    private final FilePartMapper filePartMapper;

    public FilePartRepository(FileMultipartMapper fileMultipartMapper, FilePartMapper filePartMapper) {
        this.fileMultipartMapper = fileMultipartMapper;
        this.filePartMapper = filePartMapper;
    }

    public void saveFileParts(FileMultipart fileMultipart) {
        fileMultipartMapper.save(fileMultipart);
    }

    public void saveFilePart(FilePart filePart) {
        filePartMapper.save(filePart);
    }

    public void updateSetUploaded(String sha256sum) {
        fileMultipartMapper.updateSetUploaded(sha256sum);
    }

    public List<FilePart> getFileParts(String contentId) {
        return filePartMapper.findByContentId(contentId);
    }

    public FileMultipart getFileMultipart(String sha256sum) {
        int loginUser = AuthContext.getUserId();
        FileMultipart fileMultipart = fileMultipartMapper.findBySha256sumAndUploadBy(sha256sum, loginUser);
        return fileMultipart;
    }
}
