package com.microservices.system.api.factory;

import com.microservices.common.core.domain.R;
import com.microservices.common.core.exception.ServiceException;
import com.microservices.system.api.RemoteFileService;
import com.microservices.system.api.domain.SysFile;
import com.microservices.system.api.domain.SysFileInfo;
import feign.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;

/**
 * 文件服务降级处理
 *
 * @author microservices
 */
@Component
public class RemoteFileFallbackFactory implements FallbackFactory<RemoteFileService>
{
    private static final Logger log = LoggerFactory.getLogger(RemoteFileFallbackFactory.class);

    @Override
    public RemoteFileService create(Throwable throwable)
    {
        log.error("文件服务调用失败:{}", throwable.getMessage());
        return new RemoteFileService() {

            @Override
            public R<SysFile> upload(MultipartFile file, String type, String hierarchy, String source) {
                return R.fail("上传文件失败:" + throwable.getMessage());
            }

            @Override
            public R<List<SysFileInfo>> getFileList(String fileIds) {
                return R.fail("获取文件列表失败:" + throwable.getMessage());
            }

            @Override
            public R<SysFileInfo> getFile(Long fileId) {
                return R.fail("根据文件Id获取文件信息失败:" + throwable.getMessage());
            }

            @Override
            public R<SysFileInfo> getFileByFileIdentifier(String fileIdentifier, String source) {
                return R.fail("根据文件标识获取文件信息失败:" + throwable.getMessage());
            }

            @Override
            public R<List<SysFileInfo>> getFileListByIdentifier(String fileIdentifiers) {
                return R.fail("根据文件标识列表获取文件详情列表失败:" + throwable.getMessage());
            }

            @Override
            public R<Boolean> deleteFileByIdentifiers(String fileIdentifiers, String source) {
                return R.fail("通过文件标识列表批量删除文件失败:" + throwable.getMessage());
            }

            @Override
            public Response getFileEntityByIdentifier(String fileIdentifier, String source) {
                throw new ServiceException("文件获取失败:" + throwable.getMessage());
            }

            @Override
            public R<String> packagedFile(HashMap<String, String> packagedStructure, String zipFileName, String type, String hierarchy, String source) {
                return R.fail("打包文件失败:" + throwable.getMessage());
            }

            @Override
            public R<List<String>> uploadFileToForge(String fileIdentifiers, String source) {
                return R.fail("上传本地文件到Forge失败:" + throwable.getMessage());
            }
        };
    }
}
