package com.njcn.gather.user.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.gather.user.user.component.SysFileMinioStorageService;
import com.njcn.gather.user.user.config.StorageMinioProperties;
import com.njcn.gather.user.user.mapper.SysFileMapper;
import com.njcn.gather.user.user.pojo.constant.SysFileConst;
import com.njcn.gather.user.user.pojo.po.SysFile;
import com.njcn.gather.user.user.service.ISysFileService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * 通用文件服务实现。
 */
@Service
@RequiredArgsConstructor
public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> implements ISysFileService {

    private final StorageMinioProperties storageMinioProperties;
    private final SysFileMinioStorageService sysFileMinioStorageService;

    @Override
    public SysFile saveMinioFile(String bizType, String bucketKey, String bizDir, String ownerId, MultipartFile file) {
        String fileId = UUID.randomUUID().toString().replace("-", "");
        String bucketName = storageMinioProperties.requireBucket(bucketKey);
        SysFileMinioStorageService.StoredObjectInfo storedObjectInfo =
                sysFileMinioStorageService.save(bucketName, bizDir, ownerId, fileId, file);
        SysFile sysFile = new SysFile();
        sysFile.setId(fileId);
        sysFile.setBizType(bizType);
        sysFile.setFileName(storedObjectInfo.getFileName());
        sysFile.setBucketName(storedObjectInfo.getBucketName());
        sysFile.setObjectName(storedObjectInfo.getObjectName());
        sysFile.setContentType(storedObjectInfo.getContentType());
        sysFile.setFileSize(storedObjectInfo.getFileSize());
        sysFile.setStorageType(SysFileConst.STORAGE_TYPE_MINIO);
        sysFile.setStatus(SysFileConst.STATUS_NORMAL);
        try {
            boolean saved = this.save(sysFile);
            if (!saved) {
                sysFileMinioStorageService.deleteQuietly(sysFile.getBucketName(), sysFile.getObjectName());
                throw new BusinessException(CommonResponseEnum.FAIL, "保存文件记录失败");
            }
            return sysFile;
        } catch (RuntimeException exception) {
            sysFileMinioStorageService.deleteQuietly(sysFile.getBucketName(), sysFile.getObjectName());
            throw exception;
        }
    }

    @Override
    public SysFile getActiveById(String fileId) {
        if (fileId == null || fileId.trim().isEmpty()) {
            return null;
        }
        return this.lambdaQuery()
                .eq(SysFile::getId, fileId)
                .eq(SysFile::getStatus, SysFileConst.STATUS_NORMAL)
                .one();
    }

    @Override
    public List<SysFile> listActiveByIds(List<String> fileIds) {
        if (CollUtil.isEmpty(fileIds)) {
            return Collections.emptyList();
        }
        return this.lambdaQuery()
                .in(SysFile::getId, fileIds)
                .eq(SysFile::getStatus, SysFileConst.STATUS_NORMAL)
                .list();
    }

    @Override
    public void markDeleted(SysFile file) {
        if (file == null) {
            return;
        }
        file.setStatus(SysFileConst.STATUS_DELETED);
        this.updateById(file);
    }

    @Override
    public void deleteObjectQuietly(SysFile file) {
        if (file == null) {
            return;
        }
        sysFileMinioStorageService.deleteQuietly(file.getBucketName(), file.getObjectName());
    }
}
