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

import cn.reghao.oss.store.db.mapper.DataBlockMapper;
import cn.reghao.oss.store.db.mapper.FileMetaMapper;
import cn.reghao.oss.store.model.po.DataBlock;
import cn.reghao.oss.store.model.po.FileMeta;
import cn.reghao.oss.storeapi.dto.ObjectMeta;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;

/**
 * @author reghao
 * @date 2023-05-22 16:48:40
 */
@Slf4j
@Service
public class ObjectRepository {
    private final FileMetaMapper fileMetaMapper;
    private final DataBlockMapper dataBlockMapper;

    public ObjectRepository(FileMetaMapper fileMetaMapper, DataBlockMapper dataBlockMapper) {
        this.fileMetaMapper = fileMetaMapper;
        this.dataBlockMapper = dataBlockMapper;
    }

    public void saveFileMeta(FileMeta fileMeta) {
        fileMetaMapper.save(fileMeta);
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveObject(FileMeta fileMeta, List<DataBlock> list) {
        fileMetaMapper.save(fileMeta);
        dataBlockMapper.saveAll(list);
    }

    @CacheEvict(cacheNames = "oss:store:objectMeta", key = "#objectName")
    public void updateObjectScope(int scope, String objectName) {
        fileMetaMapper.updateScopeByObjectName(scope, objectName);
        log.info("evict {}", objectName);
    }

    @CacheEvict(cacheNames = "oss:store:objectMeta", key = "#fileMeta.objectName")
    public void deleteObject(FileMeta fileMeta) {
        String contentId = fileMeta.getContentId();
        List<FileMeta> list = fileMetaMapper.findByContentId(contentId);
        if (list.size() == 1) {
            DataBlock dataBlock = dataBlockMapper.findByContentId(contentId);
            String absolutePath = dataBlock.getAbsolutePath();

            deleteObject(fileMeta, dataBlock);
            FileUtils.deleteQuietly(new File(absolutePath));
        } else {
            fileMetaMapper.delete(fileMeta);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteObject(FileMeta fileMeta, DataBlock dataBlock) {
        fileMetaMapper.delete(fileMeta);
        dataBlockMapper.delete(dataBlock);
    }

    public FileMeta getByObjectName(String objectName) {
        return fileMetaMapper.findByObjectName(objectName);
    }

    public FileMeta getByObjectId(String objectId) {
        return fileMetaMapper.findByObjectId(objectId);
    }

    public FileMeta getBySha256sum(String sha256sum) {
        return fileMetaMapper.findBySha256sum(sha256sum);
    }

    @Cacheable(cacheNames = "oss:store:objectMeta", key = "#objectName", unless = "#result == null")
    public ObjectMeta getObjectMetaByName(String objectName) {
        log.info("cache miss {}", objectName);
        ObjectMeta objectMeta = fileMetaMapper.findObjectMeta(objectName);
        return objectMeta;
    }

    public ObjectMeta getObjectMetaById(String objectId) {
        return fileMetaMapper.findObjectMetaById(objectId);
    }
}
