package com.yd.oss.api.service.impl;

import com.aliyun.oss.OSS;
import com.yd.common.exception.BusinessException;
import com.yd.common.result.Result;
import com.yd.oss.api.service.ApiMaterialService;
import com.yd.oss.feign.dto.ApiMaterialDto;
import com.yd.oss.feign.request.ApiMaterialDownloadRequest;
import com.yd.oss.feign.request.ApiMaterialListRequest;
import com.yd.oss.feign.response.ApiMaterialDownloadResponse;
import com.yd.oss.feign.response.ApiMaterialListResponse;
import com.yd.oss.service.config.OssClientFactory;
import com.yd.oss.service.dto.MaterialDto;
import com.yd.oss.service.model.Material;
import com.yd.oss.service.model.OssProvider;
import com.yd.oss.service.model.RelObjectMaterial;
import com.yd.oss.service.service.CompressedFileService;
import com.yd.oss.service.service.IMaterialService;
import com.yd.oss.service.service.IOssProviderService;
import com.yd.oss.service.service.IRelObjectMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ApiMaterialServiceImpl implements ApiMaterialService {

    @Autowired
    private IMaterialService iMaterialService;

    @Autowired
    private CompressedFileService compressedFileService;

    @Autowired
    private IRelObjectMaterialService iRelObjectMaterialService;

    @Autowired
    private IOssProviderService ossProviderService;

    @Autowired
    private OssClientFactory ossClientFactory;

    /**
     * 列表查询-材料基础信息
     * @param request
     * @return
     */
    @Override
    public Result<List<ApiMaterialListResponse>> list(ApiMaterialListRequest request) {
        List<Material> materialList = iMaterialService.queryList(MaterialDto.builder().objectType(request.getObjectType()).build());
        if (!CollectionUtils.isEmpty(materialList)) {
            List<ApiMaterialListResponse> responses = materialList.stream().map(dto -> {
                ApiMaterialListResponse response = new ApiMaterialListResponse();
                BeanUtils.copyProperties(dto,response);
                return response;
            }).collect(Collectors.toList());
            return Result.success(responses);
        }
        return Result.success();
    }

    /**
     * 下载-材料列表压缩包
     * @param request
     * @return
     */
    @Override
    public Result<ApiMaterialDownloadResponse> downloadCompressedFile(ApiMaterialDownloadRequest request) {
        // 获取提供商
        OssProvider provider = ossProviderService.getProviderByProjectId(request.getProjectBizId());
        if (provider == null) {
            throw new BusinessException("未找到项目对应的OSS提供商，projectBizId=" + request.getProjectBizId());
        }

        OSS ossClient = null;
        try {
            ossClient = ossClientFactory.createOssClient(provider);
            return compressedFileService.downloadCompressedFile(request,ossClient,provider);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }


}